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

export default function Copy(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const isCarPortFee =
    props.item?.type === 'CarPortFee' || props.item?.type === 'CarPortDeposit';

  return (
    <BetaSchemaForm<ApiTypes.Bill.HouseBills.Store>
      {...MyModalFormProps.props}
      title={`账单复制`}
      trigger={<MyButtons.Default title="复制" type="primary" size="small" />}
      wrapperCol={{ span: 24 }}
      width="800px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          const formValues = {
            type: props.item?.type,
            amount: props.item?.amount,
            discount_amount: props.item?.discount_amount,
            late_fee: props.item?.late_fee,
            company_receipt_accounts_id:
              props.item?.company_receipt_accounts_id,
            remark: props.item?.remark,
          };
          form.setFieldsValue(formValues);
        }
      }}
      onFinish={async (values) => {
        const isCarPortFee =
          props.item?.type === 'CarPortFee' ||
          props.item?.type === 'CarPortDeposit';
        return Apis.Bill.HouseBills.Store({
          ...values,
          type: props.item?.type,
          asset_car_ports_id: isCarPortFee
            ? props.item?.asset_car_ports_id
            : undefined,
          asset_houses_id: !isCarPortFee
            ? props.item?.asset_houses_id
            : undefined,
        })
          .then(() => {
            props.reload?.();
            message.success('手动添加账单成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          valueType: 'dependency',
          name: ['type'],
          columns: ({ type }) => {
            const field =
              type === 'CarPortFee' || type === 'CarPortDeposit'
                ? {
                    key: 'asset_car_port_full_name',
                    title: '车位信息',
                    colProps: { span: 18 },
                    fieldProps: {
                      disabled: true,
                    },
                    initialValue: props.item?.asset_car_port?.full_name,
                  }
                : {
                    key: 'asset_house_full_name',
                    title: '房屋信息',
                    colProps: { span: 18 },
                    fieldProps: {
                      disabled: true,
                    },
                    initialValue: props.item?.asset_house?.full_name,
                  };
            return [field];
          },
        },
        MyFormItems.EnumSelect({
          key: 'type',
          title: '类型',
          colProps: { span: 6 },
          valueEnum: HouseBillsTypeEnum,
          required: true,
          fieldProps: {
            disabled: true,
          },
        }),
        {
          key: 'amount',
          title: '金额',
          valueType: 'digit',
          colProps: { span: 8 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'discount_amount',
          title: '优惠金额',
          valueType: 'digit',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 8 },
        },

        {
          key: 'late_fee',
          title: '滞纳金',
          valueType: 'digit',
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          colProps: { span: 8 },
        },
        {
          key: 'month',
          title: '账单月份',
          valueType: 'date',
          colProps: { span: 8 },
          fieldProps: {
            picker: 'month',
            format: 'YYYY-MM',
            valueFormat: 'YYYY-MM',
            style: {
              width: '100%',
            },
            onChange: (e: any, dateString: string) => {
              form.setFieldsValue({
                start_date: rulesHelper.getMonthStartDate(dateString),
                end_date: rulesHelper.getMonthEndDate(dateString),
              });
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'start_date',
          title: '计费开始日期',
          valueType: 'date',
          colProps: { span: 8 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'end_date',
          title: '计费结束日期',
          valueType: 'date',
          colProps: { span: 8 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },

        {
          valueType: 'dependency',
          name: ['asset_projects_id'],
          columns: ({ asset_projects_id }) => {
            return [
              {
                valueType: 'group',
                columns: [
                  Selects?.ProjectAccounts({
                    key: 'company_receipt_accounts_id',
                    title: '选择收款账户',
                    params: {
                      projects_id: asset_projects_id,
                    },
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.number },
                    fieldProps: {
                      showSearch: true,
                    },
                  }),
                ],
              },
            ];
          },
        },
        {
          title: '备注',
          key: 'remark',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
