var sf = "";
var ttArr = window.location.href.split("?search=");
var title = decodeURI(window.location.href.split("?search=")[1]);
if(title==null||title.trim().length<2||ttArr.length!=2){
    title = "";
}
$(function () {
    $("#Sou").val(title);
    loadCompany(1);
    loadAll();
    VIP();
    SP();
    region_init("capital","city","area");
    $("#capital").on("change",function () {
        $("#more1").html($("#capital option:selected").text()+"â–¼");
        $("#more1").css("color","#7AAC4B");
        $(".address").css("color", "#333333");
        sf = $("#capital option:selected").text();
        loadCompany(1);
        loadAll();
    });
    $("#mm-type").mouseleave(function () {
        $("#mm-type-list").hide();
    });
    $("#mm-type-list").mouseleave(function () {
        $("#mm-type-list").hide();
        $("#mm-type-more").hide();
    });
    $("#mm-type-more").mouseleave(function () {
        $("#mm-type-list").hide();
        $("#mm-type-more").hide();
    });
    $(".address").click(function () {
        if ($(this).css("color") != "rgb(122, 172, 75)") {
            $("#more1").html("更多▼");
            $("#more1").css("color","#333333");
            $(this).css("color", "#7AAC4B");
            $(this).parent().siblings().children().css("color", "#333333");
            if($(this).html() == "不限"){
                sf = "";
            }else{
                sf = $(this).html();
                document.title=$(this).html()+"苗圃|苗木基地|苗木种植地|苗木经销商-中国苗木网miaomu.com";
                $("meta[name='keywords']").attr('content',$(this).html()+'苗圃,苗木基地,苗木种植地,苗木经销商');
                $("meta[name='description']").attr('content','中国苗木网miaomu.com为您提供'+$(this).html()+'最新苗木企业大全,苗圃,苗木种植基地,苗木经销商等苗木企业名录。');
            }
            loadCompany(1);
            loadAll();
        }
    });
    $(".logo button").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 loadCompany(page) {
    var limit = (page-1)*10;
    console.log("limit:"+limit);
    $.ajax({
        url:url+"/user2019/qyml/qymlchaxunfenye",
        data: {"sf":sf,"page":limit,"qymc":title},
        type: "post",
        dateType: "json",
        async: false,
        success: function success(response, status, xhr) {
            var result = "";
            $.each(response,function (i, list) {
                var zz = "";
                if(list.gs==1){
                    zz = "<img src='../images/三级详情页供应改danbao_23.png' title='已通过执照认证'>";
                }
                var db = "";
                if(list.danbao==1){
                    db = "<img src='../images/三级详情页供应改danbao_30.png' title='已缴费担保认证'>"
                }
                var hy = "";
                switch (list.hyjb) {
                    case 5:
                        hy="<img src='../images/皇冠级别.png' title='皇冠VIP'>";
                        break;
                    case 4:
                        hy="<img src='../images/黄钻级别.png' title='黄钻VIP'>";
                        break;
                    case 3:
                        hy="<img src='../images/红钻级别.png' title='红钻VIP'>";
                        break;
                    case 2:
                        hy="<img src='../images/绿钻级别.png' title='绿钻VIP'>";
                        break;
                    case 1:

                        break;
                    default:

                        break;
                }
                var sm = "";
                if(list.shiming==1){
                    sm = "<img src='../images/三级详情页供应改身份验证.png' title='已通过实名认证'>";
                }
                var picArr = setPic(list.id);
                var pic1 = "";
                var pic2 = "";
                if(picArr[3]==1){
                    pic1 = "<img src='"+picArr[0]+"'>";
                }else if(picArr[3]==2){
                    pic1 = "<img src='"+picArr[0]+"'>";
                    pic2 = "<img src='"+picArr[1]+"'>";
                }
                result += "<div class='list'><div><ul><li><p><img src='../images/店铺.png'>&nbsp;<a href='../company/vip/"+list.id+".html' target='_blank'>"+list.qymc+"</a>&nbsp;"+hy+"&nbsp;"+zz+"&nbsp;"+db+"&nbsp;"+sm+"</p></li><li>主要业务:"+list.zycp+"</li><li>联系人:"+list.name+"&nbsp;&nbsp;电话:"+list.mobile+"</li><li>公司地址:"+list.address+"</li><li><a href='/company/vip/"+list.id+".html' target='_blank'>进入店铺></a></li></ul></div><div><div>"+pic1+"</div><div>"+pic2+"</div></div></div>";
            });
            $(".left-info").html(result);
        },
        error: function error(e) {
            layer.msg("系统错误,请联系客服!", {icon: 5});
            console.log("loadCompany方法ajax错误");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}
function loadAll() {
    var total = 0;
    $.ajax({
        url:url+"/user2019/qyml/qymlchaxunfenyecount",
        data: {"sf":sf,"qymc":title},
        type: "get",
        dateType: "json",
        async: false,
        success: function (response, status, xhr) {
            total = response;
        },
        error: function (e) {
            layer.msg("系统错误,请联系客服!", { icon: 5 });
            console.log("查询总数ajax错误");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    $("#myPage").sPage({
        page: 1, //当前页码,必填
        total: total, //数据总条数,必填
        pageSize: 10, //每页显示多少条数据,默认10条
        showTotal: false, //是否显示总条数,默认关闭:false
        totalTxt: "共{total}条", //数据总条数文字描述,{total}为占位符,默认"共{total}条"
        noData: true, //没有数据时是否显示分页,默认false不显示,true显示第一页
        showSkip: true, //是否显示跳页,默认关闭:false
        showPN: true, //是否显示上下翻页,默认开启:true
        prevPage: "上一页", //上翻页文字描述,默认“上一页”
        nextPage: "下一页", //下翻页文字描述,默认“下一页”
        backFun: function backFun(page) {
            //点击分页按钮回调函数,返回当前页码
            //$("#pNum").text(page);
            scrollTo(0, 0);
            loadCompany(page);
        }
    });
}
function VIP() {
    $.get(url+"/user2019/qyml/tjsp", function (List) {
        var result = "";
        $.each(List, function (i, list) {
            if (i == 9) {
                return false;
            }
            result += "<li><a href='../company/vip/"+list.id+".html' target='_blank'>" + list.qymc + "</a></li>";
        });
        $("#VIP").html("<ul>"+result+"</ul>");
    });
}
function SP() {
    $.get(url+"/user2019/qyml/zxsp", function (List) {
        var result = "";
        $.each(List, function (i, list) {
            if (i == 9) {
                return false;
            }
            result += "<li><a href='../company/vip/"+list.id+".html' target='_blank'>" + list.qymc + "</a></li>";
        });
        $("#latest").html("<ul>"+result+"</ul>");
    });
}
function ZZ(id) {
    var result = false;
    $.ajax({
        url:url+"/user/qyzz/selectqyzz",
        data:{"userid":id},
        type: "post",
        dateType: "json",
        async: false,
        success:function (response, status, xhr) {
            if(response.length!=0){
                if(response[0].flag==1){
                    result = true;
                }
            }
        },
        error:function (e) {
            layer.msg("系统错误,请联系客服!", { icon: 5 });
            console.log("ZZ");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    return result;
}
function setPic(id) {
    var result = ["","","0"];
    $.ajax({
        url:url+"/info/supply/selectForPic",
        data:{"gsid":id},
        type: "post",
        dateType: "json",
        async: false,
        success: function (response, status, xhr) {
            if(response.length==1){
                var pic = response[0].picture;
                if(pic!=null){
                    pic = formatImgUrl(pic);
                }
                result[0] = pic;
                result[3] = "1";
            }else if(response.length>1){
                var pic1 = response[0].picture;
                if(pic1!=null){
                    pic1 = formatImgUrl(pic1);
                }
                var pic2 = response[1].picture;
                if(pic2!=null){
                    pic2 = formatImgUrl(pic2);
                }
                result[0] = pic1;
                result[1] = pic2;
                result[3] = "2";
            }
        },
        error: function (e) {
            layer.msg("系统错误,请联系客服!", { icon: 5 });
            console.log("setPic方法ajax错误");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
    return result;
}