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 Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Patrol.PatrolLocations.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      key={new Date().getTime()}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="600px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            location: `${props.item?.longitude || ''},${
              props.item?.latitude || ''
            }`,
          });
        }
      }}
      onFinish={async (values: any) =>
        Apis.Patrol.PatrolLocations.Update({
          ...values,
          longitude: values?.location ? values.location.split(',')[0] : '',
          latitude: values?.location ? values.location.split(',')[1] : '',
          is_enabled: values.is_enabled ? 1 : 0,
          id: props.item?.id ?? 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 },
        },
      ]}
    />
  );
}
