<template>
  <div class="memberTrial">
    <div class="time-range" v-if="status==='initial' || status==='none'">
      <div v-if="activity">
        {{activityName}}{{ activity.startTime |dateTime }} 至 {{ activity.endTime|dateTime }}
      </div>
      <div v-if="!activity">
        活动时间：2021-05-01 09:00 至 2021-06-18 24:00
      </div>
    </div>
    <div>
    <!-- <div v-if="status==='initial'"> -->
      <div class="tabsleftcss" :style="tabsleftcss">
          <van-grid :border="false" :column-num="2">
            <van-grid-item
              v-for="(item,index) in prizeList"
              :key="index">
              <div :class="actStatus === '3'|| isTemplate?'box2':'box'">
                <span v-if="item.flag == 0 && !isTemplate" class="none"></span>
                <div class="image">
                  <img :src='item.showImg'/>
                </div>
                <div class="title">{{item.skuName}}</div>
                <div class="memo">{{item.skuDesc}}</div>
                <div class="button" @click="applyTrial(item,index)" v-if="isTemplate || actStatus === '3' && item.flag != 0">申请试用</div>
                <!-- <div class="button" @click="applyTrial(item,index)" v-show="item.buttonDisabled" v-if="isTemplate || actStatus === '3'">申请试用</div> -->
                <div class="button button-disabled" v-if="item.flag == 0 && !isTemplate">申请试用</div>
              </div>
            </van-grid-item>
          </van-grid>
        </div>
    </div>
    <div><p class="tipCss" v-if="tipDis">{{ tip }}</p></div>
  </div>
</template>

<script>
import Vue from 'vue';
import {Button,Grid, GridItem,Tab, Tabs,Uploader,Icon,ImagePreview,Image as VanImage} from "vant";
import {callShare4MiniProgram} from '../../../util/share';
import apis1 from '../../../util/apis1.js';
import dateUtil from "../../../util/dateUtil.js";
import { getUrlQueryValueByKey } from '../../../util/tools.js';
import { ApplyAddressPopComService,ReminderPopComService,joinVipService } from '../../../util/comCreate.js';
Vue.use(Uploader);
Vue.use(Icon);
Vue.use(Grid);
Vue.use(GridItem);
Vue.use(VanImage);
let {getMemberTrial,applyTrialGood,checkMember} = apis1;

export default {
  props: ['isTemplate', 'datas', 'styleData', 'keyData', 'templateData'],
  components: {
    Button,
    Grid,
    GridItem,
  },
  data() {
    return {
      activityShow:false,
      activityName:'活动时间:',
      activityCss:{
          'font-size': '12px',
          '-webkit-transform':'scale(0.85)',
          'margin-left':'-0.1rem',
      },
      //不满足条件
      tipDis:false,
      drawCssTop:{
          'margin-top':window.innerHeight - 280 + 'px'
      },
      tabsleftcss:{
      },
      /**
     * 活动状态
     * 0：默认状态,没有含义
     * 1:活动尚未开始
     * 2:活动已结束
     * 3:活动正常进行
     */
      actStatus: '0',
      //初期活动状态
      status: 'none',
      // 活动信息
      activity: null,
      //领取奖品ID
      prizeListId:null,
      prizeList:[],
      apply:true
    }
  },
  created() {
    this.publicData.$on('activityLoaded', this.activityLoaded);
    this.publicData.$on('applied', (res,type,index) => {
      if(type == 1){
        this.prizeList[index].buttonDisabled = false
      }      
    })
  },

  methods : {
    //试用申请
    //50032重复申请
    //50033申请次数不够
    applyTrial(data,index){
      if(this.isTemplate) {//预览模式返回
          this.$toast('活动预览，仅供查看');
          return;
      }
      if(data.errorType == 50032 || data.errorType == 50033){
        ReminderPopComService.create(
          data.errorMsg,
          "//img10.360buyimg.com/imgzone/jfs/t1/198092/1/400/2429/61022268E1e0f0ba0/2a0485a12eb3cbbf.png",
          "//img10.360buyimg.com/imgzone/jfs/t1/136745/32/20121/48059/5fd996feE56314a28/b952f0e6f3fe811f.png",
          1,
          index,
          {},
          (submit) => {
            ReminderPopComService.close();
          }
        );
        return
      }
      //data.flag 0光了 1没光
      // if(data.flag === 1){
        var datas = {
            isEditAddress: true,
            isAlLookAddress: false,
            prizeId:data.prizeId
        };
        
        applyTrialGood({prizeId:data.prizeId}).then(res => {
          ApplyAddressPopComService.create(datas,index, (submit) => {
            ApplyAddressPopComService.close();
          });
        }).catch(res =>{
          console.log('返回值',res)
          this.prizeList[index].errorType = res.resp_code;
          this.prizeList[index].errorMsg = res.resp_msg;
          ReminderPopComService.create(
            res.resp_msg,
            "//img10.360buyimg.com/imgzone/jfs/t1/198092/1/400/2429/61022268E1e0f0ba0/2a0485a12eb3cbbf.png",
            "//img10.360buyimg.com/imgzone/jfs/t1/136745/32/20121/48059/5fd996feE56314a28/b952f0e6f3fe811f.png",
            1,
            index,
            {},
            (submit) => {
              ReminderPopComService.close();
            }
          );
        })
      // }
    },
    // 活动加载完成
    activityLoaded(data) {
      if(this.isTemplate){
        var datasAll = this.templateData;
        this.setPreviewData(this.datas.previewData);
        if (datasAll) {
          this.activity = datasAll.activity;
          this.activity.startTime = datasAll.activity.dateValue[0];
          this.activity.endTime = datasAll.activity.dateValue[1];
          // if (datasAll.draw) {
            let newData = {};
            let newPrizeList = []
            for(let i = 0;i<datasAll.draw.prizeList.length;i++){
              let obj = {
                  "flag": 0,
                  "prizeId": "1420695782825095169",
                  "showImg": datasAll.draw.prizeList[i].showImg,
                  "skuDesc": '',
                  "skuName": datasAll.draw.prizeList[i].swData.skuName,
                  "buttonDisabled":true
              };
              newPrizeList.push(obj);
            }
            newData.prizeList = newPrizeList;
            this.setPreviewData(newData);
          // }
        }else{
          //TOOD: 预览的假数据
          this.setPreviewData(this.datas.previewData);
        }
      }else{
        console.log('activityLoaded')
        this.activity = data;
        this.setGate();
      }
    },
    // 设置入口
    setGate() {
      this.actStatus = sessionStorage.getItem("actStatus");
      console.log("actStatus",this.actStatus)
      // if (this.actStatus === '3') {
          this.initData();
      // }
    },
    //数据加载
    initData() {
      getMemberTrial().then(res => {
        res.data.forEach((item, index) => {
          item.buttonDisabled = true;
          item.errorType = 0;
          item.errorMsg = '';
        });
        this.prizeList = res.data;
      })
    },
    //关闭活动说明
    closeClick() {
        this.initData("1");
        this.showActivity = false;
    },
    //活动说明按钮【确认参与】
    participateClick() {
        this.initData("1");
        this.showActivity = false;
    },
    //活动说明按钮【确认参与】
    getPrize() {
        this.initData("2");
    },
    // 设置预览数据
    setPreviewData(data,flg) {
      console.log('setPreviewData',data)
      this.status = 'initial';
      //手机预览的情况
      let letBrowser = this.getBrowser();
      if(letBrowser.plat === "android" || letBrowser.plat === "iphone"){
      }
      else{
          this.activityShow = true;
          this.activityName = '活动时间:';
      }
      this.prizeList = data.prizeList;
    },
    //判断是否手机端浏览
    getBrowser(){
          var ua = navigator.userAgent.toLowerCase();
          var btypeInfo = (ua.match( /firefox|chrome|safari|opera/g ) || "other")[ 0 ];
          if( (ua.match( /msie|trident/g ) || [] )[ 0 ] )
          {
              btypeInfo = "msie";
          }
          var pc = "";
          var prefix = "";
          var plat = "";
          //如果没有触摸事件 判定为PC
          var isTocuh = ("ontouchstart" in window) || (ua.indexOf( "touch" ) !== -1) || (ua.indexOf( "mobile" ) !== -1);
          if( isTocuh )
          {
              if( ua.indexOf( "ipad" ) !== -1 )
              {
                  pc = "pad";
              } else if( ua.indexOf( "mobile" ) !== -1 )
              {
                  pc = "mobile";
              } else if( ua.indexOf( "android" ) !== -1 )
              {
                  pc = "androidPad";
              } else
              {
                  pc = "pc";
              }
          } else
          {
              pc = "pc";
          }
          switch( btypeInfo )
          {
              case "chrome":
              case "safari":
              case "mobile":
                  prefix = "webkit";
                  break;
              case "msie":
                  prefix = "ms";
                  break;
              case "firefox":
                  prefix = "Moz";
                  break;
              case "opera":
                  prefix = "O";
                  break;
              default:
                  prefix = "webkit";
                  break
          }
          plat = (ua.indexOf( "android" ) > 0) ? "android" : navigator.platform.toLowerCase();
          return {
              version: (ua.match( /[\s\S]+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[ 1 ],     //版本
              plat: plat,                   //系统
              type: btypeInfo,              //浏览器
              pc: pc,
              prefix: prefix,                //前缀
              isMobile: (pc == "pc") ? false : true              //是否是移动端
          };
    },
  },

  filters: {
      //时间转换
      dateTime(val) {
          if (val) {
              return dateUtil.formatTime(val, 'yyyy-MM-dd hh:mm');
          }
          return val;
      },
      ellipsis(value) {
          if (!value) return ''
          if (value.length > 5) {
              return value.slice(0, 5) + '...'
          }
          return value
      },
  }
};
</script>

<style lang="scss" scoped>
.tipCss {
  font-size: 0.3rem;
  font-weight: normal;
  font-stretch: normal;
  line-height: 0.78rem;
  letter-spacing: 0rem;
  color: #fefefe;
  margin: 0.5rem;
  line-height: 1;
  margin-top: 6.1rem;
}
.time-range {
  // width: 4.84rem;
  height: 0.34rem;
  background-image:url(//img10.360buyimg.com/imgzone/jfs/t1/137800/40/26030/727/61b0ac9cEf6e82135/4a2ff2527eed07b5.png);
  background-size:100% 100%;
  text-align:center;
  font-size: 0.16rem;
  font-family: SourceHanSansCN;
  font-weight: 400;
  color: #FFFFFF;
  line-height: 0.34rem;
  position:absolute;
  top:0;
  left: 51%;
  transform:translateX(-50%);
  width: max-content;
  padding-left: 0.2rem;
  padding-right: 0.2rem;
 }

.tabsleftcss{
  // position: absolute;
  margin-top: 0.42rem;
  // left: 50%;
  // transform:translateX(-50%);
  width:100%;
  clear: both;
  overflow-y: scroll;
  padding-bottom: 1rem;
  padding-left:0.3rem;
  // padding-right:0.3rem;
  background:rgb(255, 199, 197)
}
.box2{
  width: 3.36rem;
  min-height: 5.6rem;
  background-image:url(//img10.360buyimg.com/imgzone/jfs/t1/190034/24/15992/36169/6103c4b0E3a145b27/e49d203c80a958b9.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  margin-bottom:0.1rem;
  overflow:hidden;
  position: relative;
  // position: relative;
  .image{
    width:2.8rem;
    height:2.69rem;
    margin:0px auto;
    margin-top:0.36rem;
    img{
      width:100%;
      height:100%
    }
  }
  .title{
    width: 2.56rem;
    font-size: 0.24rem;
    height: 0.54rem;
    font-family: SourceHanSansCN;
    font-weight: 400;
    color: #F23644;
    line-height: 0.27rem;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    margin:0 auto;
    margin-top:0.2rem;
    margin-bottom:0.08rem;
    text-align:left;
    word-break: break-all;
  }
  .memo{
    width: 2.56rem;
    font-size: 0.2rem;
    font-family: SourceHanSansCN;
    font-weight: 400;
    color: #FF912F;
    // line-height: 14px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    margin:0 auto;
    margin-bottom:0.26rem;
    text-align:left;
    word-break: break-all;
  }
  .button{
    width: 2.54rem;
    height: 0.56rem;
    background: linear-gradient(0deg, #FF122C 0%, #FF6F6F 100%);
    text-align: center;
    line-height: 0.56rem;
    margin-top:-0.2rem;
    color: #fff;
    font-size: 0.28rem;
    position:absolute;
    bottom:0.44rem;
    left: 50%;
    transform:translateX(-50%);
    border-radius: 0.3rem;
  }
  .button-disabled{
    background: #ccc!important;
  }
}
.box{
  width: 3.36rem;
  min-height: 5rem;
  background-image:url(//img10.360buyimg.com/imgzone/jfs/t1/184818/7/17316/33689/61091794E578eea75/4e3a3becb2f4e399.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  margin-bottom:0.1rem;
  overflow:hidden;
  position: relative;
    .image{
      width:2.8rem;
      height:2.69rem;
      margin:0px auto;
      margin-top:0.36rem;
      img{
        width:100%;
        height:100%
      }
  }
  .title{
    width: 2.56rem;
    height: 0.54rem;
    font-size: 0.24rem;
    font-family: SourceHanSansCN;
    font-weight: 400;
    color: #F23644;
    line-height: 0.27rem;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    margin:0 auto;
    margin-top:0.2rem;
    margin-bottom: 0.1rem;
    text-align:left;
    word-break: break-all;
  }
  .memo{
    width: 2.56rem;
    font-size: 10px;
    font-family: SourceHanSansCN;
    font-weight: 400;
    color: #FF912F;
    line-height: 14px;
    margin-bottom:0.26rem;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    margin:0 auto;
    text-align:left;
    word-break: break-all;
  }
}
  .none{
    width: 1.56rem;
    height: 1.58em;
    position: absolute;
    right: -0.84rem;
    top: 0.26rem;
    background-image: url(//img10.360buyimg.com/imgzone/jfs/t1/220432/11/7125/1599/61b0ae89Eb74b0c14/23878bf84477e1fa.png);
    background-repeat: no-repeat;
    background-size: contain;
  }

</style>

<style lang="scss">
  .van-grid-item{

    .van-grid-item__content{
      width: 3.36rem;
      display:-webkit-box;
      display:-webkit-flex;display:flex;
      -webkit-box-orient:vertical;
      -webkit-box-direction:normal;
      -webkit-flex-direction:column;
      flex-direction:column;
      box-sizing:border-box;
      padding:0px!important;
      border-top-left-radius: 0.1rem;
      border-top-right-radius: 0.1rem;
      border-bottom-left-radius: 0.1rem;
      border-bottom-right-radius: 0.1rem;
      background: unset;
    }
  }
  .van-tabs__content{
    padding-top: 0.3rem;
  }
  .van-tabs__wrap{
    border-top-left-radius: 0.2rem;
    border-top-right-radius: 0.2rem;
  }
  /*body{*/
  /*  background-image: url(//img10.360buyimg.com/imgzone/jfs/t1/195335/37/2478/1252077/609a5d9cE957e265d/95a3117a1d8bb6ad.png);*/
  /*  background-attachment: fixed;*/
  /*  background-repeat: no-repeat;*/
  /*  background-size: cover;*/
  /*  height: 100vh;*/
  /*}*/
</style>

