/*var UserID = sessionStorage.getItem("MiaoMuUserID");
var userInfo = JSON.parse(sessionStorage.getItem("miaomuUserInfo"));*/
if(UserID!=null&&UserID!=""){
    var forShow = userInfo.name;
    if(forShow==null||forShow==""){
        forShow = userInfo.mobile;
    }
}
UrlSearch();
var title = window.search;
if(title==undefined||title==null||title.trim().length<2){
    title = "";
}
var type = window.type;
if(type==undefined||type==null||type.trim().length<2){
    type = "";
}
$(function () {
    $("#Sou").val(title);
    loadInfo();
    UrlSearch();
    $("#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 getCompany() {
    var IDArr = window.location.href.split("?id=");
    var info =null;
    var IDs = 0;
    if(IDArr.length==2){
        IDs = window.location.href.split("?id=")[1];
    }else{
        var weburl = window.location.href;
        IDs = weburl.substring(weburl.lastIndexOf('/')+1).split(".")[0];
    }
    if(parseInt(IDs)){
        var id = parseInt(IDs);
        $(".com-Top-nav>div:first-child>a").attr("href","/company/vip/"+id+".html");
        $(".com-Top-nav>div:nth-child(2)>a").attr("href","/company/vip/about/"+id+".html");
        $(".com-Top-nav>div:nth-child(3)>a").attr("href","/company/vip/spzs/"+id+".html");
        $(".com-Top-nav>div:nth-child(4)>a").attr("href","/company/vip/syjh/"+id+".html");
        //$(".com-Top-nav>div:nth-child(5)>a").attr("href","companyMessage.html?id="+id);
        $(".com-Top-nav>div:last-child>a").attr("href","/company/vip/zzry/"+id+".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/"+id+".html?search="+content;
                }else if(type=="采购"){
                    window.location.href="/company/vip/syjh/"+id+".html?search="+content;
                }else if(type=="企业"){
                    layer.tips("请选择供应或求购","#selectType");
                }else if(type=="资讯"){
                    layer.tips("请选择供应或求购","#selectType");
                }else if(type=="百科"){
                    layer.tips("请选择供应或求购","#selectType");
                }
            }
        });
        $.ajax({
            url:url+"/user2019/qyml/listchaxungongsi",
            data:{"id":id},
            type: "post",
            dateType: "json",
            async: false,
            success: function (response, status, xhr) {
                info = response[0];
                document.title = info.qymc + '苗木供应信息';
                $("meta[name='keywords']").attr('content',info.zycp);
                $("meta[name='description']").attr('content',info.qyjj);
            },
            error: function (e) {
                layer.msg("系统错误,请联系客服!", { icon: 5 });
                console.log("getCompany方法ajax错误");
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }
    return info;
}
function getSold(page) {
    var limit = (page-1)*10;
    var IDArr = window.location.href.split("?id=");
    var IDs = 0;
    if(IDArr.length==2){
        IDs = window.location.href.split("?id=")[1];
    }else{
        var weburl = window.location.href;
        IDs = weburl.substring(weburl.lastIndexOf('/')+1).split(".")[0];
    }
    if(parseInt(IDs)){
        var id = parseInt(IDs);
        $.ajax({
            url:url+"/info/supply/selectForCompany",
            data:{"gsid":id,"page":limit,"cpmc":title,"type":type},
            type: "post",
            dateType: "json",
            async: false,
            success: function (response, status, xhr) {
                var result = "";
                if(response.length==0){
                    $(".list").html("<div>无符合条件的内容</div>");
                    layer.msg("无符合条件的内容",{icon:0});
                }else{
                    $.each(response,function (i, list) {
                        var pic = "/images/无图片.jpg";
                        var content = "无详细内容";
                        var price = "面议";
                        if(list.picture!=null&&list.picture!=""){
                            pic = list.picture;
                            pic = formatImgUrl(pic);
                        }
                        if(list.bz!=null&&list.bz!=""){
                            content = list.bz;
                        }
                        if(list.cpjg!=null&&list.cpjg!=""&&list.cpjg!=0&&list.cpjg!="æ— "&&list.cpjg!="0"){
                            price=list.cpjg;
                            if(!isNaN(price)){
                                price+="å…ƒ";
                            }
                        }
                        result +="<div><table><tr><td><img src='"+pic+"'></td><td><a href='/spzs/Show_product/"+list.id+".html' target='_blank'><p>"+list.cpmc+"</p></a><span title='"+content+"'>"+content.slice(0,30)+"</span></td><td>"+list.address+"</td><td>"+price+"</td></tr></table></div>"
                    });
                    $(".list").html(result);
                }
            },
            error: function (e) {
                layer.msg("系统错误,请联系客服!", { icon: 5 });
                console.log("getSold方法ajax错误");
                console.log(e.status);
                console.log(e.responseText);
            }
        });
    }
}
function getTotal() {
    var total = 0;
    var IDArr = window.location.href.split("?id=");
    var IDs = 0;
    if(IDArr.length==2){
        IDs = window.location.href.split("?id=")[1];
    }else{
        var weburl = window.location.href;
        IDs = weburl.substring(weburl.lastIndexOf('/')+1).split(".")[0];
    }
    if (parseInt(IDs)) {
        var id = parseInt(IDs);
        $.ajax({
            url:url+"/info/supply/countForCompany",
            data: {"gsid":id,"cpmc":title,"type":type},
            type: "post",
            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);
                getSold(page);
            }
        });
    }
}
function loadInfo() {
    var info = getCompany();
    if(info!=null){
        getSwiper(info.id);
        var zycp = "无数据";
        if(info.zycp!=null&&info.zycp!=""){
            zycp = info.zycp;
        }
        if(info.qymc){
            document.title=info.qymc;
            $("#company").html(info.qymc);
            $("#zygy").html("主要供应:"+zycp);
        }else if(info.name){
            $("#company").html(info.name);
            $("#zygy").hide();
        }else{
            $("#company").html('');
            $("#zygy").hide();
        }
        getSold(1);
        getTotal();
    }else{
        window.location.href="/qyml/";
    }
}

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 getSwiper(id){
    $.ajax({
        url:url+"/gstp/gstp/selectByUser",
        data:{"gsid":id},
        type: "get",
        dateType: "json",
        async: false,
        success: function (response, status, xhr) {
            if(response.length==1 && response[0].flag==1){
                var arr = [];
                if(response[0].pic1){
                    arr.push(response[0].pic1);
                }
                if(response[0].pic2){
                    arr.push(response[0].pic2);
                }
                if(response[0].pic3){
                    arr.push(response[0].pic3);
                }
                if(response[0].pic4){
                    arr.push(response[0].pic4);
                }
                if(arr.length>0){
                    var content = "";
                    for (var i = 0; i < arr.length; i++) {
                        content += "<div class='swiper-slide'><img src='"+formatImgUrl(arr[i])+"' style='width: 1200px;height: 400px'></div>";
                    }
                    $(".swiper-wrapper").html(content);
                    $(".upImg").hide();
                    $(".swiper-container").show();
                    var mySwiper = new Swiper('.swiper-container', {
                        autoplay: true,//可选选项,自动滑动
                        loop:true,
                    });
                }
            }
        },
        error: function (e) {
            console.log("addSeeTimes");
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}