require('swiper/swiper-3.4.2.jquery.min.js');
var _ = require('underscore')


class FindGame {
    constructor() {
        this.$kaiceWrap = $('.swiper-container-kaice')
        this.$screenShotGame = $('.screenshot-game')
        this.kaice = [] //近期开测的数据

        this.kaiceTpl = __inline('../../tpl/newGameTest.tmpl')
        this.screenShotTpl = __inline('../../tpl/screenShot.tmpl')

        this.init()
    }

    init() {
        this.getKaiceData().setChange()
    }

    getKaiceData() {
        var $items = this.$kaiceWrap.find('.swiper-slide')
        var timeReg = /[0-9]+.[0-9]+/
        var self = this
        $items.each(function (index, item) {
            var $item = $(item),
                href = $item.children('a').attr('href').replace('com','cn'),
                imgUrl = $item.find('.gameList__img img').attr('src'),
                gameName = $item.find('.gameList__con>h3').text(),
                time = timeReg.exec($item.find('.gameList__con .has-marginBot').text())

            time = time && time[0]

            self.kaice.push({
                href: href,
                imgUrl: imgUrl,
                gameName: gameName,
                time: time
            })
        })

        this.$kaiceWrap.children().html(this.kaiceTpl({
            data: this.kaice
        })).css('visibility', 'visible')

        return this
    }

    setChange() {
        var changeBtn = this.$screenShotGame.find("#changeOthers")
        var self = this
        changeBtn.click(function (e) {
            e.preventDefault()

            self.getScreenShotData()
        })

        changeBtn.click()

        return this
    }

    getScreenShotData() {
        var self = this
        $.ajax({
            type: 'GET',
            url: 'http://ku.newgame.duowan.com/index.php?type=jsonp&r=api/Randpicbykw&keyword=截图&limit=4',
            dataType: 'jsonp',
            success: function (ret) {
                if (ret instanceof Array && ret.length != 0) {
                    self.$screenShotGame.children('ul').html(self.screenShotTpl({
                        data: ret
                    }))
                } else {
                    console.log('返回结果错误')
                }
            },
            error: function (err) {
                console.log(err)
            }
        })

        return this
    }
}

$(() => {

    new FindGame

    var swiperH = new Swiper('.swiper-container-lunbo', {
        // pagination: '.swiper-pagination-h',
        // paginationClickable: true,
        // spaceBetween: 0
    });
    var swiper = new Swiper('.swiper-container-kaice', {
        slidesPerView: 4.6,
        paginationClickable: true,
        spaceBetween: 20,
        freeMode: true
    });


})


