$(function () {
    var indexSearch = {
        key: Util.getRequest('key') || "",
        type: Util.getRequest('type') || "",
        time: Util.getRequest('time') || "",
        init: function () {
            this.bindFun();
            this.renderPage()
        },
        renderPage: function () {
            if (this.type == 2) {
                $('.search_nav ').find('.search_xj').addClass('active').siblings().removeClass('active');
            } else {
                $('.search_nav ').find('.search_sp').addClass('active').siblings().removeClass('active');
            }
            if (this.time == 2) {
                $('.condition_right_content').find('.st').click()
            } else if (this.time == 3) {
                $('.condition_right_content').find('.qt').click()
            } else {
                $('.condition_right_content').find('.jt').click()
            };
            $('.input-search').val(this.key);
        },
        getData: function (type, time) {
            var self = this;
            var url = type == 1 ? apis.goodsSearch : apis.inquirySearch;
            IcController.getData(url, 'GET', {
                "offset": 8,
                "p": 1,
                "goods_name/like": self.key,
                "start_time": time
            }, function (res) {
                var getTpl = "";
                if (type == 1) {
                    getTpl = sphtml.innerHTML
                } else {
                    getTpl = xjhtml.innerHTML
                }
                if (res.errcode == 0) {
                    $('.nav_title').find('span').text(res.total)
                    var list = [];
                    if (type == 1) {
                        list = res.goods_list
                    } else {
                        list = res.inquiry_list
                    }
                    layui.laytpl(getTpl).render(list, function (html) {
                        $(".goods_data_list").html(html);
                    });
                } else if (res.errcode == type == 1 ? 105013 : 105001) {
                    $('.nav_title').find('span').text(0)
                    layui.laytpl(getTpl).render([], function (html) {
                        $(".goods_data_list").html(html);
                    });

                }
            })
        },
        bindFun: function () {
            var self = this;
            $('.search_nav li').on('click', function () {
                var index = $(this).index();
                if ($(this).hasClass('active')) {
                    return
                }
                $(this).addClass('active').siblings().removeClass('active');
                $(".goods_data_list").empty();
                $('.nav_title').find('span').text(0);
                if (index == 0) {
                    self.type = 1
                } else {
                    self.type = 2
                }
                $('.jt').click();
            });
            $('.input_btn').on('click', function () {
                var val = $.trim($('.input-search').val());
                if (!val.length) {
                    layer.msg('搜索内容不能为空')
                } else if (val.length < 3) {
                    layer.msg('搜索内容太短')
                } else {
                    window.location.replace("/indexsearch/" +decodeURIComponent(val)+ '_' + self.type + '_' + self.time);
                }
            });
            $('.zz').on('click', '.fl', function () {
                var index = $(this).index();
                var spOrXj;
                var time = Util.getDay(0);
                $(this).addClass('active').siblings().removeClass('active');
                if (self.type == 2) {
                    spOrXj = 2
                } else {
                    spOrXj = 1
                }
                if (index == 0) {
                    self.time = 1;
                    self.getData(spOrXj, time)
                } else if (index == 1) {
                    self.time = 2;
                    time = Util.getDay(-3);
                    self.getData(spOrXj, time)
                } else {
                    self.time = 3;
                    time = Util.getDay(-7);
                    self.getData(spOrXj, time)
                }
            })
        }
    };
    indexSearch.init();
})