var dialog = require('dialog');
var id = getParam('id')

var api = 'http://sy.duowan.cn'

var env = getParam('env')
if(env == 'dev') {
    api = 'http://test.sy.duowan.cn'
}else if(env == 'new') {
    api = 'http://new.sy.duowan.com'
}else {
    api = 'http://sy.duowan.cn'
}
console.log(api)

$(function(){
    //获取数据渲染
    render();

    //投票支持
    $('.g-support').on('click',function(){
        support(id);
    })
})






//渲染页面
function render () {
     request('GET',api+'/mxworld/article?_from=ajax&article_id='+id,'').then(function(result){
        var data = result.data;
        var imgUrl = data.img_url;
        var vote = data.vote_num;
        window.comment3Uniqid = data.uuid;
        $(".works").attr('src',imgUrl);
        $('.vote-num').text(vote);
        $.getScript('http://pub.dwstatic.com/common/js/articleComment.js?')
    })
   
}


//请求
function request (type,url,data) {
    return new Promise((resolve,reject)=>{
        var xhr = new XMLHttpRequest()
        xhr.withCredentials = true
        xhr.open(type,url,true);  
        var fd = data || "";
        xhr.send(fd);
        xhr.onreadystatechange = () => {
            if( xhr.readyState == 4){
                if (xhr.status == 200){
                    var result = JSON.parse(xhr.responseText);
                    if (processReject(result)){
                        resolve(result)
                    }else{
                        reject(result,xhr.status)
                    }
                }else{
                    reject(xhr.responseText,xhr.status)
                }
            }
        }
    })
}



//支持
function support (id) {
    $.ajax({
        url:api+"/mxworld/vote?_from=ajax&article_id="+id,
        dataType:"jsonp",
        success:function(result){
            if (checkLogin(result)){
                dialog({
                    title:"   ",
                    content:"投票成功",
                    skin:"popup-ui"
                }).showModal();
                var $vote = $('.vote-num')
                var voteNum = $vote.text()
                $vote.text(parseInt(voteNum)+1);
            } else{
                dialog({
                    title:"   ",
                    content:result.msg,
                    skin:"popup-ui"
                }).showModal();
            }
        },
        error:function(x,e,t) {
            console.log(x)
            console.log(e)
            console.log(t)
        }
    })
}

//处理请求异常
function processReject (result) {
    if(result.code != 0 ){
        dialog({
            title:"  ",
            content:result.msg,
            skin:"popup-ui"
        }).showModal()
        return false
    }
    
    return true
}

//是否登录
function checkLogin (result) {
    if(result.code == -7){
        dialog({
            title:"   ",
            content:"你未登录qq<br/>稍后为你跳转登录",
            skin:"popup-ui"
        }).showModal();
        var url = decodeURIComponent(result.data);
        var str = url.match(/\b(http.*)?(\?.*)?(\&.*)\b/)
        var redirectUrl = str[1] + '?redirect_url=' + location.href + str[3]
        $.ajax({
            url:redirectUrl,
            dataType:'jsonp',
            jsonp:"callback",
            success:function(result){
                setTimeout(function(){
                    location.href = result.data.oauth_url
                },3000)
            }
        })
        return false
    }
    if(result.code != 0 ){
        console.log(result.msg);
        return false
    }
    return true
}


function getParam(name) {
        //先获取#后面的参数
        var str = document.location.hash.substr(2);
        var value = getParam2(name, str);
        if (value == null) {
            str = document.location.search.substr(1);
            value = getParam2(name, str);
        }

        return value;
    };

    function getParam2(name, str) {
        //获取参数name的值
        var reg = new RegExp("(^|!|&|\\?)" + name + "=([^&]*)(&|$)");

        //再获取?后面的参数
        r = str.match(reg);
        if (r != null) {
            try {
                return decodeURIComponent(r[2]);
            } catch (e) {
                // console.log(e + "r[2]:" + r[2]);
                return null;
            }
        }
        return null;
    }