<?php include("_config/dbconfig.php"); ?>
<?php include("_includes/head.php"); ?>
<title>Graph </title>

<style>
    .table-bordered > thead > tr > th, .table-bordered > thead > tr > td, .table-bordered  tr  td {
        border-width: 1px !important; 
        border: 1px solid #676767 !important;
    }
    
    .lisitng_table.first_small   td:first-child {
            width: 1%;
        }


</style>

<?php
include("_includes/header.php");

if (isset($_SESSION['user_session'])) {
    $agent_id = $_SESSION['user_session'];
}

$event_name = "";

$ee = explode("/", $_SERVER["PHP_SELF"]);
$link = end($ee) . '?' . $_SERVER["QUERY_STRING"];


//$event_all = $obj_events_class->getEvent();

  if($_SESSION['user_role_session']=="Admin" || $_SESSION['user_role_session']=="Marketing"){
        $event_all = $obj_events_class->getAgetEvent();      
  }else {
            $event_all = $obj_events_class->getAgetEvent($agent_id);
  }


$total_denied_clients = 0;
$DR_details = array();


if (isset($_GET['event']) && !empty($_GET['event']) && isset($_GET['reasons']) && !empty($_GET['reasons'])) {
                
                
        
                $exhibition_id = base64_decode($_GET['event']);
                $event = $obj_events_class->getEvent($exhibition_id);
                $AssignClients2 = $obj_jobs_class->getAssignClients($exhibition_id, $agent_id);
                $total_denied_clients = 0;
                
                $DeniedReasons =  $obj_jobs_class->getDeniedReasons();
                $DR_details = array();
                foreach($DeniedReasons as $DR_dd){
                    $DR_details[$DR_dd['id']]['name']= $DR_dd['reason'];
                    $DR_details[$DR_dd['id']]['id']= $DR_dd['id'];
                    $DR_details[$DR_dd['id']]['count']= 0;
                    
                }
                foreach($AssignClients2 as $aaass){
                            
                    if($aaass['status_order']== 0 ){
                        foreach($DeniedReasons as $dd){
                            if($aaass['denied_reason'] == $dd['id']){
                                $aaass['denied_reason'];
                                $DR_details[$dd['id']]['count']+=1;
                                break;
                            }
                        }
                        $total_denied_clients +=1;
                    }
                }
                
                //echo '<pre>';print_r($AssignClients2); echo '</pre>'; 
                 

}
else if (isset($_GET['event']) && !empty($_GET['event'])) {

    $exhibition_id = base64_decode($_GET['event']);
    $event = $obj_events_class->getEvent($exhibition_id);
    $event_name = $event['event_name'];
    $AssignClients = $obj_jobs_class->getAssignClients($exhibition_id, $agent_id);
    $DeniedReasons =  $obj_jobs_class->getDeniedReasons();
    
    $DR_details = array();
    foreach($DeniedReasons as $DR_dd){
        $DR_details[$DR_dd['id']]['name']= $DR_dd['reason'];
        $DR_details[$DR_dd['id']]['id']= $DR_dd['id'];
        $DR_details[$DR_dd['id']]['count']= 0;

    }
    foreach($AssignClients as $aaass){

        if($aaass['status_order']== 0 ){
            foreach($DeniedReasons as $dd){
                if($aaass['denied_reason'] == $dd['id']){
                    $aaass['denied_reason'];
                    $DR_details[$dd['id']]['count']+=1;
                    break;
                }
            }
            $total_denied_clients +=1;
        }
    }
     
    /*      */
} 
else {
    /*echo "<script>window.location='events.php'</script>";
    die();*/
}
    $modal = '';
?>
<div class="col-sm-12 right_form">
    <div class="title"> 
        <h1><?php  
                    if(isset($event['event_name'])){
                        echo $event['event_name']; 
                        
                    }else {
                        echo "Graph";
                    } 
                ?>  
            <p class="date_d">
                <?php
                if (!empty($event['start_date']))
                    echo $obj_jobs_class->change_date2($event['start_date']);
                if (!empty($event['end_date']))
                    echo ' to ' . $obj_jobs_class->change_date2($event['end_date']);
                
                $xa = '';
                if (!empty($event['start_date']))$xa .= $obj_jobs_class->change_date2($event['start_date']);
                if (!empty($event['end_date']))  $xa .= ' to ' . $obj_jobs_class->change_date2($event['end_date']);
                ?> 
                
            </p>
        </h1>
    </div>
    <ol class="breadcrumb white">
        
        
        <?php if($_SESSION['user_role_session']=="Marketing"){ ?>
        <li><a href="events_marketing.php">Exhibitions</a></li>
        <?php } else {?>
        <li><a href="dashboard.php">Dashboard</a></li>
        <li><a href="events.php">Exhibitions</a></li> 
        <li class="right"><a href="event_graph_dates.php">Create Graph by Exhibition Dates </a></li>
        <?php } ?>
         <li class="active">Create Graph by Exhibitions</li>
        <?php if(isset($_GET['event']))/*{ ?>
            <li class="right"><a href="event_graph.php?event=<?= $_GET['event']?>&reasons=all">| Create Graph only Denied Exhibition</a>  </li>
        <?php } */?>
        
        
        
    </ol>
  

    <div class="col-sm-12 a_client">
        <div class="row">
            <form action="" method="GET" id="_form">
                
                <div class="col-sm-2 col-xs-2 form-group">
                    <label>Select Exhibition </label>                    
                </div>
                <div class="col-sm-3 col-xs-4 form-group">
                  <select class="chosen-select form-control" style="width:100%;" id="exi_id" tabindex="2" name="event" required="required" onchange="goEvent();">
                        <option value="">---Select---</option>
                        <?php foreach($event_all as $tl){   $sel = '';
                            if (isset($_GET['event']) && !empty($_GET['event'])) {
                            $ev_id = base64_decode($_GET['event']);
                            if($ev_id == $tl['event_id']){$sel = "selected='selected'";}
                            }
                            ?>
                        <option value="<?php echo base64_encode($tl['event_id']);?>" <?= $sel ?>><?php echo $tl['event_name'];?></option>
                        <?php } ?>
                    </select>
                </div>
            </form>
            
            <?php
                    $msg = '';
                    $count_comp = 0;  $count_deny = 0;  $count_pip= 0; $count_pen = 0;
                    $count_hot = 0;  $count_warm= 0;  $count_cold= 0; 

 if (isset($AssignClients) && count($AssignClients)>0) {
                    foreach($AssignClients as $a_client){
                        
                        if($a_client['status_order'] == 1){$count_comp +=1;}
                        else if($a_client['status_order'] ==  0){$count_deny +=1;}
                        else if($a_client['status_order'] ==  4){$count_pip +=1;}
                        else if($a_client['status_order'] ==  2){$count_pen +=1;}

                        if($a_client['pipeline']==80 && $a_client['status_order']==4 ){ $count_hot+=1;};
                        if($a_client['pipeline']==10 && $a_client['status_order']==4){ $count_cold+=1;}; 
                        if($a_client['pipeline']==50 && $a_client['status_order']==4) { $count_warm+=1;};
                    }
            ?>
                    
<?php                    

 $msg  = "<table class='lisitng_table table'> ";
 $msg .= " <tr><td> Total Clients: </td><td>".count($AssignClients)." </td></tr>";
 $msg .= "<tr><td> Pending Clients </td><td> ".$count_pen." </td></tr>";
 $msg .= "<tr><td> Accepted Clients </td><td> ".$count_comp." </td></tr>";
 $msg .= "<tr><td> Denied Clients </td><td> ".$count_deny." </td></tr>";
 $msg .= "<tr><td> Hot Clients </td><td> ".$count_hot." </td></tr>";
 $msg .= "<tr><td> Warm Clients </td><td> ".$count_warm." </td></tr>";
 $msg .= "<tr><td> Cold Clients </td><td> ".$count_cold." </td></tr>";
 
 $msg  .= "<tr><td></td><td></td></tr></table>";

?> 
                        
                        
 <?php } else if(isset($AssignClients2) && count($AssignClients2)>0){
        
       // $AssignClients = $AssignClients2;
 }
 
 else if(isset($_GET['event'])){
                        echo "No client found.";
 }else { echo "Select Exhibition. "; }
 
 
 if(isset($_GET['event']) && isset($AssignClients) && count($AssignClients)>0){
 ?>
            <div class="clearfix"></div>  
            
            <div id="Print_Box" style="position: relative;"> 
                
                <a style="position:absolute; right: 20px; top: 20px; cursor: pointer;" onclick="print_box();">Print</a>
            
            <div class="table-result" style="margin: 10px; overflow: hidden;" > 
                
                <h3> <center> <?php echo $event['event_name']?> </center> </h3>
                 
                <div  id="clients_count" class="col-sm-2" style="font-weight: 500; line-height: 30px;">
                <?php if(isset($AssignClients) && (count($AssignClients)>0)) {echo $msg;} ?>
            </div>
            
            <div class="col-sm-8">
                 <div id="container" style="min-height: 400px;  margin: 0 auto;"></div>
            </div>
            
                <div class="col-sm-2" id="clients_list">
                    
                <?php if(isset($AssignClients) && (count($AssignClients)>0)){
                            echo "<table class='lisitng_table table first_small'><tr><td>Sr.</td>
                                          <td>Company Name</td>
                                     </tr>";
                            $sr=1;
                            foreach ($AssignClients as $cc) {  
                                      if($cc['status_order'] == 1){
                                        echo "<tr><td>".$sr++."</td>
                                                  <td>".$cc['company_name']."</td>
                                             </tr>";
                                }
                            }
                            echo "<tr><td></td><td></td></tr></table>";
                }?>
                </div>
            </div>
            
            <div class="clearfix"></div>  
            
            <div class="table-result" style="margin: 10px; overflow: hidden;" > 
                <h3> <center> Denied Clients Summary </center> </h3>
                <div class="col-sm-2" id="denied_reasons" style="font-weight: 500; line-height: 30px;">
                    <?php 
                                $msg  = "<table class='lisitng_table table '> ";
                                $msg .= "<tr><td>Total Client  </td><td> ".$total_denied_clients." </td></tr>";
                                foreach($DR_details as $d11){
                                        $msg .= "<tr><td>".$d11['name'].' </td><td> '.$d11['count']." </td></tr>";
                                }
                                echo $msg  .= "<tr><td></td><td></td></tr></table>";
                    ?>
                </div>
                <div class="col-sm-8">
                     <div id="container2" style="min-height: 400px;  margin: 0 auto;"></div>
                </div>
            </div>
            <div class="clearfix"></div>
            </div>
 <?php } ?>             
        </div>
    </div>	
</div>

<?php   include("_includes/footer.php"); ?>
<?php 
        if(isset($AssignClients) && (count($AssignClients)>0)){
?>
<script type="text/javascript">
$(function () {
    $('#container').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            type: 'pie'
        },
        title: {
            text: '', //Exhibition Summary'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            name: 'Clients',
            colorByPoint: true,
            data: [
                
                {
                name: 'Accepted',
                y: <?php echo ($count_comp/count($AssignClients)*100);?>,
                selected: true                
            },{
                name: 'Pending',
                y: <?php echo ($count_pen/count($AssignClients)*100);?>
            }, {
                name: 'Denied',
                y: <?php echo ($count_deny/count($AssignClients)*100);?>,
                sliced: true
            }, {
                name: 'Hot',
                y: <?php echo ($count_hot/count($AssignClients)*100);?>
            }, {
                name: 'Warm',
                y: <?php echo ($count_warm/count($AssignClients)*100);?>
            }, {
                name: 'Cold',
                y: <?php echo ($count_cold/count($AssignClients)*100);?>
            }]
        }]
    });
});
</script>
<?php } ?>                
                
<script type="text/javascript">
$(function () {
    // Create the chart
    $('#container2').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: ''
        },
        subtitle: {
            text: ''
        },
        xAxis: {
            type: 'category'
        },
        yAxis: {
            title: {
                text: ''
            }

        },
        legend: {
            enabled: false
        },
        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true,
                    //format: '{point.y:1f}'
                     format: '{point.y:.1f}%'
                }
            }
        },

        tooltip: {
            headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
            pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
        },

        series: [{
            name: 'Denied Reason',
            colorByPoint: true,
            data: [
                
            <?php  if(count($DR_details)>0) foreach($DR_details as $d11){
                        $abx= ($d11['count']/$count_deny)*100;
                         echo "{ name:'".$d11['name']."', y: ".$abx." }, ";
                    }
            ?>
         ]
        }]
    });
});
		</script>
                
                
<script src="js/highcharts.js"></script>
<script src="js/exporting.js"></script>

<script>

    function goEvent(){
        vv = $("#exi_id").val();
        if(vv!=''){
            window.location = 'event_graph.php?event='+vv+'&1=1';        
        }else{
            window.location = 'event_graph.php';        
        }
    } 
    
    function print_box(){
				var headstr = "<html><head><title>Exhibition Summary</title></head><body>";
				var footstr = "</body>";
                                 /*<h2> <center> </center> </h2>*/
                                cc = "<div style='min-height:1300px; background:#ccc;'>";
                                cc += "<br><h3><center><?php echo $event_name; ?> </center> </h3> <br><p><center> <?php echo $xa?></center> </p>";
                                cc += $("#clients_count").html();
                                cc += '<br> <br>'+$("#container").html();
                                cc += '<br><br>'+$("#clients_list").html();
                                cc += "</div> <br><br>";
                                cc += "<h3> <center>  Denied Clients Summary </center> </h3>";
                                cc += $("#denied_reasons").html();
                                cc += $("#container2").html();

                                //$("#Print_Box").html(cc)

				var newstr = cc; // document.getElementById("Print_Box").innerHTML;
				var oldstr = document.body.innerHTML;
				document.body.innerHTML = headstr+newstr+footstr;
				window.print();
				document.body.innerHTML = oldstr;
				return false;
				
			}
</script>
 

<script>
    $(document).ready(function () {
        $(".start_date").datepicker({
            dateFormat: 'yy-mm-dd',
            numberOfMonths: 1,
            onSelect: function (selected) {
                $(".end_date").datepicker("option", "minDate", selected)
            }
        });
        $(".end_date").datepicker({
            dateFormat: 'yy-mm-dd',
            numberOfMonths: 1,
            onSelect: function (selected) {
                //$("#start_date").datepicker("option","maxDate", selected)
            }
        });
        
        cc = $("#container").html();
        cc += $("#clients_count").html();
        cc += $("#clients_list").html();
        cc += $("#denied_reasons").html();
        
        //$("#Print_Box").html(cc)
        
    });
</script>