<template>
  <div class="container">
    <Breadcrumb :items="['个人中心', '消息通知']" />
    <a-card class="general-card" title=" ">
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="right"
          >
            <a-row :gutter="18">
              <a-col :span="9">
                <a-form-item field="title" label="标题">
                  <a-input
                    v-model="formModel.title"
                    style="width: 360px"
                    placeholder="请输入标题"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="10">
                <a-form-item field="Time" label="时间">
                  <a-range-picker
                    show-time
                    format="YYYY-MM-DD HH:mm"
                    @ok="timeRangs"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="9">
                <a-form-item field="isRead" label="状态">
                  <a-select
                    v-model="formModel.isRead"
                    style="width: 360px"
                    placeholder="请选择状态"
                    :options="statusOptions"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'46px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              查询
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />

      <a-table
        row-key="id"
        :loading="loading"
        :pagination="false"
        :columns="(cloneColumns as TableColumnData[])"
        :data="renderData"
        :bordered="false"
        :size="sizeof"
        :style="highlightRow"
        style="margin-bottom: 40px"
        :filter-icon-align-left="alignLeft"
        @change="handleSortChange"
        @page-change="onPageChange"
      >
        <template #createTime="{ record }">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm') }}
        </template>
        <template #isRead="{ record }">
          {{ record.isRead == true ? '已读' : '未读' }}
        </template>
        <template #operation="{ record }">
          <a-button
            type="outline"
            size="small"
            status="success"
            @click="handleViewDetail(record)"
            style="padding: 7px; margin-right: 10px"
          >
            <template #icon><icon-list /></template>详情
          </a-button>
        </template>
        <!--        <template #operations="{ record }">-->
        <!--          <a-button-->
        <!--            type="outline"-->
        <!--            size="small"-->
        <!--            status="success"-->
        <!--            style="padding: 7px; margin-right: 10px"-->
        <!--            @click="handleRead(record)"-->
        <!--          >-->
        <!--            <template #icon><icon-list /></template>-->
        <!--            详情-->
        <!--          </a-button>-->
        <!--        </template>-->
      </a-table>
      <a-pagination
        style="float: right; position: relative; right: 1px; bottom: 25px"
        :total="pagination.total"
        :size="sizeof"
        show-total
        show-jumper
        show-page-size
        @page-size-change="onSizeChange"
        @change="onPageChange"
      />
    </a-card>

    <a-modal
      width="900px"
      :visible="visible"
      :footer="false"
      @cancel="handleCancel"
    >
      <template #title>消息详情</template>
      <a-descriptions :column="1">
        <a-descriptions-item label="标题">
          {{ selectedRecord.title }}
        </a-descriptions-item>
        <a-descriptions-item label="时间">
          {{ dayjs(selectedRecord.createTime).format('YYYY-MM-DD HH:mm') }}
        </a-descriptions-item>
        <a-descriptions-item label="备注">
          <div v-html="selectedRecord.remark"></div>
        </a-descriptions-item>
        <a-descriptions-item label="内容">
          <div v-html="selectedRecord.content"></div>
        </a-descriptions-item>
      </a-descriptions>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref, watch } from 'vue';
  import useLoading from '@/hooks/loading';
  import dayjs from 'dayjs';
  import usePagination from '@/hooks/pagination';
  import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
  import type { TableColumnData } from '@arco-design/web-vue/es/table/interface';
  import { useMessagesStore } from '@/store';
  import useTableOption from '@/hooks/table-option';
  import { BulletinsRecord } from '@/api/bulletins';

  const generateFormModel = () => {
    return {
      title: '',
      state: '',
      publishTimeBegin: '',
      publishTimeEnd: '',
    };
  };

  const { loading, setLoading } = useLoading(true);
  const renderData = ref<BulletinsRecord[]>([]);
  const formModel = ref(generateFormModel());
  const { pagination, setPagination } = usePagination();
  const {
    cloneColumns,
    showColumns,
    densityList,
    handleSelectDensity,
    handleChange,
    popupVisibleChange,
    deepClone,
  } = useTableOption();
  const sizeof = useTableOption().size;

  const messagesStore = useMessagesStore();

  const columns = computed<TableColumnData[]>(() => [
    {
      title: '标题',
      dataIndex: 'title',
    },
    {
      title: '时间',
      dataIndex: 'createTime',
      slotName: 'createTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '状态',
      dataIndex: 'isRead',
      slotName: 'isRead',
    },
    {
      title: '操作',
      dataIndex: 'operation',
      slotName: 'operation',
    },
  ]);
  const statusOptions = computed<SelectOptionData[]>(() => [
    {
      label: '已读',
      value: 'true',
    },
    {
      label: '未读',
      value: 'false',
    },
  ]);

  const visible = ref(false);
  const selectedRecord = ref({
    title: '',
    createTime: '',
    remark: '',
    content: '',
  });

  // 获取消息列表
  const fetchData = async (
    params: BulletinsRecord = { size: 10, current: 1 }
  ) => {
    setLoading(true);
    try {
      const res = await messagesStore.queryMessagesList(params);
      renderData.value = res.data.records;
      setPagination(res.data);
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  // 高亮行
  const highlightRow = (record: BulletinsRecord) => {
    if (record.top === true) {
      return 'background-color: yellow;';
    }
    return '';
  };

  // 时间范围
  const timeRangs = (dateString: string[]) => {
    // eslint-disable-next-line prefer-destructuring
    formModel.value.publishTimeBegin = dateString[0];
    // eslint-disable-next-line prefer-destructuring
    formModel.value.publishTimeEnd = dateString[1];
  };

  // 查询
  const search = () => {
    fetchData({
      ...pagination.value,
      ...formModel.value,
    } as unknown as BulletinsRecord);
  };

  // 分页发生改变
  const onPageChange = (current: number) => {
    pagination.value.page = current;
    pagination.value.current = current;
    search();
  };

  // 数据条数改变
  const onSizeChange = (Size: number) => {
    pagination.value.size = Size;
    search();
  };

  const handleViewDetail = (record: any) => {
    selectedRecord.value = record;
    visible.value = true;
  };

  const handleCancel = () => {
    visible.value = false;
  };

  search();

  // 重置
  const reset = () => {
    formModel.value = generateFormModel();
  };

  // 表格内部排序
  const alignLeft = ref(false);

  watch(() => columns.value, deepClone, { deep: true, immediate: true });
</script>

<style scoped>
  .container {
    padding: 0 20px 20px 20px;
  }
</style>