<template>
  <Page>
    <!-- 查询和全局功能 -->
    <Row>
      <Col :span="22">
      <!-- 查询 -->
      <Space v-if="data.isShowQuery" :wrap="true">
        <Input v-model:value="data.query.nickname" allowClear :placeholder="$t('system.user.nickname')" />
        <Input v-model:value="data.query.username" allowClear :placeholder="$t('system.user.username')" />
        <Input v-model:value="data.query.phone" allowClear :placeholder="$t('system.user.phone')" />
        <Input v-model:value="data.query.mail" allowClear :placeholder="$t('system.user.mail')" />
        <!-- 查询按钮 -->
        <Button @click="handleQuery" :loading="data.loading" :disabled="data.loading"
          style="background-color: #0084CA;color: white">
          {{ $t('common.search') }}
        </Button>
        <Button @click="resetQuery" :loading="data.loading" :disabled="data.loading" danger>
          {{ $t('common.reset') }}
        </Button>
      </Space>

      <!-- 全局功能 -->
      <div style="padding: 15px 0">
        <Button @click="handleOpenAdd" style="background-color: #0084CA;color: white"  >
          {{ $t('common.add') }}
        </Button>
      </div>
      </Col>

    </Row>

    <!-- 数据展示 -->
    <Table :dataSource="data.datas" :columns="data.columns" bordered :loading="data.loading"
      :pagination="data.pagination" :scroll="{ x: 1500 }" @change="handleQuery" size="small">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'action'">
          <Button type="link" size="small" @click="handleOpenEdit(record)">{{ $t('common.edit') }}</Button>
          <Button type="link" size="small" @click="handleDeleteConfirm(record)">{{ $t('common.delete') }}</Button>
          <Button type="link" size="small" @click="handleOpenAssign(record)">{{ $t('common.assign') }}</Button>
          <Button type="link" size="small" @click="handleOpenUpdateSecretKey(record)">{{ $t('system.user.updateSecretKey') }}</Button>
        </template>
        <template v-else-if="column.dataIndex === 'status'">
          <span>{{ data.statusMap[record.status] }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'roles'">
          <span>{{ record.roles && record.roles.length > 0 ? record.roles.join(',') : '' }}</span>
        </template>
      </template>
    </Table>

    <!-- 添加 -->
    <Add ref="addRef" @close="reload"></Add>
    <!-- 编辑 -->
    <Edit ref="editRef" @close="reload"></Edit>
    <!-- 删除 -->
    <DeleteConfirm ref="deleteRef"></DeleteConfirm>
    <AssignRobot ref="assignRef"></AssignRobot>
    <!-- 更新秘钥 -->
    <UpdateSecretKey ref="updateSecretKeyRef" @close="reload"></UpdateSecretKey>
  </Page>
</template>

<script setup lang="ts">
import DeleteConfirm from "#/components/myui/deleteConfirm/index.vue";
import AssignRobot from "./components/assignRobot.vue";
import Add from "./components/add.vue";
import Edit from "./components/edit.vue";
import UpdateSecretKey from "./components/updateSecretKey.vue";
import { reactive, onMounted, ref } from "vue";
import { $t } from '@vben/locales';
import { Page } from '@vben/common-ui';
import { Button, Input, Space, Table, Row, Col, message } from 'ant-design-vue';
import { user } from '#/api';
import { d } from './index.ts';

const data = reactive(d);
const addRef = ref();
const deleteRef = ref();
const editRef = ref();
const assignRef = ref();
const updateSecretKeyRef = ref();

onMounted(() => {
  handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
})

function handleOpenAdd() {
  addRef.value._open();
}

function handleOpenAssign(item) {
  // Pass user information to assign fans popup
  assignRef.value._open(item);
}

function handleOpenEdit(item) {
  editRef.value._open(item);
}

function handleOpenUpdateSecretKey(item) {
  updateSecretKeyRef.value._open(item);
}

function handleDeleteConfirm(item) {
  deleteRef.value._open($t('common.deleteConfirm') + '【' + item.nickname + '】?').then(() => {
    user.removeByUid(item).then((res) => {
      if (res.error) {
        return;
      }
      message.success($t('common.deleteSuccess'));
      handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
    })
  })
}

/**
 * 包装查询参数
 * @param context
 * @param page
 * @param filter
 * @param sorter
 */
function wrapQueryParam(page, filter, sorter) {
  let { current, pageSize } = { ...data.pagination, ...(page || {}) };
  let param = {
    pageNum: current,
    pageSize: pageSize,
    type: data.query.type,
  }
  //查询
  for (const key in data.query) {
    if (data.query[key] == null) {
      continue;
    }
    param[key] = data.query[key];
  }
  //过滤
  if (filter) {
    for (const key in filter) {
      const value = Array.isArray(filter[key]) && filter[key].length > 0 ? filter[key][0] : filter[key];
      param[key] = value || param[key];
    }
  }
  //排序
  if (sorter && sorter.field && sorter.order) {
    param.sorts = [{ name: sorter.field, desc: sorter.order == 'descend' }]
  }
  return param;
}

/**
 * 重置查询
 */
function resetQuery() {
  data.query = {}
  data.pagination.current = 1;
  handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
}

/**
 * 重置内容
 */
function reload(success) {
  success && handleQuery({ current: data.pagination.current, pageSize: data.pagination.pageSize });
}

/**
 * 查询方法
 * @param context
 * @param page
 * @param filter
 * @param sorter
 */
function handleQuery(page, filter, sorter) {
  console.log(page, filter, sorter);
  let param = wrapQueryParam(page, filter, sorter);
  data.loading = true;
  user.queryPage(param).then(res => {
    console.log(res, 111);
    if (res.data) {
      data.datas = res.data.list || [];
      data.pagination.current = res.data.pageNum || 1;
      data.pagination.pageSize = res.data.pageSize || 10;
      data.pagination.total = res.data.total;
    }
  }).finally(() => {
    data.loading = false;
  });
}
</script>

<style scoped></style>
