import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ContractArchivesConfidentialityLevelEnum,
  ContractArchivesFileStatusEnum,
  ContractArchivesFileTypeEnum,
  ContractArchivesPeriodTypeEnum,
} from '@/gen/Enums';
import { BetaSchemaForm, ProCard } from '@ant-design/pro-components';
import { Form, message } from 'antd';
import dayjs from 'dayjs';

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();

  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractArchives.Update>
      {...MyModalFormProps.props}
      title={`归档信息${props.title}`}
      trigger={<MyButtons.Edit />}
      layout="horizontal"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      labelAlign="left"
      style={{
        padding: '20px',
        height: '680px',
        overflowY: 'auto',
        overflowX: 'hidden',
      }}
      width="900px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values: any) =>
        Apis.Contract.ContractArchives.Update({
          ...values,
          id: props?.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'code',
          title: '归档编号',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
          fieldProps: {
            disabled: true,
          },
        },
        {
          key: 'name',
          title: '归档名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
          fieldProps: {
            disabled: true,
          },
        },
        {
          valueType: 'group',
          colProps: { span: 24 },
          columns: [
            MyFormItems.EnumRadio({
              key: 'period_type',
              title: '保管期限',
              colProps: { span: 12 },
              valueEnum: ContractArchivesPeriodTypeEnum,
              required: true,
            }),
            {
              name: ['period_type'],
              valueType: 'dependency',
              columns: ({ period_type }: any) => {
                return period_type === 'FixedTerm'
                  ? [
                      {
                        key: 'storage_years',
                        title: '保管年限',
                        valueType: 'digit',
                        formItemProps: { ...rulesHelper.number },
                        colProps: { span: 12 },
                        fieldProps: {
                          suffix: '年',
                          style: {
                            width: '100%',
                          },
                        },
                      },
                    ]
                  : [];
              },
            },
          ],
        },
        MyFormItems.EnumRadio({
          key: 'confidentiality_level',
          title: '保密等级',
          colProps: { span: 24 },
          formItemProps: {
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
            required: true,
          },
          valueEnum: ContractArchivesConfidentialityLevelEnum,
        }),
        {
          key: 'box_number',
          title: '文件盒编号',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
        },
        {
          key: 'cabinet_number',
          title: '文件柜编号',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 12 },
        },
        {
          key: 'archive_date',
          title: '归档日期',
          valueType: 'date',
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            minDate: dayjs().subtract(1, 'month'),
            maxDate: dayjs(),
            style: {
              width: '100%',
            },
            onChange: (date: any) => {
              console.log(date);
              form.setFieldValue('archive_year', dayjs(date).format('YYYY'));
            },
          },
          colProps: { span: 12 },
        },
        {
          key: 'archive_year',
          title: '归档年份',
          fieldProps: {
            disabled: true,
          },
          colProps: { span: 12 },
        },
        {
          valueType: 'divider',
          fieldProps: {
            orientation: 'left',
            children: '归档文件',
          },
        },
        {
          valueType: 'formList',
          dataIndex: 'contract_archive_files',
          title: '',
          formItemProps: {
            ...rulesHelper.array,
            labelCol: { span: 0 },
            wrapperCol: { span: 24 },
          },
          colProps: { span: 24 },
          fieldProps: {
            copyIconProps: false,
            itemRender: ({ listDom, action }: any, { index }: any) => {
              return (
                <ProCard
                  bordered
                  extra={action}
                  title={`归档文件${index + 1}`}
                  style={{
                    marginBlockEnd: 8,
                  }}
                >
                  {listDom}
                </ProCard>
              );
            },
          },

          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                {
                  title: '文件ID',
                  key: 'id',
                  colProps: { span: 6 },
                  fieldProps: {
                    disabled: true,
                  },
                },
                {
                  title: '名称',
                  key: 'name',
                  colProps: { span: 8 },
                  formItemProps: { ...rulesHelper.text },
                },
                MyFormItems.EnumSelect({
                  key: 'type',
                  title: '类型',
                  valueEnum: ContractArchivesFileTypeEnum,
                  colProps: { span: 8 },
                  formItemProps: { ...rulesHelper.text },
                }),
                {
                  title: '份数',
                  key: 'number',
                  valueType: 'digit',
                  colProps: { span: 6 },
                  fieldProps: {
                    style: {
                      width: '100%',
                    },
                  },
                  formItemProps: { ...rulesHelper.number },
                },
                MyFormItems.EnumSelect({
                  key: 'status',
                  title: '状态',
                  valueEnum: ContractArchivesFileStatusEnum,
                  colProps: { span: 8 },
                  formItemProps: { ...rulesHelper.text },
                }),
                {
                  title: '备注',
                  key: 'remark',
                  colProps: { span: 10 },
                },
                MyFormItems.UploadImages({
                  key: 'info',
                  title: '上传扫描件',
                  uploadType: 'file',
                  colProps: { span: 12 },
                  max: 100,
                  formItemProps: { ...rulesHelper.array },
                  className: 'my_upload_file',
                }),
              ],
            },
          ],
        },
      ]}
    />
  );
}
