<template>
  <div>
    <div class="tabbar" :class="{dark: handleType}">
      <div
        class="tabbarItem"
        @click="handleItem(item, index)"
        :class="{ activeMenu: active == index }"
        v-for="(item, index) in list"
        :key="index"
      >
        <div :class="{ aotoImg: index == 2 }">
          <img
            :src="active == index ? item.active : handleType ? item.dark : item.light"
            :width="index == 2 ? '50px' : '48px'"
            :height="index == 2 ? '50px' : '48px'"
          ></img>
        </div>
        <div class="hightImg" v-show="active == index">
          <img
            src="/static/img/icon_active@2x.png"
            width="68px"
            height="81px"
          ></img>
        </div>
        <div class="tabbarItemText" :class="{ active: active == index }">{{
          item.name
        }}</div>
      </div>
    </div>
    <van-popup
      closeIconPos="bottom-right"
      v-model="show"
      position="center"
      round
      bgColor="transparent"
      style="background: transparent"
      @close="close"
      @open="open"
    >
    <!-- :style="{backgroundImage: `url(${codeImg})`}" -->
      <div class="codeData" ref="downDiv" >
        <div class="close" @click="close">
          <img
            width="42px"
            height="42px"
            src="/static/img/icon_cancel@2x.png"
          ></img>
        </div>
        <img class="codeImg" :src="codeImg" alt="">
        <!-- <div class="text">
          <img
            width="100%"
            height="185px"
            src="https://4dscene.4dage.com/new4dkk/v2/lang/images/solutions/government/survey1.png"
          ></img>
          <div class="text-center pt-20px pb-8px text-2xl">一花五叶</div>
          <img
            width="100%"
            height="10px"
            src="https://4dscene.4dage.com/new4dkk/deng/static/img/line@2x.png"
          ></img>
          <div class="codeDataText">
            此灯组以禅宗“一花五叶”为主题，传承禅宗经典。您可欣赏花开五叶的意象，感受南宗禅衍化出临济、曹洞、法眼、沩仰和云门五家，发展壮大的辉煌历史，领悟慧能大师法脉的深邃智慧。
          </div>
          <div class="codeImg flex justify-end items-center">
            <div class="qrcode" ref="qrCodeUrl1"></div>
            <div class="qrcode" ref="qrCodeUrl2"></div>
          </div>
        </div> -->
      </div>
      <div class="codebutList">
        <div class="tips" style="text-align: center; color: #fff">
          长按保存图片
        </div>
          <!-- <div class="downText" v-if="false">
            <div class="img flex justify-center items-center">
              <div style="width: 36px; height: 36px">
                <img
                width="36px"
                height="36px"
                src="https://4dscene.4dage.com/new4dkk/deng/static/img/icon_download@2x.png"
              ></img>
              </div>
              <div class="pl-10">下载图片</div>
            </div>
            <div class="tips">完成6处打卡，即可点灯祈愿</div>
          </div> -->
          <div class="downButtom flex justify-center" v-if="codeListok.length>5">
            <!-- <div class="downButtomItem" @click="handleDown">下载图片</div> -->
            <div class="downButtomItem" @click="handleqiyuan">去祈愿</div>
          </div>
        </div>
    </van-popup>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import QRCode from 'qrcodejs2'
import wx from 'weixin-js-sdk'
import { Toast } from 'vant'
import Bus from '@/utils/bus.js'

// import QiyanQrcode from '@/components/qiyan-qrcode/qiyan-qrcode.vue'
// import uButton from "udiv-ui/components/u-button/u-button.vue";
export default {
  components: {
    // QiyanQrcode
  },
  data() {
    return {
      show: false,
      qrcode1: null,
      qrcode2: null,
      codeImg: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/01.png',
      title1: '1.现在时间戳：' + Date.now(),
      codeListok:[],
      codeList:{
        code1: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/01.png',
        code2: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/02.png',
        code3: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/04.png',
        code4: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/05.png',
        code5: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/06.png',
        code6: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/07.png',
        code7: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/11.png',
        code8: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/12.png',
        code9: 'https://4dscene.4dage.com/new4dkk/deng/static/img/code/13.png',
      },
      list: [
        {
          active: '/static/img/icon_home_active.png',
          dark: '/static/img/icon_home_dark.png',
          light: '/static/img/icon_home_light.png',
          name: '首页',
          path: '/home'
        },
        {
          active: '/static/img/icon_scan_active@2x.png',
          dark: '/static/img/icon_scan_dark@2x.png',
          light: '/static/img/icon_scan_light@2x.png',
          name: '扫码打卡'
        },
        {
          active: '/static/img/icon_map_active@2x.png',
          dark: '/static/img/icon_map_dark@2x.png',
          light: '/static/img/icon_map_light@2x.png',
          name: '地图',
          path: '/map'
        },
        {
          active: '/static/img/icon_ai_active@2x.png',
          dark: '/static/img/icon_ai_dark@2x.png',
          light: '/static/img/icon_ai_light@2x.png',
          name: '妙笔生花',
          path: '/composite'
        },
        {
          active: '/static/img/icon_lotus_active@2x.png',
          dark: '/static/img/icon_lotus_dark@2x.png',
          light: '/static/img/icon_lotus_light@2x.png',
          name: '点亮',
          path: '/prayers/clocked'
        },
        // {
        //   active: '/static/img/icon_user_active@2x.png',
        //   dark: '/static/img/icon_user_dark@2x.png',
        //   light: '/static/img/icon_user_light@2x.png',
        //   name: '我的',
        //   path: '/my'
        // }
      ],
      title: 'Hello'
    }
  },
  computed: {
    ...mapState('home', ['active']),
    handleType() {
      return this.$route.name == 'PrayersClocked'? true : false
    }
  },
  watch: {
    $route(to, from) {
      let path = to.path
      console.log('handleItem', path)
      this.list.map((item, index) => {
        if (to.path == item.path) {
          this.$store.commit('home/changeActive', index)
        }
      })
    }
  },
  mounted() {
    this.getloadlist()
    // let path = this.$route.path
    // this.list.map((item, index) => {
    //   if (path == item.path) {
    //     console.log('path', path, item, index)
    //     this.$store.commit('home/changeActive', index)
    //   }
    // })
  },
  methods: {
    getloadlist() {
      this.getRequest('/api/wx/getCode').then((res) => {
        this.codeListok = res.data || []
      })
     },
    handleItem(item, index) {
      let obj = { 
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=1':this.codeList.code1,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=2':this.codeList.code2,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=3':this.codeList.code3,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=4':this.codeList.code4,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=5':this.codeList.code5,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=6':this.codeList.code6,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=7':this.codeList.code7,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=8':this.codeList.code8,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=9':this.codeList.code9,
        'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=10':this.codeList.code9,
       };
      //  let objclockIn = { 
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=1':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=2':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=3':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=4':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=5':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=6':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=7':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=8':'SG-PHP2F5mD35e',
      //   'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=9':'SG-PHP2F5mD35e',
      //  };
      console.log('handleItem', item, index)
      let that = this
      if (item.name == '扫码打卡') {
        // 只允许通过相机扫码
        // uni.scanCode({
        //   onlyFromCamera: true,
        //   success: function (res) {
        //     console.log('条码类型：' + res.scanType)
        //     console.log('条码内容：' + res.result)
        // wx.miniProgram.postMessage({ data: { foo: 'scanCode', type: 'tabbar' } })
        // wx.miniProgram.navigateTo({
        //   url: '/pages/my/index?scanCode=true'
        // })
        // that.show = true
        // that.$store.commit('home/changeActive', index)
        // setTimeout(() => {
        //   this.creatQrCode()
        // }, 200)
        //   },
        //   fail: function (e) {
        //     console.log('扫码失败', e)
        //     that.show = true
        //   }
        // })
        wx.scanQRCode({
          needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
          scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
          success: function (res) {
            console.log(res, 'scanQRCode');
            var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
            if(!result || (result && result.indexOf('https://houseoss.4dkankan.com/project/nanHua/index.html') == -1)){
              return Toast('无效二维码')
            }
            if(result == 'https://houseoss.4dkankan.com/project/nanHua/index.html#/code?m=10'){//线下祈福
              that.$router.push('/prayers/list')
              return 
            }
            that.show = true;
            that.codeImg = obj[result];
            // that.$store.commit("changeActive", index);
            if(!that.codeListok.includes(result)){
              Bus.emit('handleItem', result)
              that.getRequest('/api/wx/clockIn', {code: result})
              that.getloadlist()
            }
          }
        });
        return
      }else if (item.name == '我的') {
        this.$store.commit('home/changeActive', 4)
        wx.miniProgram.navigateTo({
          url: '/pages/my/index',
          active: 4
        })
        return
      }
      console.log('handleItem', item, index, this.active)
      if(this.active == index) return
      // that.$store.commit('home/changeActive', index)
      if (item.path) {
        this.$router.push(item.path)
      }
    },
    handleHome() {
      console.log('开启云上观灯', uni)
      // uni.$u.route('/pages/home/home')
    },
    handleDown() {
      this.$store.dispatch('home/saveImage', this.$refs.downDiv)
    },
    handleqiyuan() {
      this.show = false
      this.$router.push('/prayers/list')
    },
    close() {
      console.log('close', this.qrcode1, this.qrcode2)
      this.show = false
      // console.log('close');
    },
    open() {},
    creatQrCode() {
      if (this.qrcode1) {
        this.qrcode1 && this.qrcode1.clear() //清除二维码
        this.qrcode2 && this.qrcode2.clear() //清除二维码
        this.qrcode1.makeCode('xxxxx')
        this.qrcode2.makeCode('ssssss')
      } else {
        this.qrcode1 = new QRCode(this.$refs.qrCodeUrl1, {
          text: 'xxxx', // 需要转换为二维码的内容
          width: 72,
          height: 72,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
        })
        this.qrcode2 = new QRCode(this.$refs.qrCodeUrl2, {
          text: 'xxxx', // 需要转换为二维码的内容
          width: 72,
          height: 72,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tabbar {
  display: flex;
  align-items: center;
  justify-content: space-around;
  position: fixed;
  bottom: 0;
  width: 100vw;
  z-index: 1000;
  height: 81px;
  left: 0;
  padding: -2px 0 16px 0;
  background-image: url(https://4dscene.4dage.com/new4dkk/deng/static/img/menu_light@2x.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .tabbarbg {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
  }
  .tabbarItem {
    text-align: center;
    width: 50px;
    font-weight: 400;
    font-size: 11px;
    color: #303030;
    line-height: 13px;
    text-align: center;
    position: relative;
    z-index: 1;
    .hightImg {
      position: absolute;
      z-index: 2;
      top: 0;
      left: -10px;
    }
    .aotoImg {
      position: relative;
      top: -5px;
    }
  }
}
.dark {
  background-image: url(https://4dscene.4dage.com/new4dkk/deng/static/img/menu_dark@2x.png);
  .tabbarItem{
    color: #fff;
  }
}
.codebutList {
  text-align: center;
  position: relative;
  width: 100%;
  // top: 76px;
  .downText {
    .img {
      margin-right: 10px;
      font-weight: 400;
      font-size: 16px;
      color: #ffffff;
      line-height: 19px;
      text-align: center;
    }
    .tips {
      padding-top: 10px;
      font-weight: 400;
      font-size: 16px;
      color: #a49d94;
      line-height: 19px;
      text-align: center;
    }
  }
  .downButtom {
    .downButtomItem {
      width: 140px;
      height: 42px;
      background: url(https://4dscene.4dage.com/new4dkk/deng/static/img/btn01@2x.png) 100% 100%
        no-repeat;
      background-size: cover;
      font-weight: 400;
      font-size: 16px;
      color: #ffffff;
      line-height: 19px;
      font-style: normal;
      margin: 0 5px;
      line-height: 42px;
      text-align: center;
    }
  }
}
.codeData {
  // background: url(https://4dscene.4dage.com/new4dkk/deng/static/img/pop2@2x.png) no-repeat cover;
  background: url(https://4dscene.4dage.com/new4dkk/deng/static/img/pop2@2x.png) 100% 100% no-repeat;
  background-size: 100% auto;
  width: calc(100vw - 20px);
  height: auto;
  // padding: 48px 52px 30px 36px;
  position: relative;
  .codeImg {
    width: 100%;
    height: auto;
    object-fit: cover;
  }
  .close {
    position: absolute;
    right: -0px;
    top: -0px;
    img{
      width: 42px;
      height: 42px;
    }
  }
  .text {
    font-weight: 400;
    font-size: 24px;
    color: #d86332;
    line-height: 29px;
    position: relative;
    padding-bottom: 72px;
    .codeDataText {
      padding: 8px 0 16px 0;
      font-weight: 400;
      font-size: 14px;
      color: #b1967b;
      line-height: 18px;
      text-align: left;
    }
    .codeImg {
      text-align: right;
      width: 100%;
      height: 72px;
      // float: right;
      .qrcode {
        padding: 5px;
        margin: 0 5px;
        background: #fff;
      }
    }
  }
}
</style>
