import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseMetersUsageTypeEnum } 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.HouseMeters.Store>
      {...MyModalFormProps.props}
      title={`添加仪表`}
      // wrapperCol={{ span: 24 }}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      trigger={<MyButtons.Create title={`仪表`} />}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
          // form.setFieldsValue(props?.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Meter.HouseMeters.Store({
          ...values,
          latest_value: values.initial_value,
          latest_time: values.initial_time,
          meter_type: props?.item?.meter_type,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        ...(props?.item?.id
          ? []
          : [
              Selects?.AssetProjects({
                key: 'asset_projects_id',
                title: '选择项目',
                colProps: { span: 24 },
                required: true,
              }),
            ]),

        // MyFormItems.EnumRadio({
        //   key: 'meter_type',
        //   title: '仪表类型',
        //   colProps: { span: 24 },
        //   valueEnum: HouseMetersMeterTypeEnum,
        //   required: true,
        // }),
        MyFormItems.EnumRadio({
          key: 'usage_type',
          title: '使用类型',
          colProps: { span: 24 },
          // valueEnum: HouseMetersUsageTypeEnum,
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(HouseMetersUsageTypeEnum));
            delete obj.GasMeter;
            delete obj.HeatingMeter;
            return obj;
          },
          required: true,
        }),
        {
          key: 'name',
          title: '仪表编号',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'multiple',
          title: '倍率',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'initial_value',
          title: '初始抄表读数',
          valueType: 'number',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
        },
        {
          key: 'initial_time',
          title: '初始抄表时间',
          valueType: 'dateTime',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            disabledDate: (current: any) => {
              // 禁用未来的日期
              return current && current > new Date();
            },
            disabledTime: (current: any) => {
              // 如果是今天，禁用未来的时间
              if (
                current &&
                current.format('YYYY-MM-DD') ===
                  new Date().toISOString().split('T')[0]
              ) {
                const now = new Date();
                return {
                  disabledHours: () => {
                    const hours = [];
                    for (let i = now.getHours() + 1; i < 24; i++) {
                      hours.push(i);
                    }
                    return hours;
                  },
                  disabledMinutes: (selectedHour: number) => {
                    if (selectedHour === now.getHours()) {
                      const minutes = [];
                      for (let i = now.getMinutes() + 1; i < 60; i++) {
                        minutes.push(i);
                      }
                      return minutes;
                    }
                    return [];
                  },
                  disabledSeconds: (
                    selectedHour: number,
                    selectedMinute: number,
                  ) => {
                    if (
                      selectedHour === now.getHours() &&
                      selectedMinute === now.getMinutes()
                    ) {
                      const seconds = [];
                      for (let i = now.getSeconds() + 1; i < 60; i++) {
                        seconds.push(i);
                      }
                      return seconds;
                    }
                    return [];
                  },
                };
              }
              return {};
            },
          },
        },
        {
          valueType: 'dependency',
          name: ['asset_projects_id', 'usage_type', 'meter_type'],
          columns: ({ asset_projects_id, usage_type }) => {
            return [
              Selects?.ChargeStandard({
                title: '选择收费标准',
                tooltip: '如无可选信息，请在【财务管理：收费标准】中先行配置',
                key: 'charge_standards_id',
                params: {
                  asset_projects_id: asset_projects_id || props?.item?.id || 0,
                  charge_type: [
                    props?.item?.meter_type === 'HouseMeter'
                      ? usage_type === 'WaterMeter'
                        ? 'WaterFee'
                        : 'ElectricityFee'
                      : props?.item?.meter_type === 'CommonMeter'
                      ? usage_type === 'WaterMeter'
                        ? 'SharedWaterFee'
                        : 'SharedElectricityFee'
                      : '',
                  ],
                },
                colProps: { span: 24 },
                // formItemProps: { ...rulesHelper.text },
              }),
            ];
          },
        },
        MyFormItems.UploadImages({
          key: 'images',
          title: '仪表图片',
          tooltip: '只能上传1张图片',
          max: 1,
          colProps: { span: 24 },
          // formItemProps: { required: true },
        }),
        {
          key: 'remark',
          title: '备注',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
