<template xmlns="">
  <div class="main">
    <div class="header">
      <div class="input">
        <a-input class="first-input" v-model:value="value1" addonBefore="关键字" placeholder="请输入用户账号或姓名" />
        <a-input class="second-input" v-model:value="value2" addonBefore="手机号码" placeholder="请输入手机号码"/>
        <a-select
            placeholder="请选择状态"
            ref="select"
            style="width: 120px"
            @focus="focus"
        >
          <a-select-option value="启用">启用</a-select-option>
          <a-select-option value="禁用">禁用</a-select-option>
        </a-select>
      </div>
      <div class="edit">
        <a-button class="query-button"><SearchOutlined />查询</a-button>
        <a-button class="add-button" @click="showAddDialog"><PlusOutlined />新增用户</a-button>
        <a-button class="delete-button" @click="showDelectDialog"><DeleteOutlined />删除用户</a-button>
      </div>
    </div>


    <!--新增、删除用户对话框-->
    <a-modal v-model:open="open1" title="新增用户" @ok="handleOk" ok-text="确认" cancel-text="取消">
      <p>新增</p>
    </a-modal>
    <a-modal v-model:open="open2" title="删除用户" @ok="hideModal" ok-text="确认" cancel-text="取消">
      <p>删除</p>
    </a-modal>


    <div class="table">
      <div>
      <a-table
          :columns="columns"
          :data-source="data"
          :loading="loading"
          :pagination="pagination"
          :row-key="record => record.login.uuid"
          @change="handleTableChange"
      >
        <template #bodyCell="{ column, text }">
          <template v-if="column.dataIndex === 'name'">{{ text.first }} {{ text.last }}</template>
        </template>
      </a-table>
  </div>
  </div>
</template>



<script setup>
import {computed, createVNode, ref} from 'vue';
import {DeleteOutlined, ExclamationCircleOutlined, PlusOutlined, SearchOutlined} from '@ant-design/icons-vue';
import {Modal} from "ant-design-vue";
import {usePagination} from 'vue-request';
import axios from 'axios';
import request from '@/public/request.js'

const value1 = ref('');
const value2 = ref('');
const open1 = ref(false);
const open2 = ref(false);
let data = null
const token = window.localStorage.getItem('token')
const requestBody = {
  "page": null,
  "limit": null,
  "search": null,
  "role": null
}

const showAddDialog = () => {
  open1.value = true;
};
const handleOk = e => {
  console.log(e);
  open1.value = false;
};

const showDelectDialog = () => {
  open2.value = true;
};
const hideModal = () => {
  open2.value = false;
};

const showDeleteConfirm = () => {
  console.log("delete Dialog")
  Modal.confirm({
    title: 'Confirm',
    icon: createVNode(ExclamationCircleOutlined),
    content: '确认删除这条数据吗',
    okText: '确认',
    cancelText: '取消',
  });
};

const columns = [
  {
    title: '序号',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: '用户名',
    dataIndex: 'username',
    key: 'username',
  },
  {
    title: '账户昵称',
    dataIndex: 'nickname',
    key: 'nickname',
  },
  {
    title: '电话',
    key: 'phone',
    dataIndex: 'phone',
  },
  {
    title: '用户状态',
    dataIndex: 'enabled',
    key: 'enabled',
  },
  {
    title: '更新时间',
    key: 'updatedAt',
    dataIndex: 'updatedAt'
  },
  {
    title: '操作',
    key: 'action',
  },
];

const rowSelection = {
  onChange: (selectedRowKeys, selectedRows) => {
    console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
  },
  getCheckboxProps: record => ({
    disabled: record.name === 'Disabled User',
    name: record.name,
  }),
};

const queryData = params => {
  return axios.get('https://randomuser.me/api?noinfo', {
    params,
  });
};
request.userAllCurrent(requestBody, token).then((res) => {
  data = res.data.data.users
})
const {
  run,
  loading,
  current,
  pageSize,
} = usePagination(queryData, {
  formatResult: res => res.data.results,
  pagination: {
    currentKey: 'page',
    pageSizeKey: 'results',
  },
});
const pagination = computed(() => ({
  total: 200,
  current: current.value,
  pageSize: pageSize.value,
}));
const handleTableChange = (pag, filters, sorter) => {
  run({
    results: pag.pageSize,
    page: pag?.current,
    sortField: sorter.field,
    sortOrder: sorter.order,
    ...filters,
  });
};
</script>


<style scoped>
.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: auto;
}

.header {
  display: flex;
}

.input {
  width: 35vw;
  margin-top: 2vh;
  margin-bottom: 1vh;
  display: flex;
  flex-direction: row;
}

.first-input {
  margin-right: 1vw;
  margin-left: 1vw;
}

.second-input {
  margin-right: 1vw;
}

.edit {
  margin-top: 2vh;
  width: 20vw;
  display: flex;
}

.query-button {
  background-color: dodgerblue;
  color:white;
  margin-left: 4vw;
  margin-right: 1vw;
}

.add-button {
  background-color: limegreen;
  color: white;
}

.delete-button {
  background-color: #CD5C5C;
  color: white;
  margin-left: 1vw;
}

.table {
  width: 100%;
  height: auto;
  margin-top: 2vh;
}
</style>
