<template>
  <div>
    <el-table :data="tableData">
      <el-table-column :label="$t('工序序号')" prop="idx" width="80" align="center"  />
      <el-table-column :label="$t('工序编码')" prop="processCode" width="150" align="center" />
      <el-table-column :label="$t('工序名称')" prop="processName" align="center" />
      <el-table-column :label="$t('common.workstation_code')" prop="workstationCode" width="150" align="center" />
      <el-table-column :label="$t('common.workstation')" prop="workstationName" align="center" />
      <el-table-column v-if="mode === 'make'" :label="$t('common.workunit_name')" 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="$t('common.workunit_name')" v-if="mode === 'makeInfo'" prop="workunitName" align="center" />
      <el-table-column :label="$t('标准工时')" prop="stdWorkingTime" align="center" />
      <el-table-column :label="$t('换型时间')" prop="lineBreakTime" align="center" />
      <el-table-column :label="$t('工时单位')" prop="stdWorkingTimeUom" align="center" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_work_time" :value="scope.row.stdWorkingTimeUom" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('基本数量')" prop="quantity" align="center" />
      <el-table-column :label="$t('委外数量')" prop="assistQuantity" align="center" />
      <el-table-column :label="$t('是否委外')" align="center" prop="outsourced" >
        <template slot-scope="scope">
          {{scope.row.outsourced === 'Y' ? $t('是') : $t('否')}}
        </template>
       </el-table-column>
      <el-table-column :label="$t('备注')" prop="remark" width="150" align="center" />
      <el-table-column width="220" v-if="mode === 'make'" :label="$t('开始时间')" 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"
           
          >
          </el-date-picker>
        </template>
      </el-table-column>
      <el-table-column width="180" :label="$t('开始时间')" v-if="mode === 'makeInfo'" prop="startTime" align="center" />
      <el-table-column width="220" v-if="mode === 'make'" :label="$t('结束时间')" 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"
           
          >
          </el-date-picker>
        </template>
      </el-table-column>
      <el-table-column width="180" :label="$t('结束时间')" v-if="mode === 'makeInfo'" prop="endTime" align="center" />
      <el-table-column
         v-if="!(mode === 'info'|| mode === 'make' || mode === 'makeInfo')"
        :label="$t('common.option')"
        align="center"
        width="120"
        fixed="right"
      >
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="small" @click="openSetProd(row, $index)">{{$t('设置物料')}}</el-button>
          <el-button
            type="text"
            size="small"
            @click="openSetQrindex(row, $index)"
          >{{$t('设置检验项')}}</el-button>
          <el-button type="text" size="small" @click="openSetTool(row, $index)">{{$t('设置工装量具')}}</el-button>
          <!-- <el-button type="text" size="small" @click="updateRow(row, $index)"
            >{{ $t('common.edit') }}</el-button
          > -->
        </template>
      </el-table-column>
      <el-table-column
        v-if="mode == 'info'|| mode == 'make'|| mode === 'makeInfo'"
        :label="$t('common.option')"
        align="center"
        width="120"
        fixed="right"
      >
        <template slot-scope="{ row, $index }">
          <el-button type="text" size="small" @click="SearchProd(row, $index)">{{$t('查看物料')}}</el-button>
          <el-button
            type="text"
            size="small"
            @click="searchQrindex(row, $index)"
          >{{$t('查看检验项')}}</el-button>
          <el-button type="text" size="small" @click="searchTool(row, $index)">{{$t('查看工装量具')}}</el-button>
          <el-button type="text" size="small" @click="clickAssi(row, $index)" v-if="row.proTaskAssistProcessList && row.proTaskAssistProcessList.length > 0"
            >{{$t('查看委外')}}</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="assiVisible"
      width="800px"
      :title="$t('委外详情')"
    >
      <el-table :data="proTaskAssistProcessList">
        <el-table-column :label="$t('外协单号')" prop="assistProcessCode" align="center" />
        <el-table-column :label="$t('外协项次')" prop="assistProcessItemCode" align="center" />
        <el-table-column :label="$t('供应商')" prop="supplierName" align="center" />
        <el-table-column :label="$t('数量')" prop="quantity" align="center" />
        <el-table-column :label="$t('单位')" prop="unit" align="center" />
        <el-table-column :label="$t('需求到货时间')" prop="scheduleEndDate" align="center" />
        <el-table-column :label="$t('实际到货时间')" prop="returnTime" align="center" />
      </el-table>
    </el-dialog>
    <el-dialog
      v-dialogDrag
      :visible.sync="showFlag"
      width="800px"
      :title="$t('工序修改')"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        inline
        label-width="100px"
        size="small"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('common.workstation')" prop="workstationId">
              <el-input
                v-model="form.workstationName"
                disabled
               
              >
                <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="$t('标准工时')" prop="stdWorkingTime">
              <el-input
                type="number"
                v-model="form.stdWorkingTime"
               
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }}</el-button>
        <el-button type="default" @click="showFlag = false">{{ $t('common.cancel') }}</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: ["process_control_code", "sys_yes_no", "mes_work_time"],
  props: {
    bomList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      /**弹窗状态 */
      showFlag: false,
      assiVisible: false,
      /**表格数据 */
      tableData: [],
      /**表单规则 */
      rules: {},
      /**表单初始化数据 */
      optType: "add",
      form: {
        stdWorkingTime: "",
        workstationId: "",
        workstationName: "",
        processRef: "",
        controlCode: "",
        stdWorkingTimeUom: "",
        idx: "",
        x: "",
        quantity: "",
        stdWorkingTime: "",
        lineBreakTime: "",
      },
      proTaskAssistProcessList: [],
      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;
    },
    // 查看委外
    clickAssi(row, idx) {
      this.proTaskAssistProcessList = row.proTaskAssistProcessList
      this.assiVisible = 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>
