<?php
session_start();
require_once('Connections/courier.php');
require_once 'functions/functions.php';

$date = date('Y-m-d');
$branch_id = $_SESSION['branch_id'];
$total_completed = mysql_fetch_assoc(mysql_query("SELECT COUNT(id) AS total_count FROM job_activities_status WHERE DATE(date_time)='".$date."' AND branch_id='$branch_id'"));
$query_job_in_transit = mysql_fetch_assoc(mysql_query("SELECT COUNT(a.id) AS total_count FROM process_claim_no a left join patient_information b on a.patient_no=b.card_number left join guest_patients g ON a.patient_no=g.card_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE a.audit_status='Job In-Transit' AND j.status='open' AND j.job_status=a.audit_status AND a.patient_status = 'In Use' AND (j.departure='$branch_id' OR j.arrival='$branch_id') ORDER BY j.id DESC"));
$query_job_received = mysql_fetch_assoc(mysql_query("SELECT COUNT(a.id) AS total_count FROM process_claim_no a left join patient_information b on a.patient_no=b.card_number left join guest_patients g ON a.patient_no=g.card_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE a.audit_status='Job Received' AND j.job_status=a.audit_status AND j.status='open' AND a.patient_status = 'In Use' AND a.status_location_id='$branch_id' ORDER BY j.id DESC"));
$query_job_in_progress = mysql_fetch_assoc(mysql_query("SELECT COUNT(a.id) AS total_count FROM process_claim_no a left join patient_information b on a.patient_no=b.card_number left join guest_patients g ON a.patient_no=g.card_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE a.audit_status='Job In-Progress' AND j.job_status=a.audit_status AND j.status='open' AND a.patient_status = 'In Use' AND a.status_location_id='$branch_id' GROUP BY a.process_claim_no ORDER BY j.id DESC"));


$last_7_days = mysql_query("SELECT DISTINCT DATE(date_time) AS date_time FROM job_activities_status ORDER BY date_time ASC LIMIT 7");
while($row = mysql_fetch_assoc($last_7_days)){
    $days_of_week = $days_of_week . "'" .date('D, jS F', strtotime($row['date_time'])). "',";
}

$last_7_days_sales = mysql_query("SELECT DISTINCT DATE(date_time) AS date_time FROM job_activities_status ORDER BY date_time ASC LIMIT 7");
while($last_7_days2_row = mysql_fetch_assoc($last_7_days_sales)){
    $days_of_week_individual = $days_of_week_individual . "" .getWeeklySales($last_7_days2_row['date_time'], 'individual'). ",";
    $days_of_week_all = $days_of_week_all . "" .getWeeklySales($last_7_days2_row['date_time'], 'all'). ",";
} 

function getWeeklySales($date, $type){
    if($type == 'individual'){
    $sales = mysql_fetch_assoc(mysql_query("SELECT COUNT(DISTINCT process_claim_no) AS total_count FROM job_activities_status WHERE job_status_other='Job Completed' AND DATE(date_time) = '$date' AND user_id='".$_SESSION['myMM_Userid']."' AND branch_id='".$_SESSION['branch_id']."'"));
    }else {
    $sales = mysql_fetch_assoc(mysql_query("SELECT COUNT(DISTINCT process_claim_no) AS total_count FROM job_activities_status WHERE job_status_other='Job Completed' AND DATE(date_time) = '$date' AND branch_id='".$_SESSION['branch_id']."'"));
    }
    if($sales['total_count']==''){
        $sales['total_count'] =0;
    }
    return $sales['total_count'];
}



?>
<h1 class="page-title"> Optical Dashboard
                        <small>Statistics, Charts & Reports</small>
                    </h1>
                    <div class="page-bar">
                        <ul class="page-breadcrumb">
                            <li>
                                <i class="icon-home"></i>
                                <a href="index.html">Home</a>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            <li>
                                <span>Dashboard</span>
                            </li>
                        </ul>
                        <div class="page-toolbar">
                            <div class="btn-group pull-right">
                                <button type="button" class="btn btn-fit-height grey-salt dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true"> Actions
                                    <i class="fa fa-angle-down"></i>
                                </button>
                                <ul class="dropdown-menu pull-right" role="menu">
                                    <li>
                                        <a href="#">
                                            <i class="icon-bell"></i> Action</a>
                                    </li>
                                    <li>
                                        <a href="#">
                                            <i class="icon-shield"></i> Another action</a>
                                    </li>
                                    <li>
                                        <a href="#">
                                            <i class="icon-user"></i> Something else here</a>
                                    </li>
                                    <li class="divider"> </li>
                                    <li>
                                        <a href="#">
                                            <i class="icon-bag"></i> Separated link</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <!-- END PAGE HEADER-->
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('summary_today_sales');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-green-sharp">
                                            <span data-counter="counterup" data-value="<?php echo $total_completed['total_count']; ?>">0</span>
                                            <small class="font-green-sharp"></small>
                                        </h3>
                                        <small>TODAY'S COMPLETED JOBS</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-pie-chart"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 100%;" class="progress-bar progress-bar-success green-sharp"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('credit_sales_administrative');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-red-haze">
                                            <span data-counter="counterup" data-value="<?php echo $query_job_in_transit['total_count']; ?>">0</span>
                                            <small class="font-red-haze"></small>
                                        </h3>
                                        <small>JOBS IN-TRANSIT</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-like"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 100%;" class="progress-bar progress-bar-success red-haze"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('list_reserved_frames');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-blue-sharp">
                                            <span data-counter="counterup" data-value="<?php echo $query_job_received['total_count']; ?>">0</span>
                                        </h3>
                                        <small>RECEIVED JOBS</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-users"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 45%;" class="progress-bar progress-bar-success blue-sharp"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('list_of_appointments_today');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-purple-soft">
                                            <span data-counter="counterup" data-value="<?php echo $query_job_in_progress['total_count']; ?>">0</span>
                                        </h3>
                                        <small>JOBS IN PROGRESS</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-user"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 57%;" class="progress-bar progress-bar-success purple-soft"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12 col-xs-12 col-sm-12">
                            <div class="portlet light ">
                                <div class="portlet-title">
                                    <div class="caption">
                                        <span class="caption-subject bold uppercase font-dark">PAST 7 DAYS COMPLETED JOBS</span>
                                    </div>
                                </div>
                                <div class="portlet-body">
                                <div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6 col-xs-12 col-sm-12" style="display:none;">
                            <div class="portlet light ">
                                <div class="portlet-title">
                                    <div class="caption ">
                                        <span class="caption-subject font-dark bold uppercase">Total Monthly Payment Mode</span>
                                    </div>
                                </div>
                                <div class="portlet-body">
                                <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
                                </div>
                            </div>
                        </div>
                    </div>

                   <head> 
                    <script src="assets/global/plugins/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
   $(document).ready(function() {
Highcharts.chart('container2', {
    chart: {
        type: 'areaspline'
    },
    title: {
        text: 'PAST 7 DAYS COMPLETED JOBS'
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        verticalAlign: 'top',
        x: 150,
        y: 100,
        floating: true,
        borderWidth: 1,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    xAxis: {
        categories: [<?php echo $days_of_week; ?>],
        plotBands: [{ // visualize the weekend
            from: 4.5,
            to: 6.5,
            color: 'rgba(68, 170, 213, .2)'
        }]
    },
    yAxis: {
        title: {
            text: 'COMPLETED JOBS'
        }
    },
    tooltip: {
        shared: true,
        valueSuffix: ''
    },
    credits: {
        enabled: false
    },
    plotOptions: {
        areaspline: {
            fillOpacity: 0.5
        }
    },
    series: [
        {
        name: 'Personal Account',
        data: [<?php echo $days_of_week_individual; ?>]
    }, {
        name: 'Lab Department',
        data: [<?php echo $days_of_week_all; ?>]
    }
    ]
});

});
</script>
</head>