<!--客存设备选择-->
<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="68px">
      <el-form-item label="用户名">
        <el-input v-model="searchForm.realOrUserName" clearable @keyup.enter.native="onSearch" placeholder="请输入用户名"/>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="searchForm.phonenumber" type="number" :maxlength="11" show-word-limit clearable @keyup.enter.native="onSearch" placeholder="请输入手机号"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch()" icon="el-icon-search">搜索</el-button>
      </el-form-item>
    </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
    >
      <el-table-column align="center" type="selection" v-if="multiple"></el-table-column>
      <el-table-column label="#" type="index" align="center"></el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber"></el-table-column>
      <el-table-column label="用户名" align="center" prop="realOrUserName"></el-table-column>
      <el-table-column label="用户类型" align="center" prop="type" width="120">
        <dict-tag slot-scope="d" :options="dict.type.user_type" :value="d.row.type"/>
      </el-table-column>
      <el-table-column label="分成比例" align="center" prop="point">
        <template slot-scope="d">{{d.row.point | money | defaultValue}} %</template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"></el-table-column>
    </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";
import {listSmUser} from "@/api/system/smUser";

export default {
  name: "smUserDialog",
  dicts: ['user_type'],
  data() {
    return {
      loadTable: false,
      tableData: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        ...this.query
      },
      total: 0,
      row: null,
      selected: [],
    }
  },
  props: {
    initSelect: {
      type: Array,
      default: null,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '选择用户'
    },
    query: {
      type: Object,
      default: () => ({})
    },
    listApi: {
      type: Function,
      default: listSmUser,
    }
  },
  methods: {
    // 打开时
    open() {
      this.searchForm = {
        code: null,
        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.id).includes(item.id)) {
            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.id).includes(item.id)){
          this.selected.push(item);
        } else if (!flag && this.selected.map(i => i.id).includes(item.id)){
          this.selected = this.selected.filter(i => i.id !== item.id);
        }
      })
    },
    // 确认选中
    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.id).includes(row.id)){
          this.$refs.multipleTable.toggleRowSelection(row, false);
          this.selected = this.selected.filter(i => i.id !== row.id);
        }else {
          this.$refs.multipleTable.toggleRowSelection(row, true);
          this.selected.push(row);
        }
      } else {
        this.row = row;
      }
    },
    // 点击搜索
    onSearch() {
      this.searchForm.pageNum = 1;
      this.searchList();
    },
    // 获取数据列表
    searchList() {
      this.loadTable = true;
      this.listApi(this.searchForm).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        // 刷新表格状态
        this.$nextTick(()=>{
          this.refreshTableSelection();
        })
      }).finally(() => {
        this.loadTable = false;
      })
    },
    // 关闭弹窗
    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>
