import {
  MyButtons,
  MyColumns,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { SurveyReleasesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import QuestionCategoriesManage from '../modals/QuestionCategoriesManage';
import QuestionsManage from '../modals/QuestionsManage';
import Create from '../modals/SurveyReleasesCreate';
import Update from '../modals/SurveyReleasesUpdate';
import SurveyResponsesByRelease from '../modals/SurveyResponsesByRelease';
import SurveysListShow from '../modals/SurveysListShow';
import SurveysManage from '../modals/SurveysManage';

export default function SurveyReleasesList({ title = '问卷发布列表' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      headerTitle={`${title}`}
      request={async (params, sort) =>
        MyProTableProps.request(params, sort, Apis.Survey.SurveyReleases.List)
      }
      toolBarRender={(action) => [
        <MyToolBarActions
          key="toolbar"
          actions={{
            add: <Create key="Create" reload={action?.reload} title="发布" />,
            questionCategories: (
              <QuestionCategoriesManage key="questionCategories" />
            ),
            questions: <QuestionsManage key="questions" />,
            surveys: <SurveysManage key="surveys" />,
          }}
        />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        {
          title: '问卷ID',
          hidden: true,
          dataIndex: 'surveys_id',
        },
        MyColumns.EnumTag({
          title: '方式',
          dataIndex: 'release_type',
          valueEnum: SurveyReleasesTypeEnum,
        }),
        MyColumns.Boolean({
          title: '是否发布',
          dataIndex: 'status',
        }),
        {
          dataIndex: ['survey', 'name'],
          title: '发布的问卷',
          search: false,
          render: (_, item: any) => (
            <SurveysListShow
              item={item?.survey}
              title={item?.survey?.name}
              trigger={<a>{item?.survey?.name}</a>}
            />
          ),
        },
        {
          title: '开始时间',
          dataIndex: 'start_date',
          search: false,
          //日期
          valueType: 'date',
        },
        {
          title: '结束时间',
          dataIndex: 'end_date',
          valueType: 'date',
          search: false,
        },
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, _index, action) => (
            <MyTableActions
              actions={{
                publish: (
                  <MyButtons.Default
                    isConfirm
                    title="立即发布"
                    description="确定要发布？"
                    disabled={item?.release_type !== 'Manual' || item.status}
                    onConfirm={() =>
                      Apis.Survey.SurveyReleases.Publish({ 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(),
                      )
                    }
                  />
                ),
                viewResponses: <SurveyResponsesByRelease releaseId={item.id} />,
              }}
              maxVisible={3}
            />
          ),
        }),
      ]}
    />
  );
}
