<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="routeCode">
        <el-input
          v-model="queryParams.routeCode"
          placeholder="请输入工艺路线编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺路线名称" prop="routeName">
        <el-input
          v-model="queryParams.routeName"
          placeholder="请输入工艺路线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enableFlag">
        <el-input
          v-model="queryParams.enableFlag"
          placeholder="请输入是否启用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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.routeId"
            @change="handleRowChange(scope.row)"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="工艺路线编号" align="center" prop="routeCode" />

      <el-table-column label="工艺路线名称" align="center" prop="routeName" />
      <el-table-column label="工艺路线说明" align="center" prop="routeDesc" />
      <el-table-column label="是否启用" align="center" prop="enableFlag" />
    </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="nextProcessName"
          />
          <el-table-column
            label="与下一道工序关系"
            width="150px"
            align="center"
            prop="linkType"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.mes_link_type"
                :value="scope.row.linkType"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="关键工序"
            width="100px"
            align="center"
            prop="keyFlag"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.sys_yes_no"
                :value="scope.row.keyFlag"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="质检确认"
            width="100px"
            align="center"
            prop="isCheck"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.sys_yes_no"
                :value="scope.row.isCheck"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="准备时间"
            align="center"
            prop="defaultPreTime"
          />
          <el-table-column
            label="等待时间"
            align="center"
            prop="defaultSufTime"
          />
        </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 { listProroute } from "@/api/mes/pro/proroute";
import { listRouteprocess } from "@/api/mes/pro/routeprocess";

export default {
  name: "RoutesProcessSelectSingle",
  components: {},
  dicts: ["sys_yes_no", "mes_link_type", "sys_yes_no"],
  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,
        routeCode: null,
        routeName: null,
        routeDesc: null,
        enableFlag: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工厂列表 */
    getList() {
      this.loading = true;
      listProroute(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,
        routeId: row.routeId,
      };
      this.processLoading = true;
      listRouteprocess(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;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRouteId == null || this.selectedRouteId == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
