var address = "不限";
var tp = "不限";
var tp0 = "不限";
var isAll = true;
var name = null;
var typeName = "绿化设计";
var pages = 0;
var storage = window.sessionStorage;
var contentNewsId = storage["contentNewsId"];
var addressActive = storage["three_page"];
$(function () {
    getbklb();
    $(".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;
            }
        }
    });
    UrlSearch();
    var type = window.type;
    var flag = false;
    if(type!=null){
        $("a[cls='title']").each(function(index, result){
            if($(this).html() == type){
                $(this).attr("class", "address three_page active");
                flag = true;
                return false;
            }
        });
    }else{
        window.location.href="../qyfw/";
    }
    if(!flag){
        window.location.href="../qyfw/";
    }
    getInfo(type,0);
    getTotal(type);
    $("a[cls='title']").click(function () {
        window.location.href="encyclopediaThreePage.html?type="+$(this).html();
    });
    console.log(addressActive);
    if(addressActive == ""){
        typeName = "绿化设计";
    } else {
        typeName = addressActive;
    }
    //这里页面的typeName与数据库字段内容不同因此做次判断重新赋值。
    if(typeName == "法律法规"){
        typeName = "法律咨询";
    } else if (typeName == "合同范文"){
        typeName = "合同样本";
    }
    //getInfo(typeName,0);
    //getTotal(typeName);
    //classClick();
});

/**
 * 分类点击
 */
function classClick(){
    //修改分类标题颜色
    if(addressActive != null && addressActive != undefined && addressActive != ""){
        $("a[cls='title']").each(function(index, result){
            if($(this).html() == addressActive){
                $(this).attr("class", "address three_page active")
                return;
            }
        })
    }
    $("a[cls='title']").click(function(){
        //这里页面的typeName与数据库字段内容不同因此做次判断重新赋值。
        typeName = $(this).html();
        if(typeName == "法律法规"){
            typeName = "法律咨询"
        } else if (typeName == "合同范文"){
            typeName = "合同样本"
        }

        storage["three_page"] = $(this).html();
        // 修改颜色
        $(this).parent().siblings().children().css("color", "#333333");
        $(this).css("color", "#ff774d");
        $(".list_01").attr("style","display: none");
        //获取数据
        getInfo(typeName,0);
        getTotal(typeName);
    })
}
/**
 * 查询列表
 * @param page
 */
function getInfo(typeName,page) {
    var data = {
        "typename":typeName,
        "page": page
    };
    $.ajax({
        url:  url+"/qyfw/commerce/commercechaxun",
        data: data,
        type: "get",
        dateType: "json",
        async: false,
        success: function success(response, status, xhr) {
            console.log("我是请求内容+=" + response);
            var result_0 = "";
            var result_1 = "";
            var result_2 = "";

            $.each(response, function (i, list) {
                var content = list.content;
                if (content != null && content.length > 50) {
                    content = content.slice(0, 50) + "...";
                }
                if(i < 10){
                    result_0 += "<div>";
                    result_0 += "<div class='width100 display_inline_block oneLine'><a href='shownews/"+list.newsid+".html' title='"+list.title+"' target='_blank'>"+ifPictureNull(list.picurl)+list.title+"</a></div>";
                    result_0 += "</div>";
                } else if (i >= 10 && i < 22){
                    result_1 += "<div>";
                    result_1 += "<div class='width100 display_inline_block oneLine'><a href='shownews/"+list.newsid+".html' title='"+list.title+"' target='_blank'>"+ifPictureNull(list.picurl)+list.title+"</a></div>";
                    result_1 += "</div>";
                } else {
                    result_2 += "<div>";
                    result_2 += "<div class='width100 display_inline_block oneLine'><a href='shownews/"+list.newsid+".html' title='"+list.title+"' target='_blank'>"+ifPictureNull(list.picurl)+list.title+"</a></div>";
                    result_2 += "</div>";
                }
            });
            if (response.length == 0) {
                layer.msg('无符合条件的结果!', { icon: 2 });
                // $(".list_00").html("<div><table><tr><td>无符合条件的结果</td></tr></table></div>");
                $(".list").attr("style","display: none");
                $(".page").attr("style","display: none");
                $(".list_00").attr("style","display: block");
            } else {
                $("#response_0").empty();
                $("#response_1").empty();
                $("#response_2").empty();
                $("#response_0").html(result_0);
                $("#response_1").html(result_1);
                $("#response_2").html(result_2);
                $(".list").attr("style","display: block");
                $(".page").attr("style","display: block");
                $(".list_00").attr("style","display: none");
            }
        },
        error: function error() {
            layer.msg('未知错误,请联系客服!', { icon: 5 });
        }
    });
}

/**
 * 获取详细内容
 */
function getContent(str){
    if(str != ''){
        $.ajax({
            url:  url+"/qyfw/commerce/commercechaxunid",
            data: {
                newsid: str
            },
            type: "get",
            dateType: "json",
            async: true,
            success: function success(response, status, xhr) {
                console.log(response)
                $(".list").attr("style","display: none");
                $(".page").attr("style","display: none");
                $(".list_01").attr("style","display: block");
                var title = "<h1>"+response[0].title+"</h1>";
                $("#content_title").html(title);
                $(".title_img").attr("src",response[0].picurl);
                $("#content_content").html(response[0].content);
            },
            error: function error() {
                layer.msg('未知错误,请联系客服!', { icon: 5 });
            }
        });
    } else {
        layer.msg("无详细内容")
    }
}
/**
 * 判断图片是否为空
 */
function ifPictureNull(str){
    if(str == ""){
        return "";
    } else {
        return "[图]"
    }
}
function getTotal(typeName) {
    var data = {
        "typename": typeName
    };
    $.ajax({
        url: url+ "/qyfw/commerce/commercechaxuncount",
        data: data,
        type: "get",
        dateType: "json",
        async: true,
        success: function (response, status, xhr) {
            console.log("/news/hyxx/commercechaxuncount====>"+ response);
            pages = response - 0;
            /**
             * 加载分页插件
             */
            $("#myPage").sPage({
                page: 1, //当前页码,必填
                total: pages, //数据总条数,必填
                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);
                    getInfo(typeName,((page - 1) * 10));
                }
            });
        },
        error: function (e) {
            layer.msg('未知错误,请联系客服!', { icon: 5 });
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}
function UrlSearch(){
    var str=window.location.href; //取得整个地址栏
    var num=str.indexOf("?");
    str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]

    var arr=str.split("&"); //各个参数放到数组里
    for(var i=0;i < arr.length;i++){
        num=arr[i].indexOf("=");
        if(num>0){
            name=arr[i].substring(0,num);
            value=decodeURI(arr[i].substr(num+1));
            this[name]=value;
        }
    }
}

function getbklb() {
    $.ajax({
        url: url+"/bklb2/bklb2/listForPage",
        type:"GET",
        dataType:"json",
        success:function (response,status,xhr) {
            if(response.length>0){
                $("#LBTA1").attr("href",response[0].skipurl);
                $("#LBTIMG1").attr("src",formatImgUrl(response[0].picurl));
                $("#LBTA2").attr("href",response[1].skipurl);
                $("#LBTIMG2").attr("src",formatImgUrl(response[1].picurl));
                $("#LBTA3").attr("href",response[2].skipurl);
                $("#LBTIMG3").attr("src",formatImgUrl(response[2].picurl));
                $("#LBTA4").attr("href",response[3].skipurl);
                $("#LBTIMG4").attr("src",formatImgUrl(response[3].picurl));
            }

        }
    });
}