import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersComplaintTypeEnum } 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={`编辑工单`}
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 20 }}
      width="600px"
      layout="horizontal"
      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: 'complaint_type',
          title: '投诉类型',
          colProps: { span: 24 },
          valueEnum: HouseWorkOrdersComplaintTypeEnum,
          required: true,
        }),
        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 },
          fieldProps: {
            placeholder: '请输入投诉人姓名',
          },
        },
        {
          key: 'reporter_phone',
          title: '联系电话',
          formItemProps: { ...rulesHelper.phone },
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '请输入手机号码',
            maxLength: 11,
          },
        },
      ]}
    />
  );
}
