<?php

require("deps.php");
require_once("common.php");


$thedate = date("Y-m-d");

if(!isset($pcrt_weekstart)) {
$pcrt_weekstart = "Sunday";
}

if ($pcrt_weekstart == "Sunday") {
if(date("w") == 0) {
$thedate2 = date("Y-m-d");
} else {
$thedate2 = date("Y-m-d",strtotime("last Sunday"));
}

} else {

if(date("N") == 1) {
$thedate2 = date("Y-m-d");
} else {
$thedate2 = date("Y-m-d",strtotime("last Monday"));
}
}



if ($pcrt_weekstart == "Sunday") {
$lthedate = date("Y-m-d",strtotime("last Saturday"));
$lthedate2 = date("Y-m-d",strtotime("last Saturday") - 518400);
} else {
$lthedate = date("Y-m-d",strtotime("last Sunday"));
$lthedate2 = date("Y-m-d",strtotime("last Sunday") - 518400);
}

$rs_find_wo = "SELECT * FROM pc_wo WHERE dropdate > '$thedate2 00:00:00' AND pcstatus != '7'";
$rs_result = mysqli_query($rs_connect, $rs_find_wo);
$wothisweek = mysqli_num_rows($rs_result);

$rs_find_wo = "SELECT * FROM pc_wo WHERE dropdate > '$lthedate2 00:00:00' AND dropdate < '$lthedate 11:59:59' AND pcstatus != '7'";
$rs_result = mysqli_query($rs_connect, $rs_find_wo);
$wolastweek = mysqli_num_rows($rs_result);

$rs_find_sr = "SELECT * FROM servicerequests WHERE sreq_datetime > '$thedate2 00:00:00'";
$rs_result = mysqli_query($rs_connect, $rs_find_sr);
$srthisweek = mysqli_num_rows($rs_result);

$rs_find_sr = "SELECT * FROM servicerequests WHERE sreq_datetime > '$lthedate2 00:00:00' AND sreq_datetime < '$lthedate 11:59:59'";
$rs_result = mysqli_query($rs_connect, $rs_find_sr);
$srlastweek = mysqli_num_rows($rs_result);

$rs_find_m = "SELECT * FROM messages WHERE messagedatetime > '$thedate2 00:00:00' AND messagedirection = 'in'";
$rs_result = mysqli_query($rs_connect, $rs_find_m);
$mthisweek = mysqli_num_rows($rs_result);

$rs_find_m = "SELECT * FROM messages WHERE messagedatetime > '$lthedate2 00:00:00' AND messagedatetime < '$lthedate 11:59:59' AND messagedirection = 'in'";
$rs_result = mysqli_query($rs_connect, $rs_find_m);
$mlastweek = mysqli_num_rows($rs_result);



echo "<table class=pad10 style=\"width:100%;\">";
echo "<tr>";

$accentcolor = "#4bc2ff";
echo "<td style=\"width:34%;vertical-align:top;\"><div class=radiustop style=\"background:#ffffff;border-top: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";
echo "<span style=\"color:$accentcolor\" class=\"boldme sizemelarger\">$wothisweek</span> &nbsp;";
echo "<span class=\"boldme sizemelarger\">".pcrtlang("This Week")."</span><br>";
echo "<span style=\"color:$accentcolor\" class=\"boldme sizemelarger\">$wolastweek</span> &nbsp;";
echo "<span class=\"boldme sizemelarger\">".pcrtlang("Last Week")."</span></div>";
echo "<div class=radiusbottom style=\"background:$accentcolor; padding:20px; color:#FFFFFF\">";
echo "<span class=boldme style=\"line-height:28px\"> ".pcrtlang("Work Orders")."<i class=\"fa fa-wrench fa-2x floatright\"></i></span></div></td>";


$accentcolor2 = "#FF5F4B";
echo "<td style=\"width:33%;vertical-align:top;\"><div class=radiustop style=\"background:#ffffff;border-top: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";
echo "<span style=\"color:$accentcolor2\" class=\"boldme sizemelarger\">$srthisweek</span> &nbsp;";
echo "<span class=\"boldme sizemelarger\">".pcrtlang("This Week")."</span><br>";
echo "<span style=\"color:$accentcolor2\" class=\"boldme sizemelarger\">$srlastweek</span> &nbsp;";
echo "<span class=\"boldme sizemelarger\">".pcrtlang("Last Week")."</span></div>";
echo "<div class=radiusbottom style=\"background:$accentcolor2; padding:20px; color:#FFFFFF\">";
echo "<span class=\"boldme\" style=\"line-height:28px\"> ".pcrtlang("Service Requests")."<i class=\"fa fa-comments fa-2x floatright\"></i></span></div></td>";

$accentcolor3 = "#4BD34B";
echo "<td style=\"width:33%;vertical-align:top;\"><div class=radiustop style=\"background:#ffffff;border-top: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";
echo "<span style=\"color:$accentcolor3\" class=\"boldme sizemelarger\">$mthisweek</span> &nbsp;";
echo "<span class=\"boldme sizemelarger\">".pcrtlang("This Week")."</span><br>";
echo "<span style=\"color:$accentcolor3\" class=\"boldme sizemelarger\">$mlastweek</span> &nbsp;";
echo "<span class=\"boldme sizemelarger\">".pcrtlang("Last Week")."</span></div>";
echo "<div class=radiusbottom style=\"background:$accentcolor3; padding:20px; color:#FFFFFF\">";
echo "<span class=boldme style=\"line-height:28px\"> ".pcrtlang("Incoming Messages")."<i class=\"fa fa-mobile fa-2x floatright\"></i></span></div></td>";



echo "</tr></table>";




# Average Repair Time

echo "<table class=pad10 style=\"width:100%;\">";
echo "<tr>";

$accentcolor4 = "#777777";
echo "<td><div class=radiustop style=\"background:#ffffff;border-top: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";


?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.bundle.min.js"></script>
<center><canvas id="myLineChart" width="700" height="200"></canvas></center>
<?php


echo "</div><div class=radiusbottom style=\"background:$accentcolor4; padding:20px; color:#FFFFFF\">";
echo "<span class=boldme style=\"line-height:28px\"> ".pcrtlang("Average Repair Time")."<i class=\"fa fa-clock-o fa-2x floatright\"></i></span></div></td>";

echo "</tr></table>";



$shift = 0;

if (function_exists('date_default_timezone_set')) {
date_default_timezone_set("$pcrt_timezone");
}

$currentdatetime = date('Y-m-d H:i:s');

$today = date('N');

$thefirstday = (time() - (86400 * $today));

$thefirstdaytime = 604799 + strtotime(date('Y-m-d 00:00:00', $thefirstday));

$theprev = 0;

while($shift < 53) {

if($shift > 0) {
$weeksago[] = "$shift ".pcrtlang("Weeks Ago");
} else {
$weeksago[] = pcrtlang("This Week");
}



$rs_findwotime = "SELECT SUM(TIMESTAMPDIFF(SECOND,dropdate,readydate)) AS totalseconds FROM pc_wo WHERE readydate != '0000-00-00 00:00:00' AND storeid = '$defaultuserstore' AND sked = '0' AND pcstatus != '7' AND pcstatus != '6' AND readydate < DATE_ADD(dropdate, INTERVAL 10 day) AND (WEEK(dropdate) = WEEK(DATE_SUB('$currentdatetime', INTERVAL $shift week)) AND YEAR(dropdate) = YEAR(DATE_SUB('$currentdatetime', INTERVAL $shift week)))";
$rs_findwotimewo = "SELECT woid FROM pc_wo WHERE readydate != '0000-00-00 00:00:00' AND storeid = '$defaultuserstore' AND sked = '0'  AND pcstatus != '7' AND pcstatus != '6' AND readydate < DATE_ADD(dropdate, INTERVAL 10 day) AND (WEEK(dropdate) = WEEK(DATE_SUB('$currentdatetime', INTERVAL $shift week)) AND YEAR(dropdate) = YEAR(DATE_SUB('$currentdatetime', INTERVAL $shift week)))";



$rs_resulttime = mysqli_query($rs_connect, $rs_findwotime);
$rs_result_qwo = mysqli_fetch_object($rs_resulttime);
$totalseconds = "$rs_result_qwo->totalseconds";
$rs_resulttimewo = mysqli_query($rs_connect, $rs_findwotimewo);
$totaltimerecords = mysqli_num_rows($rs_resulttimewo);

if ($totaltimerecords > 2) {
$averagetime = ($totalseconds / $totaltimerecords) / 86400;
$thedaysonbench = number_format($averagetime,1);
$theaverage[] = "$thedaysonbench";
$theprev = "$thedaysonbench";
} else {
$theaverage[] = "$theprev";
}
$shift = $shift + 2;
}


$theaveragejson = json_encode($theaverage);
$weeksagojson = json_encode($weeksago);

?>

<script>

var data = {
    labels:  <?php echo $weeksagojson; ?>,
    datasets: [
        {
            label: "<?php echo pcrtlang("Days") ?>",
            fill: true,
            lineTension: .5,
            backgroundColor: "rgba(119,119,119,0.4)",
            borderColor: "rgba(119,119,119,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(119,119,119,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 1,
            pointHitRadius: 10,
            data: <?php echo $theaveragejson; ?>,
            spanGaps: false,
        }
   ]
};

var ctx = document.getElementById("myLineChart");
var myLineChart = new Chart(ctx, {
    type: 'line',
    data: data,
 options: {
        responsive: false
    }
});

</script>


<?php


################################################

echo "<br><br><table class=pad10 style=\"width:100%;\">";
echo "<tr>";



$accentcolor2 = "#d787ff";
echo "<td style=\"width:50%;vertical-align:top;\">";
echo "<div class=radiustop style=\"background:$accentcolor2; padding:20px; color:#FFFFFF\">";
echo "<span class=boldme style=\"line-height:28px\"> ".pcrtlang("Device Types Past Month")."<i class=\"fa fa-desktop fa-2x floatright\"></i></span></div>";
echo "<div class=radiusbottom style=\"background:#ffffff;border-bottom: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";

$matnames = array();
$matcount = array();

?>
<canvas id="BarChart" width="350" height="400"></canvas>
<?php

$rs_findmat = "SELECT * FROM mainassettypes";
$rs_result_n = mysqli_query($rs_connect, $rs_findmat);
while($rs_result_qn = mysqli_fetch_object($rs_result_n)) {
$matid = "$rs_result_qn->mainassettypeid";
$matname = "$rs_result_qn->mainassetname";
$matnames[$matid] = "$matname";
$matcount[$matid] = "0";
}

$rs_find_wo = "SELECT * FROM pc_wo WHERE dropdate > DATE_SUB(NOW(), INTERVAL 30 day)";
$rs_result = mysqli_query($rs_connect, $rs_find_wo);
while($rs_result_q5 = mysqli_fetch_object($rs_result)) {
$pcid = "$rs_result_q5->pcid";
$rs_findowner = "SELECT * FROM pc_owner WHERE pcid = '$pcid'";
$rs_resultt = mysqli_query($rs_connect, $rs_findowner);
$rs_result_q = mysqli_fetch_object($rs_resultt);
$womatid = "$rs_result_q->mainassettypeid";
$matcount[$womatid] = $matcount[$womatid] + 1;
}

$theservicejson = json_encode(array_values($matnames));
$servicetotaljson = json_encode(array_values($matcount));






?>


<script>

var data = {
    labels: <?php echo $theservicejson; ?>,
    datasets: [
        {
            label: "<?php echo pcrtlang("Device Type"); ?>",
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)',
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)',
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1,
            data: <?php echo $servicetotaljson; ?>,
        }
    ]
};

var ctx = document.getElementById("BarChart");
var BarChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: data,
options: {
        responsive: false
    }
});


</script>
<?php



echo "</td>";

$accentcolor3 = "#0077EE";
echo "<td style=\"width:50%;vertical-align:top;\">";
echo "<div class=radiustop style=\"background:$accentcolor3; padding:20px; color:#FFFFFF\">";
echo "<span class=boldme style=\"line-height:28px\"> ".pcrtlang("Top Services Past Month")."<i class=\"fa fa-briefcase fa-2x floatright\"></i></span></div>";
echo "<div class=radiusbottom style=\"background:#ffffff;border-bottom: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";

$theservice = array();
$servicetotal = array();

?>
<canvas id="BarChartST" width="350" height="400"></canvas>
<?php

$rs_find_current_woid = "SELECT COUNT(sold_price) AS servicecount, labor_desc FROM sold_items WHERE date_sold > DATE_SUB(NOW(), INTERVAL 30 day) AND sold_type = 'labor' GROUP BY labor_desc ORDER BY servicecount DESC LIMIT 10";
$rs_result_pur = mysqli_query($rs_connect, $rs_find_current_woid);

while($rs_result_q = mysqli_fetch_object($rs_result_pur)) {
$labor_desc = "$rs_result_q->labor_desc";
$servicecount = "$rs_result_q->servicecount";


if($servicecount > 1) {
$theservice[] = $labor_desc;
$servicetotal[] = $servicecount;
}


}

$theservicejson = json_encode($theservice);
$servicetotaljson = json_encode($servicetotal);

?>


<script>

var data = {
    labels: <?php echo $theservicejson; ?>,
    datasets: [
        {
            label: "<?php echo pcrtlang("Service Count"); ?>",
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)',
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)',
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1,
            data: <?php echo $servicetotaljson; ?>,
        }
    ]
};

var ctx = document.getElementById("BarChartST");
var BarChartST = new Chart(ctx, {
    type: 'horizontalBar',
    data: data,
options: {
        responsive: false
}
});


</script>
<?php



echo "</div>";
echo "</td>";



echo "</tr></table>";

echo "<table class=pad10 style=\"width:100%\">";

$accentcolor = "#4BD34B";
echo "<td style=\"width:100%;vertical-align:top;\">";
echo "<div class=radiustop style=\"background:$accentcolor; padding:20px; color:#FFFFFF\">";
echo "<span class=boldme style=\"line-height:28px\"> ".pcrtlang("Recent Incoming Messages")."<i class=\"fa fa-mobile fa-2x floatright\"></i></span></div>";
echo "<div class=radiusbottom style=\"background:#ffffff;border-bottom: #cccccc 1px solid;border-right: #cccccc 1px solid;border-left: #cccccc 1px solid;padding:20px\">";



echo "<table class=pad5>";
$rs_findmessages = "SELECT * FROM messages WHERE messagedirection = 'in' ORDER BY messagedatetime DESC LIMIT 6";
$rs_result_n = mysqli_query($rs_connect, $rs_findmessages);
while($rs_result_qn = mysqli_fetch_object($rs_result_n)) {
$messageid = "$rs_result_qn->messageid";
$messagebody = "$rs_result_qn->messagebody";
$messagefrom = "$rs_result_qn->messagefrom";
$messagedatetime = "$rs_result_qn->messagedatetime";

if(strlen($messagebody) > 150) {
$messagebody = substr("$messagebody", 0, 150)."...";
}


#$messagebody = wordwrap($messagebody, 60, "<br />\n", true);

#$res_str = array_chunk(explode(" ",$messagebody),9);
#foreach( $res_str as &$val){
#   $val  = implode(" ",$val);
#}

echo "<tr><td><i class=\"fa fa-user fa-2x\" style=\"color:$accentcolor\"></i></td><td style=\"text-align:left;\"><span class=\"sizemelarger\">$messagebody</span><br><span style=\"color:$accentcolor\">".elaps("$messagedatetime")." ".pcrtlang("ago")."</span></td></tr>";
}
echo "</table>";

echo "<a href=messages.php?func=browsemessages class=\"linkbuttonsmall linkbuttongreen displayblock radiusall\"><i class=\"fa fa-mobile fa-lg\"></i> ".pcrtlang("Browse Messages")."</a>";

echo "</div>";
echo "</td>";
echo "</table>";








