var id;//商品ID

var toast = new auiToast();
var json = "";
/**
 * 根据ID获取商品信息
 */
function getData(id) {
    _vp.productDetail(
        {
            p_id: id,
        },
        function (res) {
            console.log(res.status == -1);
            if (res.status == 2001) {
                //需要登录
                top.location.href = "login.html";
                return;
            }
            if (res.status == -1) {
                toast.fail({
                    title: "系统繁忙",
                    duration: 2000
                });
                return;
            }
            // if(res.data.my_participant_state == 1){
            //     window.location.href = 'Opencountdown.html?id='+id;
            // }
            json = res.data;
            var joinedNum = res.data.need_number - res.data.participate_number;
            var percent = ((res.data.participate_number / res.data.need_number) * 100).toFixed(2);
            var percentOne = '期号：' + res.data.issue + ' <span>已完成 <em class="percentOne">' + percent + '%</em></span>';
            var allPeople = '总需人次 ' + res.data.need_number + ' <span>剩余人次 <em class="needPeople"> ' + joinedNum + '</em></span>';
            /*$('.cp_mian h2').text(res.data.product_name);*/
            $('.commdity-name').text(res.data.product_name);
            $('.f_bt').text(res.data.product_subheading);
            $('.singleprice').text(res.data.price);
            $('.timeNumber').html(percentOne);
            $("#productImg").attr('src', res.data.product_image);
            // $('.percentOne').text(percentOne);
            $('.percentTwo').css('width', percent + '%');
            $('.allPeople').html(allPeople);
            // $('.needPeople').text(allPeople);

            $("#content_div_b").show();
        },
        function (err) {
            console.log('请求失败' + err);
        })
};

/**
 * 点击立即夺宝
 */
function submit() {
    if (json != "") {
        if (json.need_number == json.participate_number) {
            toast.fail({
                title: "该宝贝已抢购完毕",
                duration: 1500
            });
            return;
        }
        window.location.href = 'confirm.html?state=1&id=' + id;
    }

    // _vp.participateInTheTreasure(
    //     {
    //         product_id: id,
    //     },
    //     function(res){
    //         if (res.status == 2001) {
    //             //需要登录
    //             top.location.href = "login.html";
    //             return;
    //         }else if(res.status == -1) {
    //             toast.fail({
    //                 title:"系统繁忙",
    //                 duration:2000
    //             });
    //             return;
    //         }
    //         window.location.href = 'confirm.html?state=1&id='+id;
    //         // window.location.href = 'Opencountdown.html?id='+id;
    //     }
    // )
}

/**
 * 返回首页
 */
function goIndex() {
    window.location.href = 'index.html';
}


$(function () {
    id = GetQueryString("id");
    $("#content_div_b").hide();
    getData(id);
});