//cc
(function(){
    X.pageLogic['search'] = {
        init : function(){
            $(".search_pop .zhanghu_box").click(function(){
                var obj = $(".search_pop .zhanghu_box"); 
                var offset = obj.offset();

                //点击按钮，数据，向左偏移，向右偏移
                X.querycriteriapop(X.DATA.search,'category',offset.left,offset.top+obj.height(),function(val){
                    
                    $(".search_top .zhanghu_box span").html(val.title);
                    $(".search_top .zhanghu_box span").attr('data-id',val.id);
                    $(".search_top .zhanghu_box span").attr('data-type',val.type);
                    var judgeid = $(".search_left .judge_box").val();
                    if(val.type == 'time'){
                        //type为time是隐藏文本框，显示时间选择框
                        $(".search_left .judge_box").hide();
                        $(".search_left .search_txt").hide();
                        $(".search_left .time-box").show();
                    }else {
                        $(".search_left .judge_box").show();
                        if(judgeid == 4 || judgeid == 5 ){ //当judfeid 等于4和5时隐藏文本框，只显示下拉框
                            $(".search_left .search_txt").hide();
                        }else{
                            $(".search_left .search_txt").show();
                        }
                        $(".search_left .time-box").hide();
                    }

                });
            });
            // 渲染条件框数据
            X.laytpldata("#judge-box-dot",X.DATA.search['condition'],'.judge_box');
            //渲染日期选择器
            X.daterender(".timechoice");

            // 下拉选项改变事件
            $(".search_left .judge_box").change(function(){
                var judgeid = $(this).val();
                if(judgeid == 4 || judgeid == 5){
                    $('.search_left .search_txt').hide();
                }else {
                    $('.search_left .search_txt').show();
                }
            });

            // 搜索按钮单击事件
            $(".search_top .searcg_but").click(function(){
                var typeid = $(".search_top .zhanghu_box span").attr('data-id');//属性id
                var type = $(".search_top .zhanghu_box span").attr('data-type');//属性id
                var condition = $(".search_left .judge_box").val(); //条件
                var details = $(".search_left .search_txt").val();//详情
                var time = $(".search_left .time-box .timechoice").val();//时间
                var timearr = time.split('-');
                var startdate = timearr[0]+ "-" + timearr[1] +"-"+ timearr[2];
                var enddate= timearr[3]+ "-" + timearr[4] +"-"+ timearr[5];

                var start = new Date(startdate);
                var starttime = Date.parse(start)/1000;

                var end = new Date(enddate);
                var endtime = Date.parse(end)/1000;
                var data;
                //当type为time时不用传condition，details，当condition为5或4时不用传details
                if( type != "time" ){
                    if(condition == 4 || condition == 5){
                        data = {
                            typeid: typeid,
                            condition:condition
                        }
                    }else {
                        data = {
                            typeid: typeid,
                            condition:condition,
                            details:details
                        }
                    }
                }else {
                    data = {
                        typeid: typeid,
                        starttime: starttime,
                        endtime:endtime
                    }
                }
                X.api('api/userquery',data,function (d){
                    X.laytpldata("#search-data-dot",d.datalist,".search_pop .search_data");
                    $(".more_data span").html(d.totalNum);
                    $(".search_pop .more_data-box").show();
                });
            })
        },
    };
})();
