import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersLevelEnum, 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: 24 },
          valueEnum: HouseWorkOrdersTypeEnum,
          required: true,
        }),

        {
          key: 'title',
          title: '工单标题',
          colProps: { span: 18 },
          formItemProps: { ...rulesHelper.text },
        },
        MyFormItems.EnumSelect({
          key: 'level',
          title: '优先级',
          colProps: { span: 6 },
          valueEnum: HouseWorkOrdersLevelEnum,
          required: true,
        }),
        {
          key: 'content',
          title: '工单描述',
          valueType: 'textarea',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            placeholder: '请详细描述工单内容',
          },
        },

        {
          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?.AssetProjects({
                    title: '依次选择项目',
                    key: 'asset_projects_id',
                    colProps: { span: 6 },
                    formItemProps: { ...rulesHelper.text },
                  }),
                  Selects?.AssetBuildings({
                    key: 'asset_buildings_id',
                    title: '楼栋',
                    params: {
                      asset_projects_id: asset_projects_id,
                    },
                    colProps: { span: 6 },
                    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: 6 },
                    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: 6 },
                  }),
                ],
              },
            ];
          },
        },
        MyFormItems.UploadImages({
          key: 'attachments',
          title: '相关图片',
          uploadType: 'file',
          max: 5,
          colProps: { span: 24 },
          formItemProps: { required: false },
        }),
        {
          key: 'reporter_name',
          title: '上报人姓名',
          colProps: { span: 12 },
        },
        {
          key: 'reporter_phone',
          title: '上报人手机',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.phone },
          fieldProps: {
            placeholder: '请输入手机号码',
            maxLength: 11,
            minLength: 11,
          },
        },
      ]}
    />
  );
}
