<template>
  <div>
    <el-table :data="tableData">
      <el-table-column label="序号" prop="idx" width="80" align="center"  />
      <el-table-column label="工序名称" prop="processName" align="center" />
      <el-table-column label="工序编码" prop="processCode" align="center" />
      <el-table-column label="工作中心" prop="workstationName" align="center" />
      <el-table-column v-if="mode === 'make'" label="工作单元名称" width="200" prop="workunitName" align="center" >
        <template slot-scope="scope">
          <el-input readonly v-model="scope.row.workunitName">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="handleWorkUnitClick(scope.row)"
            ></el-button>
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="工作单元名称" v-if="mode === 'makeInfo'" prop="workunitName" 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="quantity" align="center" />
      <el-table-column label="加工方式" prop="controlCode" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sap_control_code"
            :value="scope.row.controlCode"
          />
        </template>
      </el-table-column>
      <el-table-column label="标准文本代码" prop="x" align="center" />
      <el-table-column width="220" v-if="mode === 'make'" label="开始时间" align="center" prop="startTime" >
        <template slot-scope="scope">
          <el-date-picker
            v-model="scope.row.startTime"
            type="datetime"
            style="width:200px;"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始时间"
          >
          </el-date-picker>
        </template>
      </el-table-column>
      <el-table-column width="180" label="开始时间" v-if="mode === 'makeInfo'" prop="startTime" align="center" />
      <el-table-column width="220" v-if="mode === 'make'" label="结束时间" align="center" prop="endTime" >
        <template slot-scope="scope">
          <el-date-picker
            v-model="scope.row.endTime"
            type="datetime"
            style="width:200px;"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束时间"
          >
          </el-date-picker>
        </template>
      </el-table-column>
      <el-table-column width="180" label="结束时间" v-if="mode === 'makeInfo'" prop="endTime" align="center" />
      <el-table-column
         v-if="!(mode === 'info'|| mode === 'make' || mode === 'makeInfo')"
        label="操作"
        align="center"
        width="120"
        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'|| mode == 'make'|| mode === 'makeInfo'"
        label="操作"
        align="center"
        width="120"
        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" />

    <WorkuintSelect
      ref="WorkunitSelect"
      :workstationName="form.workstationName"
      @onSelected="onWorkunitSelect"
    />

    <el-dialog
      v-dialogDrag
      :visible.sync="showFlag"
      width="800px"
      title="工序修改"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        inline
        label-width="100px"
        size="small"
      >
        <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-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 WorkuintSelect from "@/components/workunitSelect/single.vue";
import ProcessQcindex from "./ProcessQcindex.vue";
import ProcessTool from "./ProcessTool.vue";
import ItemBomSelect from "@/components/itemBomSelect/single.vue";
import ProcessProd from "./ProcessProd.vue";
import WorkstationSelect from "@/components/workstationSelect/simpletableSingle.vue";
export default {
  components: {
    ItemBomSelect,
    ProcessProd,
    WorkstationSelect,
    ProcessQcindex,
    ProcessTool,
    WorkuintSelect
  },
  inject: ["mode"],
  dicts: ["sap_control_code", "sys_yes_no"],
  props: {
    bomList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      /**弹窗状态 */
      showFlag: false,
      /**表格数据 */
      tableData: [],
      /**表单规则 */
      rules: {},
      /**表单初始化数据 */
      optType: "add",
      form: {
        stdWorkingTime: "",
        workstationId: "",
        workstationName: "",
        processRef: "",
        controlCode: "",
        stdWorkingTimeUom: "",
        idx: "",
        x: "",
        quantity: "",
        stdWorkingTime: "",
        lineBreakTime: "",
      },
      currentRow: {},
      currentRowIndex: undefined,
    };
  },
  methods: {
    setList(rows) {
      this.tableData = rows.map((item) => {
        item.processItemList = [];
        item.toolList = [];
        item.qcindexList = [];
        return item;
      });
    },
    handleWorkUnitClick(row) {
      this.currentRow = row
      this.$refs['WorkunitSelect'].showFlag = true
    },
    /** 更新工序的物料信息 */
    updateItem(items) {
      this.tableData[this.currentRowIndex].processItemList = items;
    },
    updateTool(items) {
      this.tableData[this.currentRowIndex].toolList = items;
    },
    updateQcindex(items) {
      this.tableData[this.currentRowIndex].qcindexList = items;
    },
    openSetProd(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessProdRef"].showProcessProd = true;
    },
    // 获取工作单元
    onWorkunitSelect(row) {
      // console.log(45456, row);
      if (row) {
        this.currentRow.workunitName = row.workunitName
        this.currentRow.workunitId = row.workunitId
        this.currentRow.workunitCode = row.workunitCode
      }
    },
    /**查看物料 */
    SearchProd(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessProdRef"].optType = "detail";
      this.$refs["ProcessProdRef"].showProcessProd = true;
    },
    /**更新行 */
    updateRow(row, idx) {
      Object.assign(this.form, row);
      this.currentRowIndex = idx;
      this.showFlag = true;
    },
    /**查看行 */
    searchRow(row, idx) {
      this.form = Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      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 = {
        stdWorkingTime: "",
        workstationId: "",
        workstationName: "",
        processRef: "",
        controlCode: "",
        stdWorkingTimeUom: "",
        idx: "",
        x: "",
        quantity: "",
        stdWorkingTime: "",
        lineBreakTime: "",
      };
    },
    /**重置组件状态 */
    resetState() {
      this.resetForm();
      this.tableData = [];
    },
    /**质检 */
    openSetQrindex(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessQcindexRef"].showProcessProd = true;
    },
    /**查看质检 */
    searchQrindex(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessQcindexRef"].optType = "detail";
      this.$refs["ProcessQcindexRef"].showProcessProd = true;
    },
    /**工装 */
    openSetTool(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessToolRef"].showProcessProd = true;
    },
    /**查看工装 */
    searchTool(row, idx) {
      this.currentRow = row;
      this.currentRowIndex = idx;
      this.$refs["ProcessToolRef"].optType = "detail";
      this.$refs["ProcessToolRef"].showProcessProd = true;
    },
    // 查询工作中心信息
    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>
