import Vue from 'vue'
import lib from 'lib'
import _ from 'underscore'
import WjySdk from 'extSdk';

var area = lib.getParam("area")
var timeType = lib.getParam("timeType")
var weekName = ["日","一","二","三","四","五","六"]

export default {
    activated() {
    },
    methods: {
        toVideoPreview(articleId){
            WjySdk.toVideoPreview(articleId)
        },
        toCommentaryDetail(commentaryId){
            WjySdk.toCommentaryDetail(commentaryId)
        },
        toVideoDetail(articleId){
            WjySdk.toVideoDetail(articleId)
        },
        changeNav(area,timeType){

            this.area = area
            this.timeline = 0
            this.timeType = timeType
            this.articles = []

            this.getShowtimes()
            lib.setParam("area",area)
            lib.setParam("timeType",timeType)

            $("html,body").scrollTop(0)
        },
        getShowtimes(){
            let self = this
            let url = `${lib.apiUrl}/functionalTopic/showtimes.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"
            }


            self.isloading = true
            
            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",

                success: function (ret) {
                    ret = lib.formatHttpProtocol(ret);

                    lib.hideLoading()
                    self.isloading = false
                    if(ret.code == 0) {
                        let data = ret.data
                        
                        self.nextFlag = data.next
                        self.timeline = data.timeline
                        self.articles = self.articles.concat(data.articles)
                        if (self.timeType==1) {
                            self.monthList = self.parseMonthList(self.articles)
                        }
                    } else {
                        lib.showDialog(ret.msg)
                    }
                }
            })
        },
        parseMonthList(data){
            let self = this,list = {}

            let monthGroud = _.groupBy(data,(article) => {
                let date = new Date(article.releaseDate)
                return date.getFullYear()+""+self.parseTime(date.getMonth()+1)
            });

            _.each(monthGroud,(monthData,key)=>{
                let dayGroud = self.parseDayList(monthData)
                list[key] = dayGroud
            })
            return list
        },
        parseDayList(data){
            let self = this,list = {}
            let dayList = _.groupBy(data,function(article){
                
                let date = new Date(article.releaseDate)
                let time = date.getFullYear()+""+self.parseTime(date.getMonth()+1)+self.parseTime(date.getDate())+date.getDay()

                return time
            });

            return dayList
        },
        parseTime : function(value){
            return value >= 10 ? value : "0"+value
        }
    },
    data() {
        return {
            articles : [],              //视频列表
            area : area || "CN",        //地区
            timeType : timeType || 0,   //时间
            monthList : [],             //月电影列表
            timeline : 0,               //翻页标志
            isloading : false,          //是否loading
            nextFlag : 1                //下一页标志
        }
    },
    mounted() {

        lib.setTitle("电影上映表")
        this.getShowtimes()
    },
    filters : {
        parseMonth : function(value){
            return value.slice(0,4) + "." + value.slice(4,6);
        },
        parseDay : function(value){
            let date = value.slice(6,8)
            let day = value.slice(8,10)

            return date+"日 星期"+weekName[day]
        },
        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}天`
            } else {
                return `即将上映`
            }
        }
    }
}