import Vue from 'vue'
import axios from 'axios'
import axiosJsonp from 'axios-jsonp';
import lib from 'lib'
import _ from 'underscore'
import WjySdk from 'extSdk';

var type = lib.getParam("type"),
    boxAId = lib.getParam("id"),
    weekName = ["日","一","二","三","四","五","六"],
    areaName = {
        "1" : "中国",
        "2" : "美国",
        "3" : "美国",
        "4" : "美国",
        "5" : "全球"
    },
    cacheArticle = {}, //缓存榜单列表
    timeStamp = (new Date()).getTime(), 
    lastDay = lib.handleTime(timeStamp-24*60*60*1000,2).substring(0,10),
    curDay = lib.handleTime(timeStamp,2).substring(0,10),
    curMonth = lib.handleTime(timeStamp).substring(0,8),
    curFilmReleaseDate = 0; //当前上映时间

export default {
    activated() {
    },
    methods: {
        toCommentary(commentaryId,articleId){
            if(commentaryId) {
                WjySdk.toCommentaryDetail(commentaryId)
            } else if(articleId) {
                WjySdk.toVideoDetail(articleId)
            }
        },
        onScroll(){
            if(this.detailIsloading || this.detailNextFlag==0) return

            let boxScroller = this.$refs["filmDetailBoxScroller"],
                boxScrollerList = this.$refs["filmDetailBoxList"],
                scrollTop = boxScroller.scrollTop,
                boxScrollerHeight = boxScroller.clientHeight,
                boxScrollerListHeight = boxScrollerList.clientHeight;

            if(boxScrollerListHeight <= scrollTop+boxScrollerHeight+40) {
                this.getBoxOfficeDetails()
            }
        },
        //获取中国票房电影的详情页
        getArticleProDetail(){
            let self = this
            let url = `${lib.apiUrl}/functionalTopic/boxOfficeArticleProDetail.do`
 
            let param = {
                id : boxAId,
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : 1,
                token : "lucifer_test_token"
            }

            self.isloading = true
            axios({
                url: url,
                params : param,
                adapter: axiosJsonp,
                callbackParamName: 'c' // optional, 'callback' by default
            }).then((res) => {
                self.isloading = false

                let ret = lib.formatHttpProtocol(res.data);
                if(ret.code == 0) {
                    self.detailFilm = ret.data
                    self.getBoxOfficeDetails()

                    curFilmReleaseDate = ret.data.releaseDate

                    lib.setTitle(`${self.detailFilm.name}`)
                }
            })
        },
        //根据票房电影的id获取相关票房记录
        getBoxOfficeDetails(){
            if(this.detailIsloading || this.detailNextFlag==0) return

            let self = this
            let url = `${lib.apiUrl}/functionalTopic/getBoxOfficeDetails.do`
 
            let param = {
                id : boxAId,
                count : 20,
                timeline : self.detailTimeline,
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : 1,
                token : "lucifer_test_token"
            }

            self.detailIsloading = true
            axios({
                url: url,
                params : param,
                adapter: axiosJsonp,
                callbackParamName: 'c' // optional, 'callback' by default
            }).then((res) => {
                self.detailIsloading = false

                let ret = lib.formatHttpProtocol(res.data);
                let data = ret.data

                if(ret.code == 0 && data) {
                    self.detailNextFlag = data.next
                    self.detailTimeline = data.timeline
                    self.detailArticles = data.boxOfficeProArticleList.concat(self.detailArticles)
                } else {
                    self.detailNextFlag = 0
                    self.detailTimeline = 0
                }
            })
        },
    },
    data() {
        return {
            articles : [],              //列表
            curDay : curDay.replace(/-/g,"."),
            curBoxAId : 0,               //票房详情-id
            detailFilm : {},             //票房详情-电影
            detailArticles : [],         //票房详情-票房列表
            detailTimeline : 0,          //票房详情-翻页标志
            detailIsloading : false,     //票房详情-是否loading
            detailNextFlag : 1,          //票房详情-下一页标志
        }
    },
    mounted() {
        this.getArticleProDetail()
    },
    filters : {
        parseBoxOffice : function(value){
            let result = 0;
            
            if(!value) return "--"
            if(value > 1e8) {
                let num = (value/1e8).toFixed(2)
                result = `${num.substring(0,num.lastIndexOf('.')+2)}亿`
            }else if(value > 1e4){
                let num = Math.floor(value/1e4)
                result = `${num}万`
            } else {
                let num = (value/1e4).toFixed(3)
                result = `${num.substring(0,num.lastIndexOf('.')+2)}万`
            }

            return result;
        },
        parseReleaseDate : function(value){
            let now = new Date()
            let nowTime = now.getTime() 
            let restTime = (nowTime - value) /1000
            let restYear = Math.ceil(restTime / (365 * 60 * 60 * 24))
            let restDay = Math.ceil(restTime / (60 * 60 * 24))

            if(restDay>365){
                return `${restYear}年`
            }else if(restDay>=0){
                return `${restDay}天`
            }
        },
        parseDate : function(value){
            return lib.handleTime(value,2).substring(0,10)
        },
        parseUpdateTime : function(value){
            return lib.handleTime(value).substring(11,17);
        },
        parseReleaseDateStr : function(value){
            let curDate = new Date(value),
                restTime = (value -curFilmReleaseDate) /1000,
                restYear = Math.ceil(restTime / (365 * 60 * 60 * 24)),
                restDay = Math.ceil(restTime / (60 * 60 * 24))+1,
                restTimeStr = "";

            if(restDay>365){
                restTimeStr = `上映第${restYear}年`
            }else if(restDay>0){
                restTimeStr = `上映第${restDay}天`
            }else{
                restTimeStr = `预售`;
            }

            return `周${weekName[curDate.getDay()]} ${restTimeStr}`
        }
    }
}