<template>
  <el-dialog title="工作单元选择" v-if="showFlag" :visible.sync="showFlag" :modal=false width="80%">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="工作单元编号" label-width="100px" prop="workunitCode">
            <el-input
              v-model="queryParams.workunitCode"
              placeholder="请输入工作单元编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工作单元名称" label-width="100px" prop="workunitName">
            <el-input
              v-model="queryParams.workunitName"
              placeholder="请输入工作单元名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="车间名称" label-width="100px" prop="workshopName">
            <el-input
              v-model="queryParams.workshopName"
              placeholder="请输入车间名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工作中心名称" label-width="100px" prop="workstationName">
            <el-input
              v-model="queryParams.workstationName"
              placeholder="请输入工作中心名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <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-col>
      </el-row>
    </el-form>


    <el-table v-loading="loading" :data="workunitList" @selection-change="handleSelectionChange" @row-dblclick="rowClick" height="600px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工作单元编码" align="center" prop="workunitCode" />
      <el-table-column label="工作单元名称" align="center" prop="workunitName" />
      <el-table-column label="车间名称" width="100" align="center" prop="workshopName">
      </el-table-column>
      <el-table-column label="工作中心名称" width="100" align="center" prop="workstationName">
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="createTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
    </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">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getWorkunitByPlanId } from "@/api/mes/md/workunit";
export default {
  name: "TeamSelect",
  props: ['apiName'],
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班组表格数据
      workunitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workunitCode: null,
        workunitName: null,
        workshopName: null,
        workstationName: null,
        planId: this.$attrs.planId || null
      },
    };
  },
  watch: {
    showFlag: {
      handler() {
        this.getList();
      }
    }
  },
  created() {

  },
  methods: {
    rowClick(row) {
      this.selectedRows = [row];
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    },
    /** 查询工作单元列表 */
    getList() {
      this.loading = true;
      const fn = getWorkunitByPlanId
      fn(this.queryParams).then(response => {
        this.workunitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teamId);
      this.selectedRows = selection;
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRows == null || this.selectedRows.size == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    }
  }
};
</script>
