<template>
  <el-dialog title="工艺路线选择" v-if="showFlag" :visible.sync="showFlag" :modal="true" width="80%" append-to-body center>
    <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch" label-width="110px">
      <el-form-item label="成品物料号" prop="itemCode">
        <el-input v-model="queryParams.itemCode" clearable placeholder="请输入成品物名称" @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item label="成品物名称" prop="itemName">
        <el-input v-model="queryParams.itemName" clearable placeholder="请输入成品物名称" @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item label="组件号" prop="groupCounter">
        <el-input v-model="queryParams.groupCounter" clearable placeholder="请输入组件号" @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item label="打样订单号" prop="sapPrototypeMakeCode">
        <el-input v-model="queryParams.sapPrototypeMakeCode" clearable placeholder="请输入打样订单号"
          @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</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="生产版本编码" align="center" prop="productionSolutionCode" />
      <el-table-column label="组件号" align="center" prop="groupCounter" />
      <el-table-column label="产品版本名称" align="center" prop="productionSolutionName" :show-overflow-tooltip="true" />
      <el-table-column label="成品物料编码" align="center" prop="itemCode" />
      <el-table-column label="成品物料名称" align="center" prop="itemName" />
      <el-table-column label="生产用途" align="center" prop="usageName" />
      <el-table-column label="打样订单" align="center" prop="sapPrototypeMakeCode" />
      <el-table-column label="备注" 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="工序">
        <el-table v-loading="processLoading" :data="processList">
          <el-table-column label="工序编码" width="120px" fixed align="center" prop="processCode" />
          <el-table-column label="工序名称" width="150px" fixed align="center" prop="processName" />
          <el-table-column label="标准工时" width="150px" align="center" prop="stdWorkingTime" />
          <el-table-column label="工作中心" width="150px" align="center" prop="workstationName" />
          <el-table-column label="工序总量" width="100px" align="center" prop="keyFlag" />
          <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>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</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: {},
  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();
  },
  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.form = {
        productionSolutionId: null,
        productionSolutionCode: null,
        productionSolutionName: null,
        itemId: null,
        groupCounter: null,
        routeId: null,
        usageId: null,
        itemCode: null,
        itemName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRouteId == null || this.selectedRouteId == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
