<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="indexCode" />
        <el-table-column label="检测项名称" align="center" prop="indexName" />
        <el-table-column label="检测项类型" align="center" prop="indexType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.mes_index_type"
              :value="scope.row.indexType"
            />
          </template>
        </el-table-column>
        <el-table-column label="检测工具" align="center" prop="qcTool" />
        <el-table-column label="备注" prop="remark" align="center" />
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="{ row, $index }">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="small"
              @click="updateRow(row, $index)"
              >修改</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              size="small"
              @click="deleteRow($index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </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="indexCode">
              <el-input
                v-model="form.indexName"
                placeholder="请选择测项编码"
                readonly
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['QcindexSelectRef'].showFlag = true"
                ></el-button>
              </el-input>
              <QcindexSelect
                ref="QcindexSelectRef"
                @onSelected="onQcindexSelect"
              />
            </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 QcindexSelect from "@/components/qcindexSelect/single.vue";
export default {
  props: {
    process: {
      type: Object,
      default: () => ({}),
    },
  },
  components: { QcindexSelect },
  dicts: ["mes_index_type"],
  data() {
    return {
      form: {
        indexCode: "",
        indexName: "",
        indexType: "",
        qcTool: "",
        qcindexId: "",
        remark: "",
      },
      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.qcindexList;
      },
      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.indexCode == null || this.form.indexCode == 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("updateQcindex", this.tableData);
      this.showProcessProd = false;
    },
    beforeClose(done) {
      // 更新父组件的processItemList
      this.$emit("updateQcindex", this.tableData);
      done();
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        indexCode: "",
        indexName: "",
        indexType: "",
        qcTool: "",
        qcindexId: "",
        remark: "",
      };
    },
    onQcindexSelect(row) {
      if (row != undefined && row != null) {
        this.form.indexCode = row.indexCode;
        this.form.indexName = row.indexName;
        this.form.qcTool = row.qcTool;
        this.form.qcindexId = row.indexId;
      }
    },
  },
};
</script>

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