import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseMetersMeterTypeEnum,
  HouseMeterTasksGenerationMethodEnum,
} 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.Meter.HouseMeterTasks.Store>
      {...MyModalFormProps.props}
      title={`创建${props.title}`}
      width="480px"
      layout="horizontal"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      labelAlign="left"
      trigger={<MyButtons.Create title={`创建${props.title}`} />}
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      onFinish={async (values: any) =>
        Apis.Meter.HouseMeterTasks.Store({
          ...values,
          data: [values?.data_id],
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '账单任务创建成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            onChange: (val: any) => {
              form.setFieldsValue({
                house_charge_standards_id: undefined,
              });
            },
          },
        }),

        {
          key: 'month',
          title: '选择生成月份',
          valueType: 'date',
          colProps: { span: 24 },
          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: 24 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'end_date',
          title: '计费结束日期',
          valueType: 'date',
          colProps: { span: 24 },
          fieldProps: {
            style: {
              width: '100%',
            },
          },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumRadio({
          key: 'meter_type',
          title: '仪表类型',
          colProps: { span: 24 },
          valueEnum: HouseMetersMeterTypeEnum,
          required: true,
          fieldProps: {
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('generation_method', undefined);
              form.setFieldValue('data', undefined);
            },
          },
        }),
        {
          name: ['meter_type'],
          valueType: 'dependency',
          columns: ({ meter_type }: any) => {
            return meter_type === HouseMetersMeterTypeEnum.CommonMeter.value
              ? [
                  MyFormItems.EnumRadio({
                    key: 'generation_method',
                    title: '生成方式',
                    colProps: { span: 24 },
                    valueEnum: () => {
                      let obj: any = JSON.parse(
                        JSON.stringify(HouseMeterTasksGenerationMethodEnum),
                      );
                      delete obj.Unit;
                      delete obj.Building;
                      return obj;
                    },
                    required: true,
                  }),
                  Selects?.AssetMeters({
                    title: '选择仪表',
                    params: {
                      asset_projects_id:
                        form.getFieldValue('asset_projects_id'),
                      // 仪表类型必须为房屋仪表
                      meter_type: HouseMetersMeterTypeEnum.CommonMeter.value,
                    },
                    key: 'data_id',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                ]
              : meter_type === HouseMetersMeterTypeEnum.HouseMeter.value
              ? [
                  MyFormItems.EnumRadio({
                    key: 'generation_method',
                    title: '生成方式',
                    colProps: { span: 24 },
                    valueEnum: HouseMeterTasksGenerationMethodEnum,
                    required: true,
                  }),
                  {
                    name: ['generation_method'],
                    valueType: 'dependency',
                    columns: ({ generation_method }: any) => {
                      return generation_method ===
                        HouseMeterTasksGenerationMethodEnum.Building.value
                        ? [
                            Selects?.AssetBuildings({
                              title: '选择楼栋',
                              key: 'data_id',
                              params: {
                                asset_projects_id:
                                  form.getFieldValue('asset_projects_id'),
                              },
                              colProps: { span: 24 },
                              formItemProps: { ...rulesHelper.text },
                            }),
                          ]
                        : generation_method ===
                          HouseMeterTasksGenerationMethodEnum.Unit.value
                        ? [
                            Selects?.AssetUnits({
                              title: '选择单元',
                              key: 'data_id',
                              params: {
                                asset_projects_id:
                                  form.getFieldValue('asset_projects_id'),
                              },
                              colProps: { span: 24 },
                              formItemProps: { ...rulesHelper.text },
                            }),
                          ]
                        : generation_method ===
                          HouseMeterTasksGenerationMethodEnum.Meter.value
                        ? [
                            Selects?.AssetMeters({
                              title: '选择仪表',
                              params: {
                                asset_projects_id:
                                  form.getFieldValue('asset_projects_id'),
                                // 仪表类型必须为房屋仪表
                                meter_type:
                                  HouseMetersMeterTypeEnum.HouseMeter.value,
                              },
                              key: 'data_id',
                              colProps: { span: 24 },
                              formItemProps: { ...rulesHelper.text },
                              fieldProps: {
                                showSearch: true,
                              },
                            }),
                          ]
                        : [];
                    },
                  },
                ]
              : [];
          },
        },
      ]}
    />
  );
}
