<template>
  <el-dialog
    title="生箔点位选择"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    width="1000px"
    @close="close"
    @open="open"
  >
    <el-form
      :inline="true"
      class="demo-form-inline"
      label-position="right"
      label-width="80px"
      label-suffix=":"
    >
      <el-form-item label="模糊搜索">
        <el-input
          v-model="query.search"
          clearable
          size="mini"
          placeholder="可点位编号、外部编码"
          @keyup.enter.native="crud.toQuery"
        />
      </el-form-item>
      <rrOperation />
    </el-form>

    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      size="mini"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      @select="handleSelectionChange"
      @select-all="onSelectAll"
      @current-change="clickChange"
    >
      <el-table-column v-if="!isSingle" type="selection" width="55" />
      <el-table-column v-if="isSingle" label="选择" width="55">
        <template slot-scope="scope">
          <el-radio v-model="tableRadio" :label="scope.row"><i /></el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="point_code" label="点位编码" />
      <el-table-column prop="vehicle_code" label="载具编码" />
      <el-table-column prop="full_point_code" label="满轴位" :min-width="flexWidth('full_point_code',crud.data,'满轴位')" />
      <el-table-column prop="full_point_status" label="满轴位状态" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ dict.label.sch_full_point_status[scope.row.full_point_status] }}
        </template>
      </el-table-column>
      <el-table-column prop="empty_point_code" label="空轴位" :min-width="flexWidth('empty_point_code',crud.data,'空轴位')" />
      <el-table-column prop="empty_point_status" label="空轴位状态" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ dict.label.sch_empty_point_status[scope.row.empty_point_status] }}
        </template>
      </el-table-column>
      <el-table-column prop="product_area" label="生产区域">
        <template slot-scope="scope">
          {{ dict.label.product_area[scope.row.product_area] }}
        </template>
      </el-table-column>
      <el-table-column prop="point_location" label="位置">
        <template slot-scope="scope">
          {{ dict.label.point_location[scope.row.point_location] }}
        </template>
      </el-table-column>
      <el-table-column prop="ext_code" label="外部编码" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="is_used" label="是否启用">
        <template slot-scope="scope">
          {{ dict.label.is_used[scope.row.is_used] }}
        </template>
      </el-table-column>
      <el-table-column prop="mes_used" label="MES启用">
        <template slot-scope="scope">
          {{ dict.label.is_used[scope.row.mes_used] }}
        </template>
      </el-table-column>
      <el-table-column prop="update_time" label="修改时间" min-width="150" show-overflow-tooltip />
    </el-table>
    <!--分页组件-->
    <pagination />
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import crudSbpointivt from '@/views/wms/pdm/ivt/sbpointivt/sbpointivt'
import CRUD, { header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'

export default {
  name: 'SbpointivtDtl',
  components: { rrOperation, pagination },
  cruds() {
    return CRUD({ title: '机台编码', url: 'api/sbpointivt', crudMethod: { ...crudSbpointivt }, optShow: {}})
  },
  mixins: [presenter(), header()],
  dicts: ['product_series', 'product_area', 'is_used', 'point_location', 'sch_full_point_status', 'sch_empty_point_status'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    isSingle: {
      type: Boolean,
      default: true
    },
    materOptCode: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      tableRadio: null,
      checkrow: null,
      rows: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    clickChange(item) {
      this.tableRadio = item
    },
    seriesFormat(row) {
      return this.dict.label.product_series[row.product_series]
    },
    open() {
      this.crud.query.material_type_id = this.materOptCode
      this.crud.toQuery()
    },
    handleSelectionChange(val, row) {
      if (this.isSingle) {
        if (val.length > 1) {
          this.$refs.table.clearSelection()
          this.$refs.table.toggleRowSelection(val.pop())
        } else {
          this.checkrow = row
        }
      }
    },
    onSelectAll() {
      this.$refs.table.clearSelection()
    },
    close() {
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
    },
    submit() {
      // 处理单选
      if (this.isSingle && this.tableRadio) {
        this.$emit('update:dialogShow', false)
        this.$emit('setSbpointivtValue', this.tableRadio)
        return
      }
      this.rows = this.$refs.table.selection
      if (this.rows.length <= 0) {
        this.$message('请先勾选生箔点位')
        return
      }
      this.crud.resetQuery(false)
      this.$emit('update:dialogShow', false)
      this.$emit('setSbpointivtValue', this.rows)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 0px;
}
</style>
