import {
  MyButtons,
  MyColumns,
  MyProTableProps,
  useCurrentPermissions,
} from '@/common';
import { isInTimeRange } from '@/common/utils/day';
import { Apis } from '@/gen/Apis';
import { SurveyReleasesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Create from '../modals/SurveyReleasesCreate';
import Update from '../modals/SurveyReleasesUpdate';

export default function SurveyReleasesList({ title = '问卷发布列表' }) {
  const getCurrentPermissions = useCurrentPermissions();
  let toolBarRender = (action: any) => {
    return getCurrentPermissions(
      {
        add: <Create key="Create" reload={action?.reload} title="发布" />,
      },
      'MySurveyReleasesList',
    );
  };
  let tableRender = (item: any, action: any) => {
    return getCurrentPermissions(
      {
        publish: (
          <MyButtons.Default
            isConfirm
            title="立即发布"
            description="确定要发布？"
            disabled={item?.release_type !== 'Manual' || item.status}
            onConfirm={() =>
              Apis.Survey.SurveyReleases.Publish({ id: item.id }).then(() =>
                action?.reload(),
              )
            }
          />
        ),
        cancel: (
          <MyButtons.Default
            isConfirm
            title="下架"
            description="确定要下架问卷？"
            disabled={
              !item.status && !isInTimeRange(item?.start_date, item?.end_date)
            }
            onConfirm={() =>
              Apis.Survey.SurveyReleases.Cancel({ id: item.id }).then(() =>
                action?.reload(),
              )
            }
          />
        ),
        update: <Update item={item} reload={action?.reload} title={title} />,
        delete: (
          <MyButtons.Delete
            onConfirm={() =>
              Apis.Survey.SurveyReleases.Delete({ id: item.id }).then(() =>
                action?.reload(),
              )
            }
          />
        ),
      },
      'MySurveyReleasesList',
    );
  };
  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle={`${title}`}
      request={async (params, sort) =>
        MyProTableProps.request(params, sort, Apis.Survey.SurveyReleases.List)
      }
      toolBarRender={(action) => [toolBarRender(action)]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '问卷ID',
          hidden: true,
          dataIndex: 'surveys_id',
          //   search: {
          //     transform: (value) => {
          //       return { surveys_id: value };
          //     },
          //   },
        },
        MyColumns.EnumTag({
          title: '方式',
          dataIndex: 'release_type',
          valueEnum: SurveyReleasesTypeEnum,
        }),
        MyColumns.Boolean({
          title: '是否发布',
          dataIndex: 'status',
        }),
        { dataIndex: ['survey', 'name'], title: '发布的问卷', search: false },
        { dataIndex: 'start_date', title: '开始时间', search: false },
        { dataIndex: 'end_date', title: '结束时间', search: false },
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={`item_${index}`}>{tableRender(item, action)}</Space>
          ),
        }),
      ]}
    />
  );
}
