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 Archives(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractArchives.Store>
      {...MyModalFormProps.props}
      title={`合同${props.title}`}
      layout="horizontal"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      labelAlign="left"
      width="800px"
      style={{
        padding: '20px',
        height: '680px',
        overflowY: 'auto',
        overflowX: 'hidden',
      }}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            code: props?.item?.code,
            name: props?.item?.name,
          });
        }
      }}
      trigger={
        <MyButtons.Default
          color={props?.item?.btnType || 'primary'}
          variant="solid"
          disabled={props?.item?.status !== 'Signed' && props?.item?.disabled}
          title={props?.title || '归档'}
          size={props?.item?.size || 'small'}
        />
      }
      onFinish={async (values: any) => {
        console.log(values, 'values');
        return Apis.Contract.ContractArchives.Store({
          ...values,
          contracts_id: props?.item?.id,
          code: props?.item?.code,
          name: props?.item?.name,
        })
          .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: 'file_number',
        //   title: '案卷编号',
        //   formItemProps: { ...rulesHelper.text },
        //   colProps: { span: 12 },
        // },
        {
          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 },
        },
        // Address.Cascader({
        //   key: 'casacader',
        //   title: '存放地区',
        //   colProps: { span: 12 },
        //   keys: ['province', 'city', 'area', 'street'],
        // }),
        // {
        //   key: 'address',
        //   title: '详细地址',
        //   colProps: { span: 12 },
        // },
        {
          key: 'archive_year',
          title: '归档年份',
          fieldProps: {
            disabled: true,
          },
          colProps: { span: 12 },
        },
        {
          valueType: 'divider',
          fieldProps: {
            orientation: 'left',
            children: '归档文件',
          },
        },
        {
          valueType: 'formList',
          dataIndex: 'archive_files',
          title: '',
          formItemProps: {
            ...rulesHelper.array,
            labelCol: { span: 0 },
            wrapperCol: { span: 24 },
          },
          colProps: { span: 24 },
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: 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: '文件名称',
                  key: 'name',
                  colProps: { span: 12 },
                  formItemProps: { ...rulesHelper.text },
                },
                MyFormItems.EnumSelect({
                  key: 'type',
                  title: '文件类型',
                  valueEnum: ContractArchivesFileTypeEnum,
                  colProps: { span: 12 },
                  formItemProps: { ...rulesHelper.text },
                }),
                {
                  title: '文件份数',
                  key: 'number',
                  valueType: 'digit',
                  colProps: { span: 12 },
                  fieldProps: {
                    style: {
                      width: '100%',
                    },
                  },
                  formItemProps: { ...rulesHelper.number },
                },
                MyFormItems.EnumSelect({
                  key: 'status',
                  title: '文件状态',
                  valueEnum: ContractArchivesFileStatusEnum,
                  colProps: { span: 12 },
                  formItemProps: { ...rulesHelper.text },
                }),
                {
                  title: '备注',
                  key: 'remark',
                  colProps: { span: 12 },
                },
                MyFormItems.UploadImages({
                  key: 'info',
                  title: '上传扫描件',
                  uploadType: 'file',
                  colProps: { span: 12 },
                  max: 100,
                  formItemProps: { ...rulesHelper.array },
                  className: 'my_upload_file',
                }),
              ],
            },
          ],
        },
      ]}
    />
  );
}
