import { ModalForm, ProForm, ProFormDigit, ProFormText } from '@ant-design/pro-components';
import { Button, Form } from 'antd';
import { DictOptionType, DictValueEnumObj } from '@/components/DictTag';
import { useEffect, useState } from 'react';
import ProFromMap from '@/components/ProFromMap';

export type ListFormProps = {
  onCancel: (flag?: boolean, formVars?: unknown) => void;
  onSubmit: (values: API.AreaSubWay.LinePoint) => Promise<void>;
  open: boolean;
  values?: Partial<API.AreaSubWay.LinePoint>;
  jobGroupOptions?: DictOptionType[];
  statusOptions?: DictValueEnumObj;
};

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

const SubWayEdit: React.FC<ListFormProps> = (props) => {
  const [form] = Form.useForm();

  const [open, setOpen] = useState<boolean>(false);
  const [viewInfo, setViewInfo] = useState<any>({});

  useEffect(() => {
    form.resetFields();
    if (props.values) {
      form.setFieldsValue(props.values);
      setViewInfo(props.values);
    }
  }, [form, props]);

  const handleCancel = () => {
    props.onCancel();
    form.resetFields();
  };

  const handleFinish = async (values: Record<string, any>) => {
    props.onSubmit(values as API.AreaSubWay.LinePoint);
  };

  const select = (result) => {
    if (result.location) {
      const { lat, lng } = result.location;
      form.setFieldValue('latitude', lat);
      form.setFieldValue('longitude', lng);
      form.setFieldValue('address', result.address);
      const obj = {
        latitude: lat,
        longitude: lng,
        address: result.address,
      };
      setViewInfo(obj);
      setOpen(false);
    }
  };

  const cancel = () => {
    setOpen(false);
  };

  return (
    <ModalForm<{
      name: string;
      company: string;
      stationOrder: number;
    }>
      title={`${props.values ? '编辑' : '新增'}站点`}
      form={form}
      // layout="inline"
      autoFocusFirstInput
      open={props.open}
      modalProps={{
        destroyOnClose: true,
        onCancel: () => handleCancel(),
      }}
      initialValues={{
        sort: 0,
      }}
      submitTimeout={2000}
      onFinish={handleFinish}
    >
      <ProFormDigit
        name="stationId"
        label={'字典主键'}
        placeholder="请输入字典主键"
        disabled
        hidden={true}
      />
      <ProForm.Group>
        <ProFormText
          width="md"
          name="stationName"
          label="站点名称"
          placeholder="请输入名称"
          rules={[
            {
              required: true,
              message: '请输入名称！',
            },
          ]}
        />
        <ProFormDigit
          width="md"
          label="排序"
          name="stationOrder"
          placeholder="请输入排序"
          rules={[
            {
              required: true,
              message: '请输入排序！',
            },
          ]}
        />
      </ProForm.Group>
      <ProForm.Group>
        <div>
          <Button onClick={() => setOpen(true)}>选择位置信息</Button>
          <div style={{ margin: '10px 0 0 0' }}>
            {viewInfo.address ? (
              <span style={{ padding: '0 0 0 16px' }}>地址：{viewInfo.address}</span>
            ) : null}
            {viewInfo.latitude ? (
              <span style={{ padding: '0 0 0 16px' }}>
                经纬度：{viewInfo.latitude},{viewInfo.longitude}
              </span>
            ) : (
              <span style={{ padding: '0 0 0 16px', color: 'red' }}>请选择位置!</span>
            )}
          </div>
        </div>
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText
          width="md"
          name="address"
          label="地理位置"
          hidden={true}
          placeholder="请输入地理位置"
        />
        <ProFormDigit
          label="纬度"
          placeholder="请输入纬度"
          name="latitude"
          width="md"
          hidden={true}
          min={-90}
          max={90}
          fieldProps={{ controls: false }}
          rules={[
            {
              required: true,
              message: '请输入纬度！',
            },
          ]}
        />
        <ProFormDigit
          label="经度"
          placeholder="请输入经度"
          name="longitude"
          width="md"
          min={-180}
          hidden={true}
          max={180}
          fieldProps={{ controls: false }}
          rules={[
            {
              required: true,
              message: '请输入经度！',
            },
          ]}
        />
      </ProForm.Group>
      <ProFromMap open={open} onSelect={select} onCancel={cancel}></ProFromMap>
    </ModalForm>
  );
};

export default SubWayEdit;
