import type {
  ActionType,
  ProColumns,
  ProCoreActionType,
} from '@ant-design/pro-components';
import { Popconfirm } from 'antd';
import React, { useCallback, useRef, useState } from 'react';
import ConfigurableDrawerForm, {
  type ConfigurableDrawerFormRef,
} from '@/components/DrawerForm';
import EnhancedProTable from '@/components/EnhancedProTable';
import { formStatusType } from '@/constants';
import {
  createNotice,
  deleteNotice,
  getNoticePage,
  type NoticeVO,
  pushNotice,
  updateNotice,
} from '@/services/system/message/notice';
import { baseTenantColumns, formColumns } from './config';

const SyStemMessageNotice = () => {
  const tableRef = useRef<ActionType>(null);
  const configurableDrawerRef = useRef<ConfigurableDrawerFormRef>(null);
  const [type, setType] = useState<'create' | 'update'>('create');
  const [id, setId] = useState<number>(0);
  const onFetch = async (
    params: NoticeVO & {
      pageSize?: number;
      current?: number;
    },
  ) => {
    const data = await getNoticePage({
      ...params,
      pageNo: params.current,
      pageSize: params.pageSize,
    });
    return {
      data: data.list,
      success: true,
      total: data.total,
    };
  };

  const handleSend = async (record: NoticeVO) => {
    await pushNotice(record.id);
    tableRef.current?.reload();
  };

  const handleEdit = (record: NoticeVO) => {
    setType('update');
    setId(record.id);
    configurableDrawerRef.current?.open(record);
  };
  const handleSubmit = useCallback(
    async (values: NoticeVO) => {
      if (type === 'create') {
        await createNotice(values);
      } else {
        await updateNotice({
          ...values,
          id,
        });
      }
      tableRef.current?.reload();
      return true;
    },
    [type, id],
  );
  const actionColumns: ProColumns<NoticeVO> = {
    title: '操作',
    dataIndex: 'option',
    valueType: 'option',
    fixed: 'right',
    width: 120,
    render: (
      text: React.ReactNode,
      record: NoticeVO,
      _: number,
      action: ProCoreActionType | undefined,
    ) => [
      <a key="edit" onClick={() => handleEdit(record)}>
        编辑
      </a>,
      <Popconfirm
        title="是否删除?"
        key="delete"
        onConfirm={async () => {
          await deleteNotice(record.id);
          action?.reload?.();
        }}
        okText="是"
        cancelText="否"
      >
        <a style={{ color: 'var(--ant-red)' }} key="delete">
          删除
        </a>
      </Popconfirm>,
      <a key="detail" onClick={() => handleSend(record)}>
        推送
      </a>,
    ],
  };
  const columns = [...baseTenantColumns, actionColumns];
  return (
    <>
      <EnhancedProTable<NoticeVO>
        ref={tableRef}
        columns={columns}
        request={onFetch}
        headerTitle="登录日志"
        showIndex={false}
        showSelection={false}
      />
      <ConfigurableDrawerForm
        ref={configurableDrawerRef}
        title={formStatusType[type]}
        columns={formColumns(type)}
        onSubmit={handleSubmit}
      />
    </>
  );
};

export default SyStemMessageNotice;
