<!--
 * @Author: Jason Liu
 * @Date: 2022-04-22 13:54:26
 * @Desc: 
-->
<template>
  <div class="user_set_container">
    <a-row :gutter="12">
      <a-col :span="6">
        <a-spin :spinning="loading">
          <div class="institution_list_container">
            <div class="search_list">
              <a-input-search
                allowClear
                v-model.lazy="name_key"
                :placeholder="$t('请输入机构名称')"
                @search="getInstitutionList"
              >
              </a-input-search>
            </div>
            <div class="institution_list">
              <vxe-table
                ref="institutionTree"
                show-overflow
                :show-header="false"
                border="outer"
                :row-config="{ isHover: true }"
                :tree-config="{
                  transform: true,
                  rowField: 'orgId',
                  parentField: 'parentOrg',
                  line: true,
                  expandAll: true,
                  trigger: 'cell',
                }"
                :data="institutionList"
                :row-class-name="getOrgRowClass"
                @cell-click="clickOrgRow"
                height="auto"
              >
                <vxe-column
                  field="orgName"
                  :title="$t('机构名称')"
                  tree-node
                ></vxe-column>
                <template #empty>
                  <a-empty
                    :image-style="{
                      height: '80px',
                    }"
                  >
                    <span slot="description">{{ $t('暂无数据') }}</span>
                  </a-empty>
                </template>
              </vxe-table>
            </div>
          </div>
        </a-spin>
      </a-col>
      <a-col :span="18">
        <a-spin :spinning="apiLoading">
          <div class="user_list_container">
            <a-row class="user_tool">
              <a-col
                class="label_title"
                :span="16"
              >
                <a-input-search
                  allowClear
                  :placeholder="$t('请输入用户账号')"
                  class="user_input"
                  v-model="pager.queryParams.F_userName_like"
                  @search="searchUserList"
                />
                <a-input-search
                  allowClear
                  :placeholder="$t('请输入用户昵称')"
                  class="user_input"
                  v-model="pager.queryParams.F_nickName_like"
                  @search="searchUserList"
                />
              </a-col>
              <a-col
                :span="8"
                align="right"
              >
                <a-button
                  type="primary"
                  icon="search"
                  @click="searchUserList"
                  >{{ $t('查询') }}</a-button
                >
                &nbsp;
                <a-button
                  type="primary"
                  icon="plus"
                  @click="addUser"
                  v-w-auth-code="'add'"
                  >{{ $t('新增') }}</a-button
                >
              </a-col>
            </a-row>
            <div class="user_list">
              <vxe-table
                show-overflow
                show-header-overflow
                border
                resizable
                :row-config="{ isHover: true }"
                :data="userList"
                height="auto"
                @cell-dblclick="editUser"
              >
                <vxe-column
                  type="seq"
                  :title="$t('序号')"
                  width="50"
                  align="center"
                >
                </vxe-column>
                <vxe-column
                  :title="$t('头像')"
                  width="80"
                  align="center"
                >
                  <template #default="{ row }">
                    <div class="table_center">
                      <img
                        class="user_list_icon"
                        :src="row.avatarPic || user_avatar"
                        :onerror="defaultimg"
                        alt="avatar"
                      />
                    </div>
                  </template>
                </vxe-column>
                <vxe-column
                  field="userName"
                  :title="$t('账号')"
                >
                </vxe-column>
                <vxe-column
                  field="nickName"
                  :title="$t('昵称')"
                >
                  <template #default="{ row }">
                    {{ $t(row.nickName) }}
                  </template>
                </vxe-column>
                <vxe-column
                  field="phonenumber"
                  :title="$t('联系电话')"
                >
                </vxe-column>
                <vxe-column
                  field="email"
                  :title="$t('邮箱')"
                >
                </vxe-column>
                <vxe-column
                  :title="$t('操作')"
                  width="160"
                  align="center"
                >
                  <template #default="{ row }">
                    <a-button
                      class="btn"
                      type="link"
                      v-w-auth-code="'edit'"
                      @click="editUser({ row: row })"
                      >{{ $t('编辑') }}</a-button
                    >
                    <a-popconfirm
                      :title="$t('确定要删除吗')"
                      :ok-text="$t('是')"
                      :cancel-text="$t('否')"
                      placement="left"
                      @confirm="deleteUser(row)"
                      v-w-auth-code="'delete'"
                    >
                      <a-button
                        class="btn a_danger"
                        type="link"
                        >{{ $t('删除') }}</a-button
                      >
                    </a-popconfirm>
                  </template>
                </vxe-column>
                <template #empty>
                  <a-empty
                    :image-style="{
                      height: '80px',
                    }"
                  >
                    <span slot="description">{{ $t('暂无数据') }}</span>
                  </a-empty>
                </template>
              </vxe-table>
            </div>
            <vxe-pager
              background
              :current-page.sync="pager.pageNum"
              :page-size.sync="pager.pageSize"
              :total="total"
              @page-change="getUserList"
              :layouts="['Total', 'PrevJump', 'JumpNumber', 'NextJump', 'FullJump']"
            >
            </vxe-pager>
          </div>
        </a-spin>
      </a-col>
    </a-row>
    <userCard
      v-model="userCard.show"
      :edit="userCard.edit"
      :info="userCard.info"
      :user-id="userCard.info.userId"
      @update="getUserList"
    />
  </div>
</template>
<script>
  const $userService = require('w-vue-basis/src/api/userService');
  const $appService = require('w-vue-basis/src/api/appService');
  const user_avatar = require('w-vue-basis/src/assets/images/bg_header.png'); //用户头像

  /**
   * @Author: Jason Liu
   * @description: 用户卡片
   */
  const userCard = require('./userCard').default;
  export default {
    components: { userCard },
    data() {
      return {
        loading: false,
        name_key: undefined,
        institutionList: [],
        apiLoading: false, //是否加载
        userList: [], //用户列表
        user_avatar: user_avatar,
        defaultimg: `this.src='${user_avatar}'`,
        pager: {
          queryParams: {
            F_userName_like: undefined,
            F_nickName_like: undefined,
            F_deptId_eq: undefined,
            ORDER_createTime_asc: '0',
          },
          pageNum: 1,
          pageSize: 15,
        },
        total: 0,
        selectRow: {},
        userCard: {
          show: false,
          edit: true,
          info: {},
        },
      };
    },
    created() {
      this.getInstitutionList();
    },
    methods: {
      /**
       * @Author: y_zp
       * @description: 获取机构列表
       */
      getInstitutionList() {
        this.loading = true;
        $appService
          .getSysOrgTree({
            queryParams: { F_orgName_like: this.name_key },
            pageNum: 1,
            pageSize: 1000000,
          })
          .then((req) => {
            this.loading = false;
            this.institutionList = req.data;
            if (this.institutionList.length > 0) {
              this.selectRow = this.institutionList[0];
              this.pager.queryParams.F_deptId_eq = this.institutionList[0].orgId;
              this.getUserList();
            }
          })
          .finally(() => {
            this.$refs.institutionTree.setAllTreeExpand(true);
          });
      },
      /**
       * @Author: y_zp
       * @description: 点击机构
       */
      clickOrgRow({ row }) {
        this.selectRow = row;
        this.pager.queryParams.F_deptId_eq = row.orgId;
        this.getUserList();
      },
      /**
       * @Author: y_zp
       * @description: 样式信息
       */
      getOrgRowClass(e) {
        return e.row.orgId == this.selectRow.orgId ? 'row--current' : '';
      },
      /**
       * @Author: Jason Liu
       * @description: 查询用户列表
       */
      searchUserList() {
        this.pager.pageNum = 1;
        this.getUserList();
      },
      /**
       * @Author: Jason Liu
       * @description: 读取用户列表
       */
      getUserList() {
        this.apiLoading = true;
        $userService
          .getUserList({ ...this.pager })
          .then((req) => {
            this.total = Number(req.count);
            this.userList = req.data.map((item) => {
              return {
                ...item,
                avatarPic: `${item.avatar}`,
              };
            });
          })
          .finally(() => {
            this.apiLoading = false;
          });
      },
      /**
       * @Author: Jason Liu
       * @description: 编辑用户信息
       */
      editUser({ row }) {
        this.userCard = {
          show: true,
          edit: true,
          info: {
            ...row,
            avatarPic: undefined,
          },
        };
      },
      addUser() {
        this.userCard = {
          show: true,
          edit: true,
          info: {
            userId: '',
            deptId: this.selectRow.orgId,
          },
        };
      },
      /**
       * @Author: y_zp
       * @description: 删除
       */
      deleteUser(row) {
        if (row.userName == 'admin') {
          this.$message.error($t('不可以删除管理员账号'));
        } else {
          $userService.deleteUser({ userId: row.userId }).then(() => {
            this.$message.success($t('用户删除成功'));
            this.getUserList();
          });
        }
      },
    },
  };
</script>
<style lang="less" scoped>
  .user_set_container {
    .institution_list_container {
      background: #fff;
      height: calc(100vh - 104px);
      padding: 16px;
      .search_list {
        margin-bottom: 12px;
      }
      .institution_list {
        height: calc(100% - 40px);
      }
    }
    .user_list_container {
      background: #fff;
      height: calc(100vh - 104px);
      padding: 16px;
    }
    .user_tool {
      font-size: 14px;
      font-weight: 400;
      color: #000000;
      line-height: 32px;

      .user_input {
        width: 200px;
        margin-right: 16px;
      }
    }
    .user_list {
      width: 100%;
      height: calc(100% - 90px);
      display: inline-block;
      margin-top: 12px;
      .user_list_icon {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        box-shadow: 0px 1px 2px 0px #d0d6e4;
      }
    }
  }
</style>
