import { PlusOutlined } from '@ant-design/icons';
import type {
  ActionType,
  ProColumns,
  ProCoreActionType,
} from '@ant-design/pro-components';
import { message, Popconfirm } from 'antd';
import React, { useCallback, useRef, useState } from 'react';
import ConfigurableDrawerForm, {
  type ConfigurableDrawerFormRef,
} from '@/components/DrawerForm';
import EnhancedProTable from '@/components/EnhancedProTable';
import type { ToolbarAction } from '@/components/EnhancedProTable/types';
import { formStatusType } from '@/constants';
import {
  createSmsChannel,
  deleteSmsChannel,
  getSmsChannelPage,
  type SmsChannelVO,
  updateSmsChannel,
} from '@/services/system/message/sms/channel';
import { baseTenantColumns, formColumns } from './config';

const SyStemLogLogin = () => {
  const tableRef = useRef<ActionType>(null);
  const configurableDrawerRef = useRef<ConfigurableDrawerFormRef>(null);
  const [type, setType] = useState<'create' | 'update'>('create');
  const [currentItem, setCurrentItem] = useState<SmsChannelVO>();
  const onFetch = async (
    params: SmsChannelVO & {
      pageSize?: number;
      current?: number;
    },
  ) => {
    const data = await getSmsChannelPage({
      ...params,
      pageNo: params.current,
      pageSize: params.pageSize,
    });
    return {
      data: data.list,
      success: true,
      total: data.total,
    };
  };

  const handleEdit = (record: SmsChannelVO) => {
    setType('update');
    setCurrentItem(record);
    configurableDrawerRef.current?.open(record);
  };
  const handleAdd = () => {
    setType('create');
    configurableDrawerRef.current?.open({});
  };
  const toolbarActions: ToolbarAction[] = [
    {
      key: 'add',
      label: '新建',
      type: 'primary',
      icon: <PlusOutlined />,
      onClick: handleAdd,
    },
  ];
  const actionColumns: ProColumns<SmsChannelVO> = {
    title: '操作',
    dataIndex: 'option',
    valueType: 'option',
    fixed: 'right',
    width: 80,
    render: (
      text: React.ReactNode,
      record: SmsChannelVO,
      _: number,
      action: ProCoreActionType | undefined,
    ) => [
      <a key="editable" onClick={() => handleEdit(record)}>
        编辑
      </a>,
      <Popconfirm
        title="是否删除?"
        key="delete"
        onConfirm={async () => {
          await deleteSmsChannel(record.id);
          message.success('删除成功');
          action?.reload?.();
        }}
        okText="是"
        cancelText="否"
      >
        <a style={{ color: 'var(--ant-red)' }} key="delete">
          删除
        </a>
      </Popconfirm>,
    ],
  };
  const handleSubmit = useCallback(
    async (values: SmsChannelVO) => {
      if (type === 'create') {
        await createSmsChannel(values);
      } else {
        await updateSmsChannel({
          ...values,
          id: currentItem!.id,
        });
      }
      tableRef.current?.reload();
      return true;
    },
    [type, currentItem],
  );
  const columns = [...baseTenantColumns, actionColumns];
  return (
    <>
      <EnhancedProTable<SmsChannelVO>
        ref={tableRef}
        columns={columns}
        request={onFetch}
        toolbarActions={toolbarActions}
        headerTitle="短信渠道"
        showIndex={false}
        showSelection={false}
      />
      <ConfigurableDrawerForm
        ref={configurableDrawerRef}
        title={formStatusType[type]}
        columns={formColumns(type)}
        onSubmit={handleSubmit}
      />
    </>
  );
};

export default SyStemLogLogin;
