<script setup lang="ts">
import type { NotifyRecordModel, NotifyRecordQuery } from '@/model/notify.model';
import dayjs from 'dayjs';
// 接口数据
import { getNotifyRecordMethod } from '@/request/api/notify.api';
import { usePagination } from 'alova/client';

import { type VxeFormListeners, type VxeFormProps, type VxeGridInstance, type VxeGridListeners, type VxeGridProps, VxeUI } from 'vxe-pc-ui';

const model = shallowRef<NotifyRecordQuery>();
const searchFormProps = reactive<VxeFormProps<NotifyRecordQuery>>({
  titleWidth: 100,
  titleAlign: 'right',
  titleColon: true,
  data: {},
  items: [
    {
      field: 'noticeName',
      title: '名称',
      span: 6,
      itemRender: { name: 'VxeInput', props: { placeholder: '请输入通知名称' } },
    },
    {
      field: 'sendTime',
      title: '发送日期',
      span: 6,
      slots: {
        default: 'createTimes',
      },
    },
    {
      field: 'isRead',
      title: '查看状态',
      span: 6,
      itemRender: {
        name: 'VxeRadioGroup',
        props: {
          options: [
            { label: '已查看', value: 'Y' },
            { label: '未查看', value: 'N' },
          ],
        },
      },
    },
    {
      span: 6,
      itemRender: {
        name: 'VxeButtonGroup',
        options: [
          { name: 'submits', type: 'submit', content: '查询', status: 'primary' },
          { name: 'reset', type: 'reset', content: '重置', status: 'warning' },
        ],
      },
    },
  ],
});
const searchFormEmits: VxeFormListeners<NotifyRecordQuery> = {
  // 查询
  submit({ data }) {
    data.sendTimeStart = sendTimeStart.value ? dayjs(sendTimeStart.value).format('YYYY-MM-DD HH:mm') : '';
    data.sendTimeEnd = sendTimeEnd.value ? dayjs(sendTimeEnd.value).format('YYYY-MM-DD HH:mm') : '';
    onSearch(data);
  },

  // 重置
  reset({ data }) {
    sendTimeStart.value = '';
    sendTimeEnd.value = '';
    const resetData = { ...data };
    resetData.sendTimeStart = '';
    resetData.sendTimeEnd = '';
    onSearch(resetData);
  },
};
function onSearch(data: NotifyRecordQuery) {
  model.value = { ...data };
  nextTick(() => {
    (searchFormProps.data as any)! = { ...data };
  });
}
const gridRef = ref<VxeGridInstance<NotifyRecordModel>>();
const gridOptions = reactive<VxeGridProps<NotifyRecordModel>>({
  id: 'tag-list',
  border: true,
  showOverflow: true,
  height: 'auto',
  autoResize: true,
  syncResize: true,
  scrollY: { enabled: true, gt: 0 },
  toolbarConfig: {
    custom: true,
    zoom: true,
    slots: {
      tools: 'toolbar-extra',
    },
  },
  columnConfig: {
    resizable: true,
  },
  customConfig: {
    storage: true,
  },
  columns: [
    { type: 'seq', width: 70, fixed: 'left' },
    { field: 'noticeName', title: '名称' },
    { field: 'patientName', title: '用户姓名' },
    { field: 'phone', title: '手机号码' },
    { field: 'sendTime', title: '发送时间' },
    { field: 'isRead', title: '发送状态', slots: { default: 'isReadCell' } },
  ],
  data: [],
});
const gridEvents: VxeGridListeners = {};

const {
  loading,
  page,
  pageSize,
  total,
  onSuccess,
  refresh,
  send: sendRefresh,
} = usePagination((page, size) => getNotifyRecordMethod(page, size, model.value), {
  initialData: { data: [], total: 0 },
  initialPage: 1,
  initialPageSize: 100,
  watchingStates: [model],
  immediate: true,
});
onSuccess((res: any) => {
  gridRef.value?.loadData(res?.data?.data ?? []);
});
onMounted(() => {
  onSearch(toRaw(searchFormProps.data) as any);
});
// 日期验证
const sendTimeStart = ref<string>('');
const sendTimeEnd = ref<string>('');
// 禁用结束时间的日期（早于开始时间的日期）
function disabledEndDate(current: any) {
  if (!sendTimeStart.value) return false;
  return current && current < dayjs(sendTimeStart.value);
}
defineExpose({
  send: sendRefresh,
});
</script>
<template>
  <div class="page-container flex flex-col">
    <header class="flex-none mt-4">
      <vxe-form v-bind="searchFormProps" v-on="searchFormEmits">
        <template #createTimes>
          <div class="date-range-container">
            <a-date-picker v-model:value="sendTimeStart" placeholder="请选择开始日期" style="flex: 1" :show-time="{ format: 'HH:mm' }" />
            <span class="date-separator">至</span>
            <a-date-picker v-model:value="sendTimeEnd" placeholder="请选择结束日期" style="flex: 1" :disabledDate="disabledEndDate" :show-time="{ format: 'HH:mm' }" />
          </div>
        </template>
      </vxe-form>
    </header>
    <main class="flex-auto overflow-hidden">
      <vxe-grid ref="gridRef" v-bind="gridOptions" v-on="gridEvents" :loading="loading">
        <template #isReadCell="{ row }">
          <a-tag :color="row.isRead == 'Y' ? 'green' : 'red'">{{ row.isRead == 'Y' ? '已查看' : '未查看' }}</a-tag>
        </template>
        <template #toolbar-extra>
          <vxe-button style="margin-right: 12px" icon="vxe-icon-repeat" circle @click="refresh(page)"></vxe-button>
        </template>
      </vxe-grid>
    </main>
    <footer class="flex-none">
      <vxe-pager
        v-model:current-page="page"
        v-model:page-size="pageSize"
        :total="total"
        :layouts="['Home', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'End', 'Sizes', 'FullJump', 'Total']"
      />
    </footer>
  </div>
</template>
<style scoped lang="scss">
.page-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.date-range-container {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;

  .vxe-input {
    flex: 1;
    min-width: 0;
  }

  .date-separator {
    color: #666;
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    padding: 0 8px;
  }
}
</style>
