<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>
              {{ $t('searchTable.form.search') }}
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              {{ $t('searchTable.form.reset') }}
            </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 #publishTime="{ record }">
          {{ dayjs(record.publishTime).format('YYYY-MM-DD HH:mm') }}
        </template>
        <template #isRead="{ record }">
          {{ record.isRead == true? '已读' : '未读' }}
        </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>
  </div>

</template>

<script lang="ts" setup>
  import { computed, ref, watch } from 'vue';
  import useLoading from '@/hooks/loading';
  import dayjs from 'dayjs';
  import { Pagination } from '@/types/global';
  import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
  import type { TableColumnData } from '@arco-design/web-vue/es/table/interface';
  import { useBulletinsStore } from '@/store';
  import router from '@/router';
  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 {
    cloneColumns,
    showColumns,
    densityList,
    handleSelectDensity,
    handleChange,
    popupVisibleChange,
    deepClone,
  } = useTableOption();
  const sizeof=useTableOption().size;

  const bulletinsStore = useBulletinsStore();

  const pagination: any = {
    size: 10,
    current: 1
  };

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

  // 获取公告列表
  const fetchData = async (
    params: BulletinsRecord = { size: 10, current: 1 }
  ) => {
    setLoading(true);
    try {
      const res = await bulletinsStore.getBulletinsList(params);
      renderData.value = res.data.records;
      pagination.page = res.data.page;
      pagination.current = res.data.current;
      pagination.total = res.data.total;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };

  // 高亮行
  const highlightRow = (record: BulletinsRecord) => {
    if (record.top === true) {
      console.log('record', record);
      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 handleRead = async (record: BulletinsRecord) => {
      // await bulletinsStore.readBulletins({ id: record.id });
      await router.push({ name: 'Details', params: { id: record.id } });
  };

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

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

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

  search();

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

  // 表格内部排序
  const alignLeft = ref(false);
  const handleSortChange = (data: any, extra: any, currentDataSource: any) => {};

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

</script>

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

  .highlight-class {
    background-color: yellow; /* 这里可以修改为你想要的高亮颜色 */
    font-weight: bold; /* 可以添加其他样式，如加粗等 */
  }
</style>