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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Bill.Bills.Store>
      {...MyModalFormProps.props}
      title={`手动添加账单`}
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      // wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title={`手动添加账单`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Bill.Bills.Store({
          ...values,
          flow_type: BillsFlowTypeEnum.Income.value,
          year: parseInt(values.year_month.split('-')[0]),
          month: parseInt(values.year_month.split('-')[1]),
        })
          .then(() => {
            props.reload?.();
            message.success('手动添加账单成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          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,
                    },
                  }),
                ],
              },
            ];
          },
        },

        MyFormItems.EnumRadio({
          key: 'type',
          title: '费用类型',
          colProps: { span: 24 },
          valueEnum: BillPaymentsTypeEnum,
          required: true,
        }),
        {
          key: 'amount',
          title: '账单金额',
          valueType: 'digit',
          colProps: { span: 24 },
          fieldProps: {
            addonAfter: '元',
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'year_month',
          title: '账单月份',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            picker: 'month',
            format: 'YYYY-MM',
            valueFormat: 'YYYY-MM',
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '备注',
          key: 'remark',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
