import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { SelectContract } from '@/components/SelectContract';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { useSearchParams } from '@umijs/max';
import { Form, message } from 'antd';
import { useEffect, useState } from 'react';

export default function UseSeal(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const [searchParams] = useSearchParams();
  const id = searchParams.get('id') ?? 0;

  const [data, setData] = useState<any>({}); // 修复状态变量命名不一致
  const loadShow = () => {
    const currentId = props?.item?.id || Number(id);
    if (currentId) {
      Apis.Contract.Contracts.Show({ id: Number(currentId) }).then((res) => {
        setData(res?.data);
        form.setFieldsValue({
          ...res?.data,
          peoples: res?.data?.contract_peoples,
        }); // 编辑赋值
      });
    }
  };

  useEffect(() => {
    loadShow();
  }, [id]);
  return (
    <BetaSchemaForm<ApiTypes.Contract.Contracts.Update>
      {...MyModalFormProps.props}
      title={`简易变更`}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
          form.setFieldValue('organization', [props.item?.sign_department]);
        }
      }}
      trigger={
        <MyButtons.Default
          title={props?.title}
          size="small"
          color="pink"
          disabled={
            props.item?.status !== 'Signed' &&
            props.item?.status !== 'Archived' &&
            props.item?.status !== 'Approved'
          }
          variant="solid"
          type="primary"
        />
      }
      onFinish={async (values: any) => {
        console.log(values, 'values');
        return Apis.Contract.Contracts.Update({
          ...values,
          id: props?.item?.id,
          code: props?.item?.code,
          sign_department:
            values.organization?.[values.organization?.length - 1],
          income_expense_type: props?.item?.income_expense_type,
          contract_nature: props?.item?.contract_nature,
          contract_types_id: props?.item?.contract_types_id,
          start_time: props?.item?.start_time,
          end_time: props?.item?.end_time,
          settlement_mode: props?.item?.settlement_mode,
          sign_subject: props?.item?.sign_subject,
          attachments: props?.item?.attachments || [],
          peoples: data?.contract_peoples, // 使用从show接口获取的数据
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          key: 'name',
          title: '合同名称',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        SelectContract.EmployeeName({
          key: 'contract_liaison',
          title: '合同对接人',
          colProps: { span: 24 },
          required: true,
        }),
        {
          title: '合同份数',
          key: 'number_contract_copies',
          valueType: 'digit',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
          fieldProps: {
            style: { width: '100%' },
          },
        },
      ]}
    />
  );
}
