<template>
  <div style="position: relative;">
    <el-form ref="formKnowledgeUsers" :model="formData" label-width="75px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
      <filter-box :item-width="325">
        <el-form-item label="用户状态" prop="formFilter.sysUserStatus">
          <el-select class="filter-item" v-model="formData.formFilter.sysUserStatus" :clearable="true"
            placeholder="用户状态" :loading="formData.sysUserStatus.impl.loading"
            @visible-change="onSysUserStatusVisibleChange">
            <el-option v-for="item in formData.sysUserStatus.impl.dropdownList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="formFilter.sysUserLoginName">
          <el-input class="filter-item" v-model="formData.formFilter.sysUserLoginName" maxlength="12"
            :clearable="true" placeholder="用户名" />
        </el-form-item>
        <el-button type="default" :plain="true" :size="defaultFormItemSize" @click="onReset">重置</el-button>
        <el-button type="primary" :plain="true" :size="defaultFormItemSize" @click="refreshFormKnowledgeUserList(true)">查询</el-button>
        <el-button :size="defaultFormItemSize" type="primary" :plain="false" @click="onSetUser"
          :disabled="selectUsers == null || selectUsers.length <= 0">授权人员</el-button>
      </filter-box>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="formData.SysUser.impl.dataList" :size="defaultFormItemSize" row-key="userId" ref="userTable"
          header-cell-class-name="table-header-gray" @selection-change="onTableSelectionChange">
          <el-table-column label="序号" header-align="center" align="center" type="index" width="50px" :index="formData.SysUser.impl.getTableIndex" />
          <el-table-column header-align="center" align="center" type="selection" width="50px" :reserve-selection="true" />
          <el-table-column label="用户名" prop="loginName">
          </el-table-column>
          <el-table-column label="昵称" prop="showName">
          </el-table-column>
          <el-table-column label="账号类型">
            <template slot-scope="scope">
              <span>{{SysUserType.getValue(scope.row.userType)}}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <el-tag :type="getUserStatusType(scope.row.userStatus)" :size="defaultFormItemSize">{{SysUserStatus.getValue(scope.row.userStatus)}}</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <el-col :span="24">
          <el-row type="flex" justify="end" style="margin-top: 10px;">
            <el-pagination
              :total="formData.SysUser.impl.totalCount"
              :current-page="formData.SysUser.impl.currentPage"
              :page-size="formData.SysUser.impl.pageSize"
              :page-sizes="[10, 20, 50, 100]"
              layout="total, prev, pager, next, sizes"
              @current-change="formData.SysUser.impl.onCurrentPageChange"
              @size-change="formData.SysUser.impl.onPageSizeChange">
            </el-pagination>
          </el-row>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
/* eslint-disable-next-line */
import { DropdownWidget, TableWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import { DictionaryController } from '@/api';
import { KnowledgeManage } from '@/api/gptController.js';

export default {
  name: 'formKnowledgeUsers',
  props: {
    knowledgeId: {
      type: [String, Number],
      required: true
    }
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data () {
    return {
      formData: {
        formFilter: {
          sysUserStatus: undefined,
          sysUserLoginName: undefined
        },
        formFilterCopy: {
          sysUserStatus: undefined,
          sysUserLoginName: undefined
        },
        sysUserStatus: {
          impl: new DropdownWidget(this.loadSysUserStatusDropdownList)
        },
        SysUser: {
          impl: new TableWidget(this.loadSysUserData, this.loadSysUserVerify, true, false)
        },
        isInit: false
      },
      selectUsers: []
    }
  },
  methods: {
    /**
     * 用户状态下拉数据获取函数
     */
    loadSysUserStatusDropdownList () {
      return new Promise((resolve, reject) => {
        let params = {};
        DictionaryController.dictSysUserStatus(this, params).then(res => {
          resolve(res.getList());
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 用户状态下拉框显隐
     */
    onSysUserStatusVisibleChange (show) {
      this.formData.sysUserStatus.impl.onVisibleChange(show).catch(e => {});
    },
    getUserStatusType (status) {
      if (status === this.SysUserStatus.NORMAL) {
        return 'success';
      } else if (status === this.SysUserStatus.LOCKED) {
        return 'danger';
      } else {
        return 'info';
      }
    },
    onTableSelectionChange (values) {
      this.selectUsers = values;
    },
    onSetUser () {
      let params = {
        knowledgeId: this.knowledgeId,
        userIdListString: this.selectUsers.map((item) => {
          return item.userId
        }).join(',')
      }

      KnowledgeManage.addKnowledgeUser(this, params).then(res => {
        this.$message.success('授权成功');
        this.refreshFormKnowledgeUserList(true);
        this.$refs.userTable.clearSelection();
        this.selectUsers = [];
      }).catch(e => {});
    },
    onCancel () {
      this.$router.go(-1);
    },
    onReset () {
      this.$refs.formKnowledgeUsers.resetFields();
      this.refreshFormKnowledgeUserList(true);
    },
    loadSysUserData (params) {
      params.knowledgeId = this.knowledgeId;
      params.sysUserDtoFilter = {
        loginName: this.formData.formFilterCopy.sysUserLoginName,
        userStatus: this.formData.formFilterCopy.sysUserStatus
      }
      return new Promise((resolve, reject) => {
        KnowledgeManage.listNotInKnowledgeUser(this, params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          });
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 获取检测函数，返回true正常获取数据，返回false停止获取数据
     */
    loadSysUserVerify () {
      this.formData.formFilterCopy.sysUserLoginName = this.formData.formFilter.sysUserLoginName;
      this.formData.formFilterCopy.sysUserStatus = this.formData.formFilter.sysUserStatus;
      return true;
    },
    refreshFormKnowledgeUserList (reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formData.SysUser.impl.refreshTable(true, 1);
      } else {
        this.formData.SysUser.impl.refreshTable();
      }
      this.formData.sysUserStatus.impl.onVisibleChange(true).catch(e => {});
      this.formData.isInit = true;
    },
    initFormData () {
    },
    formInit () {
      this.initFormData();
      this.refreshFormKnowledgeUserList();
    }
  },
  computed: {
    getContextHeightStyle () {
      return [
        {'height': (this.getClientHeight - 142) + 'px'}
      ]
    },
    ...mapGetters(['getClientHeight'])
  },
  created () {
    this.formInit();
  }
}
</script>

<style>
</style>
