<?php
$color_pallete  = wpcfe_report_color_pallete();
$status_report  = wpcfe_report_status();
$begend_dates   = wpcfe_first_last_date();
$records        = array();
$beginning_date = apply_filters( 'wpcfe_dashboard_beginning_date', $begend_dates['first'] );
$ending_date    = apply_filters( 'wpcfe_dashboard_ending_date', $begend_dates['last'] );
$dates          = wpcef_get_dates( $beginning_date, $ending_date );
// Create status variables
if( !empty( $status_report  ) ){
    foreach ($status_report as $s_variable) {
        ${wpcfe_to_slug($s_variable)} = array();
    }
}
foreach ( $dates as $date ) {    
    if( !empty( $status_report  ) ){
        foreach ($status_report as $s_variable) {
            ${wpcfe_to_slug($s_variable)}[] = wpcfe_get_report_count( $date, $s_variable );
        }
    }
}
// Create data set object
$dataset = array();
if( !empty( $status_report  ) ){
    $counter = 0;
    foreach ($status_report as $s_variable) {
        $dataset[] = array(
            'label' => $s_variable,
            'backgroundColor' => '#'.$color_pallete[$counter],
            'borderColor'   => '#'.$color_pallete[$counter],
            'borderWidth'   => 1,
            'data'          => ${wpcfe_to_slug($s_variable)}
        );
        $counter++;
    }
}
?>
<?php do_action( 'wpcfe_before_dashboard_status_report' ); ?>
<div id="wpcfe-status-report" class="row mb-4">
    <?php
    if( !empty( $status_report ) ){
        $total_shipments = wpcfe_get_all_shipment_count();
        $counter = 0;
        foreach ( $status_report as $status ) {
            $shipment_count      = wpcfe_get_shipment_status_count( $status );
            $shipment_percentage = 0;
            if( $total_shipments || $shipment_count ){
                $shipment_percentage = ( $shipment_count / $total_shipments ) * 100;
            }
            ?>   
            <div class="col-sm-6 col-md-3 col-lg-3 mb-4">
                <div class="card classic-admin-card">
                    <div class="card-body">
                        <div class="pull-right">
                        <i class="fa fa-line-chart" style="color:<?php echo '#'.$color_pallete[$counter] ?> !important;"></i>
                        </div>
                        <h6 style="color:<?php echo '#'.$color_pallete[$counter] ?> !important;"><?php echo $status; ?></h6>
                        <h4 class="text-dark h1"><?php echo $shipment_count; ?></h4>
                        <p class="text-dark">
                        <?php
                        printf( __("%s of 100"), number_format($shipment_percentage, 2, '.', ''), $total_shipments );
                        ?>
                        </p>
                    </div>
                    <div class="progress">
                        <div class="progress-bar bg info darken-3" role="progressbar" style="width: <?php echo $shipment_percentage; ?>%; background-color: <?php echo '#'.$color_pallete[$counter] ?> !important;" aria-valuenow="<?php echo $shipment_percentage; ?>" aria-valuemin="0" aria-valuemax="100"></div>
                    </div>
                </div>
            </div>
            <?php
            $counter++;
        }
    }
    ?>
</div>
<?php do_action( 'wpcfe_after_dashboard_status_report' ); ?>
<div id="wpcfe-graph-report" class="row my-4 py-4 border-top bg-white ">
    <div class="col-lg-12">
        <h2 class="h5 text-center"><?php printf( esc_html__('Report for the Month of %s', 'wpcargo-frontend-manager'), date("F",strtotime($begend_dates['last'])) ); ?></h2>
    </div>   
    <div class="col-lg-12 d-block d-sm-none">
        <div class="list-group list-group-flush">
            <?php foreach ($dates as $m_key => $m_date ): ?>
                <p class="h5 py-2 border-bottom" data-toggle="collapse" href="#mdata<?php echo $m_key; ?>" role="button" aria-expanded="false" aria-controls="mdata<?php echo $m_key; ?>"><?php echo $m_date; ?> <i class="fa fa-th-list float-right text-info" aria-hidden="true"></i></p>
                <section id="mdata<?php echo $m_key; ?>" class="<?php echo $m_key != 0 ? 'collapse' : '' ; ?>">
                <?php 
                    $mcounter = 0;
                    foreach ($status_report as $s_variable) {
                        ?>
                        <a class="list-group-item list-group-item-action"><?php echo $s_variable; ?>
                            <span class="badge badge-pill pull-right" style="background-color:<?php echo '#'.$color_pallete[$mcounter] ?> !important; font-size: 1em;"><?php echo ${wpcfe_to_slug($s_variable)}[$m_key]; ?>
                            </span>
                        </a>
                        <?php
                        $mcounter++;
                    }
                ?>
                </section>
            <?php endforeach; ?>
        </div>
    </div>
    <div class="col-lg-12 d-none d-sm-block">
        <?php 
        $template = wpcfe_include_template( 'chart' );
        require_once( $template );
        ?>
    </div>
</div>
<?php do_action( 'wpcfe_after_dashboard_graph_report' ); ?>