<template>
  <el-dialog
    title="设备选择"
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    width="1000px"
    @close="close"
  >
    <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.device_code"
          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="device_code" label="设备编码" min-width="130" show-overflow-tooltip />
      <el-table-column prop="device_name" label="设备名称" min-width="130" show-overflow-tooltip />
      <el-table-column prop="class_name" label="设备类型" show-overflow-tooltip />
      <el-table-column prop="device_spec" label="规格" min-width="130" show-overflow-tooltip />
      <el-table-column prop="device_model" label="型号" />
      <el-table-column prop="supplier_name" label="供应商" />
      <el-table-column prop="device_type" label="设备属性" :formatter="formatTypeName"/>
      <el-table-column prop="workprocedure_name" label="工序" />
      <el-table-column prop="is_produceuse" label="生产用途" show-overflow-tooltip />
      <el-table-column prop="remark" label="备注" 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 CRUD, { header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import crudDevicemaintenanceplanmst from '@/api/wms/sb/devicemaintenanceplanmst'

export default {
  name: 'Device',
  components: { rrOperation, pagination },
  cruds() {
    return CRUD({
      title: '设备',
      url: 'api/devicemaintenanceplanmst/queryDevice',
      crudMethod: { ...crudDevicemaintenanceplanmst }, optShow: {}})
  },
  mixins: [presenter(), header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    isSingle: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dialogVisible: false,
      tableRadio: null,
      checkrow: null,
      rows: [],
      XLList: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    clickChange(item) {
      this.tableRadio = item
    },
    formatTypeName(row, cloum) {
      if (row.device_type === '01') {
        return '资产'
      } else if (row.device_type === '02') {
        return '普通设备'
      } else if (row.device_type === '03') {
        return '其他'
      }
    },
    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('tableChanged2', 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('tableChanged2', this.rows)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-dialog__body {
  padding-top: 0px;
}
</style>

