<template>
  <view class="backstageApp-root">
    <view class="title-root"
      >Android由于省电设置与后台进程限制，为了确保京西到家在后台或者锁屏状态下有一个好的运行效果，请检查以下设置都已按要求设置完成。
      <br />
      注意：通知权限较为特殊请按教程进行设置。</view
    >

    <!-- 通知 -->
    <view class="backstageList" @tap="setUp(backstageList[0].type)">
      <view class="title">{{ backstageList[0].title }}</view>
      <view class="center-root">
        <view v-if="isThree" class="right rigeht-ok"
          ><jx-icon icon="zhengque" color="#4eb331" />已设置</view
        >
        <view v-else class="right"
          >去设置<jx-icon icon="gengduo" color="#4eb331"></jx-icon
        ></view>
      </view>
    </view>

    <!-- 锁屏通知 -->
    <view class="backstageList" @tap="setUp(backstageList[1].type)">
      <view class="title">{{ backstageList[1].title }}</view>
      <view class="center-root">
        <view v-if="isTow" class="right rigeht-ok"
          ><jx-icon icon="zhengque" color="#4eb331" />已设置</view
        >
        <view v-else class="right"
          >去设置<jx-icon icon="gengduo" color="#4eb331"></jx-icon
        ></view>
      </view>
    </view>

    <!-- 忽略电池优化 -->
    <view class="backstageList" @tap="setUp(backstageList[2].type)">
      <view class="title">{{ backstageList[2].title }}</view>
      <view class="center-root">
        <view v-if="isNone" class="right rigeht-ok">
          <jx-icon icon="zhengque" color="#4eb331" />
          已设置
        </view>
        <view v-else class="right"
          >去设置<jx-icon icon="gengduo" color="#4eb331"></jx-icon
        ></view>
      </view>
    </view>

    <!-- 来单提醒测试 -->
    <view class="backstageList">
      <view class="title" style="display: flex;">
        <view style="white-space: nowrap;width: fit-content;">
           {{ backstageList[3].title }}:
        </view>
        <input
          class="ipt"
          v-model="currentValue"
          :placeholder="cidNotice ? '请重新登录更新通知信息' : '请输入订单号'"
          style="
            border: 1rpx solid #eee;
            margin-top: -4rpx;
            font-weight:lighter;
            padding: 10rpx;
           font-size: 28rpx;"
           confirm-type="search"
           :disabled="cidNotice"
          :style="{
            backgroundColor: cidNotice ? '#eee' : ''
          }"
        />
      </view>
      <view class="center-root">
        <view class="right" style="width: 100rpx;" @tap="setUp(backstageList[3].type)">测试</view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup >
import toast from '@/utils/toast'
import { onShow } from '@dcloudio/uni-app'
import order from "@/api/https/order"
import merchant from "@/api/https/merchant"
import { getStorage } from "@/utils/storage";
import { ref } from 'vue'
const MyPlugin = uni.requireNativePlugin('RC-NoticeMonitoring')

const isNone = ref<boolean>(false)
const isTow = ref<boolean>(false)
const isThree = ref<boolean>(false)

const currentValue = ref<string>('')

onShow(() => {
  // 检查电池白名单
  MyPlugin.isBatteryOptimizations((res: number) => {
    if (res == 1) {
      isNone.value = true
    } else {
      isNone.value = false
    }
  })

  // 通知亮屏
  MyPlugin.isNotification((res: number) => {
    if (res == 1) {
      isTow.value = true
    } else {
      isTow.value = false
    }
  })
  getCid()
})

const backstageList = ref<Array<any>>([
  {
    title: '需开启使用通知权限',
    type: 1,
  },
  {
    title: '需开启锁屏通知权限',
    type: 2,
  },
  {
    title: '需忽略电池优化',
    type: 3,
  },
  {
    title: '来单通知测试',
    type: 4,
  },
])

/*************************************************
 * 设置
 */
function setUp(type: number) {
  switch (type) {
    // 通知权限
    case 1:
      uni.navigateTo({ url: '/subPages/merchantChild/setNotice/setNotice' })
      break

    // 锁屏权限
    case 2:
      MyPlugin.OpenNoticePermission((res: number) => {
        toast('权限已开启', 3)
      })
      break

    // 锁屏权限
    case 3:
      MyPlugin.OpenBatteryOptimizations((res: number) => {
        toast('权限已开启', 3)
      })
      break
    case 4:
     testOrderPush()
      break
  }
}

/**
 * 测试订单通知
 */
async function testOrderPush(){
  if(cidNotice.value) return
  if(currentValue.value.length === 0) return toast('请填写订单号')
  let orderRes = await order.get_orders({vendorOrderID:currentValue.value,})
  if(orderRes.data.totalCount){
    if(orderRes.data.data[0].jxStoreID !== getStorage('storeID') ) return toast('门店信息有误，请重新登录')
    await order.test_uni_app_push(currentValue.value)
    toast('推送成功')
  }else{
    toast('未查询到该订单，请确认订单号是否正确')
  }
}

/**
 * 获取个人信息cid
 */
 let cidNotice  = ref<boolean>(false)
async function getCid() {
    let infoRes = await merchant.get_self_info()
    if (infoRes.code == 0)    {
      if (getStorage('cid') == infoRes.data.c_id) {
            cidNotice.value = false
        } else {
            cidNotice.value = true
        }
    }
}
</script>

<style lang="scss" scoped>
@import './backstageApp.scss';
</style>
<style>
page {
  background-color: rgb(247, 247, 247);
}
</style>