<template>
  <div style="position: relative;">
    <el-form ref="formKnowledgeUser" :model="formData" label-width="75px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
      <filter-box :item-width="350">
        <el-form-item label="用户名" prop="formFilter.sysUserLoginName">
          <el-input class="filter-item" v-model="formData.formFilter.sysUserLoginName"
          maxlength="12"
                    :clearable="true" placeholder="输入用户名 / 昵称查询" @change="refreshFormKnowledgeUser(true)" />
        </el-form-item>
        <el-button slot="operator" type="default" :plain="true" :size="defaultFormItemSize" @click="onReset">重置</el-button>
        <el-button slot="operator" type="primary" :plain="true" :size="defaultFormItemSize" @click="refreshFormOnlinePageUser(true)">查询</el-button>
        <el-button type="primary" :size="defaultFormItemSize" @click="onAddRow()">添加用户</el-button>
      </filter-box>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="formData.SysUser.impl.dataList" :size="defaultFormItemSize" @sort-change="formData.SysUser.impl.onSortChange"
                  header-cell-class-name="table-header-gray">
          <el-table-column label="序号" header-align="center" align="center" type="index" width="50px" :index="formData.SysUser.impl.getTableIndex" />
          <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-column label="操作" fixed="right" width="80px">
            <template slot-scope="scope">
              <el-button class="btn-table-delete" type="text" :size="defaultFormItemSize" @click="onDeleteRow(scope.row)">
                移除
              </el-button>
            </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 {TableWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import { KnowledgeManage } from '@/api/gptController.js';
import formKnowLedgeUserEdit from './edit.vue';

export default {
  name: 'formKnowledgeUser',
  props: {
    knowledgeId: {
      type: [String, Number],
      required: true
    }
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data () {
    return {
      formData: {
        formFilter: {
          sysUserLoginName: undefined
        },
        SysUser: {
          impl: new TableWidget(this.loadSysUserData, this.loadSysUserVerify, true, false)
        },
        isInit: false
      }
    }
  },
  methods: {
    onReset () {
      this.$refs.formKnowledgeUser.resetFields();
      this.refreshFormKnowledgeUser(true);
    },
    getUserStatusType (status) {
      if (status === this.SysUserStatus.NORMAL) {
        return 'success';
      } else if (status === this.SysUserStatus.LOCKED) {
        return 'danger';
      } else {
        return 'info';
      }
    },
    onAddRow () {
      this.$dialog.show('添加授权用户', formKnowLedgeUserEdit, {
        area: ['1100px', '600px']
      }, {
        knowledgeId: this.knowledgeId
      }).catch(e => {
        this.refreshFormKnowledgeUser(true);
      });
    },
    onDeleteRow (row) {
      this.$confirm('是否移除此用户?').then(res => {
        let params = {
          knowledgeId: this.knowledgeId,
          userId: row.userId
        }
        return KnowledgeManage.deleteKnowledgeUser(this, params);
      }).then(res => {
        this.$message.success('移除授权用户成功');
        this.refreshFormKnowledgeUser(true);
      }).catch(e => {});
    },
    /**
     * 用户管理数据获取函数，返回Primise
     */
    loadSysUserData (params) {
      return new Promise((resolve, reject) => {
        params.knowledgeId = this.knowledgeId;
        params.sysUserDtoFilter = {
          loginName: this.formData.formFilter.sysUserLoginName
        }
        KnowledgeManage.getSysUserListByKnowledgeId(this, params).then(res => {
          resolve({
            dataList: res.data.dataList,
            totalCount: res.data.totalCount
          });
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 用户管理数据获取检测函数，返回true正常获取数据，返回false停止获取数据
     */
    loadSysUserVerify () {
      return true;
    },
    /**
     * 更新用户管理
     */
    refreshFormKnowledgeUser (reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formData.SysUser.impl.refreshTable(true, 1);
      } else {
        this.formData.SysUser.impl.refreshTable();
      }
      this.formData.isInit = true;
    },
    onResume () {
      this.refreshFormKnowledgeUser();
    },
    initFormData () {
    },
    formInit () {
      this.initFormData();
      this.refreshFormKnowledgeUser();
    }
  },
  computed: {
    getTableHeight () {
      return (this.getMainContextHeight - 92);
    },
    ...mapGetters(['getMainContextHeight'])
  },
  created () {
    this.formInit();
  }
}
</script>
