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={`创建工单`}
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      width="600px"
      layout="horizontal"
      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: 24 },
          // valueEnum: HouseWorkOrdersTypeEnum,
          valueEnum: () => {
            let obj: any = JSON.parse(JSON.stringify(HouseWorkOrdersTypeEnum));
            delete obj.Complaint;
            delete obj.RenovationAcceptance;
            delete obj.EquipmentMaintenance;
            delete obj.RenovationInspection;
            delete obj.Emergency;
            delete obj.EmergEquipmentMaintenancency;
            delete obj.SecurityInspection;
            delete obj.Complaint;
            delete obj.QualityCheck;
            return obj;
          },
          required: true,
        }),
        {
          name: ['type'],
          valueType: 'dependency',
          columns: ({ type }: any) => {
            return type === HouseWorkOrdersTypeEnum.Repair.value
              ? [
                  MyFormItems.EnumRadio({
                    key: 'location',
                    title: '报修位置',
                    colProps: { span: 24 },
                    valueEnum: HouseWorkOrdersLocationEnum,
                    required: true,
                    fieldProps: {
                      onChange: () => {
                        form.setFieldValue('asset_buildings_id', undefined);
                        form.setFieldValue('asset_units_id', undefined);
                        form.setFieldValue('asset_houses_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: 12 },
                                        formItemProps: {
                                          labelCol: { span: 8 },
                                          wrapperCol: { span: 16 },
                                          ...rulesHelper.text,
                                        },
                                        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: 6 },
                                        formItemProps: {
                                          labelCol: { span: 0 },
                                          wrapperCol: { span: 24 },
                                          ...rulesHelper.text,
                                        },
                                        fieldProps: {
                                          placeholder: '选择单元',
                                          showSearch: true,
                                        },
                                      }),
                                      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: {
                                          labelCol: { span: 0 },
                                          wrapperCol: { span: 24 },
                                          ...rulesHelper.text,
                                        },
                                        fieldProps: {
                                          placeholder: '选择房屋',
                                          showSearch: true,
                                        },
                                        colProps: { span: 6 },
                                      }),
                                    ],
                                  },
                                ];
                              },
                            },
                          ]
                        : !location
                        ? []
                        : [
                            Selects?.AssetProjects({
                              title: '选择项目',
                              key: 'asset_projects_id',
                              colProps: { span: 24 },
                              formItemProps: { ...rulesHelper.text },
                            }),
                          ];
                    },
                  },
                ]
              : type === HouseWorkOrdersTypeEnum.Incident.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);
                      },
                    },
                  }),
                ]
              : [];
          },
        },

        {
          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: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          key: 'reporter_phone',
          title: '联系电话',
          formItemProps: { ...rulesHelper.phone },
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '请输入手机号码',
            maxLength: 11,
            minLength: 11,
          },
        },
      ]}
    />
  );
}
