<template>
  <el-dialog :title="$t('工艺路线选择')" v-if="showFlag" :visible.sync="showFlag" :modal="true" width="80%" append-to-body center>
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" inline v-show="showSearch" label-width="110px">
      <el-form-item :label="$t('成品物料号')" prop="itemCode">
        <el-input v-model="queryParams.itemCode" clearable @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('成品物名称')" prop="itemName">
        <el-input v-model="queryParams.itemName" clearable @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="processRoutesList" highlight-current-row @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick" height="400">
      <el-table-column width="50" align="center">
        <template v-slot="scope">
          <el-radio v-model="selectedRouteId" :label="scope.row.productionSolutionId"
            @change="handleRowChange(scope.row)">{{ "" }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column :label="$t('生产版本编码')" align="center" prop="productionSolutionCode" />
      <el-table-column :label="$t('产品版本名称')" align="center" prop="productionSolutionName" :show-overflow-tooltip="true" />
      <el-table-column :label="$t('成品物料编码')" align="center" prop="itemCode" />
      <el-table-column :label="$t('成品物料名称')" align="center" prop="itemName" />
      <el-table-column :label="$t('生产用途')" align="center" prop="usageName" />
      <el-table-column :label="$t('common.remark')" align="center" prop="remark" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-tabs type="border-card" style="margin-top: 30px">
      <el-tab-pane :label="$t('工序')">
        <el-table v-loading="processLoading" :data="processList">
          <el-table-column :label="$t('工序序号')" align="center" fixed prop="idx" />
          <el-table-column :label="$t('工序编码')" width="120px" fixed align="center" prop="processCode" />
          <el-table-column :label="$t('工序名称')" width="150px" fixed align="center" prop="processName" />
          <el-table-column :label="$t('标准工时')" width="150px" align="center" prop="stdWorkingTime" />
          <el-table-column :label="$t('common.workstation_name')" width="150px" align="center" prop="workstationName" />
          <el-table-column :label="$t('common.workstation_code')" width="150px" align="center" prop="workstationCode" />
          <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>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">{{ $t('common.confirm') }}</el-button>
      <el-button @click="showFlag = false">{{ $t('common.cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>




import {
  listProductionSolution,
} from "@/api/mes/pro/productionSolution";
import { listProductionSolutionProcess } from "@/api/mes/pro/productionSolutionProcess";
export default {
  name: "RoutesProcessSelectSingle",
  components: {},
  props: {
    itemCode: null,
  },
  dicts: ["mes_work_time"],
  data() {
    return {
      showFlag: false,
      // 选中数组
      selectedRouteId: undefined,
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      processList: [],
      // 品牌表格数据
      processRoutesList: [],
      // // 品牌名称
      // brandName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      processLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productionSolutionId: null, // 可以查简称与英文名
        productionSolutionCode: null,
        productionSolutionName: null,
        statsu: "PREPARE",
        itemId: null,
        groupCounter: null,
        itemCode: null,
        itemName: null,
        routeId: null,
        usageId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    itemCode: {
      handler(val) {
        if (val) {
          this.queryParams.itemCode = this.itemCode
          this.getList()
        }
      }, 
      immediate: true
    }
  },
  methods: {
    /** 查询工厂列表 */
    getList() {
      this.loading = true;
      listProductionSolution(this.queryParams).then((response) => {
        this.processRoutesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      const params = {
        pageNum: 1,
        pageSize: 1000,
        productionSolutionId: row.productionSolutionId,
      };
      this.processLoading = true;
      listProductionSolutionProcess(params)
        .then(({ rows }) => {
          this.processList = rows;
          this.processLoading = false;
        })
        .catch(() => {
          this.processLoading = false;
        });
      if (row) {
        this.selectedRows = row;
      }
    },
    getProcess() { },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //双击选中
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },

    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        productionSolutionId: null, // 可以查简称与英文名
        productionSolutionCode: null,
        productionSolutionName: null,
        statsu: "PREPARE",
        itemId: null,
        groupCounter: null,
        itemCode: null,
        itemName: null,
        routeId: null,
        usageId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("queryForm");
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRouteId == null || this.selectedRouteId == 0) {
        this.$notify({
          title: this.$t('common.sys_tip'),
          type: "warning",
          message: this.$t('请至少选择一条数据!'),
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
