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';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  console.log(props.item, 'pppp');
  return (
    <BetaSchemaForm<ApiTypes.Company.CompanyProjectReceiptAccounts.Store>
      {...MyModalFormProps.props}
      form={form}
      title={`${props.title}`}
      wrapperCol={{ span: 24 }}
      width="500px"
      trigger={
        <MyButtons.Create
          title={`${props.title}`}
          size={props?.item?.size || 'middle'}
        />
      }
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          form.setFieldsValue({
            companies_id: props?.item?.companies_id || undefined,
            projects_id: props?.item?.id || undefined,
          });
        }
      }}
      key={new Date().getTime()}
      onFinish={async (values: any) =>
        Apis.Company.CompanyProjectReceiptAccounts.Store({
          ...values,
          companies_id: props?.item?.companies_id || values?.companies_id,
          projects_id: props?.item?.id || values?.projects_id,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.Companies({
                key: 'companies_id',
                title: '选择机构',
                colProps: { span: 24 },
                required: true,
                fieldProps: {
                  onChange: (val: any) => {
                    form.setFieldsValue({
                      projects_id: undefined,
                      receipt_accounts_id: undefined,
                    });
                  },
                },
              }),
            ]),
        {
          valueType: 'dependency',
          name: ['companies_id'],
          columns: ({ companies_id }) => {
            return [
              ...(props?.item?.id
                ? []
                : [
                    Selects?.AssetProjects({
                      key: 'projects_id',
                      title: '选择项目',
                      params: {
                        companies_id:
                          companies_id || props?.item?.companies_id || 0,
                      },
                      colProps: { span: 24 },
                      formItemProps: { ...rulesHelper.number },
                      fieldProps: {
                        showSearch: true,
                      },
                    }),
                  ]),
              Selects?.CompanyAccounts({
                key: 'receipt_accounts_id',
                title: '选择收款账户',
                params: {
                  companies_id:
                    companies_id || props?.item?.companies_id || undefined,
                },
                colProps: { span: 24 },
                formItemProps: { ...rulesHelper.number },
              }),
            ];
          },
        },
      ]}
    />
  );
}
