<template>
  <el-dialog :title="$t('工序查询页面')"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= showModal
    width="80%"
    >
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item :label="$t('所属工序')" prop="processName">
            <el-select v-model="queryParams.processName" :placeholder="$t('请选择工序')" filterable
                  remote
                  :remote-method="getProcess"
                  :loading="loading">
                <el-option
                  v-for="item in processOptions"
                  :key="item.processId"
                  :label="item.processName"
                  :value="item.processName"
                ></el-option>
              </el-select>
          </el-form-item>
          <el-form-item :label="$t('工序编码')" prop="processCode">
            <el-input
              v-model="queryParams.processCode"
              :placeholder="$t('请输入工序编码')"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </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="protaskList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
        <el-table-column  width="55" align="center" >
          <template v-slot="scope">
            <el-radio v-model="selectedTaskId" :label="scope.row.processId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
          </template>
        </el-table-column>
     <el-table-column :label="$t('工序编码')" align="center" prop="processCode"/>

      <el-table-column :label="$t('工序名称')" align="center" prop="processName" />
      <el-table-column :label="$t('是否启用')" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      </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="showFlag=false">{{ $t('common.cancel') }}</el-button>
    </div>
    </el-dialog>
  </template>

  <script>



  import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/mes/pro/process";
  import {listAllProcess} from "@/api/mes/pro/process";
  import WorkstationSelect from "@/components/workstationSelect/simpletableSingle.vue"
  export default {
    name: "ProtaskSelect",
    dicts: ['sys_yes_no'],
    components: {WorkstationSelect},
    props: {
      workorderId: null,
      workorderCode: null,
      processId: null,
      processCode: null,
      workstationId: null,
      workstationCode: null,
      showModal: {
        type: Boolean,
        default: false
      }
    },
    watch: {
      workorderId(v){
        this.queryParams.workorderId = v;
      }
    },
    data() {
      return {
        showFlag: false,
        selectedTaskId: undefined,
        selectedRow: undefined,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 生产任务表格数据
        protaskList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
         pageNum: 1,
        pageSize: 10,
        processCode: null,
        processName: null,
        enableFlag: null,
        },
        // 表单参数
        form: {},

      };
    },
    created() {
      this.getList();
      this.getProcess();
    },
    methods: {
      /** 查询生产任务列表 */
      getList() {
        this.loading = true;
        listProcess(this.queryParams).then(response => {
          this.protaskList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      //查询工序信息
      getProcess(keyword = null){
        listAllProcess({processName: keyword}).then( response =>{
          this.processOptions = response.data;
        });
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
       this.form = {
        processName: null,
        processCode: null,
        };
        this.getProcess()
        this.resetForm("queryForm");
        this.handleQuery();
      },

      handleCurrent(row){
        if(row){
            this.selectedRow = row;
          }
      },
      //行双击选中
      handleRowDbClick(row){
        if(row){
          this.selectedRow = row;
          this.$emit('onSelected',this.selectedRow);
          this.showFlag = false;
        }
      },
      // 单选选中数据
      handleRowChange(row) {
        debugger;
        if(row){
          this.selectedRow = row;
        }
      },
      //确定选中
      confirmSelect(){
          if(this.selectedTaskId ==null || this.selectedTaskId==0){
              this.$notify({
                  title:this.$t('提示'),
                  type:'warning',
                  message: this.$t('请至少选择一条数据!')
              });
              return;
          }
          this.$emit('onSelected',this.selectedRow);
          this.showFlag = false;
      }
    }
  };
  </script>
