<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #tableTitle>
        <c-button type="primary" @click="handlerReadAllMsg">全部标注已读</c-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <DetailModal @register="register" />
  </div>
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import DetailModal from './DetailModal.vue';
  import { getMyNewsList, editCementSend, syncNotic, readAllMsg, getOne } from './mynews.api';
  import { columns, searchFormSchema } from './mynews.data';
  import { useListPage, Button as CButton, useModal, BasicTable, TableAction } from '@jdlinker/ui';
  import { useAppStore } from '@jdlinker/func';
  import { useMessageHref } from '../../../system/message/components/useSysMessage';

  const checkedKeys = ref<Array<string | number>>([]);
  const searchInfo = { logType: '1' };
  const [register, { openModal: openDetail }] = useModal();
  const appStore = useAppStore();
  const { goPage } = useMessageHref();
  const { tableContext } = useListPage({
    designScope: 'mynews-list',
    tableProps: {
      title: '我的消息',
      api: getMyNewsList,
      columns,
      formConfig: {
        schemas: searchFormSchema,
        fieldMapToTime: [['fieldTime', ['createTime_begin', 'createTime_end'], 'YYYY-MM-DD HH:mm:ss']]
      }
    }
  });
  const [registerTable, { reload }] = tableContext;
  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '查看',
        onClick: handleDetail.bind(null, record)
      }
    ];
  }

  /**
   * 查看
   */
  function handleDetail(record) {
    let anntId = record.anntId;
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    editCementSend({ anntId: anntId }).then(() => {
      reload();
      syncNotic({ anntId: anntId });
    });
    const openModalFun = () => {
      openDetail(true, {
        record,
        isUpdate: true
      });
    };
    goPage(record, openModalFun);
  }
  // 日志类型
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  function callback(key) {
    searchInfo.logType = key;
    reload();
  }

  //全部标记已读
  function handlerReadAllMsg() {
    readAllMsg({}, reload);
  }

  /**
   * 选择事件
   */
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  function onSelectChange(selectedRowKeys: (string | number)[]) {
    checkedKeys.value = selectedRowKeys;
  }

  onMounted(() => {
    initHrefModal();
  });
  function initHrefModal() {
    let params = appStore.getMessageHrefParams;
    console.log('initHrefModal...', params);
    if (params) {
      let anntId = params.id;
      if (anntId) {
        editCementSend({ anntId: anntId }).then(() => {
          reload();
          syncNotic({ anntId: anntId });
        });
      }
      let detailId = params.detailId;
      if (detailId) {
        getOne(detailId).then((data) => {
          console.log('getOne', data);
          openDetail(true, {
            record: data,
            isUpdate: true
          });
          appStore.setMessageHrefParams('');
        });
      }
    }
  }
</script>
