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

var type = lib.getParam("type")

var today = new Date(),curYear = today.getFullYear(),curMonth = today.getMonth()+1,curDate = today.getDate()
var weekName = ["日","一","二","三","四","五","六"]
var list4NowCache = [] 

export default {
    activated() {
    },
    methods: {
        toggleCalendar(){
            $(".blueray-hd").toggleClass("active");
        },
        changeNav(type){
            let self = this

            self.timeType = type
        },
        changeMonth(value){ 
            let self = this
            let today = new Date();

            self.month = value
            self.timeline = 0

            if (curMonth == value && curYear==self.year) { //判断是否本月,取缓存数据
                self.articles = list4NowCache
                self.nextFlag = 0
        
                self.dateList = self.parseDayList(self.mergeFilm(self.articles))
                self.initPic()

                Vue.nextTick(function(){
                    self.scrollToCurDay()
                })

            } else {
                self.articles = []
                self.dateList = []
                self.getBluRayList()
            }
            
            Vue.nextTick(function(){
                self.toggleCalendar()
            })
        },
        changeYear(flag){ 
            let self = this
            
            self.articles = []
            self.dateList = []
            self.month = 1
            self.year = parseInt(this.year) + 1*flag

            self.getBluRayCalendar(self.year)
            self.getBluRayList()
        },
        toVideoDetail(articleId){
            WjySdk.toVideoDetail(articleId)
        },
        getBluRayCalendar(year){
            let self = this
            let url = `${lib.apiUrl}/functionalTopic/bluRayCalendar.do`
            let param = {
                year : year,
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : 1,
                token : "lucifer_test_token"
            }

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

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

                    if(ret.code == 0) {
                        let list = ret.data.list
                        self.calendarList = []
                        _.each(list,function(item){
                            let date = new Date(item.time)
                            self.calendarList.push({
                                month : date.getMonth()+1,
                                count : item.count
                            })
                        })
                    } else {
                        lib.showDialog(ret.msg)
                    }
                }
            });
        },
        //当月全部电影列表
        bluRayList4Now(){
            let self = this
            let url = `${lib.apiUrl}/functionalTopic/bluRayList4Now.do`
            let param = {
                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);

                    self.isloading = false
                    if(ret.code == 0) {
                        let data = ret.data

                        if(data) {
                            list4NowCache = data.articles
                            self.articles = data.articles
                            self.timeline = 0
                            self.nextFlag = 0
                        }
                    
                        self.dateList = self.parseDayList(self.mergeFilm(self.articles))
                        self.initPic()

                        Vue.nextTick(function(){
                            self.scrollToCurDay()
                        })
                    } else {
                        lib.showDialog(ret.msg)
                    }
                }
            })
        },
        getBluRayList(){
            let self = this
            let url = `${lib.apiUrl}/functionalTopic/bluRayList.do`
            let param = {
                year : self.year,
                month : self.month,
                count : 10,
                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);

                    self.isloading = false
                    if(ret.code == 0) {
                        let data = ret.data

                        if(data) {
                            self.articles = self.articles.concat(data.articles)
                            self.timeline = data.timeline
                            self.nextFlag = data.next
                        } else {
                            self.nextFlag = false
                        }
                    
                        self.dateList = self.parseDayList(self.mergeFilm(self.articles))
                        self.initPic()
                    } else {
                        lib.showDialog(ret.msg)
                    }
                }
            })
        },
        parseDayList(data){
            let self = this,mergeArr=[]
            let dayList = _.groupBy(data,function(article){
                
                let date = new Date(article.time)
                let time = date.getFullYear()+""+self.parseTime(date.getMonth()+1)+self.parseTime(date.getDate())+date.getDay()

                return time
            });

            return dayList
        },
        //合并id+时间戳相同的电影
        mergeFilm(data){
            let self = this,arr={}

            _.each(data,function(item1){
                item1.typelist = [item1.type]
                _.each(data,function(item2){
                    if (item1.aid==item2.aid && item1.time==item2.time) {
                        if(item1.typelist.indexOf(item2.type)<=-1){
                            item1.typelist.push(item2.type)
                        }
                    }
                })

                if(!arr[item1.aid]) arr[item1.aid] = item1
            })

            return (_.toArray(arr))
        },
        parseTime(value){
            return value >= 10 ? value : "0"+value
        },
        initScroll : function(){
            let self = this
            let scrollTop=$(window).scrollTop(),itemTop = [];
            let len = $(".blueray-bd .item-date").length;
            let nextTop = 0,text;
            let $bluerayHd = $(".blueray-hd")
    
            $(".blueray-bd .item-date").each(function(key){
                itemTop.push($(this).offset().top-100);
            })
    
            if(scrollTop>20) $bluerayHd.addClass("is-fixed")
            else $bluerayHd.removeClass("is-fixed")
    
            for(var i=0; i<len; i++){
                nextTop = itemTop[i+1];

                if( itemTop[i] <= scrollTop && scrollTop <= nextTop){
                    text = $(".blueray-bd .item-date").eq(i).find(".title-date").text()
                } else if(scrollTop >= itemTop[len-1]){
                    text = $(".blueray-bd .item-date").eq(len-1).find(".title-date").text()
                }

                self.curShowDate = text
            }
        },
        scrollToCurDay(){
            let self = this
            let curTime = curYear+""+self.parseTime(curMonth)+""+self.parseTime(curDate)
            let dateArr = [],curIndex=0,itemTop=[]
            let height = $(".blueray-bd .title-date").height()

            $(".blueray-bd .item-date").each(function(key){
                itemTop.push($(this).offset().top-100);
            })

            _.each(self.dateList,function(item,key){
                dateArr.push(key.substring(0,8))
            })

            for(let i=0;i<dateArr.length; i++) {
                if(dateArr[i] == curTime) {
                    curIndex = i
                    break
                } else if(dateArr[i] > curTime){
                    curIndex = i>0 ? i-1 : 0
                    break
                }
            }
            
            $("html,body").animate({
                scrollTop : itemTop[curIndex]+60
            })
        },
        initPic() {
            Vue.nextTick(function(){
                $(".page-zt-blueray img").lazyload({
                    failurelimit : 5,
                    skip_invisible : false,
                    effect  : "fadeIn"
                }) 
            }) 
        }
    },
    data() {
        return {
            articles : [],
            showDate : "-- 周-",
            showNum : "-",
            calendarList : [],
            timeline : 0,
            isloading : false,
            nextFlag : 1,
            dateList : [],
            year : "--",
            month : "--",
            curShowDate : "",
            timeType : 0
        }
    },
    mounted() {

        lib.setTitle("蓝光发售表")

        this.year = curYear,this.month = curMonth

        this.getBluRayCalendar(this.year)
        this.bluRayList4Now()
    },
    filters : {
        parseDay : function(value){
            let weekName = ["日","一","二","三","四","五","六"]
            let date = value.slice(6,8)
            let day = value.slice(8,10)

            return date+"日 星期"+weekName[day]
        }
    }
}