<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="物料名称" prop="sapItemName" align="center" />
        <el-table-column label="单位" prop="unitOfMeasure" align="center" />
        <el-table-column label="数量" prop="quantity" align="center" />
        <el-table-column label="备注" prop="remark" align="center" />
        <el-table-column
          label="操作"
          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)"
              >修改</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              size="small"
              @click="deleteRow($index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150"
          v-if="optType === 'detail'"
        >
          <template slot-scope="{ row, $index }">
            <el-button
              type="text"
              icon="el-icon-search"
              size="small"
              @click="searchRow(row, $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>

    <!-- 选择Bom物料 -->
    <el-dialog
      :visible.sync="showFlag"
      title="选择Bom物料"
      width="800px"
      append-to-body
    >
      <el-table
        :data="bomList"
        @current-change="handleRowChange"
        @row-dblclick="handleRowDbClick"
      >
        <el-table-column width="50" align="center">
          <template v-slot="scope">
            <el-radio
              v-model="selectedItemId"
              :label="scope.row.itemId"
              @change="handleRowChange(scope.row)"
              >{{ "" }}</el-radio
            >
          </template>
        </el-table-column>
        <el-table-column label="项目号" prop="idx" />
        <el-table-column label="物料名称" prop="sapItemName" />
        <el-table-column label="组件数量" prop="quantity" />
        <el-table-column label="单位" prop="unitOfMeasure" />
        <el-table-column label="副单位" prop="deputyUnitOfMeasure" />
        <el-table-column label="主副单位转换比例" prop="unitConvert" />
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="反冲" prop="recoil" />
      </el-table>
      <div slot="footer">
        <el-button @click="configmSelect" v-if="!(optType === 'detail')"
          >确认</el-button
        >
        <el-button @click="showFlag = false">取消</el-button>
      </div>
    </el-dialog>

    <!-- 编辑物料关联信息 -->
    <el-dialog
      :visible.sync="editConnectVisible"
      width="400px"
      title="编辑物料关联信息"
    >
      <el-form :model="form" inline size="small" label-width="80px">
        <el-form-item label="物料名称">
          <el-input disabled v-model="form.sapItemName"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input type="number" v-model="form.quantity"></el-input>
        </el-form-item>
        <el-form-item label="单位">
          <el-input disabled v-model="form.unitOfMeasure"></el-input>
        </el-form-item>
        <el-form-item label="副单位">
          <el-input v-model="form.deputyUnitOfMeasure"></el-input>
        </el-form-item>
        <el-form-item label="主副单位转换比例">
          <el-input type="number" v-model="form.unitConvert"></el-input>
        </el-form-item>
        <el-form-item label="反冲">
          <el-input v-model="form.idx"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          type="primary"
          size="small"
          @click="submitEditConnect"
          v-if="!(optType === 'detail')"
          >确认</el-button
        >
        <el-button size="small" @click="editConnectVisible = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    bomList: {
      type: Array,
      default: () => [],
    },
    process: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      form: {
        productionSolutionProcessItemId: "",
        productionSolutionProcessId: "",
        sapItemCode: "",
        sapItemName: "",
        usageCode: "",
        unitOfMeasure: "",
        deputyUnitOfMeasure: "",
        unitConvert: "",
        quantity: "",
        remark: "",
        idx: "",
        recoil: "",
      },
      currentRowIdx: undefined,
      tableData: [],
      selectedItemId: undefined,
      selectedRows: null,
      showFlag: false,
      showProcessProd: false,
      editConnectVisible: false,
    };
  },
  computed: {
    title() {
      return "设置" + this.process.itemName + "物料";
    },
  },
  watch: {
    process: {
      handler(val) {
        this.tableData = val.processItemList;
      },
      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.showFlag = true;
    },
    /**确认选择关联的Bom */
    configmSelect() {
      if (this.selectedItemId == null || this.selectedItemId == 0) {
        return;
      }
      this.tableData.unshift({
        ...this.selectedRows,
        processId: this.process.processId,
      });
      this.showFlag = false;
    },
    /**编辑 */
    updateRow(row, idx) {
      this.resetForm();
      Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.editConnectVisible = true;
    },
    deleteRow(index) {
      this.tableData.splice(index, 1);
    },
    cancleConnect() {
      this.$emit("updateItem", this.tableData);
      this.showProcessProd = false;
    },
    beforeClose(done) {
      // 更新父组件的processItemList
      this.$emit("updateItem", this.tableData);
      done();
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        productionSolutionProcessItemId: "",
        productionSolutionProcessId: "",
        sapItemCode: "",
        sapItemName: "",
        usageCode: "",
        unitOfMeasure: "",
        deputyUnitOfMeasure: "",
        unitConvert: "",
        quantity: "",
        remark: "",
        idx: "",
        recoil: "",
      };
    },
    /** 提交编辑关联信息 */
    submitEditConnect() {
      this.tableData.splice(this.currentRowIdx, 1, {
        ...this.form,
      });
      this.editConnectVisible = false;
    },
    /**查看 */
    searchRow(row, idx) {
      this.resetForm();
      Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      this.editConnectVisible = true;
    },
  },
};
</script>

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