<template>
	<view>
		<basics v-if="PageCur=='basics'" id="basics"></basics>
		<application v-if="PageCur=='application'" :dys="dys" :dzg="dzg"></application>
		<works v-if="PageCur=='works'" id = "works"></works>
		<my v-if="PageCur=='my'" id = "my"></my>
		<view class="cu-bar tabbar bg-white shadow foot" v-if="isShowTabBar">
			<view class="action" @click="NavChange" data-cur="basics">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/basics' + [PageCur=='basics'?'_cur':''] + '.png'"></image>
				</view>
				<view :class="PageCur=='basics'?'text-blue':'text-gray'">首页</view>
			</view>
			<view v-if="loginCorpId" class="action" @click="NavChange" data-cur="application">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/application' + [PageCur == 'application'?'_cur':''] + '.png'"></image>
          <view v-if="hidCount>0" class="cu-tag badge">{{hidCount}}</view>
				</view>
				<view :class="PageCur=='application'?'text-blue':'text-gray'">应用</view>
			</view>
			<view v-if="loginCorpId" class="action" @click="NavChange" data-cur="works">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/works' + [PageCur == 'works'?'_cur':''] + '.png'"></image>
					<view v-if="mesCount>0" class="cu-tag badge">{{mesCount}}</view>
				</view>
				<view :class="PageCur=='works'?'text-blue':'text-gray'">通知</view>
			</view>
			<view class="action" @click="NavChange" data-cur="my">
				<view class='cuIcon-cu-image'>
					<image :src="'/static/tabbar/my' + [PageCur == 'my'?'_cur':''] + '.png'"></image>
				</view>
				<view :class="PageCur=='my'?'text-blue':'text-gray'">我的</view>
			</view>
		</view>
    <uni-popup ref="popup1" background-color="#fff" :mask-click="false" style="position: relative">
      <view style="text-align: center;color:#606266;width: 500upx;padding: 40upx;margin-bottom: 40upx;">
        <view style="margin-bottom: 40upx;">温馨提示</view>
        <view>有未签署的安全承诺,点击确认前往签署</view>
      </view>
      <view style="display: flex;width: 100%;position: absolute;bottom: 0;">
        <button class="cu-btn bg-green" style="flex: 1;border-radius: 0" @click="modalConfirm1">确定</button>
      </view>
    </uni-popup>
	</view>
</template>

<script>
	import {
		basePath,
		isRest,
		loginUserId,
		formatDate,
		loginUser,
		setloginUserId,
		setCorpinfoId,
		setDeptId,
		setloginUser,
		adminPath,
		setVersion,
		corpinfoId,
		setIsRest
	} from '@/common/tool.js';
  import uniPopup from '@/components/more-select/uni-popup/uni-popup.vue';

	export default {
    components: {
      uniPopup,
    },
		data() {
			return {
				initflag:false,
				PageCur: 'basics',
				wgtVer:'',
				version:'',
				brand:'other',
				mesCount:0,
        hidCount:0,
        dys:0,
        dzg:0,
        isShowTabBar:false,
        loginCorpId: ''
			}
		},
		onBackPress() {
			//console.log("press back");

			uni.showModal({
				title: '是否退出',
				confirmText: '退出',
				success: function(res) {
					if (res.confirm) {
						setloginUserId(null);
						setCorpinfoId(null);
						setDeptId(null);
						setloginUser(null);
						uni.removeStorage({key: 'USER'});
						uni.reLaunch({
							url: '/pages/login/home'
						});
					}
				}.bind(this)//此处bind是关键
			})
			return true;
		},
		onShow() {
			var _this = this;
			_this.$nextTick(() => { //等待组件渲染结束才调用
				if(_this.initflag) {
					console.info("index的initflag=="+_this.initflag);
					if(_this.PageCur=='basics' || _this.PageCur=='message') {
						var page = _this.selectComponent("#" + _this.PageCur);//按子组件ID 获取子组件对象
						// page.getData();//调用子组件init方法
					}
					 if(_this.PageCur=='works') {
					 	var page = _this.selectComponent("#" + _this.PageCur);//按子组件ID 获取子组件对象
						page.getData();//调用子组件init方法
						page.getPcData();//调用子组件init方法
					 }
					 if(_this.PageCur=='my') {
					 	var page = _this.selectComponent("#" + _this.PageCur);//按子组件ID 获取子组件对象
						page.getData();//调用子组件init方法
					 }
				}

				_this.initflag=false;//false;子组件调用跳转方法时设为true
			});
			_this.$nextTick(() => { //等待组件渲染结束才调用
				if(_this.PageCur=='basics') {
					var page = _this.selectComponent("#" + _this.PageCur);//按子组件ID 获取子组件对象
					// page.getData();//调用子组件init方法
				}
			});

      this.mesCount = 0
      this.hidCount = 0
      this.dys = 0
      this.dzg = 0
			this.getRedPoint();
      this.getPromiseUnsigned()
		},
 		onLoad() {
      this.loginCorpId = loginUser.CORPINFO_ID
			 this.plusReady();
			// this.checkPlatform();
			// this.checkUpdate("android");
		},
		plusReady() {
			uni.startPullDownRefresh();

		},
		onPullDownRefresh() {
			var _this = this;
			// console.info(_this.PageCur)
			if(_this.PageCur=='basics' || _this.PageCur=='message' || _this.PageCur=='works') {
				var page = _this.selectComponent("#" + _this.PageCur);//按子组件ID 获取子组件对象
				page.getData();//调用子组件init方法
			}
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {

			getRedPoint() {
        if (loginUser.CORPINFO_ID) {

          var _this = this;
          uni.showLoading({
            title: '请稍候'
          })
          uni.request({
            url: basePath + '/app/trafficNotice/getRedPoint',
            method: 'POST',
            dataType: 'json',
            header: {
              'Content-type':'application/x-www-form-urlencoded'
            },
            data: {
              CORPINFO_ID:loginUser.CORPINFO_ID,
              USER_ID:loginUser.USER_ID,
            },
            success: (res) => {
              if("success" == res.data.result){
                uni.hideLoading();
                _this.mesCount = res.data.count;
                if(_this.mesCount > 0){
                  uni.setTabBarBadge({ //显示数字
                    index: 2,//tabbar下标
                    text: _this.mesCount//数字
                  })
                }else{
                  uni.hideTabBarRedDot({ //隐藏红点
                    index:2
                  })
                }
              } else {
                uni.showToast({
                  title: res.data.message,
                  duration: 2000
                });
              }
            },
            fail: (res) => {
              uni.showToast({
                icon: 'none',
                title: "服务器正在升级,请稍后再试。",
                duration: 2000
              });
            }
          });

          uni.request({
            url: basePath + '/app/hidden/getCountByUserId',
            method: 'POST',
            dataType: 'json',
            header:{
              'Content-type':'application/x-www-form-urlencoded'
            },
            data: {
              userId: loginUserId,
              USER_NAME: loginUser.NAME,
              CHECK_DEPARTMENT_ID: loginUser.DEPARTMENT_ID,
              IS_MAIN:loginUser.ISMAIN,
              CORPINFO_ID:loginUser.CORPINFO_ID,
              USER_ID:loginUser.USER_ID,
            },
            success: (res) => {
              if (res.data != null) {
                _this.hidCount += res.data.hidCount.dys || 0
                _this.hidCount += res.data.hidCount.dzg || 0
                _this.dys += res.data.hidCount.dys || 0
                _this.dzg += res.data.hidCount.dzg || 0
              }
            }
          });
        }
			},
			NavChange: function (e) {
				this.PageCur = e.currentTarget.dataset.cur
			},
			setInit(flag) {//子组件调用init方法后 无需再执行onShow方法
				this.initflag = flag;
			},
			// 获取当前版本号
			plusReady() {
				var that = this;

				// 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, function (inf) {
					that.wgtVer = inf.version;  //获取当前版本号
					setVersion(that.wgtVer);
					that.version = plus.runtime.version;
					uni.getSystemInfo({

						success(res) {
							that.brand = res.brand.toUpperCase();
							that.checkPlatform();
              console.log(that.brand) //手机牌子
              console.log(res.model) //手机型号
              console.log(res.screenWidth) //屏幕宽度
              console.log(res.screenHeight) //屏幕高度
						}
					});
				});

			},
			getBrand() {
				var that = this;
				uni.getSystemInfo({

					success(res) {
						that.brand = res.brand.toUpperCase();
						console.log(that.brand) //手机牌子
						console.log(res.model) //手机型号
						console.log(res.screenWidth) //屏幕宽度
						console.log(res.screenHeight) //屏幕高度
					}
				});
			},
			checkPlatform() {
				var _this=this;
				uni.getSystemInfo({
					success: (res) => {
						_this.checkUpdate(res.platform);
					}
				})
			},
			// 自动更新
			checkUpdate(platform){
				var that=this;
				uni.request({
					url: basePath + '/app/versionmanager/getVersion',  //获取最新版本号
					method: 'POST',
					data: {
						FILETYPE:platform,
						CORPINFO_ID:loginUser.CORPINFO_ID,
						USER_ID:loginUser.USER_ID,
					  },
					success: res => {

						if(res.data.pd.VERSION>that.wgtVer){
              if(platform=="android"){
                plus.nativeUI.confirm( "请保持应用最新版本,否则将无法使用。", function(e){
                  if(e.index == 0){
                    // if(that.brand == 'HUAWEI' || that.brand == 'XIAOMI' || that.brand == 'VIVO' || that.brand == 'OPPO' ){ //这几个牌子去自带应用商店升级
                    if(that.brand == 'XIAOMI'){ //这几个牌子去自带应用商店升级
                      plus.runtime.openURL("http://app.mi.com/detail/1509254", function(e1){// 本机应用商店升级
                      });
                    } else if(that.brand == 'VIVO'){ //这几个牌子去自带应用商店升级
                      plus.runtime.openURL("https://h5coml.vivo.com.cn/h5coml/appdetail_h5/browser_v2/index.html?appId=3457593", function(e1){// 本机应用商店升级
                      });
                    } else if(that.brand == 'OPPO' || that.brand == 'ONEPLUS' || that.brand == 'HUAWEI'){ //这几个牌子去自带应用商店升级
                      plus.runtime.openURL("market://details?id=uni.UNIC846DA5", function(e1){// 本机应用商店升级
                      });
                    }else if( that.brand == 'HUAWEI'){ //这几个牌子去自带应用商店升级
                      plus.runtime.openURL("https://url.cloud.huawei.com/n8nzgul61O", function(e1){// 本机应用商店升级
                      });
                    } else {
                      var url2 = encodeURI('https://qaaq.qhdsafety.com/file/uploadFiles/qaaqapp/app.apk');
                      plus.runtime.openURL(url2, function(res) {});
                    }
                    setTimeout(function() {
                      plus.runtime.quit();
                    }, 2000);
                  } else {
                    plus.runtime.quit();
                  }
                }, "下载更新", ["下载"] );
              }else{
                plus.nativeUI.confirm( "请保持应用最新版本,否则将无法使用。", function(e){
                  if(e.index == 0){
                    var url='itms-apps://itunes.apple.com/cn/app/id1632339513?mt=8';// 应用在appstore的地址
                    plus.runtime.openURL(url);
                    setTimeout(function() {
                      const threadClass = plus.ios.importClass("NSThread");
                      const mainThread = plus.ios.invoke(threadClass, "mainThread");
                      plus.ios.invoke(mainThread, "exit");
                    }, 2000);
                  }
                }, "下载更新", ["下载"] );
              }
						}
					},

					fail: (res) => {
						uni.showToast({
							icon: 'none',
							title: "服务器正在升级,请稍后再试。",
							duration: 2000
						});
					}
				});
			},
      getPromiseUnsigned(){
        uni.request({
          url: basePath + '/app/corppromise/ISSIGN_II',
          method: 'POST',
          dataType: 'json',
          header: {
            'Content-type':'application/x-www-form-urlencoded'
          },
          data: {
            USER_ID: loginUserId
          },
          success: (res) => {
            if(res.data.ISSIGN === 1){
              this.isShowTabBar = false;
              this.$nextTick(()=>{
                this.$refs.popup1.open()
              })
            }else {
              this.isShowTabBar = true;
              this.$nextTick(()=>{
                this.$refs.popup1.close()
              })
            }
          },
          fail: (res) => {
            uni.showToast({
              icon: 'none',
              title: "服务器正在升级,请稍后再试。",
              duration: 2000
            });
          }
        });
      },
      modalConfirm1(){
        uni.reLaunch({
          url:'/pages/my/promise/sign'
        })
      },
		}
	}
</script>

<style>

</style>