<template>
  <el-dialog
    title="设备扩展项"
    append-to-body
    fullscreen
    :visible.sync="dialogVisible"
    @open="open"
    @close="close"
  >
    <el-row :gutter="20">
      <el-col :span="18" style="border: 1px solid white">
        <span />
      </el-col>
      <el-col :span="6" style="margin-bottom: 20px">
        <span style="float: right">
           <el-button icon="el-icon-close" size="mini" type="info" @click="dialogVisible = false">取 消</el-button>
           <el-button icon="el-icon-check" size="mini" type="primary" @click="saveCheck">保存</el-button>
        </span>
      </el-col>
    </el-row>
    <el-card class="box-card" shadow="never">
      <el-form ref="form" disabled :inline="true" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-form-item label="设备编码">
          <el-input v-model="form.device_code" placeholder="自动生产" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="设备名称" prop="dtl_count">
          <el-input v-model="form.device_name" style="width: 200px;" />
        </el-form-item>
      </el-form>
    </el-card>
    <div class="crud-opts2" style="margin-top: 30px">
      <span class="role-span">扩展项明细</span>
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-plus"
          size="mini"
          @click="addrow"
        >
          新增扩展项
        </el-button>
      </span>

    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      :data="tableData"
      style="width: 100%;"
      size="mini"
      border
      :highlight-current-row="true"
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="item_code" label="项点编码" align="center" />
      <el-table-column prop="item_name" label="项点名称" align="center" />
      <el-table-column prop="item_value" label="项点值" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.is_show === '1' && scope.row.data_type !== '01'">{{ scope.row.item_value }}</span>
          <span v-if="scope.row.is_show === '1' && scope.row.data_type === '01'">{{ is_or_no(scope.row.item_value) }}</span>
          <el-input
            v-else-if="scope.row.is_show === '0' && (scope.row.data_type === '' || scope.row.data_type === '03')"
            v-model="scope.row.item_value"
          />
          <el-input
            type="number"
            v-else-if="scope.row.is_show === '0' && scope.row.data_type === '02'"
            v-model="scope.row.item_value"
          />
          <el-select
            ref="select"
            v-else-if="scope.row.is_show === '0' && scope.row.data_type === '01'"
            v-model="scope.row.item_value"
            placeholder=""
          >
            <el-option
              v-for="item in dict.IS_OR_NOT"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="data_type" label="默认值数据类型">
        <template slot-scope="scope">
          {{ dict.label.ITEM_VALUE_TYPE[scope.row.data_type] }}
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" />
      <el-table-column align="center" label="操作" width="160" fixed="right">
        <template scope="scope">
          <el-button v-if="scope.row.is_show === '1'" type="primary" class="filter-item" size="mini" icon="el-icon-edit" @click="editData(scope.row,scope.$index)" />
          <el-button v-else type="success" class="filter-item" size="mini" icon="el-icon-check" @click="saveData(scope.row,scope.column,scope.$index)" />
          <el-button type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="deleteRow(scope.$index, tableData)" />
        </template>
      </el-table-column>
    </el-table>
    <AddDtl :dialog-show.sync="dtlShow" @tableChanged="tableChanged" />
  </el-dialog>
</template>

<script>
import CRUD, { crud } from '@crud/crud'
import crudDevice from '@/api/wms/pdm/device'
import AddDtl from '@/views/wms/pdm/device/AddDtl'

export default {
  name: 'DeviceItemDialog',
  components: { AddDtl },
  mixins: [crud()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  dicts: ['ITEM_VALUE_TYPE', 'IS_OR_NOT'],
  data() {
    return {
      dialogVisible: false,
      nowindex: '',
      nowrow: null,
      dtlShow: false,
      materShow: false,
      form: {
        device_id: '',
        device_code: '',
        device_name: ''
      },
      tableData: [],
      rules: {
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    is_or_no(val) {
      if (val === '1') {
        return '是'
      } else {
        return '否'
      }
    },
    editData(row, index) {
      this.tableData[index].is_show = '0'
    },
    saveData(row, column, index) {
      this.tableData[index].is_show = '1'
    },
    handleCurrentChange(current) {
      if (current !== null) {
        this.nowrow = current
      } else {
        this.nowrow = null
      }
    },
    async queryMater(index, row) {
      this.materShow = true
      this.nowindex = index
      this.nowrow = row
    },
    open() {
      crudDevice.getItemByDevice({ device_id: this.form.device_id }).then(res => {
        this.tableData = res
        // 将明细变成不可编辑
        for (let i = 0; i < this.tableData.length; i++) {
          const row = this.tableData[i]
          this.tableData.splice(i, 1, row)
        }
      })
    },
    close() {
      this.$emit('AddChanged')
    },
    tableChanged(rows) {
      rows.forEach((item) => {
        this.$set(item, 'item_value', item.default_value)
      })
      var array = ([...this.tableData, ...rows])
      this.tableData = this.unique(array)
    },
    unique(arr) {
      // 根据唯一标识item_code来对数组进行过滤
      // 定义常量 res,值为一个Map对象实例
      const res = new Map()
      // 返回arr数组过滤后的结果，结果为一个数组   过滤条件是，如果res中没有某个键，就设置这个键的值为1
      return arr.filter((arr) => !res.has(arr.item_code) && res.set(arr.item_code, 1))
    },
    setForm(row) {
      this.dialogVisible = true
      this.form = row
    },
    addrow() {
      this.dtlShow = true
    },
    deleteRow(index, rows) {
      rows.splice(index, 1)
      this.nowindex = ''
      this.nowrow = null
    },

    saveCheck() {
      if (this.tableData.length === 0) {
        this.crud.notify('请至少选择一条明细', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].is_show === '0') {
          this.crud.notify('尚有未完成编辑的项点值,序号为' + (i + 1) + '，请检查！')
          return false
        }
      }
      crudDevice.savaDeviceItem({ 'row': this.form, 'rows': this.tableData }).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.dialogVisible = false
      })
    }
  }
}
</script>

<style>
.crud-opts2 {
  padding: 0 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}
.crud-opts2 .crud-opts-right2 {
  margin-left: auto;
  padding: 4px 4px;
}
.input-with-select  {
  background-color: #fff;
}
</style>
