import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import { BetaSchemaForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Patrol.PatrolLocations.Store>
      {...MyModalFormProps.props}
      title={`添加${props.title}`}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`添加${props.title}`} />}
      onFinish={async (values: any) =>
        Apis.Patrol.PatrolLocations.Store({
          ...values,
          longitude: values?.location ? values.location.split(',')[0] : '',
          latitude: values?.location ? values.location.split(',')[1] : '',
          is_enabled: values.is_enabled ? 1 : 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.AssetProjects({
          title: '关联项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          key: 'name',
          title: '点位名称',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '请输入点位名称',
          },
        },
        {
          title: '位置信息',
          key: 'remark',
          formItemProps: { ...rulesHelper.text },
          valueType: 'textarea',
          colProps: { span: 24 },
          fieldProps: {
            placeholder: '请输入点位的具体位置信息',
          },
        },
        {
          title: '是否启用',
          key: 'is_enabled',
          valueType: 'switch',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
