import Vue from 'vue'
import lib from 'lib'
import _ from 'underscore'

var articleId = lib.getParam("articleId");

export default {
    data() {
        return {
            articleId : articleId,
            article : {}, //影片
            areas : [], //地区
            categories : [], //电影分类
            filmmakers : [], //影人
            watchOnline : {}, //在线源
            moreCommentaries : {},//更多解说
            commentaryCount : 0,
            articleType: 1,// 1:电影,2:电视剧,3:动画
            isShowSynopsis : 0,
            ups : [], //up主列表
            curUpsCommentaries : [], //当前主播视频列表
            alias : ["无"], //别名
            downloadUrl : lib.downloadUrl,
            photos : {},
            images : [],
            boxOffice : 0,
            trailers : {},
            firstTrailer : {}
        }
    },
    mounted() {
        this.getArticle();
    },
    activated() {
    },
    methods: {
        //展示剧情详情
        showSynopsis(){
            this.isShowSynopsis = this.isShowSynopsis==1 ? 0 : 1;
        },
        previewImage(type,commentIndex,picIndex){
            let self = this;
            let items = [],picList = [];
            let arr = [];

            switch (type){
                case 1 : //影人
                    _.each(self.filmmakers,(item,index)=>{
                        arr.push(item.pic)
                    })

                    picList = arr.splice(0,8)

                    lib.setPreviewImage(picList,picIndex,140,200)
                    break;
                case 2 : //剧照
                    _.each(self.images,(item,index)=>{
                        arr.push(item.largeUrl)
                    })

                    picList = arr.splice(0,8)

                    lib.setPreviewImage(picList,picIndex)
                    break;
            }
        },
        //切换up主播放列表
        changeUpList(upIndex){
            $(".v-mod-jieshuo .list-ups li").eq(upIndex).addClass("active").siblings().removeClass("active");

            this.curUpsCommentaries = this.ups[upIndex].commentaries
            this.commentaryCount = this.ups[upIndex].count
        },
        //获取文章详情
        getArticle(){
            let self = this;
            let url = `${lib.apiUrl}/share/article.do`;
            let param = {
                articleId : articleId,
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : 1,
                token : "lucifer_test_token"
            }

            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",
                
                success:  (ret) => {
                    var ret = lib.formatHttpProtocol(ret);
                    
                    if (ret.result == 1){
                        var data = ret.data;
                        var article = data.article;

                        self.boxOffice = data.boxOffice;
                        self.article = article;
                        self.areas = article.areas;
                        self.categories = article.categories;
                        self.articleType = article.type;

                        if (data.photos) {
                            self.photos = data.photos;
                            self.images = data.photos.images;
                        }
                        if (article.watchOnline) {
                            self.watchOnline = article.watchOnline;
                        }

                        if (article.filmmakers) {
                            let directors = article.filmmakers.directors
                            let actors = article.filmmakers.actors

                            if(directors) self.filmmakers = self.filmmakers.concat(directors)
                            if(actors) self.filmmakers = self.filmmakers.concat(actors)
                        }

                        if(data.ups) {
                            self.ups = data.ups;
                            self.commentaryCount = data.ups[0].count;
                            self.curUpsCommentaries = data.ups[0].commentaries;
                        } else {
                            self.commentaryCount = data.commentaryCount;
                            self.curUpsCommentaries = data.moreCommentaries ? data.moreCommentaries : [];
                        }

                        if(data.trailers) {
                            self.trailers = data.trailers
                            self.firstTrailer = data.trailers.videos[0]
                        }

                        if (article.alias && article.alias.length>0){
                            self.alias = article.alias.slice(0,1);
                        }

                        lib.setTitle(`${lib.appName}-${article.name}`);
                        lib.setWxShare({
                            title: `《${article.name}》`, 
                            desc: `搜片源、看解说，快上5分钟看剧平台-${lib.appName}`, 
                            link: `${location.href}`, 
                            imgUrl: `${article.cover}`
                        });
                    } 
                }
            });
        }
    },
    filters : {
        parseReleaseDate(value){
            return lib.handleTime(value).substring(0,11)
        },
        parseBoxOffice(value){
            let result = 0;

            if(value > 1e7) {
                result = "全球票房"+(value/1e8).toFixed(1) + "亿元"
            } else{
                result = ''
            }

            return result;
        }
    }
}