<template>
  <div class="process-prod">
    <!-- 设置工装量具弹窗 -->
    <el-dialog
      :visible.sync="showProcessProd"
      width="800px"
      :before-close="beforeClose"
      append-to-body
      :title="$t('设置工装量具')"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-if="!(optType === 'detail')"
            >{{ $t('common.add') }}</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="tableData">
        <el-table-column :label="$t('编号')" align="center" prop="itemCode">
        </el-table-column>
        <el-table-column
          :label="$t('名称')"
          align="center"
          prop="itemName"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          :label="$t('型号')"
          align="center"
          prop="specification"
          :show-overflow-tooltip="true"
        />
        <el-table-column :label="$t('类型')" align="center" prop="itemTypeName" />
        <el-table-column :label="$t('要求数量')" align="center" prop="quantity" />
        <el-table-column :label="$t('common.remark')" prop="remark" align="center" />
        <el-table-column
          :label="$t('common.option')"
          align="center"
          width="150"
          v-if="!(optType === 'detail')"
        >
          <template slot-scope="{ row, $index }">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="small"
              @click="updateRow(row, $index)"
              >{{ $t('common.edit') }}</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              size="small"
              @click="deleteRow($index)"
              >{{ $t('common.del') }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('common.option')"
          align="center"
          width="150"
          v-if="optType === 'detail'"
        >
          <template slot-scope="{ row, $index }">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="small"
              @click="searchRow(row, $index)"
              >{{$t('查看详情')}}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button type="primary" size="small" @click="cancleConnect"
          >{{$t('role.close')}}</el-button
        >
      </div>
    </el-dialog>

    <!-- 编辑工装量具关联信息 -->
    <el-dialog
      :visible.sync="editConnectVisible"
      width="800px"
      :title="$t('工装量具关联信息')"
    >
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('工装量具编码')" prop="toolCode">
              <el-input
                v-model="form.itemCode"
               
                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="$t('工装量具名称')" 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="$t('要求数量')" prop="quantity">
              <el-input
                type="number"
                v-model="form.quantity"
               
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('common.remark')" 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"
          v-if="!(optType === 'detail')"
          >{{$t('确认')}}</el-button
        >
        <el-button size="small" @click="editConnectVisible = false"
          >{{ $t('common.cancel') }}</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 {
      optType: "add",
      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.$t('设置') + this.process.itemName + this.$t('工装量具');
    },
  },
  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;
    },
    /**查看 */
    searchRow(row, idx) {
      this.resetForm();
      Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      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>
