﻿//window.onerror = function(){return true;}
//function $(id){return document.getElementById(id);}  
//function getHeight(){$("fahuo").style.height=$("forml").offsetHeight-85+"px";}  
//window.onload = function(){getHeight();}  
/*///////////////////////////////////////// ORDERJSFGX /////////////////////////////////////////*/

var time = 5;
var inter_num = 0;
function check_order() {

    var form = /^1[3,4,5,6,7,8]\d{9}$/;
    var tel = $("input[name=tel]").val();
    if (!form.test(tel)) {
        common.showMsg('手机号码不正确，请重新填写！', time);
        document.not3check.tel.focus();
    }
    

    $.ajax({
        url: '/index.php?s=/Api/Order/check_order',
        data: {tel: tel},
        type: 'post',
        dataType: "json",
        beforeSend: function () {
            common.shadeClose('数据处理中...', false);
        },
        success: function (result) {
            // console.log(result);
            common.close();
            if (result.status == 1) {

                var html = template.render("checkresult", result);
                // console.log(html);
                $("#notcheckresult").html(html);
            } else {
                common.showMsg(result.info);
            }
        },
        error: function () {
            common.showMsg('请检查您的网络！');
        }

    });

}
// 加载积分信息
function load_point() {
    $.ajax({
        url:'/index.php?m=Api&c=User&a=getUserIntegral',
        type:"post",
        data:{},
        dataType:'json',
        beforeSend:function(){
            common.shadeClose('数据加载中...',false);
        },
        success:function(result){
            common.close();
            if(result.status == 1){
                const data = result.data;
                inter_num = data.available;
                // console.log('请求inter_num', inter_num)
            }
        }
    });
}
function postcheck() {
    var time = 5;
    if (allow_buy == false) {
        var name = "";

        $("#goods_specs .spec_div").each(function (i) {
            var selected = $(this).find("li.mouon");
            //console.log($(this).find("li.mouon"));
            var display = $(this).find("li.mouon").css('display');
            if ((selected.length <= 0 && name == "") || display == 'none') {
                name = $(this).attr('name');
                return;
            }
        });
        common.showMsg("请选择" + name + "!", time);
        return;
    }
    if (is_has_gift == 1) {
        // console.log('111');
        // var product_gift = $('input[name=gift_product]:checked').length;
        // if(product_gift < 1){
        //   common.showMsg("请选择活动产品!",time);
        // return;
        // }
        var gift_type = $('input[name=gift_type]').val();
        if (gift_allow_buy == false && gift_type == 1) {


            var name = "";

            $("#gift_goods_specs .spec_div").each(function (i) {
                var selected = $(this).find("li.mouon");

                var display = $(this).find("li.mouon").css('display');
                if ((selected.length <= 0 && name == "") || display == 'none') {
                    name = $(this).attr('name');
                    return;
                }

            });
            common.showMsg("请选择活动产品" + name + "!", time);
            return;
        }
    }
    console.log('inter_num',inter_num);
    console.log('integral',integral);
    // if(parseInt(inter_num) > parseInt(integral)) {
        $("input[name=product_id]").val(product_id);
        try {
            if (document.form.name.value == "") {
                common.showMsg("请填写姓名！", time);
                document.form.name.focus();
                return false;
            }
            var name = /^[\u4e00-\u9fa5]{2,6}$/;
            if (!name.test(document.form.name.value)) {
                common.showMsg('姓名格式不正确，请重新填写！', time);
                document.form.name.focus();
                return false;
            }
        } catch (ex) {}
        try {
            if (document.form.mob.value == "") {
                common.showMsg('请填写手机号码！', time);
    
                document.form.mob.focus();
                return false;
            }
            var form = /^1[3,4,5,6,7,8,9]\d{9}$/;
            if (!form.test(document.form.mob.value)) {
                common.showMsg('手机号码格式不正确，请重新填写！', time);
    
                document.form.mob.focus();
                return false;
            }
        } catch (ex) {
        }
        try {
            if (document.form.province.value == "") {
                common.showMsg('请选择所在省！', time);
    
                document.form.province.focus();
                return false;
            }
        } catch (ex) {
        }
        try {
            if (document.form.city.value == "") {
                common.showMsg('请选择所在市！', time);
    
                document.form.city.focus();
                return false;
            }
        } catch (ex) {
        }
        try {
            if (document.form.area.value == "") {
                common.showMsg('请选择所在区！', time);
    
                document.form.area.focus();
                return false;
            }
        } catch (ex) {
        }
        try {
            if (document.form.address.value == "") {
                common.showMsg('请填写详细地址！', time);
    
                document.form.address.focus();
                return false;
            }
        } catch (ex) {
        }
        var form_data = $("#form").serialize();
         deal_field.set_field('form_data',form_data);
        var order_id = document.form.orderid.value;
        var tel = document.form.mob.value;
        var max_shop_price = $('input[name=max_shop_price]').val();
        document.form.name.value = '';
        document.form.mob.value = '';
        $.ajax({
            url: $('#form').attr('action'),
            data: form_data,
            type: 'post',
            dataType: "json",
            beforeSend: function () {
                // common.shadeClose('订单提交中...', false);
            },
            success: function (result) {

                if (result.status == 1) {
                    console.log('result', result)
                     var is_weixin = (function(){return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1})();
                        if(!is_weixin) {
                             window.location.href = '/index.php?s=/Home/User/pay_detail&orderId=' + result.id + '&tel=' + tel + '&maxShop=' + max_shop_price;
                        } else {
                            // setTimeout(function(){
                                
                            //      window.location.href = 'http://show.w8.mufengyue.com/index.php?s=/Home/User/oauth2_pay&orderId=' + result.id + '&tel=' + tel + '&maxShop=' + max_shop_price + '&referrer_url=' + encodeURIComponent(window.location.href) + '&openid='+openid;

                            // },200);
                             window.location.href = '/index.php?s=/Home/User/pay_detail&orderId=' + result.id + '&tel=' + tel + '&maxShop=' + max_shop_price;
                        }
                    // window.location.href = '/index.php?s=/Home/User/pay_detail&orderId=' + result.id + '&tel=' + tel + '&maxShop=' + max_shop_price;
                    // window.location.href = '/Application/Home/View/default/PayDetail1/index.html?orderId=' + result.id + '&tel=' + tel;
                    // common.show_msg({
                    //     btns: 1,
                    //     btn: ['确定'],
                    //     content: "恭喜你，订单提交成功。<br/>订单号为：" + order_id,
                    //     yes: function () {
                    //         common.close();
                    //
                    //     },
                    //     no: function () {
                    //         common.close();
                    //     }
                    // });
                    
                    // common.showMsg('订单提交成功！',15);
    
                    
                } else {
                    common.showMsg(result.msg);
                }
            },
            error: function () {
                common.showMsg('请检查您的网络！');
            }
    
        });
    // } else {
    //     common.showMsg('请选择规格或是积分不足', 3)
    // }
    //return true;
}

try {
    new PCAS("province", "city", "area");
} catch (ex) {
}
try {
    var thissrc = document.getElementById("yzm").src;

    function refreshCode() {
        document.getElementById("yzm").src = thissrc + "?" + Math.random();
    }
} catch (ex) {
}

function add_class(obj) {
    allow_buy = false;
    product_id = false;
    if ($(obj).hasClass("mouon") || $(obj).hasClass("disabled")) {
        return;
    } else {
        $(obj).siblings().removeClass('mouon');
        $(obj).addClass("mouon");
    }
    deal_spec(obj);
    deal_is_buy();
    //deal_color_img(obj);
}


// function deal_color_img(obj){
//   var data_name = $(obj).attr('data-name');
//   var data_value = $(obj).attr('data-value');
//   console.log(data_name);

//   console.log(data_value);
// }
function deal_is_buy() {
    /* 开始更改状态 */
    var product_json = product_arr;
    var symbol = '￥';
    var specs_array = new Array();
    var regexp = '';
    $("#goods_specs .spec_div").each(function (i) {
        var selected = $(this).find("li.mouon");

        if (selected.length > 0)
            specs_array[i] = selected.attr("data-id") + ':' + selected.attr("data-value");
        else
            specs_array[i] = "\\\d+:\.+";
    });
    console.log('specs_array',specs_array);

    var specKey = '';
    $('#goods_specs .spec_div').each(function () {
        spec_id = $(this).find('li.mouon').attr('data-id');
        spec_name = $(this).find('li.mouon').attr('data-name');
        spec_value = $(this).find('li.mouon').attr('data-value');
        //选择值 加入数组
        if ($(this).find('li.mouon').length === 0) {
            return false;
        } else {
            specKey += spec_id + ':' + spec_value + ';';
        }
    });


    if ($('#goods_specs .spec_div').length == $('#goods_specs .spec_div li.mouon').length) {
        console.log(specKey);
        console.log('product_json',product_json);
         console.log('product_json_specKey',product_json[specKey]);
        if (product_json[specKey] != undefined) {

            var item = product_json[specKey];
            // console.log(item);
            // if (item.goods_number <= 0) {

            //     //  seajs.use("layer",function(k){
            //     //   k.open({
            //     //     content: '此产品库存不足',
            //     //     style: 'background-color:rgba(255,255,255,1);  color:#000; border:none;text-align:center;',
            //     //     time: 1
            //     //   });
            //     // });
            //     return;
            // }
            product_info = item;

            // alert(item.products_sn);
            // $("#product_sn_info").html("型号："+item.products_sn);


            if (item.icon) {
                var imgObj = $("#thumb");
                $(imgObj).attr("pre_src", $(imgObj).attr("src"));
                $(imgObj).attr("src", item.icon);

            }
            product_id = item.id;

            allow_buy = true;
            $("input[name=goods_price]").val(item.shop_price);
            $(".price").text('￥' + item.shop_price);
            // $("#integral3").text(item.integral);
            $("#integral,#integral3").text(item.integral);
            integral = item.integral;
            price_count();


            // console.log(allow_buy);
            //   console.log(product_id);

        } else {
            // seajs.use("layer",function(k){
            //   k.open({
            //     content: '此产品库存不足',
            //     style: 'background-color:rgba(255,255,255,1);  color:#000; border:none;text-align:center;',
            //     time: 1
            //   });
            // });

            $(this).removeClass('active');
            $(this).removeClass('selected');
            $(this).css('display', "none");
            //alert(allow_buy);
        }
    }

}

//处理规格的显示问题
function deal_spec(obj) {
    // console.log(product_arr);
    var product_array = product_arr;
    var spec_key_has_array = new Array();
    var index = 0;//当前选中的规格顺序
    var prevall_count = 0;
    var prevall_li_count = 0;
    if ($(obj).hasClass('mouon')) {
        spec_key_has_array[0] = $(obj).attr("data-id") + ':' + $(obj).attr("data-value");
        index = $(obj).attr("data-index");
    }
    $(obj).parents('.spec_div').prevAll().each(function (k, item) {
        // console.log(item);
        var selected = $(item).find(".mouon");
        if (selected.length > 0) {
            spec_key_has_array[k + 1] = selected.attr("data-id") + ':' + selected.attr("data-value");
            prevall_li_count++;
        }
        prevall_count++;
    });


    spec_key_has_array.reverse();
    // spec_key_has_array.push(';');
    // console.log(spec_key_has_array.join(';'));
    var spec_key_has = spec_key_has_array.join(';');
    spec_key_has = spec_key_has + ";";


    // console.log(prevall_li_count);
    // console.log(prevall_count);
    if (prevall_li_count == prevall_count) {

        $(obj).parents('.spec_div').nextAll().find('li').css('display', 'none');
    }


    var spec_index = parseInt(index) + 1;
    // console.log(spec_index);
    // console.log(product_array);
    for (gi in product_array) {
        // console.log(gi);
        var item = product_array[gi];
        var reg = new RegExp(spec_key_has);
        // console.log(reg);
        var flage = reg.test(gi);
        // console.log(flage);
        if (flage) {

            var gi_array = gi.split(';');
            // console.log(gi_array);
            // console.log(spec_index);
            for (var j = spec_index; j < gi_array.length; j++) {
                var spec = gi_array[j];
                // console.log(spec);
                if (spec && spec != '') {

                    var spec_content_array = spec.split(':');
                    // console.log(spec_content_array);
                    $('#spec_content .spec_div li[data-id="' + spec_content_array[0] + '"][data-value="' + spec_content_array[1] + '"]').css('display', 'block');
                    $('#spec_content .spec_div li[data-id="' + spec_content_array[0] + '"][data-value="' + spec_content_array[1] + '"]').removeClass('disabled');
                    if (spec_index == j) {
                        if (item.goods_number <= 0) {
                            $('#spec_content .spec_div li[data-id="' + spec_content_array[0] + '"][data-value="' + spec_content_array[1] + '"]').addClass('disabled');
                        }
                    }


                }

            }

            if ($(obj).attr("data-name").indexOf('颜色') > -1) {

                if (item.icon) {
                    var type = $(obj).attr('type');
                    var product_name = $(obj).attr('data-value');
                    var imgObj = $("#thumb");
                    if (type == 'gift') {
                        imgObj = $("#gift_thumb");
                    } else {
                        $("#goods_product_name").text("-" + product_name);
                    }

                    $(imgObj).attr("pre_src", $(imgObj).attr("src"));
                    $(imgObj).attr("src", item.icon);

                }
            }

        }


    }
}


function gift_add_class(obj) {
    gift_allow_buy = false;
    gift_product_id = false;
    if ($(obj).hasClass("mouon") || $(obj).hasClass("disabled")) {
        return;
    } else {
        $(obj).siblings().removeClass('mouon');
        $(obj).addClass("mouon");
    }
    gift_deal_spec(obj);
    gift_deal_is_buy();
    //deal_color_img(obj);
}

function gift_deal_is_buy() {
    /* 开始更改状态 */
    var product_json = product_arr;
    var symbol = '￥';
    var specs_array = new Array();
    var regexp = '';
    $("#gift_goods_specs .spec_div").each(function (i) {
        var selected = $(this).find("li.mouon");

        if (selected.length > 0)
            specs_array[i] = selected.attr("data-id") + ':' + selected.attr("data-value");
        else
            specs_array[i] = "\\\d+:\.+";
    });


    var specKey = '';
    $('#gift_goods_specs .spec_div').each(function () {
        spec_id = $(this).find('li.mouon').attr('data-id');
        spec_name = $(this).find('li.mouon').attr('data-name');
        spec_value = $(this).find('li.mouon').attr('data-value');
        //选择值 加入数组
        if ($(this).find('li.mouon').length === 0) {
            return false;
        } else {
            specKey += spec_id + ':' + spec_value + ';';
        }
    });


    if ($('#gift_goods_specs .spec_div').length == $('#gift_goods_specs .spec_div li.mouon').length) {
        // console.log(specKey);
        if (product_json[specKey] != undefined) {

            var item = product_json[specKey];
            // console.log(item);
            if (item.goods_number <= 0) {

                //  seajs.use("layer",function(k){
                //   k.open({
                //     content: '此产品库存不足',
                //     style: 'background-color:rgba(255,255,255,1);  color:#000; border:none;text-align:center;',
                //     time: 1
                //   });
                // });
                return;
            }
            product_info = item;

            // alert(item.products_sn);
            // $("#product_sn_info").html("型号："+item.products_sn);


            if (item.icon) {
                var imgObj = $("#thumb");
                $(imgObj).attr("pre_src", $(imgObj).attr("src"));
                $(imgObj).attr("src", item.icon);

            }
            gift_product_id = item.id;
            $('input[name=gift_product_id]').val(gift_product_id);

            gift_allow_buy = true;
            // console.log(gift_product_id+gift_allow_buy);

            // $(".shop_price_input").val(item.shop_price);
            // $(".shop_price").html("￥"+item.shop_price);


            // console.log(allow_buy);
            //   console.log(product_id);

        } else {
            // seajs.use("layer",function(k){
            //   k.open({
            //     content: '此产品库存不足',
            //     style: 'background-color:rgba(255,255,255,1);  color:#000; border:none;text-align:center;',
            //     time: 1
            //   });
            // });

            $(this).removeClass('active');
            $(this).removeClass('selected');
            $(this).css('display', "none");
            //alert(allow_buy);
        }
    }

}

//处理规格的显示问题
function gift_deal_spec(obj) {
    // console.log(product_arr);
    var product_array = product_arr;
    var spec_key_has_array = new Array();
    var index = 0;//当前选中的规格顺序
    var prevall_count = 0;
    var prevall_li_count = 0;
    if ($(obj).hasClass('mouon')) {
        spec_key_has_array[0] = $(obj).attr("data-id") + ':' + $(obj).attr("data-value");
        index = $(obj).attr("data-index");
    }
    $(obj).parents('.spec_div').prevAll().each(function (k, item) {
        // console.log(item);
        var selected = $(item).find(".mouon");
        if (selected.length > 0) {
            spec_key_has_array[k + 1] = selected.attr("data-id") + ':' + selected.attr("data-value");
            prevall_li_count++;
        }
        prevall_count++;
    });


    spec_key_has_array.reverse();
    // spec_key_has_array.push(';');
    // console.log(spec_key_has_array.join(';'));
    var spec_key_has = spec_key_has_array.join(';');
    spec_key_has = spec_key_has + ";";


    // console.log(prevall_li_count);
    // console.log(prevall_count);
    if (prevall_li_count == prevall_count) {

        $(obj).parents('.spec_div').nextAll().find('li').css('display', 'none');
    }


    var spec_index = parseInt(index) + 1;
    // console.log(product_array);
    for (gi in product_array) {
        // console.log(gi);
        var item = product_array[gi];
        var reg = new RegExp(spec_key_has);
        var flage = reg.test(gi);
        // console.log(flage);
        if (flage) {

            var gi_array = gi.split(';');
            // console.log(spec_index);
            for (var j = spec_index; j < gi_array.length; j++) {
                var spec = gi_array[j];
                // console.log(spec);
                if (spec && spec != '') {

                    var spec_content_array = spec.split(':');
                    $('#gift .spec_div li[data-id="' + spec_content_array[0] + '"][data-value="' + spec_content_array[1] + '"]').css('display', 'block');
                    $('#gift .spec_div li[data-id="' + spec_content_array[0] + '"][data-value="' + spec_content_array[1] + '"]').removeClass('disabled');
                    if (spec_index == j) {
                        if (item.goods_number <= 0) {
                            $('#gift .spec_div li[data-id="' + spec_content_array[0] + '"][data-value="' + spec_content_array[1] + '"]').addClass('disabled');
                        }
                    }


                }

            }

            if ($(obj).attr("data-name").indexOf('颜色') > -1) {

                if (item.icon) {
                    var type = $(obj).attr('type');
                    var imgObj = $("#thumb");
                    if (type == 'gift') {
                        imgObj = $("#gift_thumb");
                    }

                    $(imgObj).attr("pre_src", $(imgObj).attr("src"));
                    $(imgObj).attr("src", item.icon);

                }
            }

        }


    }
}


/*///////////////////////////////////////// ORDERJSFGX /////////////////////////////////////////*/
function pricea(obj) {
    if (obj != 'undefined') {
        add_class(obj);
    }
    var product = document.form.product.alt;
    for (var i = 0; i < document.form.product.length; i++) {
        if (document.form.product[i].checked == true) {
            var product = document.form.product[i].alt;
            break;
        }
    }
    if (document.form.mun.value == "" || document.form.mun.value == 0) {
        var mun = 1;
    } else {
        var mun = document.form.mun.value;
    }
    // alert(product);
    var price = product * mun;
    is_has_gift = $('input[name=product]:checked').attr('open_gift');
    ;
    $('input[name=is_has_gift]').val(is_has_gift);
    if (is_has_gift == 1) {
        $("#gift").show();
        //礼品总价
        var product_gift = $('input[name=gift_product]:checked').attr('alt');

        if (document.form.gift_mun.value == "" || document.form.gift_mun.value == 0) {
            var gift_mun = 1;
        } else {
            var gift_mun = document.form.gift_mun.value;
        }
        // alert(product);
        // console.log(product_gift);
        // console.log(gift_mun);
        var gift_price = product_gift * gift_mun;
        // console.log(gift_price);
        price = price + gift_price;
    } else {
        $("#gift").hide();
    }


}

function price_count() {
    // var goods_price = $('.shop_price_input').html();
    var goods_price = $('input[name=goods_price]').val();
    var gift_price = $('input[name=gift_price]').val();

    price = parseInt(goods_price) + parseInt(gift_price);


    $(".shop_price").html("￥" + price);
    $(".shop_price_input").val(price);
    // document.getElementById("b1").checked='checked';
    document.form.price.value = price;
    document.form.zfbprice.value = price;
    //document.getElementById("showprice").innerHTML=price;
    // document.getElementById("zfbyh").innerHTML='';

}

function price_gift(obj) {
    if (obj != 'undefined') {
        if ($(obj).hasClass("mouon") || $(obj).hasClass("disabled")) {
            return;
        } else {
            $(obj).siblings().removeClass('mouon');
            $(obj).addClass("mouon");
        }
    }

    //商品总价
    // var product = document.form.product.alt;
    // for(var i=0;i<document.form.product.length;i++){
    //   if(document.form.product[i].checked==true){
    //     var product = document.form.product[i].alt;
    //     break;
    //   }
    // }
    // if(document.form.mun.value=="" || document.form.mun.value==0){
    //   var mun=1;
    // } else{
    //   var mun=document.form.mun.value;
    // }
    // // alert(product);
    // var price=product*mun;

    var price = $(".goods_price").val();
    //礼品总价
    var product_gift = $('input[name=gift_product]:checked').attr('alt');

    if (document.form.gift_mun.value == "" || document.form.gift_mun.value == 0) {
        var gift_mun = 1;
    } else {
        var gift_mun = document.form.gift_mun.value;
    }
    $('input[name=gift_price]').val(product_gift);
    // alert(product);
    // console.log(product_gift+gift_mun);
    price_count();
}

function priceb() {
    var cpxljg = document.getElementById("product");
    var product = cpxljg.options[document.getElementById("product").options.selectedIndex].title;
    if (document.form.mun.value == "" || document.form.mun.value == 0) {
        var mun = 1;
    } else {
        var mun = document.form.mun.value;
    }
    var price = product * mun;
    document.getElementById("b1").checked = 'checked';
    document.form.price.value = price;
    document.form.zfbprice.value = price;
    //document.getElementById("showprice").innerHTML=price;
    document.getElementById("zfbyh").innerHTML = '';
}

//***************************  支付宝价格  ***************************
function zfbprize() {
    sprice = document.form.zfbprice.value;
    // alert(sprice);
    document.form.price.value = (sprice * notzfbzk * 0.1).toFixed(0)
}

/*///////////////////////////////////////// ORDERJSFGX /////////////////////////////////////////*/
function changeItem(i) {

    if (i == 1) {
//document.getElementById("paydiv1").style.display = "block";
//document.getElementById("paydiv0").style.display = "none";
        if (notzfbzk != 0) {
            zfbprize();
            document.getElementById("zfbyh").innerHTML = '<font color=red><b><s>&nbsp;原价：' + document.form.zfbprice.value + '元&nbsp;</s>&nbsp;' + notzfbzk + '折</b></font>';
        }
    } else {
//oprize1();
//document.getElementById("paydiv0").style.display = "block";
//document.getElementById("paydiv1").style.display = "none";
        document.getElementById("zfbyh").innerHTML = '';
        document.form.price.value = document.form.zfbprice.value;
    }
}


/*///////////////////////////////////////// ORDERJSFGX /////////////////////////////////////////*/
var llref = '';
if (document.referrer.length > 0) {
    llref = document.referrer;
}
try {
    if (llref.length == 0 && opener.location.href.length > 0) {
        llref = opener.location.href;
    }
} catch (e) {
}

/*///////////////////////////////////////// ORDERJSEND /////////////////////////////////////////*/