import Http from '@/util/axios'
import {decrypt} from './data-analysis'
import md5 from 'md5';

const prefix = '/zero2/myships'
const icdPrefix = 'https://sinoagent.i.sinotrans.com' // 生产
// const icdPrefix = 'http://sinoicd.i.sinotrans.com' // 测试
class Api {

  public getInfoByDestPort(data: any) {
    return Http.post(prefix + '/sp/ports/getDestPort', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 查询单日新增靠泊船舶列表
   */
  public getBerthsOfNumber(data: any) {
    return Http.post(prefix + '/exapi/berth/berth/vessel', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 查询单日港外待靠泊数量及时长
   */
  public getMoorsOfNumber(data: any) {
    return Http.post(prefix + '/exapi/berth/wait/num', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 4.2查询单日新增离泊数量及靠泊时长
   * @param data
   */
  public getLeaveBerth(data: any) {
    return Http.post(prefix + '/exapi/berth/unberth/num', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取船舶事件
   * @param data
   */
  public getShipEvent(data: any) {
    return Http.post(prefix + '/sp/ships/ihs/event', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 集装箱查询接口
   * @param data
   */
  public getContainersScreening(data: any) {
    return Http.post(prefix + '/exapi/movements/containersScreening', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 提单号查询接口
   * @param data
   */
  public getContainersBillOfLadingScreening(data: any) {
    return Http.post(prefix + '/exapi/movements/billofLadingScreening', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取内贸船舶信息
   * @param data
   */
  public getInnerShipData(data: string) {
    return Http.post(prefix + '/sp/ships/cert/mmsi', {mmsi: data}).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 查询具体船舶静态详情
   * @param data 参数
   */
  public async getShipMessage(data: any) {
    return await Http.post(prefix + '/sp/ships/posAndAissta', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 模糊搜索 AIS 船舶库
   * @param data 参数
   */
  public async searchBoathouse(data: any) {
    let shipData: any[] = []

    await Http.post(prefix + '/sp/search/shipAisWithTime', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code) {
        shipData = data.data
        this.searchDictionary('2').then((res: any) => {
          if (res) {
            shipData.forEach((item2: any) => {
              for (let i = 0, j = res.length; i < j; i++) {
                if (item2.aisShiptypeId + '' === res[i].id + '') {
                  item2.type = res[i].valueChs
                }
                if (!item2.type) {
                  item2.type = '其他类型'
                }
              }
            })
          }
        })
      }
    })
    return shipData
  }

  /**
   * 模糊查询港口
   * @param keyword
   */
  public searchPortKeyWord(keyword: any) {
    return Http.post(prefix + '/sp/search/ports', {keyword: keyword})
  }

  /**
   * 到港查询
   * @param data
   */
  public getArrivePortQuery(data: any) {
    return Http.post(prefix + '/sp/ports/arrivalStatistics', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    }).catch((err) => {
      return err
    })
  }

  /**
   * 离港查询
   * @param data
   */
  public getLeavePortQuery(data: any) {
    return Http.post(prefix + '/sp/ports/departureStatistics', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    }).catch((err) => {
      return err
    })
  }

  /**
   * 预计到港查询
   * @param data
   */
  public getYjPortQuery(data: any) {
    return Http.post(prefix + '/sp/ports/expectedArrivalStatistics', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    }).catch((err) => {
      return err
    })
  }

  public getPortList(data: any) {
    data.portLevelId = '1'
    return Http.post(prefix + '/sp/ports/search', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  public getPortWeather(data: any) {
    return Http.post(prefix + '/exapi/weather/singleMomentData', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  public getWeather(data: any) {
    return Http.post(prefix + '/exapi/weather/singleMomentData', data).then((res: any) => {
      const DesKey = res.headers.deskey
      const data = decrypt(res.data, DesKey)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取字典
   * type
   * 1:  // 获取船舶国籍字典
   * 2:  // 获取船舶类型字典
   * 3:  // 获取航行状态字典
   */
  public searchDictionary(type: string) {
    const param: { name: string } = {name: ''}
    if (type === '1') {
      param.name = 'ais_nationality_id'
    } else if (type === '2') {
      param.name = 'ais_ship_type_id'
    } else if (type === '3') {
      param.name = 'ais_nav_status_id'
    }
    return Http.post(prefix + '/sp/dict', param).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 查询船舶历史轨迹
   * @param data
   */
  public getHistoryTrack(data: any) {
    return Http.post(prefix + '/sp/ships/position/history', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  public getShipHistoryInterval(data: any) {
    return Http.post(prefix + '/sp/ships/position/history/intervalHour', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取最新的提醒新信息
   * @param data
   */
  public getLastRemindInfo(data: any) {
    return Http.post(prefix + '/ms/event/getAllEventByTime', data).then((res: any) => {
      const data = this.decryptUtil(res)
      return data
    })
  }

  /**
   * 获取船舶港口历史
   * @param data
   */
  public getShipPort(data: any) {
    return Http.post(prefix + '/sp/ships/port/history', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  public getShipCnName(data: any) {
    return Http.post(prefix + '/sp/ships/getCnNameByMMSI', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  public getDestPort(data: any) {
    return Http.post(prefix + '/sp/ports/getDestPort', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取数据分析图片地址
   * @param data
   * @private
   */
  public getDataAnalysisImg(data: any) {
    return Http.post(prefix + '/uc/public/getMotImg', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data.url
      }
    })
  }

  /**
   * 获取最新的提醒消息
   * @param data
   */
  public getMessageLast(data: any) {
    return Http.post(prefix + '/ms/event/getLatestAreaEvent', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取船舶挂靠港记录
   * @param data
   */
  public getPortReport(data: any) {
    return Http.post(prefix + '/sp/auth/ships/port/v2/history_api', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取用户区域列表菜单
   * @param data
   */
  public getAreaMenuList(data: any) {
    return Http.post(prefix + '/ms/label/getAreaGroup', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 查询区域范围内的船舶数据
   */
  public getAreaData(data: any) {
    return Http.post(prefix + '/sp/auth/region/latest/dataAalysis', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 获取船队数据
   */
  public async getShipRanks(data: any) {
    return await Http.post(prefix + '/ms/business/getFollowShip', data).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  private decryptUtil(data: any) {
    const DesKey = data.headers['deskey']
    return decrypt(data.data, DesKey)
  }

  /**
   * 模糊查询港口信息
   * @param keyword
   */
  public getPort(keyword: string) {
    return Http.post(prefix + '/sp/search/ports', {keyword: keyword}).then((res: any) => {
      const data = this.decryptUtil(res)
      if (data.code === '0') {
        return data.data
      }
    })
  }

  /**
   * 通过港口id获取港口的信息
   * @param data
   */
  public getPortInfo(data: any) {
    return Http.post(prefix + '/sp/ports/ihs/port', {portId: data}).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result.data
      }
    })
  }

  public getShipRouteSearch(data: any) {
    return Http.post(prefix + '/exapi/shipRoute/search', data).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  public getFollowFlag(data: any) {
    return Http.post(prefix + '/ms/shipRoute/getFollowFlag', data)
  }

  public getPortRouteList() {
    return Http.post(prefix + '/ms/shipRoute/getFollow', 'POST', {})
  }

  public getRouteInfo(data: any) {
    return Http.post(prefix + '/ms/shipRoute/get', data)
  }

  public getStatusTrack(data: any) {
    return Http.post(prefix + '/exapi/shipRoute/statusTrack', data).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  public getShipDoc(data: any) {
    return Http.post(prefix + '/sp/ihs/v2/shipArchive', data).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result.data
      }
    })
  }

  public testToken(appToken: string) {
    return Http.get(icdPrefix + '/isa/sysClient/checkToken?appToken=' + appToken).then((res: any) => {
      return res.data
    })
  }

  public getShipFleet(token: string, companyId: string) {
    return Http.get(icdPrefix + '/isa/sysClientFleet/getShipsMmsi?companyId=' + companyId, {
      headers: {
        Authorization: token
      }
    }).then((res: any) => {
      return res.data
    })
  }

  public getShipIdByMmmsi(mmsi: string) {
    return Http.post(prefix + '/sp/ships/getShipIdByMMSI', {mmsiList: mmsi}).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  /**
   * 根据mmsi查询船舶静态信息
   * @param mmsi
   */
  public getShipBymmsi(mmsi: string) {
    return Http.post(prefix + '/sp/auth/ships/getAisStaticLatest', {mmsiList: mmsi}).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  /**
   * 预测船舶目的港及剩余里程和时长
   */
  public getPredictTime(data: any) {
    return Http.post(prefix + '/exapi/shipRoute/predict', data).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  /**
   * 获取台风列表
   */
  public getTyphoonList() {
    return Http.post(prefix + '/weather/typhoonList', {daysnumber: -1}).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  /**
   * 获取台风详情
   */
  public getTyphoonInfo(id: string) {
    return Http.post(prefix + '/weather/typhoonData', {id: id}).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  /**
   * 根据节点查询航线路由
   */
  public getRouteByNodes(data: any) {
    return Http.post(prefix + '/exapi/shipRoute/routingNodes', data).then((res: any) => {
      const result = this.decryptUtil(res)
      if (result.code === '0') {
        return result
      }
    })
  }

  /**
   * 通告预警地区和分类标签下通告预警条数(用于柱状图)
   */
  public getIncidentCount() {
    const id = 'al00009'
    const key = '99bd0e09e068a19600fb4ef1e1890200'
    const time = parseInt(new Date().getTime() / 1000 + '')
    return Http.post('https://talk.anlantech.cn/standard/incident/count', {}, {
      headers: {
        'anlan-version': 'v2',
        'anlan-appid': id,
        'anlan-timestamp': time,
        'anlan-token': md5('' + id + time + key)
      }
    }).then((res) => {
      return res.data
    })
  }

  /**
   * 通告预警地区和分类标签下通告预警条数(用于柱状图)
   */
  public getIncidentInfo(param?: any) {
    const id = 'al00009'
    const key = '99bd0e09e068a19600fb4ef1e1890200'
    const time = parseInt(new Date().getTime() / 1000 + '')
    let typeIds = param && param.typeIds ? param.typeIds : '';
    let org = param && param.org ? param.org : '';
    let begin = param && param.begin ? param.begin : '';
    let end = param && param.end ? param.end : '';
    let page = param && param.page ? param.page : '';
    let size = param && param.size ? param.size : '';
    return Http.get('https://talk.anlantech.cn/standard/incidentInfo?' + `typeIds=${typeIds}&org=${org}&begin=${begin}&end=${end}&page=${page}&size=${size}`, {
      headers: {
        'anlan-version': 'v2',
        'anlan-appid': id,
        'anlan-timestamp': time,
        'anlan-token': md5('' + id + time + key)
      }
    }).then((res) => {
      return res.data
    })
  }

  /**
   * 分类标签地址
   */
  public getIncidentTypeTags() {
    const id = 'al00009'
    const key = '99bd0e09e068a19600fb4ef1e1890200'
    const time = parseInt(new Date().getTime() / 1000 + '')
    return Http.get('https://talk.anlantech.cn/standard/incident/tags', {
      headers: {
        'anlan-version': 'v2',
        'anlan-appid': id,
        'anlan-timestamp': time,
        'anlan-token': md5('' + id + time + key)
      }
    }).then((res) => {
      return res.data
    })
  }

  /**
   * 地区列表地址
   */
  public getIncidentOrgsTags() {
    const id = 'al00009'
    const key = '99bd0e09e068a19600fb4ef1e1890200'
    const time = parseInt(new Date().getTime() / 1000 + '')
    return Http.get('https://talk.anlantech.cn/standard/incident/orgs', {
      headers: {
        'anlan-version': 'v2',
        'anlan-appid': id,
        'anlan-timestamp': time,
        'anlan-token': md5('' + id + time + key)
      }
    }).then((res) => {
      return res.data
    })
  }

  /**
   * 获取车辆信息
   */
  // public  getCarInfo() {
  //   return Http.post('/car/carhisttrack/car/track/getCarHistoricalTrackSingle',{carNum: '琼A2AJ02'}).then((res: any) => {
  //     console.log(res)
  //   })
  // }
  /**
   * 获取车辆信息
   */
  public getCarInfo() {
    return Http.post('/zero2/car/carhisttrack/car/track/getCarHistoricalTrack').then((res: any) => {
      return res.data
    })
  }
  public getCarTrack (data: any) {
    return Http.post('/zeroi/free/freetrade/api/getCarTrack', data).then(res => {
      return res.data
    })
  }
}

const HttpApi = new Api()
export default HttpApi
