<!--version: 1, 设备选择弹窗-->

<template>
  <el-dialog :title="title" :visible="show" width="60%" top="2vh" @open="open" @close="close"
             :append-to-body="true">
    <el-form  size="small" :inline="true" label-width="8em">
      <slot name="search"></slot>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="tableData"
      v-loading="loadTable"
      @row-click="changeSelection"
      @row-dblclick="select"
      @select-all="selectionAll"
      @select="changeSelection"
      highlight-current-row
    >
      <slot name="columns"></slot>
    </el-table>

    <pagination
      :limit.sync="searchForm.pageSize"
      :page.sync="searchForm.pageNum"
      :total="total"
      @pagination="searchList">
    </pagination>

    <template #footer>
      <el-button type="primary" @click="submit()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {clone} from "@/utils";

export default {
  name: "DeviceDialog",
  props: {
    listApi: {
      type: Function,
      default: null,
      required: true,
    },
    idProp: {
      type: String,
      default: 'id',
    },
    // 标题
    title: {
      type: String,
      default: '选择'
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    show: {
      type: Boolean,
      default: false
    },
    query: {
      type: Object,
      default: () => ({})
    },
    initSelect: {
      type: Array,
      default: null,
    },
  },
  data() {
    return {
      loadTable: false,
      tableData: [],
      searchForm: {},
      total: 0,
      row: null,
      selected: [],
    }
  },
  methods: {
    // 获取数据列表
    async searchList() {
      this.loadTable = true;
      if (this.listApi == null) {
        this.$message.error("未配置列表api")
        return ;
      }
      this.listApi(this.searchForm).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        // 刷新表格状态
        this.$nextTick(()=>{
          this.refreshTableSelection();
        })
      }).finally(() =>{
        this.loadTable = false;
      })
    },
    // 打开时
    open() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        ...this.query,
      }

      if (this.initSelect) {
        this.selected = clone(this.initSelect);
      } else {
        this.selected = [];
      }

      this.searchList();
    },
    // 刷新表格的选中状态
    refreshTableSelection() {
      if(this.multiple){
        this.tableData.forEach(item => {
          if (this.selected.map(j => j[this.idProp]).includes(item[this.idProp])) {
            this.$refs.multipleTable.toggleRowSelection(item, true);
          } else {
            this.$refs.multipleTable.toggleRowSelection(item, false);
          }
        });
      }
    },
    // 全选
    selectionAll(val){
      let flag = val.length > 0;
      this.tableData.forEach(item => {
        if (flag && !this.selected.map(i => i[this.idProp]).includes(item[this.idProp])){
          this.selected.push(item);
        } else if (!flag && this.selected.map(i => i[this.idProp]).includes(item[this.idProp])){
          this.selected = this.selected.filter(i => i[this.idProp] !== item[this.idProp]);
        }
      })
    },
    // 确认选中
    submit() {
      if (this.multiple) {
        this.$emit('select', this.selected);
      } else {
        this.select(this.row);
      }
    },
    // 更换某一行的选中状态
    changeSelection(row){
      if(this.multiple){
        if (this.selected.map(i => i[this.idProp]).includes(row[this.idProp])){
          this.$refs.multipleTable.toggleRowSelection(row, false);
          this.selected = this.selected.filter(i => i[this.idProp] !== row[this.idProp]);
        }else {
          this.$refs.multipleTable.toggleRowSelection(row, true);
          this.selected.push(row);
        }
      } else {
        this.row = row;
      }
    },
    // 点击搜索
    onSearch() {
      this.searchForm.pageNum = 1;
      this.searchList();
    },
    // 关闭弹窗
    close() {
      this.$emit('update:show', false);
      this.searchForm.pageNum = 1;
      this.searchForm.pageSize = 10;
    },
    // 选中一行
    select(row) {
      if (!this.multiple) {
        if (!row) return this.$message.error('请选择一行');
        this.selected = [row];
        this.$emit('select', row);
      }
    },
  },
}
</script>
