import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseWorkOrdersComplaintTypeEnum,
  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,
          type: HouseWorkOrdersTypeEnum.Complaint.value,
        })
          .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,
          },
        },
      ]}
    />
  );
}
