import { Toast } from 'vant'
import baseAxios, { iRet } from '../index'
import { iNearbyOutLet, iOutLetDetail } from './types'
import Bridge from '@/utils/jsBridge2'
import { number } from 'bitcoinjs-lib/types/script'
class AddressService {
  static instance: AddressService
  static getInstance() {
    if (!AddressService.instance) {
      AddressService.instance = new AddressService()
    }
    return AddressService.instance
  }

  getNearby(data: { bank_code: number; number: number }) {
    const isDev = process.env.NODE_ENV === 'development'
    if (isDev) {
      return baseAxios<iNearbyOutLet[]>({
        method: 'post',
        url: '/address/nearby',
        data: {
          ...data,
          // 120.14606,30.279242
          longitude: '120.14606',
          latitude: '30.279242',
        },
      })
    } else {
      return new Promise<iRet<iNearbyOutLet[]>>((resolve) => {
        function getGps() {
          new Bridge().bridge_getGps(null, (params: any) => {
            console.log(params, 'show params')
            const list = params.split(',')
            resolve(
              baseAxios<iNearbyOutLet[]>({
                method: 'post',
                url: '/address/nearby',
                data: {
                  ...data,
                  longitude: list[0],
                  latitude: list[1],
                },
              }),
            )
          })
        }
        getGps()
      })
    }
  }

  getNote(data: { id: number }) {
    return baseAxios({
      method: 'post',
      url: '/address/note',
      data,
    })
  }

  getOutlet(data: { outlet_name?: string; id?: number }) {
    let theData = {
      bank_code: Number(process.env.VUE_APP_BANK_CODE),
    } as any
    if (data.outlet_name) {
      theData.outlet_name = data.outlet_name
    } else if (data.id !== undefined) {
      theData.id = data.id
    }
    return baseAxios<iOutLetDetail>({
      url: '/address/getOutlet',
      method: 'post',
      data: theData,
    })
  }
  getStaffOutlet() {
    return baseAxios<{
      fid: number,
      first_branch: string,
      first_sub_branch: string,
      fs_id: number,
      second_branch: string,
      second_sub_branch: string,
      sid: number,
      ss_id: number
    }>({
      url: '/staff/outlet',
      method: 'get',
    })
  }

  getOutlets(data: {
    /* 传ssid(二级支行) */
    id: number; 
    level: number
  }) {
    return baseAxios<iNearbyOutLet>({
      url: '/address/getOutlets',
      method: 'post',
      data:data
    })
  }
}


export default AddressService
