import Vue from 'vue';
import $ from 'jquery';
import lib from 'lib';

import wx from 'weixin-js-sdk';


let pathname = location.pathname;
let domain = pathname.indexOf('fhtest')>-1 ? 'http://test.api.fhvideo.oxzj.net' : 'http://api-fhvideo.duowan.com';

export default {
    data() {
        return {
            articleId: lib.getLocalData('articleId'),
            articleDetail: {},
            user: {},
            type: 0,
            barrages: [],
            createTime: "",
            // url : this.initDownloadUrl()
            url : "http://a.app.qq.com/o/simple.jsp?pkgname=com.ouj.fhvideo"
        }
    },
    mounted() {
        this.getArticleDetail();
    },
    beforeDestroy() {
    },
    methods: {
        /**获取文章详情*/
        getArticleDetail() {
            if (this.articleId == null) {
                return;
            }

            let self = this;
            let url = `${domain}/other/shareArticle.do?articleId=${self.articleId}`;
            
            $.ajax({
                type: "get",
                url: url,
                dataType: "jsonp",
                success: function (ret) {
                    if (ret.code == 0) {
                        let _data = ret.data;
                        let barrages = _data.barrages?_data.barrages:[];

                        for(var i=0; i<barrages.length; i++) {
                            barrages[i].createTime = self.handlerTime(barrages[i].createTime);
                        }

                        self.barrages = barrages;
                        self.articleDetail = _data.articleDetail;
                        self.user = _data.articleDetail.user;
                        self.type = 1;
                        self.createTime = self.handlerTime(_data.articleDetail.createTime);

                        self.shareObj = {
                            title: _data.articleDetail.user.nick+"的动态",
                            desc: _data.articleDetail.title,
                            img: _data.articleDetail.user.head
                        }

                        self.wxHandler();
                    }
                }
            });
        },
        handlerTime(createTime){
            var current =  (new Date()).getTime();
            var restTime = (current - createTime)/1000; 


            var day = Math.floor(restTime/(60*60*24));
            var hour = Math.floor(restTime%(60*60*24)/(60*60));
            var min = Math.floor(restTime%(60*60*24)%(60*60)/(60));
            var second = Math.floor(restTime%(60*60*24)%(60*60)%60);

            if (day>1) {
                return day+"天前";
            }
            if (hour>1) {
                return hour+"小时前";
            }
            if (min>1) {
                return min+"分钟前";
            } else {
                return second+"秒前";
            }
        },
        initDownloadUrl(){
            // var u = navigator.userAgent;

            // if (u.match(/micromessenger/ig)) {
            //     return 'http://a.app.qq.com/o/simple.jsp?pkgname=com.duowan.lolbox'
            // } else {
            //     var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            //     var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

            //     if (isAndroid) {
            //         return 'http://box.dwstatic.com/upgrade/download.php';
            //     } else if(isiOS) {
            //         return 'https://itunes.apple.com/cn/app/duo-wan-fan-he-lol-he-zi/id698076980?mt=8';
            //      }
            // } 
            var u = navigator.userAgent;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

            if (isAndroid) {
                return 'http://www.hiyd.com/static/apkDownload?appid=12';
            } else {
                return 'https://itunes.apple.com/us/app/%E9%A5%AD%E7%9B%92%E8%A7%86%E9%A2%91/id1237446875?l=zh&ls=1&mt=8';
            }
        },
        wxHandler: function () {
            let self = this;
            if (!navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger") {
                return;
            }
            var url = 'http://m.hiyd.com/weixin/getJsSign',
                data = {
                    url: location.href,
                    appid: 7
                };

            $.ajax({
                type: "get",
                url: url,
                data: data,
                dataType: "jsonp",

                success: function (ret) {
                    ret.data.debug = false;
                    ret.data.jsApiList = ["onMenuShareTimeline", "onMenuShareAppMessage"];
                    wx.config(ret.data);
                    wx.ready(function () {
                        //分享到朋友圈
                        wx.onMenuShareTimeline({
                            title: self.shareObj.desc,		//分享标题
                            imgUrl: self.shareObj.img,		//分享图标
                            success: function () {

                            },
                            cancel: function () {
                                // 用户取消分享后执行的回调函数
                            }
                        });
                        //分享给朋友
                        wx.onMenuShareAppMessage({
                            title: self.shareObj.title, 		//分享标题
                            desc: self.shareObj.desc,
                            imgUrl: self.shareObj.img, 	//分享图标
                            success: function () {
                            },
                            cancel: function () {
                                // 用户取消分享后执行的回调函数
                            }
                        });
                    });
                }
            });
        }
    }
}