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 area = lib.getParam("area")
var timeType = lib.getParam("timeType") * 1
var weekName = ["日","一","二","三","四","五","六"]
var cacheArticle = {}

let timeStamp = (new Date()).getTime()
let lastDay = lib.handleTime(timeStamp-24*60*60*1000,2).substring(0,10)
let curDay = lib.handleTime(timeStamp,2).substring(0,10)
let curMonth = lib.handleTime(timeStamp).substring(0,8)

export default {
    activated() {
    },
    methods: {
        toVideoPreview(articleId){
            WjySdk.toVideoPreview(articleId)
        },
        toCommentaryDetail(commentaryId){
            WjySdk.toCommentaryDetail(commentaryId)
        },
        toVideoDetail(articleId){
            if (articleId==0){
                lib.showTip("暂无解说视频")
            } else {
                WjySdk.toVideoDetail(articleId)
            }
        },
        changeNav(area,timeType,type=false){ //type:true/false=>地区切换/时间切换

            if(this.isloading) return
            if(type) { 
                this.area = area

                switch(area){
                    case "CN" : 
                        this.timeType = 1
                        break;
                    case "USA" : 
                        this.timeType = 1
                        break;
                    case "WORLD" : 
                        this.timeType = 5    
                        break;
                }
            } else {
                this.timeType = timeType
            }
            this.nextFlag = 1
            this.timeline = 0
            this.articles = []

            this.getBoxOfficeList()
            this.handleUpdataTip()
            lib.setParam("area",this.area)
            lib.setParam("timeType",this.timeType)

            $("html,body").scrollTop(0)
        },
        handleUpdataTip(){
            let areaName = {
                "CN" : "中国",
                "USA" : "美国",
                "WORLD" : "全球"
            }

            switch(this.timeType){
                case 1 : 
                    if(this.area=="USA") { //北美当日数据比中国晚一天
                        this.updataTip = `${lastDay} 当日票房（每日更新）`
                    } else {
                        this.updataTip = `${curDay} 当日票房（每小时更新）`
                    }
                    break;
                case 3 : 
                    this.updataTip = `${curMonth} 票房（每日更新）` 
                    break;
                default :
                    this.updataTip = `${areaName[this.area]}影史总票房（每日更新）` 
                    break;
            }
        },
        getBoxOfficeList(){
            let self = this
            let url = `${lib.apiUrl}/functionalTopic/boxOfficeList.do`
           
            let param = {
                area : self.area,
                timeType : self.timeType,
                count : 20,
                timeline : self.timeline,
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : 1,
                token : "lucifer_test_token"
            }

            //取缓存数据
            if(cacheArticle[`${self.area}${self.timeType}`]) {
                self.nextFlag = 0
                self.articles = cacheArticle[`${self.area}${self.timeType}`]
                return
            }

            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 && ret.data.boxOffices) {
                    let data = ret.data
                    
                    self.nextFlag = data.next
                    self.timeline = data.timeline
                    self.articles = self.articles.concat(data.boxOffices)

                    //缓存实时和月榜数据
                    if(self.timeType==1||self.timeType==3) cacheArticle[`${self.area}${self.timeType}`]=self.articles
                } else {
                    self.nextFlag = 0
                    self.timeline = 0
                }
            })
        }
    },
    data() {
        return {
            articles : [],              //列表
            area : area || "CN",        //地区
            timeType : timeType || 1,   //时间类型
            timeline : 0,               //翻页标志
            isloading : false,          //是否loading
            nextFlag : 1,               //下一页标志
            updataTip : ``  //更新提示
        }
    },
    mounted() {
        lib.setTitle("票房榜")
        this.handleUpdataTip()
        this.getBoxOfficeList()
    },
    filters : {
        parseBoxOffice : function(value){
            let result = 0;
            
            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}天`
            }
        }
    }
}