import request from './request'
import qs from 'qs'
import { Base64 } from 'js-base64'
import store from '@/store'

/**
 * 公共接口-登录接口获取token
 */
export function authorizeLogin(data) {
  return request({
    url: ` /ubms-foreign/authorize/login`,
    method: 'post',
    data: qs.stringify(data),
  })
}

/**
 * 公共接口-刷新token
 */
export function oauthToken(data) {
  return request({
    url: `/ubms-foreign/oauth/token`,
    method: 'post',
    data: qs.stringify(data),
  })
}

/**
 * 物业公告-通知列表
 */
export function findManagementNoticePage(data) {
  return request({
    url: `/ubms-foreign/base/managementnotice/foreignFindManagementNoticePage`,
    method: 'post',
    data,
  })
}

/**
 * 物业公告-通知详情
 */
export function findNoticeById(data) {
  return request({
    url: `/ubms-foreign/base/managementnotice/foreignFindNoticeById`,
    method: 'post',
    data,
  })
}

/**
 * 获取服务地址-积余访客zsjy（只用在招商积余大厦项目上）
 * 唯一编码：code=th_va
 */
export function getAuthorVisitorJumpAddress(page = 'home') {
  const userInfo = store.state.userInfo
  const origin = 'https://zsjy-txmobile.cmpo1914.com:8443'
  const pagePathMap = {
    home: {
      serviceName: '积余访客zsjy', // 操作权限 - 用户只需登录
      jumpPath: 'pages/vistorInviteOrBook/vistorInviteOrBook',
      jumpAddress: `${origin}/#/pages/vistorInviteOrBook/vistorInviteOrBook?loginToken=${Base64.encode(
        userInfo.userMobile,
      )}`,
    },
    detail: {
      serviceName: '访客预约详情页', // 操作权限 - 用户只需登录
      description: '访客预约详情页（短信跳转）',
      jumpPath: 'pages/visiotrAppointment/toUpload/toUpload',
      jumpAddress: `${origin}/#/pages/visiotrAppointment/toUpload/toUpload/?loginToken=${Base64.encode(
        userInfo.userMobile,
      )}&id={id}`,
    },
    approval: {
      serviceName: '被访人审核页面', // 操作权限 - 用户只需登录
      description: '被访人审核页面（短信跳转）',
      jumpPath: 'pages/visiotrAppointmentApproval/approval/approval',
      jumpAddress: `${origin}/#/pages/visiotrAppointmentApproval/approval/approval/?loginToken=${Base64.encode(
        userInfo.userMobile,
      )}&id={id}&type=detail`,
    },
  }

  let current = pagePathMap[page] || pagePathMap['home']
  const result = current.jumpAddress

  return result
}

/**
 * 获取服务地址-访客预约（兰溪谷一期）
 * @description
 * 唯一编码：code=th_applet
 */
export function getWeChatAppletJumpAddress(page = 'home') {
  const userInfo = store.state.userInfo
  const origin = 'https://ai-cmty.cmsk1979.com/applet'

  const pagePathMap = {
    home: {
      serviceName: '访客预约', // 操作权限 - 用户只需登录
      jumpPath: '/',
      jumpAddress: `${origin}/#/pages/visitor/temp_visitor_register/temp_visitor_register?projectId=14cffa4b73a74572&wechatId=${userInfo.openid}`,
    },
  }

  let current = pagePathMap[page]
  const result = current.jumpAddress

  return result
}

export default {
  authorizeLogin,
  oauthToken,
  findManagementNoticePage,
  findNoticeById,
  getAuthorVisitorJumpAddress,
  getWeChatAppletJumpAddress,
}
