<?php
include("../../packages/require.php");
include("controller/controller_report.php");
$curpage = "report-shipment";
$page_name = "module/report/index.php";
if(is_array($data_charts)){
    $sum_ship = 0;
    $total_array = count($data_charts);
    foreach($data_charts as $charts){
        $sum_ship = $sum_ship + $charts['total_po'];
    }
    $average = round($sum_ship/$total_array);
}
function checkNull($param){
    $result = "";

    if($param != NULL){
        $result = $param;
    } else {
        $result = 0;
    }

    return $result;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?=$title['report'];?></title>
    <?php include($global['root']."packages/head.php");?>
</head>
<body class="<?=$global['body-css'];?>">
    <div class="wrapper">

        <?php include($global['root']."section/header.php");?>
      
        <?php include($global['root']."section/sidebar.php");?>

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                  <h1>
                    Module Report Purchase Order
                    <small>Showing report purchase order</small>
                  </h1>
                  <ol class="breadcrumb">
                        <li><a href="<?=$path['dashboard'];?>"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Report Purchase Order</li>
                  </ol>
            </section>

            <!-- Main content -->
            <section class="content">
                <!-- Default box -->
                <div class="box">
                    <div class="box-header with-border">
                        <div class="row">
                            <div class="col-sm-4">
                                <h3 class="box-title full-xs xs-down10">
                                    Report Purchase Order
                                </h3>
                            </div>
                            <div class="col-sm-8">
                                <div class="head-btn text-right"></div>
                            </div>
                        </div>
                    </div>
                    <div class="box-body">
                        <div class="nav-tabs-custom">
                            <ul class="nav nav-tabs" style="border-bottom: 0;">
                                <li class="active">
                                    <a href="<?=$path['report'];?>">Report Purchase Order</a>
                                </li>
                            </ul>
                        </div>
                        <?php if($acsr_chart == 1){ ?>
                        <h4 class="cbox-title">Purchase Order Summary</h4>
                        <div class="row">
                            <div class="col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group down1">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <?php if($C_start_format != ""){ ?>
                                    <input type="text" class="form-control" placeholder="Filter Date" id="cdate-filter" value="<?=date("m/d/Y", strtotime($C_start_format));?> - <?=date("m/d/Y", strtotime($C_end_format));?>">
                                    <?php } else { ?>
                                    <input type="text" class="form-control" placeholder="Filter Date" id="cdate-filter">
                                    <?php } ?>
                                    <input type="hidden" id="csdate" value="<?=date("d-m-Y", strtotime($C_start_format));?>">
                                    <input type="hidden" id="cedate" value="<?=date("d-m-Y", strtotime($C_end_format));?>">
                                </div>
                            </div>
                            <div class="col-sm-3 col-xs-12">
                                <div class="input-group down1">
                                    <span class="input-group-addon"><i class="fa fa-filter"></i></span>
                                    <select class="form-control input-sort" name="cregion" id="cregion-select">
                                        <option value="0">All Region</option>
                                        <?php foreach($data_region as $region){ ?>
                                        <option <?php if($C_region == $region['region_id']){ echo "selected"; } ?> value="<?=$region['region_id'];?>"><?=ucfirst($region['region_title']);?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-3 col-xs-12">
                                <div class="input-group down1">
                                    <span class="input-group-addon"><i class="fa fa-filter"></i></span>
                                    <select class="form-control input-sort" name="cstatus" id="cstatus-select">
                                        <option <?php if($C_status == 'all'){ echo "selected"; } ?> value="all">All Status</option>
                                        <option <?php if($C_status == 'open'){ echo "selected"; } ?> value="open">Open</option>
                                        <option <?php if($C_status == 'prepared'){ echo "selected"; } ?> value="prepared">Prepared</option>
                                        <option <?php if($C_status == 'progress'){ echo "selected"; } ?> value="progress">Progress</option>
                                        <option <?php if($C_status == 'complete'){ echo "selected"; } ?> value="complete">Complete</option>
                                        <option <?php if($C_status == 'cancel'){ echo "selected"; } ?> value="cancel">Cancel</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-3 col-xs-12">
                                <div class="text-right">
                                    <?php if($C_start_date != "" || $C_region != "0"){ ?>
                                    <a href="<?=$path['report'];?>" id="cbtn-clear" class="btn btn-default full-xs xs-down10">
                                        <i class="fa fa-times"></i> Clear Filter
                                    </a>
                                    <?php } ?>
                                    <?php if($acsr_export == 1){ ?>
                                    <a href="javascript:void(0);" onclick="confirmExportSummary();" class="btn bg-maroon full-xs xs-down10 hide">
                                        <i class="fa fa-download"></i> Export Report
                                    </a>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                        <div class="alert alert-info alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <h4><i class="icon fa fa-info"></i> Info!</h4>
                            This chart shows the movement of new purchase orders that enter the system every day and you can filter by date and source of the order.
                        </div>
                        <div class="graphic-box">
                            <?php if(is_array($data_charts)){ ?>
                            <div class="row">
                                <div class="col-md-2 col-sm-3 col-xs-12">
                                    <div class="hcontent-chart">
                                        <div class="htext-chart">Highest PO</div>
                                        <div class="hnum-chart"><?=$data_charts[0]['highest_total'];?></div>
                                        <div class="hdate-chart"><?=date("d M Y", strtotime($data_charts[0]['highest_date']));?></div>
                                    </div>
                                </div>
                                <div class="col-md-2 col-sm-3 col-xs-12">
                                    <div class="hcontent-chart">
                                        <div class="htext-chart">Lowest PO</div>
                                        <div class="hnum-chart"><?=$data_charts[0]['lowest_total'];?></div>
                                        <div class="hdate-chart"><?=date("d M Y", strtotime($data_charts[0]['lowest_date']));?></div>
                                    </div>
                                </div>
                                <div class="col-md-2 col-sm-3 col-xs-12">
                                    <div class="hcontent-chart">
                                        <div class="htext-chart">Average PO</div>
                                        <div class="hnum-chart"><?=$average;?></div>
                                        <div class="hdate-chart">/ Days</div>
                                    </div>
                                </div>
                            </div>
                            <div class="chart-responsive">
                                <div class="chart" id="line-chart" style="height: 300px;"></div>
                            </div>
                            <?php  } else { ?>
                            <div class="chart-empty">
                                Data graphic charts not available.
                            </div>
                            <?php } ?>
                        </div>
                        <?php } ?>
                        <input type="hidden" value="<?=$N_start_date;?>" id="start-date">
                        <input type="hidden" value="<?=$N_end_date;?>" id="end-date">
                        <input type="hidden" value="1" id="table-page"> 
                        <input type="hidden" id="redir-po" value="<?=$path['po'];?>">
                        <input type="hidden" id="redir-url" value="<?=$path['report'];?>">
                        <?php if($acsr_bar == 1){ ?>
                        <h4 class="cbox-title">Region Report Summary</h4>
                        <div class="row">
                            <div class="col-sm-3 col-xs-12">
                                <div class="input-group down1">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <?php if($B_start_format != ""){ ?>
                                    <input type="text" class="form-control" placeholder="Filter Date" id="bdate-filter" value="<?=date("m/d/Y", strtotime($B_start_format));?> - <?=date("m/d/Y", strtotime($B_end_format));?>">
                                    <?php } else { ?>
                                    <input type="text" class="form-control" placeholder="Filter Date" id="bdate-filter">
                                    <?php } ?>
                                    <input type="hidden" id="bsdate" value="<?=date("d-m-Y", strtotime($B_start_format));?>">
                                    <input type="hidden" id="bedate" value="<?=date("d-m-Y", strtotime($B_end_format));?>">
                                </div>
                            </div>
                            <div class="col-sm-3 col-xs-12">
                                <div class="input-group down1">
                                    <span class="input-group-addon"><i class="fa fa-filter"></i></span>
                                    <select class="form-control input-sort" name="bstatus" id="bstatus-select">
                                        <option <?php if($B_status == 'all'){ echo "selected"; } ?> value="all">All Status</option>
                                        <option <?php if($B_status == 'open'){ echo "selected"; } ?> value="open">Open</option>
                                        <option <?php if($B_status == 'prepared'){ echo "selected"; } ?> value="prepared">Prepared</option>
                                        <option <?php if($B_status == 'progress'){ echo "selected"; } ?> value="progress">Progress</option>
                                        <option <?php if($B_status == 'complete'){ echo "selected"; } ?> value="complete">Complete</option>
                                        <option <?php if($B_status == 'cancel'){ echo "selected"; } ?> value="cancel">Cancel</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-6 col-xs-12">
                                <div class="text-right">
                                    <?php if($B_start_date != "" || $B_status != "all"){ ?>
                                    <a href="<?=$path['report'];?>" id="cbtn-clear" class="btn btn-default full-xs xs-down10">
                                        <i class="fa fa-times"></i> Clear Filter
                                    </a>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                        <div class="alert alert-info alert-dismissible">
                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                            <h4><i class="icon fa fa-info"></i> Info!</h4>
                            This chart shows the total amount purchase orders each region that enter the system which status po is not cancel and you can filter by date.
                        </div>
                        <div class="graphic-box">
                            <?php if(is_array($data_charts)){ ?>
                            <div class="chart-responsive">
                                <div class="chart" id="bar-chart" style="height: 300px;"></div>
                            </div>
                            <?php  } else { ?>
                            <div class="chart-empty">
                                Data graphic charts not available.
                            </div>
                            <?php } ?>
                        </div>
                        <?php } ?>

                        <?php if($acsr_table == 1){ ?>
                        <h4 class="cbox-title">Status Purchase Order Report</h4>
                        <div class="row hide">
                            <div class="col-md-3 col-sm-3 col-xs-12">
                                <div class="input-group down1">
                                    <div class="input-group-addon">
                                        <i class="fa fa-calendar"></i>
                                    </div>
                                    <?php if($N_start_date != ""){ ?>
                                    <input type="text" class="form-control" placeholder="Filter Date" id="date-filter" value="<?=date("m/d/Y", strtotime($N_start_date));?> - <?=date("m/d/Y", strtotime($N_end_date));?>">
                                    <?php } else { ?>
                                    <input type="text" class="form-control" placeholder="Filter Date" id="date-filter">
                                    <?php } ?>
                                </div>
                            </div>
                            <?php if($N_start_date != ""){ ?>
                            <div class="col-md-3 col-sm-3 col-xs-12">
                                <div class="down1">
                                    <a href="<?=$path['report'];?>" id="btn-clear" class="btn btn-default full-xs xs-down10">
                                        <i class="fa fa-times"></i> Clear Filter
                                    </a>
                                </div>
                            </div>
                            <?php } ?>
                        </div>
                        <div class="body-content">
                            <div class="alert alert-info alert-dismissible">
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                                <h4><i class="icon fa fa-info"></i> Info!</h4>
                                This report shows you about purchase order status that happen in that current date.
                            </div>
                            <div class="table-responsive">
                                <table class="table table-bordered table-middle table-report">
                                    <thead>
                                        <tr>
                                            <th class="text-center">Region</th>
                                            <th class="text-center">Open</th>
                                            <th class="text-center">Prepared</th>
                                            <th class="text-center">Progress</th>
                                            <th class="text-center">Complete</th>
                                            <th class="text-center">Cancel</th>
                                            <th class="text-center">Total</th>
                                        </tr>
                                    </thead>
                                    <tbody id="table-list">
                                        <?php if(is_array($data_stats)){ $sum=0;foreach($data_stats as $stats){ ?>
                                        <tr>
                                            <td class="text-center bold"><?=ucfirst($stats['region_title']);?></td>
                                            <td class="text-center bold"><?=checkNull($stats['total_open']);?></td>
                                            <td class="text-center bold"><?=checkNull($stats['total_prepared']);?></td>
                                            <td class="text-center bold"><?=checkNull($stats['total_progress']);?></td>
                                            <td class="text-center bold"><?=checkNull($stats['total_complete']);?></td>
                                            <td class="text-center bold"><?=checkNull($stats['total_cancel']);?></td>
                                            <td class="text-center bold"><?=checkNull($stats['total_po']);?></td>
                                        </tr>
                                        <?php $sum+=checkNull($stats['total_po']);} } ?>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <td colspan="6" class="text-right bold">Total Purchase Order</td>
                                            <td class="text-center bold"><?=$sum;?></td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                        <?php } ?>
                    </div>
                </div>
                <!-- /.box-footer-->
            </section>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->

        <?php include($global['root']."section/footer.php");?>

    </div>
    <!-- ./wrapper -->

    <?php include($global['root']."packages/footer.php");?>
    <script src="<?=$global['absolute-url'];?>module/report/js/index.js?<?=mt_rand(10,1000);?>"></script>
    <?php if(is_array($data_charts)){ ?>
    <script type="text/javascript">
        // LINE CHART
        const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
        var line = new Morris.Line({
          element: 'line-chart',
          resize: true,
          data: [
            <?php foreach($data_charts as $charts){ ?>
            {y: '<?=date("Y-m-d", strtotime($charts['po_date']));?>', total: <?=$charts['total_po'];?>},
            <?php } ?>
          ],
          xkey: 'y',
          ykeys: ['total'],
          xLabelFormat: function(d) {
          return d.getDate()+' '+(monthNames[d.getMonth()])+' '+d.getFullYear(); 
          },
          labels: ['Total Purchase Order'],
          lineColors: ['#3c8dbc'],
          hideHover: 'auto',
          "dateFormat": function(unixTime) {
                var d = new Date(unixTime);
                var monthNames = [
                    "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
                ];
                return d.getDate() + ' ' + monthNames[d.getMonth()] + ' ' + d.getFullYear();
            }
        });
        $('#line-chart svg').css({width: $('#line-chart').width() });
        $('#line-chart svg').css({height: $('#line-chart').height() });
    </script>
    <?php } ?>
    <?php if(is_array($data_bars)){ ?>
    <script type="text/javascript">
        //BAR CHART
        var bar = new Morris.Bar({
          element: 'bar-chart',
          resize: true,
          data: [
            <?php foreach($data_bars as $bars){ ?>
            {y: '<?=$bars['region_title'];?>', a: '<?=$bars['total_amount'];?>', b: '<?=$bars['total_po'];?>'},
            <?php } ?>
          ],
          barColors: ['#00a65a'],
          xkey: 'y',
          ykeys: ['a'],
          labels: ['Total Amount'],
          xLabelMargin : 10,
          hideHover: 'auto'
        });
    </script>
    <?php } ?>
</body>
</html>