<template>
  <div class="page">
    <a-page-header :ghost="false" title="秘钥管理">
      <div class="ele-text-secondary">
        API key 是您访WebSoft-API
        的密钥，具有该账户完全的权限，请您妥善保管。
      </div>
    </a-page-header>
    <div class="ele-body">
      <a-card :bordered="false">
        <!-- 表格 -->
        <ele-pro-table
          ref="tableRef"
          row-key="id"
          :columns="columns"
          :datasource="datasource"
          :where="defaultWhere"
          cache-key="userBalanceLogTable"
        >
          <template #toolbar>
            <a-space>
              <a-button type="primary" class="ele-btn-icon" @click="openEdit()">
                <span>创建 API key</span>
              </a-button>
            </a-space>
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'accessSecret'">
              <span>
                sk-******************
              </span>
            </template>
            <template v-if="column.key === 'action'">
              <a-space>
                <a-popconfirm
                  placement="topRight"
                  title="确定要删除此用户吗？"
                  @confirm="remove(record)"
                >
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- 编辑弹窗 -->
    <AccessKeyEdit v-model:visible="showEdit" :data="current" @done="reload"/>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive} from 'vue';
import {message} from 'ant-design-vue/es';
import type {EleProTable} from 'ele-admin-pro/es';
import type {
  DatasourceFunction,
  ColumnItem
} from 'ele-admin-pro/es/ele-pro-table/types';
import AccessKeyEdit from './components/accesskey-edit.vue';
import {toDateString} from 'ele-admin-pro/es';
import {pageAccessKey, removeAccessKey} from '@/api/system/access-key';
import {AccessKey, AccessKeyParam} from '@/api/system/access-key/model';
import {messageLoading} from 'ele-admin-pro/es';

// 表格实例
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
// 表格列配置
const columns = ref<ColumnItem[]>([
  {
    title: '名称',
    key: 'accessKey',
    dataIndex: 'accessKey',
    showSorterTooltip: false
  },
  {
    title: 'Key',
    key: 'accessSecret',
    dataIndex: 'accessSecret',
    showSorterTooltip: false
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    align: 'center',
    customRender: ({text}) => toDateString(text, 'yyyy-MM-dd HH:mm:ss'),
    width: 180,
  },
  {
    title: '操作',
    key: 'action',
    width: 180,
    fixed: 'right',
    align: 'center'
  }
]);

// 表格选中数据
const selection = ref<AccessKey[]>([]);
// 是否显示编辑弹窗
const showEdit = ref(false);
// 当前编辑数据
const current = ref<any>(null);

// 默认搜索条件
const defaultWhere = reactive({
  code: '',
  phone: '',
  username: '',
  nickname: '',
  userId: undefined
});


// 表格数据源
const datasource: DatasourceFunction = ({where}) => {
  return pageAccessKey({...where});
};

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


/* 删除单个 */
const remove = (row: AccessKey) => {
  const hide = messageLoading('请求中..', 0);
  removeAccessKey(row.id)
    .then((msg) => {
      hide();
      message.success(msg);
      reload();
    })
    .catch((e) => {
      hide();
      message.error(e.message);
    });
};

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

</script>

<script lang="ts">
export default {
  name: 'AccessKeyIndex'
};
</script>
