import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

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

  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractArchives.Update>
      {...MyModalFormProps.props}
      title={`归档信息${props.title}`}
      trigger={
        <MyButtons.Default
          type="primary"
          size="small"
          title={props.item?.is_sealed ? '解封' : '封存'}
        />
      }
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      labelAlign="left"
      width="500px"
      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,
          code: props?.item?.code,
          name: props?.item?.name,
          period_type: props?.item?.period_type,
          confidentiality_level: props?.item?.confidentiality_level,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          key: 'is_sealed',
          title: '是否封存',
          valueType: 'switch',
          width: '100%',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.boolean },
          fieldProps: {
            checkedChildren: '操作封存',
            unCheckedChildren: '取消封存',
            onChange: () => {
              form.setFieldValue('seal_date', undefined);
            },
          },
        },

        {
          name: ['is_sealed'],
          valueType: 'dependency',
          columns: ({ is_sealed }: any) => {
            return is_sealed
              ? [
                  {
                    key: 'seal_date',
                    title: '封存日期',
                    valueType: 'date',
                    formItemProps: { ...rulesHelper.text },
                    colProps: { span: 24 },
                  },
                ]
              : [];
          },
        },
      ]}
    />
  );
}
