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

export default function WorkOrderCreate(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.WorkOrder.HouseWorkOrders.Store>
      {...MyModalFormProps.props}
      title={`创建工单`}
      wrapperCol={{ span: 24 }}
      width="600px"
      trigger={<MyButtons.Create title={`创建工单`} />}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values) =>
        Apis.WorkOrder.HouseWorkOrders.Store(values)
          .then(() => {
            props.reload?.();
            message.success('创建工单成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        MyFormItems.EnumRadio({
          key: 'type',
          title: '工单类型',
          colProps: { span: 8 },
          // valueEnum: HouseWorkOrdersTypeEnum,
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(HouseWorkOrdersTypeEnum));
            delete obj.Complaint;
            return obj;
          },
          required: true,
          fieldProps: {
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('calculation_mode', undefined);
            },
          },
        }),
        MyFormItems.EnumRadio({
          key: 'location',
          title: '报修位置',
          colProps: { span: 8 },
          valueEnum: HouseWorkOrdersLocationEnum,
          required: true,
          fieldProps: {
            onChange: () => {
              // 切换计量单位时清空计费模式
              form.setFieldValue('asset_projects_id', undefined);
            },
          },
        }),

        {
          name: ['location'],
          valueType: 'dependency',
          columns: ({ location }: any) => {
            return location === HouseWorkOrdersLocationEnum.MyHome.value
              ? [
                  Selects?.AssetProjects({
                    title: '选择项目',
                    key: 'asset_projects_id',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                    fieldProps: {
                      onChange: () => {
                        // 切换计量单位时清空计费模式
                        form.setFieldValue('asset_buildings_id', undefined);
                        form.setFieldValue('asset_units_id', undefined);
                        form.setFieldValue('asset_floors_id', undefined);
                      },
                    },
                  }),
                  {
                    valueType: 'dependency',
                    name: [
                      'asset_projects_id',
                      'asset_buildings_id',
                      'asset_units_id',
                    ],
                    columns: ({
                      asset_projects_id,
                      asset_buildings_id,
                      asset_units_id,
                    }) => {
                      return [
                        {
                          valueType: 'group',
                          columns: [
                            Selects?.AssetBuildings({
                              key: 'asset_buildings_id',
                              title: '楼栋',
                              params: {
                                asset_projects_id: asset_projects_id,
                              },
                              colProps: { span: 8 },
                              formItemProps: {
                                ...rulesHelper.number,
                              },
                              fieldProps: {
                                showSearch: true,
                                onChange: () => {
                                  form.setFieldsValue({
                                    asset_units_id: undefined,
                                    asset_floors_id: undefined,
                                  });
                                },
                              },
                            }),
                            Selects?.AssetUnits({
                              key: 'asset_units_id',
                              title: '单元',
                              params: {
                                asset_projects_id: asset_projects_id,
                                asset_buildings_id: asset_buildings_id,
                              },
                              colProps: { span: 8 },
                              formItemProps: {
                                ...rulesHelper.number,
                              },
                            }),
                            Selects?.AssetHouses({
                              title: '房屋',
                              key: 'asset_houses_id',
                              params: {
                                asset_projects_id: asset_projects_id,
                                asset_buildings_id: asset_buildings_id,
                                asset_units_id: asset_units_id,
                              },
                              formItemProps: { ...rulesHelper.text },
                              colProps: { span: 8 },
                            }),
                          ],
                        },
                      ];
                    },
                  },
                ]
              : !location
              ? []
              : [
                  Selects?.AssetProjects({
                    title: '选择项目',
                    key: 'asset_projects_id',
                    colProps: { span: 24 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                ];
          },
        },
        {
          key: 'content',
          title: '工单描述',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            placeholder: '请详细描述工单内容',
            rows: 4,
          },
        },

        MyFormItems.UploadImages({
          key: 'attachments',
          title: '相关图片',
          tooltip: '最多上传5张图片',
          // uploadType: 'file',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),

        // {
        //   key: 'reporter_name',
        //   title: '上报人姓名',
        //   colProps: { span: 12 },
        // },
        // {
        //   key: 'reporter_phone',
        //   title: '上报人手机',
        //   // formItemProps: { ...rulesHelper.phone },
        //   colProps: { span: 12 },
        //   fieldProps: {
        //     placeholder: '请输入手机号码',
        //     maxLength: 11,
        //     minLength: 11,
        //   },
        // },
      ]}
    />
  );
}
