<template>
  <PageWrapper>
    <a-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset" />
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :row-selection="{ onChange: onSelectionChange }"
      >
        <template #tableTitle>
          <a-button
            type="danger"
            @click="handleDelete()"
            :disabled="!selectionData.length"
            v-perm="['sys:loginLog:batchDelete']"
          >
            <template #icon>
              <DeleteOutlined />
            </template>
            删除
          </a-button>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a-button
                type="primary"
                @click="handleDetail(record.id)"
                v-perm="['sys:loginLog:detail']"
              >
                <template #icon><EyeOutlined /></template>
                详情
              </a-button>
              <a-button
                type="primary"
                danger
                @click="handleDelete(record.id)"
                v-perm="['sys:loginLog:delete']"
              >
                <template #icon><DeleteOutlined /></template>
                删除
              </a-button>
            </a-space>
          </template>
        </template>
      </BasicTable>
    </a-card>

    <editDialog
      v-if="editVisible"
      :loginlogId="loginlogId"
      v-model:visible="editVisible"
      @success="reloadTable('noRefresh')"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h, nextTick, defineAsyncComponent } from 'vue';
  import { EyeOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import { schemas } from './loginLog/querySchemas';
  import { useForm } from '@/components/Form/index';
  import { TableAction } from '@/components/Table';
  import { getLoginLogList, loginLogDelete, loginLogBatchDelete } from '@/api/system/loginLog';
  import { columns } from './loginLog/columns';
  import { Modal, message } from 'ant-design-vue';

  /**
   * 导入组件
   */
  const editDialog = defineAsyncComponent(() => import('./loginLog/edit.vue'));

  /**
   * 定义参数变量
   */
  const loginlogId = ref(0);
  const editVisible = ref(false);
  const selectionData = ref([]);
  const tableRef = ref();

  /**
   * 定义查询参数
   */
  const formParams = reactive({
    username: '',
    type: '',
    status: '',
  });

  /**
   * 加载数据列表
   * @param res 参数
   */
  const loadDataTable = async (res: any) => {
    const result = await getLoginLogList({ ...formParams, ...res });
    return result;
  };

  /**
   * 刷新数据列表
   * @param noRefresh 参数
   */
  function reloadTable(noRefresh = '') {
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }

  /**
   * 注册
   */
  const [register, {}] = useForm({
    rowProps: { gutter: [16, 0] },
    colProps: {
      xs: 24,
      sm: 24,
      md: 12,
      lg: 8,
      xl: 6,
    },
    labelCol: { span: 6, offset: 0 },
    schemas,
  });

  /**
   * 执行提交表单
   * @param values 参数
   */
  function handleSubmit(values) {
    for (const key in formParams) {
      formParams[key] = '';
    }
    for (const key in values) {
      formParams[key] = values[key];
    }
    reloadTable();
  }

  /**
   * 执行重置
   */
  function handleReset() {
    for (const key in formParams) {
      formParams[key] = '';
    }
    reloadTable();
  }

  /**
   * 执行查询详情
   * @param id 参数
   */
  const handleDetail = async (id) => {
    loginlogId.value = id;
    await nextTick();
    editVisible.value = true;
  };

  /**
   * 执行删除
   * @param id 参数
   */
  async function handleDelete(id) {
    Modal.confirm({
      title: '提示',
      content: '确定要删除？',
      onOk: async () => {
        id ? await loginLogDelete(id) : await loginLogBatchDelete(selectionData.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }

  /**
   * 选项发生变化
   * @param value 参数
   */
  function onSelectionChange(value) {
    selectionData.value = value;
  }
</script>

<style lang="scss" scoped></style>
