<template>
  <el-drawer size="75%" title="选择企微账号" :visible.sync="shows" append-to-body destroy-on-close class="qw-user-select-drawer">
    <div class="drawer-container">
      <!-- 搜索表单 -->
      <el-card shadow="never" class="search-card">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px" class="search-form">
          <el-row :gutter="16">
            <el-col :span="6">
            <el-form-item label="销售公司" prop="companyId">
              <el-select v-model="queryParams.companyId" placeholder="销售公司" size="small" @change="handleCompanyChange">
                <el-option
                  v-for="dict in qwCompanyList"
                  :key="dict.companyId"
                  :label="dict.companyName"
                  :value="dict.companyId"
                />
              </el-select>
            </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企微用户名" prop="qwUserName">
                <el-input v-model="queryParams.qwUserName" placeholder="请输入企微用户名" clearable size="small" @keyup.enter.native="handleQuery"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企微用户 ID" prop="qwUserId">
                <el-input v-model="queryParams.qwUserId" placeholder="请输入企微用户 ID" clearable size="small" @keyup.enter.native="handleQuery"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>

      <!-- 企微账号列表 -->
      <el-card shadow="never" class="table-card">
        <div slot="header" class="card-header">
          <span>企微账号列表</span>
          <el-tag v-if="selectedRows.length" type="success">已选择 {{ selectedRows.length }} 个账号</el-tag>
        </div>
        <el-table border v-loading="loading" :data="qwUserList" :row-key="getRowKeys" @selection-change="handleSelectionChange" size="small" ref="table" height="500">
          <el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
          <el-table-column label="企微用户名" align="center" prop="qwUserName"/>
          <el-table-column label="企微用户id" align="center" prop="qwUserId"/>
          <el-table-column label="vid" align="center" prop="vid"/>
          <el-table-column label="uid" align="center" prop="uid"/>
          <el-table-column label="ipad服务器id" align="center" prop="serverId"/>
        </el-table>

        <!-- 分页组件 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          style="margin-top: 20px;"
        />
      </el-card>

      <!-- 底部按钮 -->
      <div class="footer-actions">
        <el-button @click="shows = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确定选择</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import {queryQwList} from "../../api/qw/qwUser";
import {listCompany} from "../../api/company/company";

export default {
  name: "QwUserSelectTwo",
  data() {
    return {
      rows: [], // 已选择的行数据
      selectedRows: [], // 已选择的行数据（别名）
      shows: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 企微账号表格数据
      qwUserList: [],
      qwCompanyList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        qwUserName: null,
        qwUserId: null
      }
    };
  },
  methods: {
    // 获取企业数据
    getCompanyData() {
      listCompany().then(response => {
        this.qwCompanyList = response.rows;
        // 默认选中第一个企业
        if (this.qwCompanyList && this.qwCompanyList.length > 0) {
          this.queryParams.companyId = this.qwCompanyList[0].companyId;
          // 重新加载页面数据
          this.handleQuery();
        }
      });
    },
    // 企业改变时触发查询
    handleCompanyChange() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    setRows(rows){
      this.shows = true;
      this.rows = rows || [];
      this.selectedRows = rows || []; // 同步更新
      this.getList();
      // 获取企业列表并默认选中第一个
      this.getCompanyData();
    },
    initSelect(){
      let row = this.rows;
      if(row && row.length > 0 && this.$refs.table){
        row.forEach(row => {
          this.$refs.table.toggleRowSelection(row, true);
        })
      }
    },
    getRowKeys(item){
      return item.id;
    },
    /** 查询企微账号列表 */
    getList() {
      this.loading = true;
      queryQwList(this.queryParams).then(response => {
        if (this.$refs.table) {
          this.$refs.table.clearSelection();
        }

        this.qwUserList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.initSelect();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      // 重置后选中第一个企业
      if (this.qwCompanyList && this.qwCompanyList.length > 0) {
        this.queryParams.companyId = this.qwCompanyList[0].companyId;
      }
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.rows = selection;
      this.selectedRows = selection; // 同步更新
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    submitForm(){
      this.$nextTick(() => {
        this.$emit("success", {ids: this.ids, rows: this.rows})
        this.shows = false;
        this.$refs.table.clearSelection();
      })
    },
  }
};
</script>
<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .el-dialog__wrapper{
    z-index: 100000;
  }
  .app-container{padding: 0}
  .dialog-footer{
    position: absolute;
    bottom: 0;
    right: 20px;
    background: #FFF;
  }
</style>
