<template>
  <div class="process-prod">
    <!-- 设置工装量具弹窗 -->
    <el-dialog
      :visible.sync="showProcessProd"
      width="800px"
      :before-close="beforeClose"
      append-to-body
      title="设置工装量具"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="tableData">
        <el-table-column label="编号" align="center" prop="itemCode" >
        </el-table-column>
        <el-table-column label="名称" align="center" prop="itemName" :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
        <el-table-column label="类型" align="center" prop="itemTypeName" />
        <el-table-column label="要求数量" align="center" prop="quantity" />
        <el-table-column label="备注" prop="remark" align="center" />
      </el-table>

      <div slot="footer">
        <el-button type="primary" size="small" @click="cancleConnect"
        >关闭</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑工装量具关联信息 -->
    <el-dialog
      :visible.sync="editConnectVisible"
      width="800px"
      title="编辑工装量具关联信息"
    >
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工装量具编码" prop="toolCode" >
              <el-input v-model="form.itemCode"  placeholder="请选择数据"
                        disabled>
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['ItemSelectRef'].showFlag = true"
                ></el-button>
              </el-input>
              <ItemSelect ref="ItemSelectRef" @onSelected="onItemSelect" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工装量具名称" prop="itemName">
              <el-input
                type="text"
                v-model="form.itemName"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="要求数量" prop="quantity">
              <el-input
                type="number"
                v-model="form.quantity"
                placeholder="请输入要求数量"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" size="small" @click="addOrEditubmit"
        >确认</el-button
        >
        <el-button size="small" @click="editConnectVisible = false"
        >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import ItemSelect from "@/components/itemSelect/single.vue";
  export default {
    props: {
      process: {
        type: Object,
        default: () => ({}),
      },
    },
    components: { ItemSelect },
    dicts: ['mes_index_type'],
    data() {
      return {
        form: {
          itemCode: "",
          itemName: "",
          specification:"",
          itemTypeName:"",
          remark: "",
          quantity: "",
          toolId:"",
        },
        currentRowIdx: undefined,
        tableData: [],
        selectedItemId: undefined,
        selectedRows: null,
        showFlag: false,
        optType:null,
        showProcessProd: false,
        editConnectVisible: false,
      };
    },
    computed: {
      title() {
        return "设置" + this.process.itemName + "工装量具";
      },
    },
    watch: {
      process: {
        handler(val) {
          this.tableData = val.toolList;
        },
        deep: true,
      },
    },
    methods: {
      handleRowChange(row) {
        if (row) {
          this.selectedRows = row;
        }
      },
      handleRowDbClick(row) {
        if (row) {
          this.selectedRows = row;
          this.tableData.unshift(this.selectedRows);
          this.showFlag = false;
        }
      },
      /**新增按钮 */
      handleAdd() {
        this.resetForm();
        this.optType='add';
        this.editConnectVisible = true;
      },
      /**编辑 */
      updateRow(row, idx) {
        this.resetForm();
        Object.assign(this.form, row);
        this.currentRowIdx = idx;
        this.optType='edit';
        this.editConnectVisible = true;
      },
      /**新增 */
      configmSelect() {
        if (this.form.itemCode == null || this.form.itemCode.length == 0) {
          return;
        }
        this.tableData.unshift({
          ...this.form,
          processId: this.process.processId,

        });
        this.editConnectVisible = false;
      },
      /** 提交编辑关联信息 */
      submitEditConnect() {
        this.tableData.splice(this.currentRowIdx, 1, {
          ...this.form,
        });
        this.editConnectVisible = false;
      },
      addOrEditubmit(){
        if(this.optType=='add'){
          this.configmSelect();
        }else{
          this.submitEditConnect();
        }
      },
      deleteRow(index) {
        this.tableData.splice(index, 1);
      },
      cancleConnect() {
        this.$emit("updateTool", this.tableData);
        this.showProcessProd = false;
      },
      beforeClose(done) {
        // 更新父组件的processItemList
        this.$emit("updateTool", this.tableData);
        done();
      },
      /**重置表单 */
      resetForm() {
        this.form = {
          itemCode: "",
          itemName: "",
          specification:"",
          itemTypeName:"",
          remark: "",
          quantity: "",
          toolId:"",
        };
      },
      onItemSelect(row){
        if (row != undefined && row != null) {
          this.form.itemCode = row.itemCode;
          this.form.itemName = row.itemName;
          this.form.specification = row.specification;
          this.form.itemTypeName = row.itemTypeName;
          this.form.toolId = row.itemId;
        }
      }
    },
  };
</script>

<style>
  .process-prod {
    padding: 0 20px;
  }
</style>
