<template>
  <div class="page" :class="{active:loaded, mini:inMini}">
    <div class="title">{{title}}</div>
    <div class="seller-tip"><span class="seller-name">{{sellerName}}</span>的分享</div>
    <div class="rili">
      <div class="img"></div>
      <div class="day">{{day}}</div>
      <div class="month">{{month}}</div>
      <div class="good">{{todayFortune.suitable}}</div>
      <div class="bad">{{todayFortune.avoid}}</div>
    </div>
    <div class="header no-img">头条聚光灯</div>
    <div class="homes">
      <div class="new-title">{{headlines.title}}</div>
      <div class="pic" :style="{'background-image': 'url(' + mainImgUrl + ')'}" ></div>

      <div class="tip1">{{headlines.des}}<div class="more" @click="goHome" v-if="headlines.origin">详情</div></div>
      
      <div class="commet" v-if="headlines.comment">
        <div class="header-img" :style="{'background-image': 'url(' + avatar + ')'}"></div>
        <div class="commet-info">
          <div class="commet-name">观点：</div>
          <div class="commet-say">{{headlines.comment}}</div>
        </div>
      </div>
    </div>

    <div class="header" v-if="domesticNews.length>0">热点大事件</div>
    <div class="homes" v-if="domesticNews.length>0">
      <div class="box" v-for="(item, index) in domesticNews" v-bind:key="index">
        <div class="new-title">{{item.title}}</div>
        <div class="tip1">{{item.des}}<div class="more" @click="jump(item)" v-if="item.origin">详情</div></div>
      </div>
    </div>

    <!-- <div class="header" v-if="intelNews.length>0">七彩万花筒</div>
    <div class="homes" v-if="intelNews.length>0">
      <div class="box" v-for="(item, index) in intelNews" v-bind:key="index" @click="jump(item)">
        <div class="tip1">{{item.title}}</div>
        <div class="tip2">{{getText(item.des, 92)}}</div>
      </div>
    </div>

    <div class="header" v-if="socialNews.length>0">趣味小常识</div>
    <div class="homes" v-if="socialNews.length>0">
      <div class="box" v-for="(item, index) in socialNews" v-bind:key="index" @click="jump(item)">
        <div class="tip1">{{item.title}}</div>
        <div class="tip2">{{getText(item.des, 92)}}</div>
      </div>
    </div>

    <div class="header" v-if="footerImage">每日笑一笑</div> -->

    <img :src="footerImage" class="footer-pic" alt="" v-if="footerImage">

    <div class="car-parent">
      <div class="dot"></div>
      <div class="car">
        <div class="dot incar"></div>
        <div class="car-box">
          <div class="car-img" :style="{'background-image': 'url(' + avatar + ')'}"></div>
          <div class="car-info">
            <div class="car-name">{{sellerName}}</div>
            <div class="car-com">{{companyName}}</div>
          </div>
          <div class="car-code">
            <img class="code-img" :src="wechatQrCode" :v-if="wechatQrCode">
          </div>
        </div>
      </div>
      <div class="long-touch"><div class="long-line"></div>{{wechatQrCode?"长按二维码联系TA":"暂无二维码信息"}}</div>
    </div>
    

    <share-footer v-if="showShare" @doshare="doshare" :sellerid="sellerId" :wxqrcode="wechatQrCode" :push="push"></share-footer>
    <toastTip ref="toastTip"></toastTip>
    <div class="dui-share__methods" v-if="isShow" @click="closeModal">
      <div class="gudide-des">从菜单中点击转发分享</div>        
      <div class="share-yindao">
        <img src="//yun.duiba.com.cn/h5/miniprogram/kejiji/images/guide.png" alt="">
      </div>
    </div>
  </div>
</template>

<script>
import { get, post } from "@js/request";
import { getUrlParameter, isWeChatApplet, getEllipsisText } from "@js/tooler";
import { shareConfig, updateLink } from "@js/share";
import ShareFooter from '../../components/shareFooter'
import toastTip from '../../components/toastTip'
import { startTime, endTime } from '../../../comm/readSec'

const SCID = getUrlParameter('scid');
const PUSH = getUrlParameter('push') == "push";
const API = {
  h5info: '/kjy/mp/content/h5info'
}

const inMiniprogram = getUrlParameter("mini") == 1;
const FROM = getUrlParameter("from");
export default {
  data() {
    return {
      title: '',
      day: 1,
      month: '',
      mainImgUrl: '//yun.dui88.com/h5/miniprogram/kejiji/web/rili.png',
      footerImage: '//yun.dui88.com/h5/miniprogram/kejiji/web/rili.png',
      showShare: false,
      todayFortune: {}, //今日运势
      headlines: {}, //头条新闻
      socialNews: [], //社会新闻
      intelNews: [], //国际新闻
      domesticNews: [], //国内新闻
      isShow: false,
      sellerName: "",
      companyName: "",
      avatar: "",
      wechatQrCode: "",
      loaded: false,
      sellerId: null,
      inMini: false,
      push: false
    };
  },
  components: {ShareFooter, toastTip},
  methods: {
    getText(str, size){
      return getEllipsisText(str, size);
    },
    showToday() {
      // this.$router.push("/today");
      if(inMiniprogram){
        wx.miniProgram.navigateTo({
          url: '/pages/news/news_today/main?num=3'
        })
      }
      else{

      }
    },
    async initData(){
      // let a = await get('/kjy/mp/seller/getScId', {contentId: 54});
      // console.log(a);
      // this.tip = window.navigator.userAgent.toLowerCase();
      let res = await get(API.h5info, { scid: SCID });
      console.log(res);
      if (res.success) {
        this.loaded = true;
        this.title = res.data.title;
        this.mainImgUrl = res.data.mainImgUrl;
        console.log(res.data.mainImgUrl);
        let dailyData = JSON.parse(res.data.text);
        let date = new Date(res.data.releaseTime || Date.now());
        this.day = date.getDate();
        this.month = ("一-二-三-四-五-六-七-八-九-十-十一-十二".split("-"))[date.getMonth()] + "月";
        this.todayFortune = dailyData.todayFortune;
        this.socialNews = dailyData.socialNews;
        this.intelNews = dailyData.intelNews;
        this.headlines = dailyData.headlines;
        this.domesticNews = dailyData.domesticNews;
        this.footerImage = dailyData.footerImage;

        let seller = res.data.sellerCardVO;
        this.sellerId = seller.id;
        this.sellerName = seller.sellerName;
        this.companyName = seller.companyName;
        this.avatar = seller.avatar;
        this.wechatQrCode = seller.wechatQrCode;

        if(inMiniprogram && res.data.myself){
          this.showShare = true;
        }
        if (FROM === 'singlemessage' && res.data.myself) {
          this.isShow = true;
        }
        var link = location.origin + location.pathname + location.search;
        link = link.replace("mini=1", "mini=0");
        link = link.replace("push=push", "push=0");
        shareConfig({
          link: link,
          title: this.title,
          desc: '来自' + this.sellerName + '的分享',
          img: location.protocol + this.mainImgUrl,
          debug: false
        });
      } else {
        if (res.code == '100010') {
          this.getH5Authorize();
        } else if (res.code == '100020') {
          
        }
      }
      
    },
    getH5Authorize(){
      let url = '/kjy/authorize/wxh5base?uri=/kjy/mp/daily?scid=' + SCID;
      window.location.href = url;
    },
    closeModal() {
      this.isShow = false;
    },
    goHome(){
      let url = this.headlines.origin;
      if(!url){
        return;
      }
      if(inMiniprogram){
        this.showTip();
        return;
        // wx.miniProgram.navigateTo({
        //   url: '/pages/news/news_link/main?url=' + encodeURIComponent(url)
        // })
      }
      else{
        window.location.href = url;
        // let scid = this.$mp.query.scid;
        // this.pageUrl = 'https://kjj.m.duibatest.com.cn/kjy/mp/contentview?scid=' + scid + '&currentView=minipg';
      }
    },
    jump(item){
      console.log(item);
      let url = item.origin;
      if(!url){
        return;
      }
      if(inMiniprogram){
        this.showTip();
        return;
        // wx.miniProgram.navigateTo({
        //   url: '/pages/news/news_link/main?url=' + encodeURIComponent(url)
        // })
      }
      else{
        window.location.href = url;
        // let scid = this.$mp.query.scid;
        // this.pageUrl = 'https://kjj.m.duibatest.com.cn/kjy/mp/contentview?scid=' + scid + '&currentView=minipg';
      }
    },
    doshare(){
      
    },
    showTip(){
      this.$refs.toastTip.showTip("小程序中不支持跳转");
    }
  },
  created() {
    this.inMini = inMiniprogram;
    this.initData();
    startTime(SCID);
    if(PUSH){
      this.showShare = true;
      this.push = true;
    }
  }
};
</script>

<style lang="less" scoped src="./today.less">
</style>