import { showToast, showToastBack } from '@/common/libraries/naviHelper'
import { getApiLoading } from '@/common/libraries/apiLoading'
import { Apis } from '@/gen/Apis'
export function sendChatMessage(data: {
  msgtype?: string
  page?: string
  enterChat?: boolean
  title?: string
  imgUrl?: string
}) {
  if (!data?.page) {
    return showToast('请设置跳转页面路径！')
  }
  uni.showLoading({
    title: '加载中...',
    mask: true
  })
  uni.qy.sendChatMessage({
    msgtype: data?.msgtype || 'miniprogram', //消息类型，必填
    enterChat: data?.enterChat || false, //为true时表示发送完成之后顺便进入会话，仅移动端3.1.10及以上版本支持该字段
    miniprogram: {
      appid: 'wx31500e871924b903', //小程序的appid
      title: data?.title || '邀请你绑定房屋', //小程序消息的title
      imgUrl:
        data?.imgUrl ||
        'https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01K58GCT5DGWKBSYH0G7118XT4.jpg', //小程序消息的封面图
      page: data?.page //小程序消息打开后的路径，注意要以.html作为后缀，否则在微信端打开会提示找不到页面
    },
    success: function () {
      //todo:
      uni.hideLoading()
      showToast('发送成功！')
    },
    fail: function (err) {
      //todo:
      console.log('sendChatMessage fail', err)
      uni.hideLoading()
      if (err?.errMsg !== 'qy__sendChatMessage:fail cancel') {
        showToast(err?.errMsg || '当前操作，只能在企业微信上聊天工具栏操作')
      }
    }
  })
}

//发消息助手
export function shareToExternalContact(data: {
  msgtype?: string
  page?: string
  enterChat?: boolean
  title?: string
  imgUrl?: string
  externalUserIds?: string[]
}) {
  if (!data?.page) {
    return showToast('请设置跳转页面路径！')
  }
  uni.showLoading({
    title: '加载中...',
    mask: true
  })
  uni.qy.shareToExternalContact({
    externalUserIds: data?.externalUserIds || [],
    text: {
      content: data?.title || '待缴费账单' // 文本内容
    },
    attachments: [
      {
        msgtype: data?.msgtype || 'miniprogram', //消息类型，必填
        miniprogram: {
          appid: 'wx31500e871924b903', //小程序的appid
          title: data?.title || '待缴费账单', //小程序消息的title
          imgUrl:
            data?.imgUrl ||
            'https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01K58GCT5DGWKBSYH0G7118XT4.jpg', //小程序消息的封面图
          page: data?.page //小程序消息打开后的路径，注意要以.html作为后缀，否则在微信端打开会提示找不到页面
        }
      }
    ],
    success: function () {
      //todo:
      uni.hideLoading()
      showToast('发送成功！')
    },
    fail: function (err) {
      //todo:
      console.log('sendChatMessage fail', err)
      uni.hideLoading()
      if (err?.errMsg !== 'qy__sendChatMessage:fail cancel') {
        showToast(err?.errMsg || '当前操作，只能在企业微信上聊天工具栏操作')
      }
    }
  })
}

export function navigateToMiniProgram(data: { path?: string; envVersion: string; extraData: any }) {
  //打开客户端
  uni.showLoading({
    title: '加载中',
    mask: true
  })
  uni.navigateToMiniProgram({
    appId: 'wx31500e871924b903',
    path: data?.path || '',
    extraData: data?.extraData || {},
    envVersion: data?.envVersion || 'release',
    success() {
      // 打开成功
      uni.hideLoading()
    },
    fail() {
      uni.hideLoading()
    }
  })
}

export function shareToExternalMoments(
  data: { content?: string; attachments: any },
  dataTasks: { customer_moments_id: string; company_employee_backlogs_id: string }
) {
  //发朋友圈
  console.log('shareToExternalMoments', data, dataTasks)
  wx.qy.shareToExternalMoments(
    {
      text: {
        content: data?.content || '' // 文本内容
      },
      attachments: data?.attachments,
      success(res: any) {
        if (res.errMsg == 'qy__shareToExternalMoments:ok') {
          getApiLoading(Apis.Customer.CustomerMomentTasks.Store, dataTasks).then(res => {
            showToast('发送成功！')
            console.log(res)
          })
        }
        console.log('分享回调结果success', res)
      }
    },
    function (res: any) {
      console.log('分享回调结果', res)
      if (res.err_msg == 'shareToExternalMoments:ok') {
        getApiLoading(Apis.Customer.CustomerMomentTasks.Store, dataTasks).then(res => {
          showToast('发送成功！')
          console.log(res)
        })
        // showToast('发送成功！')
      }
    }
  )
}

export function shareToExternalContactTask(
  data: { content?: string; attachments: any },
  dataTasks: { customer_moments_id: string; company_employee_backlogs: string }
) {
  //任务发送1对1群发客户
  uni.qy.shareToExternalContact({
    text: {
      content: data?.content || '' // 文本内容
    },
    attachments: data?.attachments || [],
    success: function (res) {
      //todo:
      console.log('shareToExternalContact', res)
      getApiLoading(Apis.Customer.CustomerMomentTasks.Store, dataTasks).then(res => {
        showToast('发送成功！')
        console.log(res)
      })
      console.log('success', res)
    },
    fail() {
      console.log('fail')
      // 失败逻辑
    },
    complete() {
      console.log('complete')
      // 完成逻辑
    }
  })
}
export function shareToExternalChatTask(
  data: { content?: string; attachments: any },
  dataTasks: { customer_moments_id: string; company_employee_backlogs: string }
) {
  //任务发送1对1群发客户
  uni.qy.shareToExternalChat({
    text: {
      content: data?.content || '' // 文本内容
    },
    attachments: data?.attachments || [],
    success: function (res) {
      //todo:
      getApiLoading(Apis.Customer.CustomerMomentTasks.Store, dataTasks).then(res => {
        showToast('发送成功！')
        console.log(res)
      })
      console.log('success', res)
    },
    fail() {
      console.log('fail')
      // 失败逻辑
    },
    complete() {
      console.log('complete')
      // 完成逻辑
    }
  })
}
