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

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const [sealLogId, setSealLogId] = useState<number>(0);

  const handleOpen = (open: boolean) => {
    if (open && props.item?.id) {
      Apis.Contract.ContractSealLogs.List({
        contracts_id: props.item.id,
      }).then((res: any) => {
        const list = res?.data || [];
        if (list.length > 0) {
          const log = list[0];
          setSealLogId(log.id);
          form.resetFields();
          form.setFieldsValue({
            ...log,
            seal_ids: log?.seal_info?.map((item: any) => ({
              seal_id: item?.id || item,
            })),
          });
        }
      });
    }
  };

  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractSealLogs.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={handleOpen}
      onFinish={async (values: any) =>
        Apis.Contract.ContractSealLogs.Update({
          ...values,
          contracts_id: props?.item?.id,
          is_use_seal: values?.is_use_seal ? 1 : 0,
          is_external_seal_usage: values?.is_external_seal_usage ? 1 : 0,
          id: sealLogId,
          seal_ids: values?.seal_ids?.map((item: any) => item.seal_id),
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.Employees({
          title: '用印申请人',
          key: 'company_employees_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          required: true,
        }),

        {
          key: 'number_contract_copies',
          title: '合同份数',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        {
          title: '用印时间',
          key: 'use_seal_time',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            style: {
              width: '100%',
            },
            disabledDate: (current: any) => {
              const today = new Date();
              today.setHours(0, 0, 0, 0);
              return current && current > today;
            },
          },
        },
        {
          title: '交易方用印',
          tooltip: '该合同中，非我司主体的交易方，是否已经盖章',
          key: 'is_use_seal',
          valueType: 'switch',
          colProps: { span: 24 },
          fieldProps: {
            checkedChildren: '已用印',
            unCheckedChildren: '未用印',
          },
        },
        {
          valueType: 'formList',
          dataIndex: 'seal_ids',
          title: '实际用章',
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            copyIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                Selects?.CompanySeals({
                  title: '',
                  key: 'seal_id',
                  colProps: { span: 24 },
                  required: true,
                  fieldProps: {
                    placeholder: '请选择要使用的印章（可搜索）',
                  },
                }),
              ],
            },
          ],
        },
        {
          title: '备注',
          key: 'remarks',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
