<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"
            v-if="!(optType === 'detail')"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="tableData">
        <el-table-column label="物料名称" prop="itemName" align="center" />
        <el-table-column label="数量" prop="quantity" align="center" />
        <el-table-column label="单位" prop="unitOfMeasure" align="center" />
        <el-table-column label="是否倒冲" prop="isBackflush" align="center">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_yes_no"
              :value="scope.row.isBackflush"
            />
          </template>
        </el-table-column>
        <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" v-if="mode != 'info'">
          <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="itemName" />
        <el-table-column label="数量" prop="quantity" />
        <el-table-column label="单位" prop="unitOfMeasure" />
        <el-table-column label="存储地点" prop="storeAddr" />
        <el-table-column label="报废百分比" prop="scrapPercentage" />
        <el-table-column label="单位用量" prop="unitUsage" />
        <el-table-column label="虚拟项目标识" prop="virtualProjectLog">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_yes_no"
              :value="scope.row.virtualProjectLog"
            />
          </template>
        </el-table-column>
        <el-table-column label="副单位" prop="deputyUnitOfMeasure" />
        <el-table-column label="主副单位转换比例" prop="unitConvert" />
        <el-table-column label="存储地点2" prop="storeAddr2" />
        <el-table-column label="是否倒冲" prop="isBackflush">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_yes_no"
              :value="scope.row.isBackflush"
            />
          </template>
        </el-table-column>
      </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="物料名称" prop="itemName">
          <el-input disabled v-model="form.itemName"></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unitOfMeasure">
          <el-input disabled v-model="form.unitOfMeasure"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input type="number" v-model="form.quantity"></el-input>
        </el-form-item>
        <el-form-item label="存储地点" prop="storeAddr">
          <el-input v-model="form.storeAddr"></el-input>
        </el-form-item>
        <el-form-item label="报废百分比" prop="scrapPercentage">
          <el-input type="number" v-model="form.scrapPercentage"></el-input>
        </el-form-item>
        <el-form-item label="单位用量" prop="unitUsage">
          <el-input type="number" v-model="form.unitUsage"></el-input>
        </el-form-item>
        <el-form-item label="虚拟项目标识" prop="virtualProjectLog">
          <el-select
            v-model="form.virtualProjectLog"
            placeholder="请选择虚拟项目标识"
          >
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="副单位" prop="deputyUnitOfMeasure">
          <el-input v-model="form.deputyUnitOfMeasure"></el-input>
        </el-form-item>
        <el-form-item label="主副单位转换比例" prop="unitConvert">
          <el-input type="number" v-model="form.unitConvert"></el-input>
        </el-form-item>
        <el-form-item label="存储地点2" prop="storeAddr2">
          <el-input v-model="form.storeAddr2"></el-input>
        </el-form-item>
        <el-form-item label="是否倒冲" prop="isBackflush">
          <el-select v-model="form.isBackflush" placeholder="请选择是否倒冲">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </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: () => ({}),
    },
  },
  dicts: ["sys_yes_no"],
  data() {
    return {
      optType: "add",
      form: {
        quantity: "",
        itemName: "",
        itemId: "",
        unitOfMeasure: "",
        scrapPercentage: "",
        isBackflush: "",
        virtualProjectLog: "",
        storeAddr: "",
        unitUsage: "",
        storeAddr2: "",
        processRef: "",
        requirementNumber: "",
        requirementProjectNumber: "",
      },
      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;
    },
    /**查看 */
    searchRow(row, idx) {
      this.resetForm();
      Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      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 = {
        quantity: "",
        itemName: "",
        itemId: "",
        unitOfMeasure: "",
        scrapPercentage: "",
        isBackflush: "",
        virtualProjectLog: "",
        storeAddr: "",
        unitUsage: "",
        storeAddr2: "",
        processRef: "",
        requirementNumber: "",
        requirementProjectNumber: "",
      };
    },
    /** 提交编辑关联信息 */
    submitEditConnect() {
      this.tableData.splice(this.currentRowIdx, 1, {
        ...this.form,
      });
      this.editConnectVisible = false;
    },
  },
};
</script>

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