<template>
  <el-dialog :title="$t('工作中心选择')" v-if="showFlag" :visible.sync="showFlag" :modal="showModal" width="80%" center>
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-row>
        
          <el-form-item :label="$t('工作中心编号')" label-width="120" prop="workstationCode">
            <el-input v-model="queryParams.workstationCode" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        
        
          <el-form-item :label="$t('common.workstation_name')" label-width="120" prop="workstationName">
            <el-input v-model="queryParams.workstationName" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        
        
          <el-form-item :label="$t('所属工序')" label-width="120" prop="processName">
            <el-input v-model="queryParams.processName" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
     
          <el-form-item :label="$t('所在车间')" prop="workshopName">
            <el-select v-model="queryParams.workshopId">
              <el-option v-for="item in workshopOptions" :key="item.workshopId" :label="item.workshopName"
                :value="item.workshopId"></el-option>
            </el-select>
          </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-row>
      <el-row>
        <el-col :span="24"> </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="workstationList" @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick">
      <el-table-column width="55" align="center">
        <template v-slot="scope">
          <el-radio v-model="selectedWorkstationId" :label="scope.row.workstationId"
            @change="handleRowChange(scope.row)">{{ "" }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column :label="$t('工作中心编号')" align="center" prop="workstationCode">
      </el-table-column>
      <el-table-column :label="$t('common.workstation_name')" align="center" prop="workstationName" />
      <el-table-column :label="$t('工作中心地点')" align="center" prop="workstationAddress" />
      <el-table-column :label="$t('所在车间名称')" align="center" prop="workshopName" />
      <el-table-column :label="$t('所属工序')" align="center" prop="processName" />
      <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" />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">{{ $t('common.confirm') }}</el-button>
      <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>


import {
  listWorkstation,
  getWorkstation,
  delWorkstation,
  addWorkstation,
  updateWorkstation,
} from "@/api/mes/md/workstation";
// import { listAllProcess } from "@/api/mes/pro/process";
import { listAllWorkshop } from "@/api/mes/md/workshop";
export default {
  name: "WorkstationSelect",
  dicts: ["sys_yes_no"],
  data() {
    return {
      // processId: this.processId,

      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      selectedWorkstationId: undefined,
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作中心表格数据
      workstationList: [],
      //车间选项
      workshopOptions: [],
      //工序选项
      processOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workstationCode: null,
        workstationName: null,
        workstationAddress: null,
        workshopId: null,
        workshopCode: null,
        workshopName: null,
        processId: null,
        processCode: null,
        processName: null,
        enableFlag: null,
        stdWorkingTime: null,
      },
      // 表单参数
      form: {},
    };
  },
  props: {
    processId: {
      type: Number | undefined,
      default: undefined,
    }, //外部传入的工序过滤条件
    workstationCode: {
      type: Number | undefined,
      default: undefined,
    },
    showModal: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.getList();
    this.getWorkshops();
    this.getProcess();
  },
  watch: {
    processId: {
      handler(val) {
        this.queryParams.processId = val;
        this.getList();
      },
      immediate: true,
    },
    workstationCode: {
      handler(val) {
          this.queryParams.workstationCode = val;
          this.getList();
      },
      immediate: true,
    },
  },
  methods: {
    /** 查询工作中心列表 */
    getList() {
      this.loading = true;
      listWorkstation(this.queryParams).then((response) => {
        console.log(this.queryParams);
        this.workstationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询车间信息
    getWorkshops() {
      listAllWorkshop().then((response) => {
        this.workshopOptions = response.data;
      });
    },
    // //查询工序信息
    // getProcess() {
    //   listAllProcess().then((response) => {
    //     this.processOptions = response.data;
    //   });
    // },
    // 取消按钮
    cancel() {
      this.showFlag=false;
      this.resetQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        workstationAddress: null,
        workshopId: null,
        workshopCode: null,
        workshopName: null,
        processId: null,
        processCode: null,
        processName: null,
        enableFlag: "Y",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        stdWorkingTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        workstationAddress: null,
        workshopId: null,
        workshopCode: null,
        workshopName: null,
        processId: null,
        processCode: null,
        processName: null,
        enableFlag: "Y",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        stdWorkingTime: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    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.selectedWorkstationId == null ||
        this.selectedWorkstationId == 0
      ) {
        this.$notify({
          title: this.$t('common.sys_tip'),
          type: "warning",
          message: this.$t('请至少选择一条数据!'),
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
