<link href="TaskMgt/styles12.css" rel="stylesheet" />
<script type="text/javascript" src="TaskMgt/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="TaskMgt/knockout-2.2.1.js"></script>
<script type="text/javascript" src="TaskMgt/globalize.min.js"></script>
<script type="text/javascript" src="TaskMgt/dx.chartjs.js"></script>

<?php 
include ('CRM/functions.php');
$deptid=  $s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."' " ,'mst_dpt_heirarchy');	
$LowerDeptList=GETLowerDeptUser($_SESSION['UserID'],$deptid,$_SESSION['ACGCompanyId']);
$arr=array_push($LowerDeptList,$deptid);
?>

<?php 
if(isset($_REQUEST['search'])){
    
    if($_REQUEST['dept']!=""){
        if($_REQUEST['team']!=""){
           $teamsql=DB_fetch_assoc(DB_query("select SUM(total_com)as tot,COUNT(*)as cnt FROM perf_task_assign a,perf_task_assign_details b WHERE a.assign_to='team' and a.teamid='". $_REQUEST['team']."' and a.id=b.task_id ",$db)) ;
           $avg_grth=round($teamsql['tot']/$teamsql['cnt'],2);
            $final_task_value=$avg_grth;
        }
        else{
             if($_REQUEST['employee']!=""){
            $user=DB_fetch_assoc(DB_query("select w.id,e.emp_name,w.realname from empmaster e,www_users w where w.empid=e.id and e.id='".$_REQUEST['employee']."' ",$db));
           $grth=DB_fetch_assoc(DB_query("SELECT SUM(total_com)as tot,COUNT(*)as cnt FROM perf_task_assign a,perf_task_assign_details b WHERE a.user_id='". $user['id']."' and a.id=b.task_id ",$db)) ; 
           $avg_grth=round($grth['tot']/$grth['cnt'],2);
            $final_task_value=$avg_grth;
        }
        else
        {    
            $str='';
         $user=DB_query("select id from www_users where mst_dpt_heirarchy='".$_REQUEST['dept']."' ",$db);
            while($user_res=DB_fetch_assoc($user)){
                $str1[]=$user_res['id'];
            }
            $str=implode(',',$str1);
           //echo $str;
           if(isset($str)){
            $grth=DB_fetch_assoc(DB_query("SELECT SUM(total_com)as tot,COUNT(*)as cnt FROM perf_task_assign a,perf_task_assign_details b WHERE a.user_id in (".$str.") and a.id=b.task_id ",$db)) ; 
            
           $avg_grth=round($grth['tot']/$grth['cnt'],2);
            $final_task_value=$avg_grth;
           }
        }
        
        }
       
    }else
    {
          if($_REQUEST['team']!=""){
   
           $teamsql=DB_fetch_assoc(DB_query("select SUM(total_com)as tot,COUNT(*)as cnt FROM perf_task_assign a,perf_task_assign_details b WHERE a.assign_to='team' and a.teamid='". $_REQUEST['team']."' and a.id=b.task_id ",$db)) ;
           $avg_grth=round($teamsql['tot']/$teamsql['cnt'],2);
            $final_task_value=$avg_grth;
        }
       
    }
}
else
{

    $grth=DB_fetch_assoc(DB_query("SELECT SUM(total_com)as tot,COUNT(*)as cnt FROM perf_task_assign a,perf_task_assign_details b WHERE a.user_id='". $_SESSION['UserID']."' and a.id=b.task_id ",$db)) ;
     $avg_grth=round($grth['tot']/$grth['cnt'],2);
     $final_task_value=$avg_grth;
        
}

?>

<div style="width: 100%">
    <div style="width: 43%;float:left;background-color: #ccc;">
        <form method="post" action="">
        <table width="100%" >
            <tr>
        <th>Department</th>
            <td><select name="dept" id="dept" onchange="javascript:getusers(this,'dept')">
            <option value="">Select</option>
        <?php 
        foreach($LowerDeptList as $val){ 
         $dpt= $s->GetFieldValue('deptmaster_hierarchy'," id='".$val."' " ,'deptname');	 
          $p='';
         if(isset($_REQUEST['search']))
         {
            
             if($val==$_REQUEST['dept'])
             {
                 $p="selected";
             }
         }
         else
         {
             if($val==$deptid)
             {
                 $p="selected";
             }
         }
         ?>
            <option value="<?php echo $val; ?>" <?php  echo $p; ?> ><?php echo $dpt; ?></option>
            <?php } ?>
            </select></td>
            </tr>
            <tr>
                <th>Team</th>
                <td><select name="team" id="team" onchange="javascript:getusers(this,'team')" ><option value="">Select</option><?php $teamsql= DB_query("select * from perf_team_master where companyid='".$_SESSION['ACGCompanyId']."' ",$db); while($teamres=DB_fetch_assoc($teamsql)) { ?> <option value="<?php echo  $teamres['id'] ?>" <?php if($teamres['id']==$_REQUEST['team']) { echo "selected"; } ?> ><?php echo  $teamres['team_name'] ?></option> <?php } ?>
                    </select>
                </td>
            </tr>
            <tr>
            <th>Employee</th>
            
        <td id="empdata"><select name="employee" id="employee" >
                <option value="">Select</option>
                 <?php 
                 if(isset($_REQUEST['search']))
                 {
                  $usersql=Db_query("select * from www_users where mst_dpt_heirarchy='".$_REQUEST['dept']."'",$db);
                 }
                 else
                 {
                    $usersql=Db_query("select * from www_users where mst_dpt_heirarchy='".$deptid."'",$db); 
                 }
                  while($user=DB_fetch_assoc($usersql))
                  {
                      $emp1=DB_fetch_assoc(DB_query("select e.id,e.emp_name from empmaster e,www_users w where w.empid=e.id and e.id='".$user['empid']."' ",$db)); 
                      $p1='';
         if(isset($_REQUEST['search']))
         {
            
             if($emp1['id']==$_REQUEST['employee'])
             {
                 $p1="selected";
             }
         }
         else
         {
             if($user['id']==$_SESSION['UserID'])
             {
                 $p1="selected";
             }
         }
                 ?>
                <option value="<?php  echo $emp1['id']  ?>" <?php echo $p1; ?>  ><?php  echo $emp1['emp_name']  ?></option>
                <?php }     ?>
            </select></td>
            </tr>
            
            <tr>
                <td colspan="2" style="text-align:center"><input type="submit" value="search" name="search" class="search"></td>
            </tr>
       </table>
            </form>
</div>   
<div id="chartContainer" style="float:left; width: 45%;"></div>
</div>
<div  style="float:left; width: 12%; text-align: center; padding-top: 70px;">
    <?php $colorsql=DB_query("select * from perf_range_master",$db); 
    while($colorres=DB_fetch_assoc($colorsql)) { ?>
    <div style=" float:left; width: 100%; text-align:left; "><span style="background-color:<?php echo  $colorres['color_code']; ?> ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: medium"><strong>&nbsp;&nbsp;&nbsp;<?php echo $colorres['perf_caption'] ?></strong></span></div> <br>
        <?php } ?>
</div>
<div style="clear:both;"></div>
</div>
<?php $range= DB_fetch_assoc(DB_query("select min(rangefrom),max(rangeto) from perf_range_master",$db)); ?>

<input type="hidden" id="minvalue" value="<?php echo $range['min(rangefrom)'] ?>" >
<input type="hidden" id="maxvalue" value="<?php echo $range['max(rangeto)'] ?>" >
<input type="hidden" id="finalvalue" value="<?php echo $final_task_value; ?>" >

<script type="text/javascript">
var minvalue=$('#minvalue').val();
var maxvalue=$('#maxvalue').val();
var finalvalue=$('#finalvalue').val();
$(function ()  
{
    $("#chartContainer").dxCircularGauge({
    scale: {
        startValue: minvalue,
        endValue: maxvalue,
        majorTick: {
        tickInterval: 10
        }
    },

    rangeContainer: {
                backgroundColor: "none",
                ranges: [
                <?php $sql=DB_query("select * from perf_range_master",$db);
                   while($result=DB_fetch_assoc($sql)){
                   
                    ?>
                 {
                        startValue:<?php echo $result['rangefrom'];?>,
                        endValue: <?php echo $result['rangeto'];?>,
                        color: "#<?php echo $result['color_code']; ?>"
        
                },
                <?php } ?>
                ]
    },
    needles: [{value: finalvalue}],
    markers: [{value: finalvalue}]
    }); 
}

);

function getusers(str,type)
{
    var team=document.getElementById("team").value;
        //alert(team);
    //alert(str.value)
         var dept=document.getElementById("dept").value;
         //var team=document.getElementById("team").value;
         /*if(dept=="")
             {
                 document.getElementById("team").value='';
                 document.getElementById("employee").value='';
             }*/
         
         var xmlhttp;
         if (window.XMLHttpRequest)
             {// code for IE7+, Firefox, Chrome, Opera, Safari
                 xmlhttp=new XMLHttpRequest();
             }
         else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
             }
         xmlhttp.onreadystatechange=function()
         {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            // alert(xmlhttp.responseText);
            document.getElementById("empdata").innerHTML=xmlhttp.responseText;
            }
         }
     
     if((type=="dept") && (dept!=""))
     {
          //   alert("ajax_alldeptuser.php?emp="+str.value+"&type="+type);
            xmlhttp.open("POST","ajax_alldeptuser.php?emp="+str.value+"&type="+type,true);
            xmlhttp.send();
     }
    else if((type=="team") && (dept!=""))
    {
        //alert("ajax_alldeptuser.php?team="+str.value+"&dept="+dept);
        xmlhttp.open("POST","ajax_alldeptuser.php?team="+str.value+"&dept="+dept,true);
        xmlhttp.send();
    }
    else if((type=="team") && (dept==""))
    {
       
         // alert("ajax_alldeptuser.php?team="+team);
            xmlhttp.open("POST","ajax_alldeptuser.php?team="+team,true);
            xmlhttp.send();
    }
    
}
</script>
    