
function queryEmpClick(){
    //每次点击前清空表格数据
    $(".table-tbody").empty('');
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    var start_score = $("#start-score").val();
    var end_score = $("#end-score").val();
    var flag = param_method(start_score,end_score);
    if(flag==false){
        return;
    }
    if( start_date=='' || end_date==''){
        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/employee/order";
    getEmployeeInfosClick(url,param,start_score,end_score)
}

function initQueryEmp(){
    //默认初始化前一周查所有
    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/employee/order";
    getEmployeeInfos(url,param);
}
/**
 * click
 * @param {*} url 
 * @param {*} param 
 */
function getEmployeeInfosClick(url,param,start_score,end_score){
    post_common_service( url,param,function(data){
        var strTbody='';
        for(var i=0;i<data.length;i++){
            var score = 1000-filterByZeroHandle(average(data[i]).toFixed(2),'0');
            if(score>=parseFloat(start_score) && score<=parseFloat(end_score)){
            strTbody+="<tr><td width='67px;'>"+(i+1)+"</td><td width='87px;'>"+data[i].user.fee_station_name+"</td>"
                +"<td width='67px;'>"+data[i].user.truename+"</td><td width='67px;'>"+"</td>"
                +"<td width='87px;'>"+data[i].user.workno+"</td><td width='55px;'>"+ getItemScore(data[i], '环境') +"</td>"
                +"<td width='55px;'>"+getItemScore(data[i], '仪容仪表')+"</td><td width='55px;'>"+getItemScore(data[i], '表情')+"</td>"
                +"<td width='55px;'>"+getItemScore(data[i], '动作')+"</td><td width='55px;'>"+getItemScore(data[i], '文明用语')+"</td>"
                +"<td width='55px;'>"+ getItemScore(data[i], '工作纪律') +"</td><td width='55px;'>"+data[i].check_all_score+"</td>"
                +"<td width='67px;'>"+filter(data[i].checked_num,'0')+"</td><td width='67px;'>"+filterByZeroHandle(average(data[i]).toFixed(2),'0')+"</td>"
                +"<td width='55px;'>"+score+"</td><td width='55px;'>"+"</td><td>"+"</td></tr>";
        }
    }
       $(".table-tbody").append(strTbody);   
    },function(error){
        return "" ;
    });
}

/**
 * init
 * @param {*} url 
 * @param {*} param 
 */
function getEmployeeInfos(url,param){
    post_common_service( url,param,function(data){
        var strTbody='';
        for(var i=0;i<data.length;i++){
            strTbody+="<tr><td width='67px;'>"+(i+1)+"</td><td width='87px;'>"+data[i].user.fee_station_name+"</td>"
                +"<td width='67px;'>"+data[i].user.truename+"</td><td width='67px;'>"+"</td>"
                +"<td width='87px;'>"+data[i].user.workno+"</td><td width='55px;'>"+ getItemScore(data[i], '环境') +"</td>"
                +"<td width='55px;'>"+getItemScore(data[i], '仪容仪表')+"</td><td width='55px;'>"+getItemScore(data[i], '表情')+"</td>"
                +"<td width='55px;'>"+getItemScore(data[i], '动作')+"</td><td width='55px;'>"+getItemScore(data[i], '文明用语')+"</td>"
                +"<td width='55px;'>"+ getItemScore(data[i], '工作纪律') +"</td><td width='55px;'>"+data[i].check_all_score+"</td>"
                +"<td width='67px;'>"+filter(data[i].checked_num,'0')+"</td><td width='67px;'>"+filterByZeroHandle(average(data[i]).toFixed(2),'0')+"</td>"
                +"<td width='55px;'>"+(1000-filterByZeroHandle(average(data[i]).toFixed(2),'0'))+"</td><td width='55px;'>"+"</td><td>"+"</td></tr>";
        }
       $(".table-tbody").append(strTbody);   
    },function(error){
        return "" ;
    });
}

function average(obj){
    if(filter(obj.checked_num,'0') == 0){
        return 0 ;
    }else{
        return obj.check_all_score / filter(obj.checked_num,'1')
    }
}

/**
 * 过滤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 exportExcel(){
    var dept_id = $("#fsList").val();
    var start_date = $("#start-time").val();
    var end_date = $("#end-time").val();
    var start_score = $("#start-score").val();
    var end_score = $("#end-score").val();
    var flag = param_method(start_score,end_score);
    if(flag==false){
        return;
    }
    if( start_date=='' || end_date==''){
        tip("请选择起止日期");
        return ;
    }else{
        start_date += " 00:00:00";
        end_date += " 00:00:00";
    }
    window.open( getserveraddr() + "/file/employee/order?dept_id="+dept_id+
    "&start_date="+start_date+"&end_date="+end_date+"&start_score="+start_score+"&end_score="+end_score);
}


function param_method(start_score,end_score){
    
    if(start_score==''||end_score==''){
        tip("请输入得分区间");
        return false;
    }else if(!(/^[0-9]*$/).test(start_score) || !(/^[0-9]*$/).test(end_score)){
        tip("数字不合法！");
        return false;
    }else if(parseInt(start_score)>parseInt(end_score)){
        tip("开始得分不能比结束得分大");
        return false;
    }else if(start_score<0 || start_score>1000 || end_score<0 || end_score>1000){
        tip("得分只能在0-1000之间");
        return false;
    }else{
        return true;
    }
}
