
/**
 * 点击查询
 */
function queryTypeClick(){
    //每次点击前清空表格数据
    $(".table-tbody").empty('');
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    if(start_date==null||start_date==''||start_date==undefined||
    end_date==null||end_date==''||end_date==undefined){
        tip("请选择起止日期");
                return ;
    }else{
        start_date += " 00:00:00";
        end_date += " 00:00:00";
    }
  
    var param = {
        "dept_id": $("#fsList").val(),
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "statistics/score/item/info";
    var strTbody='';
      post_common_service( url,param,function(data){
          // 管道中心受检人平均每人次扣分
          check_people_avg(data);
            },function(error){
                return "" ;
     });
}

/**
 * 表格初始化
 */
function initQueryType(){
    // 默认初始化前一周查所有
    var endD = new Date();
    var now = new Date();
    var startD = new Date(now.getTime() - 7 * 24 * 3600 * 1000);
    var start_time = startD.getFullYear()+"-"+(startD.getMonth()+1)+"-"+startD.getDate(); 
    var end_time = endD.getFullYear()+"-"+(endD.getMonth()+1)+"-"+endD.getDate(); 
    // 默认初始化时间页面回显
    $("#start-time").val(start_time);
    $("#end-time").val(end_time);
    var start_date = start_time+" 00:00:00"; 
    var end_date = end_time+" 00:00:00"; 
    
    var param = {
        "start_date": start_date,
        "end_date": end_date,
    }
    var url = "statistics/score/item/info";
    var strTbody='';
   
    post_common_service( url,param,function(data){
        // 管道中心受检人平均每人次扣分
        check_people_avg(data); 
            },function(error){
                return "" ;
     });
    
}
/**
 * 表格拼接及数据传入
 * @param {*} data 
 * @param {*} i 
 * @param {*} avg 
 */
function gethtml(data,i,avg,lists){
    var strTbody = "";

    if(i==0 || i==7 || i==13){
        
        num = 1;
        var name = "";
        var rowspan = 0;
          order = 1;
        if(i==0){
            name = "连云港";
            rowspan = 7;
            for(var i=0;i<lists.length;i++){
                if(avg < lists[i]){
                    order = order + 1;
                }
            }
        }else if(i==7){
            name = "盐城";
            rowspan = 6;
            for(var i=0;i<lists.length;i++){
                if(avg < lists[i]){
                    order = order + 1;
                }
            }
        }else if(i==13){
            name = "南通";
            rowspan = 8;
            for(var i=0;i<lists.length;i++){
                if(avg < lists[i]){
                    order = order + 1;
                }
            }
        }
        strTbody += "<tr><td rowspan="+rowspan+">"+order+"</td>"+"<td rowspan="+rowspan+">"+name+"</td>";
        strTbody+="<td>"+num+"</td><td>"+data.name+"</td><td>"+filter(data.people_num,'0')
                   +"</td><td>"+filter(data.checked_people_num,'0')
                   +"</td><td>"+filter(data.checked_num,'0')+"</td><td>"+getItemScore(data, '环境卫生')
                   +"</td> <td>"+getItemScore(data, '仪容仪表')+"</td><td>"+getItemScore(data, '表情')
                   +"</td><td>"+getItemScore(data, '收费动作')+"</td><td>"+getItemScore(data, '文明用语')
                   +"</td><td>"+getItemScore(data, '工作纪律')+"</td><td>"+data.all_check_score+"</td><td>"
                   +filterByZeroHandle(filterJudge(data.all_check_score,data.checked_num),'0')
                   +"</td><td rowspan="+rowspan+">"+avg+"</td>";
        strTbody += "</tr>";
    }else{
        num ++;
         
        strTbody += "<tr><td>"+num+"</td>";
        strTbody+="<td>"+data.name+"</td><td>"+data.people_num+"</td><td>"
                  +filter(data.checked_people_num,'0')+"</td><td>"+filter(data.checked_num,'0')
                  +"</td><td>"+getItemScore(data, '环境卫生')+"</td><td>"+getItemScore(data, '仪容仪表')
                  +"</td><td>"+getItemScore(data, '表情')+"</td><td>"+getItemScore(data, '收费动作')
                  +"</td><td>"+getItemScore(data, '文明用语')+"</td><td>"+getItemScore(data, '工作纪律')
                  +"</td><td>"+data.all_check_score+"</td><td>"
                  +filterByZeroHandle(filterJudge(data.all_check_score,data.checked_num),'0')+"</td>";
        strTbody += "</tr>";
          
    }
    return strTbody;
}

function getSum(){}
/**
 * 管道中心受检人平均每人次扣分
 * @param {*} data 
 */
function check_people_avg(data){
    var dataArray = new Array();
        var sum = 0;
        var sum1 = 0;
        for(k=0;k<data.length;k++){
                sum = parseFloat(sum)+ parseFloat(filterByZeroHandle(data[k].all_check_score,'0'));
                sum1 = parseFloat(sum1)+ parseFloat(filterByZeroHandle(data[k].checked_num,'0'));
            if(k==6){
                dataArray.push(filterJudge(sum,sum1));
                sum = 0;
                sum1 = 0;
            }
            if(k==12){  
                dataArray.push(filterJudge(sum,sum1));
                sum = 0;
                sum1 = 0;
            }
            if(k==20){
                dataArray.push(filterJudge(sum,sum1)); 
                sum = 0;
                sum1 = 0;
            }
        }        
        //console.log(dataArray)
     
        for(i=0;i<data.length;i++){
           if(i==0){
                $(".table-tbody").append(gethtml(data[i],i,dataArray[0],dataArray));
           }else if(i==7){
                $(".table-tbody").append(gethtml(data[i],i,dataArray[1],dataArray));
           }else if(i==13){
                $(".table-tbody").append(gethtml(data[i],i,dataArray[2],dataArray));
           }else{
                $(".table-tbody").append(gethtml(data[i],i));
               
           }
        } 
}

/**
 * 过滤undefined
 * @param {*} value 
 * @param {*} default_display_value 
 */
function filter(value , default_display_value){
    if(value!='' && value!=undefined ){
        return value ;
    }else if(default_display_value != undefined){
        return default_display_value ;
    }else{
        return "" ;
    }
}

/**
 * 获取扣分详情
 * @param {*} obj 
 * @param {*} check_item_name 
 */
function getItemScore(obj, check_item_name){
    var names = obj.checked_socre_name ;
    var scores = obj.checked_score ;

    for(var i=0;i<names.length ;i++){
        if( names[i].indexOf(check_item_name) > -1){
            return scores[i] ;
        }
    }
    return 0 ;
}

/**
 * 过滤除数为0和保留二位小数时出现的0.00
 * @param {*} value 
 * @param {*} default_display_value 
 */
function filterByZeroHandle(value , default_display_value){
        if(isNaN(value)){
            return default_display_value ;
        }else{
            if((value+'').endsWith('.00')){
                return value.substring(0,value.length-3);
            }
            return value ;
        }
}

function filterJudge(num1,num2){
        if(num2==0){
            return 0;
        }else{
            return (num1/num2).toFixed(2);
        }
}

function exportExcel(){
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    if( start_date=='' || end_date==''){
        tip("请选择起止日期");
        return ;
    }else{
        start_date += " 00:00:00";
        end_date += " 00:00:00";
    }
    window.open( getserveraddr() + "/file/score/item/info?start_date="+start_date+"&end_date="+end_date);
}






