<template>
  <ele-page>
    <!-- 搜索表单 -->
    <member-search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="roleId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        :highlight-current-row="true"
        cache-key="systemRoleTable"
      >
        <template #toolbar> </template>
        <template #action="{ row }">
          <el-link type="primary" underline="never" @click="openEdit(row)">
            详情
          </el-link>
          <el-divider direction="vertical" />
          <el-link type="danger" underline="never" @click="remove(row)">
            注销
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <member-edit v-model="showEdit" :data="current" @done="reload" />
  </ele-page>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { ElMessageBox } from 'element-plus';
  import { EleMessage } from 'ele-admin-plus';
  import type { EleProTable } from 'ele-admin-plus';
  import type {
    DatasourceFunction,
    Columns
  } from 'ele-admin-plus/es/ele-pro-table/types';
  import MemberSearch from './components/member-search.vue';
  import MemberEdit from './components/member-edit.vue';
  import { removeRoles } from '@/api/system/role';
  import type { Member, MemberParam } from '@/api/xm/member/model';
  import { pageMembers } from '@/api/xm/member';

  defineOptions({ name: 'SystemRole' });

  /** 表格实例 */
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  /** 表格列配置 */
  const columns = ref<Columns>([
    {
      type: 'selection',
      columnKey: 'selection',
      width: 50,
      align: 'center' /* ,
      fixed: 'left' */
    },
    {
      type: 'index',
      columnKey: 'index',
      width: 50,
      align: 'center'
    },
    {
      prop: 'memberId',
      label: '会员ID',
      width: 80
    },
    {
      prop: 'channel.name',
      label: '注册渠道',
      minWidth: 120
    },
    {
      prop: 'realname',
      label: '姓名',
      width: 80
    },
    {
      prop: 'telephoneMd5',
      label: '手机号MD5',
      minWidth: 300
    },
    {
      prop: 'onlineIp',
      label: '最后在线Ip',
      width: 170,
      align: 'center'
    },
    {
      prop: 'onlineTime',
      label: '最后在线时间',
      width: 170,
      align: 'center'
    },
    {
      prop: 'createTime',
      label: '注册时间',
      width: 170,
      align: 'center'
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 200,
      align: 'center' /* ,
      fixed: 'right' */,
      slot: 'action',
      hideInPrint: true,
      hideInExport: true
    }
  ]);

  /** 表格选中数据 */
  const selections = ref<Member[]>([]);

  /** 当前编辑数据 */
  const current = ref<Member | null>(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 是否显示权限分配弹窗 */
  const showAuth = ref(false);

  /** 表格数据源 */
  const datasource: DatasourceFunction = ({ pages, where, orders }) => {
    return pageMembers({ ...where, ...orders, ...pages });
  };

  /** 搜索 */
  const reload = (where?: MemberParam) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row?: Member) => {
    current.value = row ?? null;
    showEdit.value = true;
  };



  /** 删除单个 */
  const remove = (row?: Member) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error({ message: '请至少选择一条数据', plain: true });
      return;
    }
    ElMessageBox.confirm(
      '确定要删除“' + rows.map((d) => d.roleName).join(', ') + '”吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading({
          message: '请求中..',
          plain: true
        });
        removeRoles(rows.map((d) => d.roleId))
          .then((msg) => {
            loading.close();
            EleMessage.success({ message: msg, plain: true });
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error({ message: e.message, plain: true });
          });
      })
      .catch(() => {});
  };
</script>
