<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <members-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="systemMembersTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
        </template>
        <!-- 角色列 -->
        <template v-slot:roleName="{ row }">
          <el-tag type="primary" size="mini">
            {{ row.roleName }}
          </el-tag>
        </template>
        <!-- 状态列 -->
        <template v-slot:status="{ row }">
          <el-switch
            :active-value="0"
            :inactive-value="1"
            v-model="row.status"
            @change="editStatus(row)"
          />
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此用户吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
          <el-link
            v-if="row.maJia"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openMaJia(row)"
          >
            {{ row.maJiaId ? '修改马甲' : '披上马甲' }}
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <members-edit
      :data="current"
      :visible.sync="showEdit"
      :organization-list="organizationList"
      @done="reload"
    />
    <!-- 马甲弹窗 -->
    <members-majia :data="current" :visible.sync="showMaJia" @done="reload" />
  </div>
</template>

<script>
  import MembersSearch from './components/members-search.vue';
  import MembersEdit from './components/members-edit.vue';
  import MembersMajia from './components/members-majia.vue';
  import { listOrganizations } from '@/api/institution/organization';
  import {
    pageMembers,
    removeMembers,
    removeMembersBatch,
    updateMembersStatus
  } from '@/api/sylive/members';

  export default {
    name: 'SystemMembers',
    components: { MembersSearch, MembersEdit, MembersMajia },
    data() {
      return {
        organizationList: [],
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center'
          },
          {
            prop: 'userId',
            label: 'ID',
            minWidth: 45,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'uname',
            label: '姓名',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'nickname',
            label: '微信昵称',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'mobile',
            label: '手机号',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'organizationName',
            label: '机构',
            showOverflowTooltip: true,
            minWidth: 130
          },
          {
            prop: 'roleName',
            label: '角色',
            showOverflowTooltip: true,
            minWidth: 60,
            slot: 'roleName'
          },
          {
            prop: 'createTime',
            label: '创建时间',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            sortable: 'custom',
            width: 100,
            resizable: false,
            slot: 'status'
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示马甲弹窗
        showMaJia: false
      };
    },
    created() {
      this.organizationQuery();
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return pageMembers({ ...where, ...order, page, limit });
      },
      /* 查询机构 */
      organizationQuery() {
        listOrganizations()
          .then((list) => {
            this.organizationList = this.$util.toTreeData({
              data: list,
              idField: 'organizationId',
              parentIdField: 'parentId'
            });
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 打开马甲弹窗 */
      openMaJia(row) {
        this.current = row;
        this.showMaJia = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeMembers(row.userId)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的用户吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            removeMembersBatch(this.selection.map((d) => d.userId))
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({ lock: true });
        updateMembersStatus(row.userId, row.status)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
          })
          .catch((e) => {
            loading.close();
            row.status = !row.status ? 1 : 0;
            this.$message.error(e.message);
          });
      }
    }
  };
</script>
