import { mapGetters,mapActions } from 'vuex'
import { pageTo, getCheckLogin, isLogin } from '@/utils/util.js'
import { getUser, wxLoginFn } from '@/utils/auth'
import { postWxCode, getDictionaries } from '@/api/mini'
import { getStaffApi } from '@/api/common'
import constant from '@/constant'
export default {
	data() {
		return {
			// 全局混入，插入链接参数
			option: null,
			// 全局混入，插入全局常量
			constant: this.$constant,
			// 全局混入，插入全局工具函数
			$util: this.$util,
			// 全局混入，插入全局跳转函数
      pageTo: pageTo,
      AUTHORITY_SHOW: false,
      shopAdminId: null,
      MODEL_TITLE: '您还没有开店哦',
      MODEL_BTN_TEXT: '免费申请',
      EXPER_AUTH_SHOW: false
		}
	},
	computed: {
		...mapGetters({ loginInfo: "getLoginInfo" }),
    ...mapGetters(["getLoginInfo"])
	},
	// 页面生命周期才触发
  onLoad: function (option) {
		this.option = option || null
		if (option.token) {
			uni.setStorageSync('token', option.token)
		}
		if (option.account) {
			uni.setStorageSync('account', option.account)
		}
    uni.setStorageSync('currentQuery', JSON.stringify(option))
    console.log(uni.getStorageSync('HOME_STATUS_STROAGE_SHOW'), 'this.shopAdminId', this.getLoginInfo, this.getLoginInfo.openShop)
    if (this.$util.isEmpty(this.getLoginInfo)) {
      wxLoginFn(true).then(res => {
        //客户可以访问的地址
        this.EXPER_AUTH_SHOW = res.data.showDemo == 1 ? true : false
        var urlArr = ['admin/shopAdmin/bind', 'pagesClient/official', 'pagesClient/member/bind', 'pagesShare/pay', 'pagesPurchase/ghsProduct'];
        var redirectPage = res.redirectPage;
        var needRedirect = true;
        urlArr.forEach((item, index, array) => {
          if (redirectPage.indexOf(item) != -1) {
            needRedirect = false;
          }
        })
        if (needRedirect === true) {
          //客户访问不能查看的页面跳转去官网
          console.log('res.data.damin', res.data.admin.openShop)
          if (res.data.shopAdminId == '0' && res.data.admin.openShop == '1') {
            this.AUTHORITY_SHOW = true;
            uni.hideTabBar()
            uni.setStorageSync('HOME_STATUS_STROAGE_SHOW', res.data.shopAdminId)
          } else if (res.data.admin.openShop == '2' && res.data.shopAdminId == '0') {
            this.MODEL_TITLE = '已申请'
            this.MODEL_BTN_TEXT = '查看申请进度'
            this.AUTHORITY_SHOW = true
            uni.hideTabBar()
            uni.removeStorageSync('HOME_STATUS_STROAGE_SHOW')
            uni.removeStorageSync('HOME_REFRESH')
            uni.setStorageSync('OPEN_SHOP_ID', res.data.admin.openShop)
          } else {
            this.AUTHORITY_SHOW = false;
            uni.showTabBar()
            uni.removeStorageSync('HOME_REFRESH')
          }
        }
        getUser(true)
        //获取门店信息
        this.setUserShopAll()
        if (this.init) {
          this.init()
        }
      })
    }
	},
	onShow(){
		if (isLogin()&&!this.$util.isEmpty(this.getLoginInfo)) {
			if (this.init) {
				this.init()
      }
    }
    if (uni.getStorageSync('HOME_REFRESH') == '1') {
      this.MODEL_TITLE = '已申请'
      this.MODEL_BTN_TEXT = '查看申请进度'
      this.AUTHORITY_SHOW = true
      uni.hideTabBar()
    }
	},
	methods: {
    ...mapActions(['setUserShopAll']),
    toFreeApply_HOME () {
      let status = ''
        if (uni.getStorageSync('HOME_STATUS_STROAGE_SHOW') == '0') {
          this.$util.pageTo({
            url: "/pagesClient/official/index",
            query: {
              ...this.option
            }
          });
        } else {
          getStaffApi().then(res => {
            console.log(res)
            if (res.data.openShop == '2') {
              status = '2'
            } else if (res.data.openShop == '3') {
              status = '3'
            }
            this.$util.pageTo({
              url: '/admin/home/result',
              query: {
                status
              }
            })
          }).catch(err => {})
      }
    },
    experienceFun(){
      /**姜枫 2021.04.30  小程序审核体验账号**/
      postWxCode().then(subRes => {
        if (this.$util.isEmpty(subRes)) {
          return false
        }
        uni.setStorageSync('token', subRes.data.token)
        uni.setStorageSync('shopId', subRes.data.admin.currentShopId)
        getDictionaries({ token: subRes.data.token }).then(res => {
          if (this.$util.isEmpty(res)) {
            return false
          }
          this.$store.commit('setDictionariesInfo', {
            ...res.data.dict,
            ...res.data
          })
          this.$store.commit('setMyShopInfo', {
            ...res.data.shop,
            ...res.data
          })
        })
        this.$store.commit('setLoginInfo', {
          ...subRes.data.admin,
          ...subRes.data
        })
        uni.removeStorageSync('HOME_STATUS_STROAGE_SHOW')
        uni.removeStorageSync('OPEN_SHOP_ID')
        this.AUTHORITY_SHOW = false
        uni.showTabBar()
        //启动更新逻辑 shish 2020.5.18
        if (subRes.data.update == 1) {
          const updateManager = wx.getUpdateManager()
          updateManager.onCheckForUpdate(function (res) {
            // 请求完新版本信息的回调
            console.log(res.hasUpdate)
          })
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {})// 新版本下载失败
          return true;
        }
        getUser(true)
        //获取门店信息
        this.setUserShopAll();
        uni.switchTab({url:'/admin/home/workbench'})
      })
    },
	},
	mounted() {
		if (this.mInit) {
			// #ifdef H5
			this.mInit()
			// #endif
			// #ifdef MP-WEIXIN
			// wxLoginFn().then(res => {
			// 	this.mInit()
			// })
			// #endif
		}
	}
}
