<template>
  <el-dialog title="人员选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="800px"
    center
    :before-close="cancelEquipmentForm"
  >
    <el-row :gutter="20">
      <!--人员数据-->
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="人员编码" prop="itemCode">
            <el-input
              v-model="queryParams.itemCode"
              placeholder="请输入人员编码"
              clearable
              style="width: 150px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="人员名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入人员名称"
              clearable
              style="width: 150px"
              @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="itemList" @selection-change="handleEquipmentSelectionChange" ref="myTable">
          <el-table-column width="50" align="center" type="selection"></el-table-column>
          <!-- 序号 -->
          <el-table-column type="index" width="90" align="center" :index="indexMethod" label="序号"></el-table-column>
          <el-table-column label="人员编码" align="center" key="itemCode" prop="userName" v-if="columns[0].visible" >
          </el-table-column>
          <el-table-column label="人员名称"  align="left" key="itemName" prop="nickName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPersonForm">确 定</el-button>
        <el-button @click="cancelEquipmentForm">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRepairPersonList} from "@/api/device/faultReport";
import { Message } from 'element-ui'

export default {
  name: "MdItemSingle",
  data() {
    return {
      treeData1:[],
      showFlag:false,
      // 选中数组
      selectedItemId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total:0,
      // 人员表格数据
      itemList: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: undefined,
        itemCodeGet: '',
        itemCode : undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `人员编码`, visible: true },
        { key: 1, label: `人员名称`, visible: true },
      ]
    };
  },
  created() {
    this.getList();
  },

  methods: {
      // 表单重置
      reset() {
        this.form = {
          itemId: null,
          itemCode: this.processId,
          itemType: null,
        };
        this.resetForm("form");
      },

    handleEquipmentSelectionChange (val) {
       this.itemList = val;
    },

  	 // 生成表头序号
        indexMethod(index){
          return index+1 ;
        },
    //

    /** 查询设备编码列表*/
    getList() {
      this.loading = true;
      getRepairPersonList(this.queryParams).then(response => {
          this.itemList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 节点单击事件
    handleNodeClick(data) {
      console.log('id',data.id);
      this.queryParams.equipmentTypeCode = data.equipmentTypeCode;
      console.log(this.equipmentTypeCode);
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.equipmentTypeCode = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
		handleEquipmentSelectionChange(selection) {
			this.ids = selection.map(item => item.nickName);
			this.single = selection.length!==1;
			this.multiple = !selection.length;
			console.log("多选框",this.ids);
		},

    submitPersonForm() {
      const data = this.ids;
      const nickName = data.toString();
      if(data.length >1){
        Message.warning("只能选择一个报修人,请勿选择多个！");
      }else{
        if(this.queryParams.itemCodeGet == ""){
            this.queryParams.itemCodeGet = nickName;
        }
           this.selectedRows = this.queryParams.itemCodeGet;
           this.$emit('onSelected', this.selectedRows);
           this.getList();
           this.showFlag = false;
           this.queryParams.itemCodeGet = "";
      }
    }
  }
};
</script>
