$(function () {
    showInfo();
    $(".detailInfo").css("left", $("#XS").outerWidth() + 110 + "px");
    $("#XS").mouseenter(function () {
        $(".detailInfo").show();
    });
    $(".detailInfo").mouseenter(function () {
        $(".detailInfo").show();
    });
    $("#XS").mouseleave(function () {
        $(".detailInfo").hide();
    });
    $(".detailInfo").mouseleave(function () {
        $(".detailInfo").hide();
    });
    $("#DL").click(function () {
        sessionStorage.setItem("miaomuLoginTo", window.location.href);
        window.location.href = "/reg201206/login06.html"
    });
    $("#tj").click(function () {
        var info3 = loadInfo();
        var gsid3 = info3.gsid;
        if (UserID == null || UserID == "") {
            layer.confirm('对不起,您尚未登录!', {
                btn: ['登录', '注册'] //按钮
            }, function () {
                window.location.href = "/reg201206/login06.html";
            }, function () {
                window.location.href = "/reg201206/register06.html";
            });
        } else if (gsid3 == null || gsid3 == "") {
            layer.msg("该产品不支持线上报价,请电话联系", {icon: 2});
        } else {
            //if(userInfo.hyjb<2){
            var times = checkTimes();
            if (times) {
                releasePrice(times);
            } else {
                layer.confirm('对不起,您已到达发布上限!', {
                    btn: ['升级会员', '残忍拒绝'] //按钮
                }, function () {
                    window.location.href = "/about/hyfw.html";
                }, function () {
                    //close
                });
            }
            /*}else{
                releasePrice();
            }*/
        }
    });
    $("#searchAll").click(function () {
        var content = $("#Sou").val();
        var type = $("#selectType").val();
        if (content.trim().length < 2) {
            layer.tips("请至少输入两个字符", "#Sou");
        } else {
            if (type == "供应") {
                window.location.href = "/spzs/?search=" + content;
            } else if (type == "采购") {
                window.location.href = "/syjh/qiugou.htm?search=" + content;
            } else if (type == "企业") {
                window.location.href = "/qyml/?search=" + content;
            } else if (type == "资讯") {
                window.location.href = "/hyxx/informationSearch.html?search=" + content;
            } else if (type == "百科") {
                window.location.href = "/qyfw/encyclopediaSearch.html?search=" + content;
            }
        }
    });
});

function loadInfo() {
    var IDArr = window.location.href.split("?info_id=");
    var info = null;
    var IDs = 0;
    if (IDArr.length == 2) {
        IDs = window.location.href.split("?info_id=")[1];
    } else {
        var weburl = window.location.href;
        IDs = weburl.substring(weburl.lastIndexOf('/') + 1).split(".")[0];
    }
    if (parseInt(IDs)) {
        var info_id = parseInt(IDs);
        $.ajax({
            url: url + "/mysj/info/selectInfoById",
            data: {"id": info_id},
            type: "post",
            dateType: "json",
            async: false,
            success: function (response, status, xhr) {
                info = response[0];
            },
            error: function (e) {
                layer.msg("系统错误,请联系客服!", {icon: 5});
                console.log("读取信息ajax错误");
                console.log(e.status);
                console.log(e.responseText);
            }
        });

        $("#ZF").click(function () {
            if (getStatus()) {
                layer.confirm("请选择支付方式", {
                    btn: ['微信', '支付宝'], //按钮
                    btnAlign: 'c'
                }, function () {
                    layer.closeAll();
                    toPay(telPrice, 'WX', '查看求购电话', info_id, 0);
                }, function () {
                    layer.closeAll();
                    toPay(telPrice, 'ALI', '查看求购电话', info_id, 0);
                });
            } else {
                window.location.href = '/reg201206/register06.html';
            }
        });
    }
    addSeeTimes(info_id);
    return info;
}

function showInfo() {
    var day = 0;
    var info = loadInfo();
    if (info != null) {
        if (info.picture) {
            $("#gongdan").attr('src', info.picture);
            $("#gd").attr('href', info.picture);
            $("#d33").show();
        }
        var show_gg = info.content;
        if (show_gg.indexOf('cm') !== -1) {
            var temp_arr = show_gg.split('cm');
            show_gg = temp_arr[0] + '公分';
        } else {
            show_gg = '';
        }
        document.title = info.country + info.city + '求购' + show_gg + info.showname + "|" + info.showname + '求购信息-中国苗木网miaomu.com';
        $("meta[name='keywords']").attr('content', info.country + info.city + show_gg + info.showname + ',' + info.showname + '求购信息,' + show_gg + info.showname + '采购信息');
        $("meta[name='description']").attr('content', '中国苗木网miaomu.com发布最新求购信息:' + info.country + info.city + '求购' + info.hits + info.danwei + show_gg + info.showname + "," + info.showname + '求购信息供您参考。');
        $("#collect").click(function () {
            insertCollection(info.showname);
        });
        $("#share").click(function () {
            var share = document.getElementById("share");
            var clip = new ClipboardJS(share, {
                text: function () {
                    return "我在苗木网看到求购:" + info.showname + ",点击链接或复制链接到浏览器查看→" + window.location.href;
                }
            });
            clip.on('success', function (e) {
                console.log(e);
                clip.destroy();// 复制完毕删除,否则会有创建多个clipboard对象
                layer.msg("链接已复制,去粘贴分享吧!", {icon: 1});
            });
            clip.on('error', function (e) {
                console.log(e);
                layer.msg("浏览器版本过低,请手动复制链接", {icon: 2});
            });
            share.click();
            clip.destroy();
        });
        getBuy(info.gsid);
        var gsid = info.gsid;
        var showName = info.showname;
        var hits = info.hits;
        var address = info.address;
        var gg = info.chanpinguige;
        var content = info.content;
        var price = info.price;
        var yx = info.period;
        var fb = info.dateandtime;
        var linkman = info.linkman;
        var phone = info.phone;
        if (price == null || price <= 0 || price == "") {
            price = "面议";
        } else {
            price += "å…ƒ";
        }
        if (!gg) {
            gg = "无描述";
        }
        $("#D1-title").html(showName);
        $("#XS").html(showName);
        $("#hits").html(hits);
        $("#address").html(address);
        $("#gg").html(content);
        $("#content").html(gg);
        $("#priceShow").html(price);
        $("#fb").html(fb.slice(0, 10));
        $("#seeTimes").html(info.seeTimes + 1 + "次");
        fb = fb.slice(0, 10);
        switch (yx) {
            case "一周":
                day = 7 * 24 * 60 * 60 * 1000;
                break;
            case "半月":
                day = 15 * 24 * 60 * 60 * 1000;
                break;
            case "一个月":
                day = 30 * 24 * 60 * 60 * 1000;
                break;
            case "三个月":
                day = 90 * 24 * 60 * 60 * 1000;
                break;
            case "六个月":
                day = 180 * 24 * 60 * 60 * 1000;
                break;
            case "一年":
                day = 365 * 24 * 60 * 60 * 1000;
                break;
            default :
                day = 0;
                break;
        }
        if (day == 0) {
            $("#bj").html("报价长期有效");
            $("#tj").removeAttr("disabled");
        } else {
            var lastTime = new Date(new Date(fb).getTime() + day);
            var d1 = new Date().toLocaleDateString();
            var d2 = new Date(d1.replace(/\//g, "-"));
            d2.setHours(0);
            lastTime.setHours(0);
            console.log("最后日期:" + lastTime);
            console.log("当前日期:" + d2);
            var last = ((lastTime - d2) / 1000 / 60 / 60 / 24);
            if (last <= 0) {
                $("#bj").html("报价已过期");
            } else {
                $("#day").html(last);
                $("#tj").removeAttr("disabled");
            }
        }
        var datainfo = {
            "country": '',
            "pdType": '',
            "page": 0,
            "showname": showName
        };
        $.ajax({
            url: url + "/mysj/info/listqg03",
            data: datainfo,
            type: "post",
            dateType: "json",
            async: false,
            success: function (res, status, xhr) {
                var result = '';
                if (res.length > 1) {
                    var temp = 0;
                    $.each(res, function (i, list) {
                        if (temp < 8) {
                            result += "<div style='margin-bottom: 20px;'><a href='/syjh/bussiness_info/" + list.infoId + ".html' target='_blank'>" + list.showname + "<br>" + list.dateandtime.slice(0, 10) + "</a></div>";
                            temp++;
                        } else {
                            return false;
                        }
                    });
                } else {
                    $(".D8").hide();
                    $(".D8-text").hide();
                }
                $(".D8-text").html(result);
            }
        });
        if (gsid != null && gsid != "") {
            var companyInfo = loadCompany(gsid);
            if (companyInfo != null) {
                if (companyInfo.qymc != null && companyInfo.qymc != "") {
                    $("#company,#company2").html(companyInfo.qymc);
                    $("#zygy").html("主要供应:" + companyInfo.zycp);
                    $(".com-Top-nav>div:first-child>a").attr("href", "/company/vip/" + gsid + ".html");
                    $(".com-Top-nav>div:nth-child(2)>a").attr("href", "/company/vip/about/" + gsid + ".html");
                    $(".com-Top-nav>div:nth-child(3)>a").attr("href", "/company/vip/spzs/" + gsid + ".html");
                    $(".com-Top-nav>div:nth-child(4)>a").attr("href", "/company/vip/syjh/" + gsid + ".html");
                    //$(".com-Top-nav>div:nth-child(5)>a").attr("href","companyMessage.html?id="+gsid);
                    $(".com-Top-nav>div:last-child>a").attr("href", "/company/vip/zzry/" + gsid + ".html");
                    $("#company2").attr("href", "/company/vip/" + gsid + ".html");
                    $("#company3").attr("href", "/company/vip/spzs/" + gsid + ".html");
                    $("#searchThis").click(function () {
                        var content = $("#Sou").val();
                        var type = $("#selectType").val();
                        if (content.trim().length < 2) {
                            layer.tips("请至少输入两个字符", "#Sou");
                        } else {
                            if (type == "供应") {
                                window.location.href = "/company/vip/spzs/" + gsid + ".html?search=" + content;
                            } else if (type == "采购") {
                                window.location.href = "/company/vip/syjh/" + gsid + ".html?search=" + content;
                            } else if (type == "企业") {
                                layer.tips("请选择供应或求购", "#selectType");
                            } else if (type == "资讯") {
                                layer.tips("请选择供应或求购", "#selectType");
                            } else if (type == "百科") {
                                layer.tips("请选择供应或求购", "#selectType");
                            }
                        }
                    });
                    var zz = "";
                    var zzsm = "æ— ";
                    if (info.gs == 1) {
                        zz = "<img src='/images/三级详情页供应改danbao_23.png' title='已通过执照认证'>";
                        zzsm = " 公司认证<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                    }
                    var hy = "";
                    var hysm = "æ— ";
                    switch (companyInfo.hyjb) {
                        case 5:
                            hy = "<img src='/images/皇冠级别.png' title='皇冠VIP'>";
                            hysm = " 皇冠VIP";
                            break;
                        case 4:
                            hy = "<img src='/images/黄钻级别.png' title='黄钻VIP'>";
                            hysm = " 黄钻VIP";
                            break;
                        case 3:
                            hy = "<img src='/images/红钻级别.png' title='红钻VIP'>";
                            hysm = " 红钻VIP";
                            break;
                        case 2:
                            hy = "<img src='/images/绿钻级别.png' title='绿钻VIP'>";
                            hysm = " 绿钻VIP";
                            break;
                        case 1:

                            break;
                        default:

                            break;
                    }
                    var sm = "";
                    var smsm = "";
                    if (companyInfo.shiming == 1) {
                        sm = "<img src='/images/三级详情页供应改身份验证.png' title='已通过实名认证'>";
                        smsm = " 实名认证<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    var db = "";
                    var dbsm = "";
                    if (companyInfo.danbao == 1) {
                        db = "<img src='/images/三级详情页供应改danbao_30.png' title='已缴费担保认证'>";
                        $(".D3-3").html(db + "求购商已缴纳担保金,平台监督,防止上当受骗,公平公正。");
                        dbsm = " 担保认证";
                    }
                    $("#tb").html(hy + sm + zz);
                    $("#DE1").html(companyInfo.qymc);
                    $("#DE2").html(hy + hysm);
                    $("#DE3").html(companyInfo.sf + companyInfo.city);
                    $("#DE4").html(zz + zzsm + sm + smsm + db + dbsm);
                    if (companyInfo.touxiang != null && companyInfo.touxiang != "") {
                        $("#tx").attr("src", companyInfo.touxiang);
                        $("#tx").css("border-radius", "75px");
                    }
                }
            }
        }
        if (UserID != null && UserID != "" && userInfo != null) {
            //if(userInfo.hyjb>1){
            $(".D5-1").html("联系人:" + linkman);
            $(".D5-2").html("联系方式:<a href='javascript:void(0);' id='showPhone' style='color: #ff774d' target='_blank'>点击查看联系方式</a>");
            $(".D5").css("background-color", "white");
            $("#showPhone").click(function () {
                var IDArr = window.location.href.split("?info_id=");
                var IDs = 0;
                if (IDArr.length == 2) {
                    IDs = window.location.href.split("?info_id=")[1];
                } else {
                    var weburl = window.location.href;
                    IDs = weburl.substring(weburl.lastIndexOf('/') + 1).split(".")[0];
                }
                if (parseInt(IDs)) {
                    var info_id = parseInt(IDs);
                    console.log(info_id);
                    showTel(info_id);
                }
            });
            /*}else{
                $(".greenTexts").html("对不起,您不是正式会员,");
                $(".D5-2").hide();
            }*/
        }
    } else {
        window.location.href = "/syjh/qiugou.htm";
    }
}

function loadCompany(gsid) {
    var companyInfo = null;
    $.ajax({
        url: url + "/user2019/qyml/listchaxungongsi",
        data: {"id": gsid},
        type: "post",
        dateType: "json",
        async: false,
        success: function (response, status, xhr) {
            companyInfo = response[0];
        },
        error: function (e) {
            layer.msg("系统错误,请联系客服!", {icon: 5});
            console.log("读取公司信息ajax错误");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    return companyInfo;
}

function addSeeTimes(id) {
    $.ajax({
        url: url + "/mysj/info/addSeeTimes",
        data: {"infoId": id},
        type: "get",
        dateType: "json",
        async: false,
        success: function (response, status, xhr) {
            if (response != 1) {
                console.log("addSeeTimes!=1 ===>" + response);
            }
        },
        error: function (e) {
            console.log("addSeeTimes");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}

function getBuy(id) {
    var info_id = parseInt(window.location.href.split("?info_id=")[1]);
    $.ajax({
        url: url + "/mysj/info/selectInfoForCompany",
        data: {"gsid": id, "type": "采购", "page": 0},
        type: "post",
        dateType: "json",
        async: false,
        success: function success(response, status, xhr) {
            var result = "";
            if (response.length > 1) {
                var temp = 0;
                $.each(response, function (i, list) {
                    if (temp < 8) {
                        if (list.infoId != info_id) {
                            result += "<div style='margin-bottom: 20px;'><a href='/syjh/bussiness_info/" + list.infoId + ".html' target='_blank'>" + list.showname + "<br>" + list.dateandtime.slice(0, 10) + "</a></div>";
                            temp++;
                        }
                    } else {
                        return false;
                    }
                });
            } else {
                result = "无内容";
            }
            $(".D7").html(result);
        },
        error: function error(e) {
            layer.msg("系统错误,请联系客服!", {icon: 5});
            console.log("getBuy方法ajax错误");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}