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 WorkOrderUpdate(
  props: MyBetaModalFormProps & { item: any },
) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.WorkOrder.HouseWorkOrders.Update>
      {...MyModalFormProps.props}
      title={`编辑工单`}
      wrapperCol={{ span: 24 }}
      width="600px"
      key={new Date().getTime()}
      trigger={<MyButtons.Edit title={`编辑`} />}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          const formValues = {
            ...props.item,
          };
          form.setFieldsValue(formValues);
        }
      }}
      form={form}
      request={async () => {
        const res = await Apis.WorkOrder.HouseWorkOrders.Show({
          id: props.item.id,
        });
        return res.data;
      }}
      onFinish={async (values) =>
        Apis.WorkOrder.HouseWorkOrders.Update({
          ...values,
          id: props.item.id,
        })
          .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,
        }),

        {
          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 },
        }),
      ]}
    />
  );
}
