import {
  MyButtons,
  MyColumns,
  MyProTableProps,
  useCurrentPermissions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  EmergencyEventsCompensationTypeEnum,
  EmergencyEventsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Assign from './modals/ApplyClose';
import Create from './modals/Create';
import Review from './modals/Review';
import Update from './modals/Update';

export default function Index({ title = '突发事件' }) {
  const getCurrentPermissions = useCurrentPermissions();

  let toolBarRender = (action: any) => {
    return getCurrentPermissions(
      {
        create: <Create key="Create" reload={action?.reload} title={title} />,
      },
      'MyEvents',
    );
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        show: (
          <MyButtons.View
            key="show"
            title="查看"
            to={`/emergency/events/show/${item.id}`}
          />
        ),
        assign: <Assign item={item} reload={action?.reload} />,
        update: <Update item={item} reload={action?.reload} title={title} />,
        review: <Review item={item} reload={action?.reload} />,
        delete: (
          <MyButtons.Delete
            onConfirm={() =>
              Apis.Emergency.EmergencyEvents.Delete({
                id: item.id,
              }).then(() => action?.reload())
            }
          />
        ),
      },
      'MyEvents',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Emergency.EmergencyEvents.List,
        )
      }
      headerTitle={`${title}列表`}
      toolBarRender={(action: any) => [toolBarRender(action)]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '名称',
          dataIndex: 'name',
        },
        {
          title: '所属项目',
          dataIndex: ['asset_project', 'name'],
          search: false,
        },
        MyColumns.EnumTag({
          title: '状态',
          dataIndex: 'status',
          valueEnum: EmergencyEventsStatusEnum,
        }),
        MyColumns.EnumTag({
          title: '赔偿方',
          dataIndex: 'compensation_type',
          valueEnum: EmergencyEventsCompensationTypeEnum,
        }),
        {
          title: '赔偿事项',
          search: false,
          render: (_, item: any) => {
            return (
              <div>
                <div>赔偿比例：{item?.compensation_rate}%</div>
                <div>赔偿金额：{item?.compensation_amount}元</div>
                <div>已支付赔偿金额：{item?.compensation_paid_amount}元</div>
              </div>
            );
          },
        },
        {
          title: '分类',
          search: false,
          render: (_, item: any) => {
            return `${item?.one_emergency_categories_name} / ${item?.two_emergency_categories_name}`;
          },
        },
        {
          title: '等级',
          dataIndex: 'emergency_event_levels_name',
          search: false,
        },
        MyColumns.UpdatedAt(),
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={index}>{tableRender(item, action)}</Space>
          ),
        }),
      ]}
    />
  );
}
