<template>
  <div>
    <el-table :data="tableData">
      <el-table-column label="序号" prop="idx" align="center" />
      <el-table-column label="工序名称" prop="processName" align="center" />
      <el-table-column label="工作站" prop="workstationName" align="center" />
      <el-table-column label="标准工时" prop="stdWorkingTime" align="center" />
      <el-table-column label="换行时间" prop="lineBreakTime" align="center" />
      <el-table-column
        label="工时单位"
        prop="stdWorkingTimeUom"
        align="center"
      />
      <el-table-column
        label="加工方式"
        prop="processingMethod"
        align="center"
        width="215"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.process_control_code"
            :value="scope.row.processingMethod"
          />
        </template>
      </el-table-column>
      <el-table-column label="基本数量" prop="quantity" align="center" />
      <el-table-column label="计量单位" prop="unitOfMeasure" align="center" />
      <el-table-column label="雇员数量" prop="employeesNum" align="center" />
      <el-table-column label="关键工序" prop="keyFlag" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yes_no"
            :value="scope.row.keyFlag"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否质检" prop="isCheck" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yes_no"
            :value="scope.row.isCheck"
          />
        </template>
      </el-table-column>
      <el-table-column label="工序备注" prop="processRemark" align="center" />
      <el-table-column
        v-if="mode != 'info'"
        label="操作"
        align="center"
        width="300"
        fixed="right"
      >
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="small" @click="openSetProd(row, $index)">
            设置物料
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="openSetQrindex(row, $index)"
          >
            设置检验项
          </el-button>
          <el-button type="text" size="small" @click="openSetTool(row, $index)">
            设置工装量具
          </el-button>
          <el-button type="text" size="small" @click="updateRow(row, $index)"
            >修改</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="mode == 'info'"
        label="操作"
        align="center"
        width="310"
        fixed="right"
      >
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="small" @click="SearchProd(row, $index)">
            查看物料
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="searchQrindex(row, $index)"
          >
            查看检验项
          </el-button>
          <el-button type="text" size="small" @click="searchTool(row, $index)">
            查看工装量具
          </el-button>
          <el-button type="text" size="small" @click="searchRow(row, $index)"
            >查看工序信息</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <ProcessProd
      :process="currentRow"
      :bomList="bomList"
      ref="ProcessProdRef"
      @updateItem="updateItem"
    />
    <ProcessQcindex :process="currentRow" ref="ProcessQcindexRef" />
    <ProcessTool :process="currentRow" ref="ProcessToolRef" />

    <el-dialog
      v-dialogDrag
      :visible.sync="showFlag"
      width="800px"
      title="工序设置"
    >
      <el-form
        ref="form"
        class="page-form"
        :model="form"
        size="small"
        :inline="true"
        label-width="8em"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作站" prop="workstationId">
              <el-input
                v-model="form.workstationName"
                disabled
                placeholder="请选择工作站信息"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleWorkstationSelect"
                ></el-button>
              </el-input>
              <WorkstationSelect
                ref="WorkstationSelect"
                :processId="form.processId"
                @onSelected="onWorkstationSelected"
              ></WorkstationSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准工时" prop="stdWorkingTime">
              <el-input
                type="number"
                v-model="form.stdWorkingTime"
                placeholder="请输入标准工时"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="换型时间" prop="lineBreakTime">
              <el-input
                type="number"
                v-model="form.lineBreakTime"
                placeholder="请输入换型时间"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工时单位" prop="stdWorkingTimeUom">
              <el-select
                v-model="form.stdWorkingTimeUom"
                clearable
                placeholder="请选择工时单位"
              >
                <el-option
                  v-for="dict in dict.type.mes_time_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </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-col :span="12">
            <el-form-item label="工序计量单位" prop="unitOfMeasure">
              <el-input
                v-model="form.unitOfMeasure"
                placeholder="请输入工序计量单位"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="雇员数量" prop="employeesNum">
              <el-input
                type="number"
                v-model="form.employeesNum"
                placeholder="请输入雇员数量"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="加工方式" prop="processingMethod">
              <el-select
                v-model="form.processingMethod"
                placeholder="请选择加工方式"
                clearable
                style="width: 150px"
              >
                <el-option
                  v-for="dict in dict.type.process_control_code"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="form.processingMethod == 'PP02'">
            <el-col :span="12">
              <el-form-item
                label="采购信息记录号"
                prop="procurementInformationId"
              >
                <el-input
                  v-model="form.procurementInformationId"
                  placeholder="请输入采购信息记录号"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="供应商科目编号" prop="vendorSubjectId">
                <el-input
                  v-model="form.vendorSubjectId"
                  placeholder="请输入供应商科目编号"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="净价" prop="netPrice">
                <el-input
                  v-model="form.netPrice"
                  placeholder="请输入净价"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="采购组织" prop="procurementOrganization">
                <el-input
                  v-model="form.procurementOrganization"
                  placeholder="请输入采购组织"
                ></el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="12">
            <el-form-item label="关键工序" prop="keyFlag">
              <el-select
                v-model="form.keyFlag"
                placeholder="请选择是否为关键工序"
                clearable
                style="width: 150px"
              >
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否质检" prop="isCheck">
              <el-select
                v-model="form.isCheck"
                placeholder="请选择是否质检"
                clearable
                style="width: 150px"
              >
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="吹膜宽度" prop="blownFilmWidth">
              <el-input
                type="number"
                v-model="form.blownFilmWidth"
                placeholder="请输入吹膜宽度"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="吹膜厚度" prop="blownFilmThickness">
              <el-input
                type="number"
                v-model="form.blownFilmThickness"
                placeholder="请输入吹膜厚度"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="印数宽度" prop="printingWidth">
              <el-input
                type="number"
                v-model="form.printingWidth"
                placeholder="请输入印数宽度"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="形状" prop="shape">
              <el-select
                v-model="form.shape"
                clearable
                placeholder="请选择形状"
              >
                <el-option
                  v-for="dict in dict.type.product_shape"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Inlay" prop="inlay">
              <el-input v-model="form.inlay" placeholder="Inlay"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="印张长度" prop="printLength">
              <el-input
                type="number"
                v-model="form.printLength"
                placeholder="请输入印张长度"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="印张宽度" prop="printWidth">
              <el-input
                type="number"
                v-model="form.printWidth"
                placeholder="请输入印张宽度"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button type="default" @click="showFlag = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ItemBomSelect from "@/components/itemBomSelect/single.vue";
import ProcessProd from "./ProcessProd.vue";
import ProcessQcindex from "./ProcessQcindex.vue";
import ProcessTool from "./ProcessTool.vue";
import WorkstationSelect from "@/components/workstationSelect/simpletableSingle.vue";
export default {
  components: {
    ItemBomSelect,
    ProcessProd,
    ProcessQcindex,
    ProcessTool,
    WorkstationSelect,
  },
  inject: ["mode"],
  dicts: [
    "process_control_code",
    "sys_yes_no",
    "mes_time_type",
    "product_shape",
  ],
  props: {
    bomList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      /**弹窗状态 */
      showFlag: false,
      /**表格数据 */
      tableData: [],
      /**表单规则 */
      rules: {},
      /**表单初始化数据 */
      form: {
        productionSolutionId: "",
        recordId: "",
        processId: "",
        workstationId: "",
        keyFlag: "",
        isCheck: "",
        idx: "",
        workstationCode: "",
        processingMethod: "",
        processName: "",
        processRemark: "",
        quantity: "",
        unitOfMeasure: "",
        employeesNum: "",
        stdWorkingTime: "",
        lineBreakTime: "",
        stdWorkingTimeUom: "",
        controlCode: "",
        procurementInformationId: "",
        vendorSubjectId: "",
        netPrice: "",
        procurementOrganization: "",
        remark: "",
        blownFilmWidth: "",
        blownFilmThickness: "",
        printingWidth: "",
        shape: "",
        inlay: "",
        printLength: "",
        printWidth: "",
      },
      currentRow: {},
      currentRowIndex: undefined,
    };
  },
  methods: {
    setList(rows) {
      this.tableData = rows.map((item) => {
        item.processItemList = [];
        item.qcindexList = [];
        item.toolList = [];
        return item;
      });
    },
    /** 更新工序的物料信息 */
    updateItem(items) {
      this.tableData[this.currentRowIndex].processItemList = items;
    },
    openSetProd(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessProdRef"].showProcessProd = true;
    },
    openSetQrindex(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessQcindexRef"].showProcessProd = true;
    },
    openSetTool(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessToolRef"].showProcessProd = true;
    },
    /**查看物料 */
    SearchProd(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessProdRef"].optType = "detail";
      this.$refs["ProcessProdRef"].showProcessProd = true;
    },
    /**查看行 */
    searchRow(row, idx) {
      this.form = Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      this.showFlag = true;
    },
    /**查看质检 */
    searchQrindex(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessQcindexRef"].optType = "detail";
      this.$refs["ProcessQcindexRef"].showProcessProd = true;
    },
    /**查看工装 */
    searchTool(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessToolRef"].optType = "detail";
      this.$refs["ProcessToolRef"].showProcessProd = true;
    },
    /**更新行 */
    updateRow(row, idx) {
      Object.assign(this.form, row);
      this.currentRowIndex = idx;
      this.showFlag = true;
    },
    /**表单提交 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.showFlag = false;
          this.tableData.splice(this.currentRowIndex, 1, {
            ...this.currentRow,
            ...this.form,
          });
        }
      });
    },
    getComData() {
      return this.tableData;
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        productionSolutionId: "",
        recordId: "",
        processId: "",
        workstationId: "",
        keyFlag: "",
        isCheck: "",
        idx: "",
        workstationCode: "",
        processingMethod: "",
        processName: "",
        processRemark: "",
        quantity: "",
        unitOfMeasure: "",
        employeesNum: "",
        stdWorkingTime: "",
        lineBreakTime: "",
        stdWorkingTimeUom: "",
        controlCode: "",
        procurementInformationId: "",
        vendorSubjectId: "",
        netPrice: "",
        procurementOrganization: "",
        remark: "",
        blownFilmWidth: "",
        blownFilmThickness: "",
        printingWidth: "",
        shape: "",
        inlay: "",
        printLength: "",
        printWidth: "",
      };
    },
    /**重置组件状态 */
    resetState() {
      this.resetForm();
      this.tableData = [];
    },
    // 查询工作站信息
    handleWorkstationSelect() {
      this.$refs.WorkstationSelect.showFlag = true;
    },
    onWorkstationSelected(row) {
      if (row != undefined && row != null) {
        this.form.workstationId = row.workstationId;
        this.form.workstationName = row.workstationName;
        this.form.stdWorkingTime = row.stdWorkingTime;
      }
    },
  },
};
</script>

<style></style>
