<template>
	<view class="right-nav row">
		<view
        class="right-nav-button col-12"
        v-for="(item,index) in buttonGroup"
        >
      <u-image v-if="item.type !== 'wechat' && item.type !== 'chat'" :src="item.image" width="93" height="93" @click="handle(item.type)"></u-image>
      <button v-if="item.type === 'wechat'" @click="imageShow = true" :src="item.image" style="padding-left:0;">
        <u-image :src="item.image" width="93" height="93"></u-image>
      </button>
      <view v-if="item.type === 'chat'" style="position: relative;">
        <u-badge v-if="msgCount>0" type="error" max="99" :value="msgCount" absolute :offset="[0,0]" custom-style="z-index:1;"></u-badge>
        <u-image :src="item.image" width="93" height="93" @click="handle(item.type)"></u-image>
      </view>
    </view>
	</view>
  <u-overlay :show="imageShow" @click="imageShow = false">
    <view class="" style="display:flex;align-items: center;justify-content:center;width:100%;height:100vh;">
      <u-image mode="widthFix" :src="imageUrl" height="auto" width="100vw"></u-image>
    </view>
  </u-overlay>
</template>

<script>
	import { imghost } from '@/config/host.js'
  import functions from "@/utils/functions";
  import {getConfig} from "@/api/other";
  import {userChatCount} from "@/api/user";
	export default{
		data() {
			return {
        imageShow:false,
        imageUrl:imghost +'/qrcode-wechat.png',
				title: 'Hello',
				loading: true,
				buttonGroup:[
					{
						image:imghost+"/right-button-wechat.png",
						type:'wechat'
					},{
						image:imghost+"/right-button-chat.png",
						type:'chat'
					},{
						image:imghost+"/right-button-phone.png",
						type:'phone'
					},
				],
        kefu_phone:'',
        msgCount:0,
			}
		},
		onLoad() {

		},
    mounted() {
      let that = this;
      if(uni.getStorageSync('token')){
        that.getMsgNumber();
        setInterval(() => {
          that.getMsgNumber();
        },5000)
        that.getKFWechat();
      }
    },
		methods: {
      getMsgNumber(){
        userChatCount().then(res => {
          this.msgCount = res.data.count
        });
      },
      getKFWechat(){
        getConfig('kefu_weixin').then((res) => {
          this.imageUrl = res.data.kefu_weixin;
        })
      },
      callPhone(phone){
        uni.makePhoneCall({
          phoneNumber: phone.toString()
        });
      },
			handle(type){
					switch (type) {
						case 'chat':
              this.navTo("/pages/ChatDetail/index?id=0&type=2",true);
              break;
            case 'wechat':
              // uni.openCustomerServiceChat({
              //   extInfo: {url: ''},
              //   corpId: '',
              //   success(res) {
              //     console.log(res)
              //   },
              //   fail(res) {
              //     console.log(res)
              //   },
              // });
              functions.success('跳转微信客服');
              break;
            default:
              this.callPhone(this.kefu_phone)
					}
			},
      navTo(url,token = false) {
        if (token) {
          if (uni.getStorageSync('token')) {
            uni.navigateTo({
              url: url
            })
          } else {
            uni.navigateTo({
              url: '/pages/Login/index'
            })
          }
        }else{
          uni.navigateTo({
            url: url
          })
        }
      },
      getPhone(){
        getConfig('kefu_phone').then(res => {
          this.kefu_phone = res.data.kefu_phone;
        })
      }
		},
    created() {
      this.getPhone();
    },
	}
</script>

<style lang="scss">
	@import './components/index.scss';
</style>