<template>
  <el-dialog :title="$t('common.choose_workunit')" 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="$t('工作单元编号')" label-width="100px" prop="workunitCode">
            <el-input
              v-model="queryParams.workunitCode"
             
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('common.workunit_name')" label-width="100px" prop="workunitName">
            <el-input
              v-model="queryParams.workunitName"
             
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item :label="$t('车间名称')" label-width="100px" prop="workshopName">
            <el-input
              v-model="queryParams.workshopName"
             
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('common.workstation_name')" label-width="100px" prop="workstationName">
            <el-input
              v-model="queryParams.workstationName"
             
              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">{{ $t('common.search') }}</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</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="$t('common.workunit_code')" align="center" prop="workunitCode" />
      <el-table-column :label="$t('common.workunit_name')" align="center" prop="workunitName" />
      <el-table-column :label="$t('车间名称')" width="100" align="center" prop="workshopName">
      </el-table-column>
      <el-table-column :label="$t('common.workstation_name')" width="100" align="center" prop="workstationName">
      </el-table-column>
      <el-table-column :label="$t('开始时间')" align="center" prop="createTime" />
      <el-table-column :label="$t('更新时间')" 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">{{ $t('common.confirm') }}</el-button>
      <el-button @click="showFlag = false">{{ $t('common.cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>


import { listTeam } from "@/api/mes/cal/team";
import { listWorkunit } 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 = this.apiName ? listWorkunit : listTeam
      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: this.$t('提示'),
          type: 'warning',
          message: this.$t('请至少选择一条数据!')
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    }
  }
};
</script>
