<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/*
|--------------------------------------------------------------------------
| Reports Class
|--------------------------------------------------------------------------
*/

class Vendora_Report {

    /*
    |--------------------------------------------------------------------------
    | Init
    |--------------------------------------------------------------------------
    */

    public static function init() {

        add_action(
            'wp_dashboard_setup',
            array( __CLASS__, 'register_dashboard_widget' )
        );

    }

    /*
    |--------------------------------------------------------------------------
    | Register Widget
    |--------------------------------------------------------------------------
    */

    public static function register_dashboard_widget() {

        wp_add_dashboard_widget(
            'vendora_orders_stats',
            'WooCommerce Orders Statistics',
            array( __CLASS__, 'dashboard_widget_content' )
        );

    }

    /*
    |--------------------------------------------------------------------------
    | Render Front Tab
    |--------------------------------------------------------------------------
    */

    public static function render_front_tab() {
        ob_start();
        echo '<div class="vs-tab" id="tab-reports-dashboard" style="display:none;">';
        self::dashboard_widget_content();
        echo '</div>';
        return ob_get_clean();
    }

    public static function dashboard_widget_content() {

        if ( ! class_exists( 'WooCommerce' ) ) {

            echo '<p>WooCommerce is not installed.</p>';
            return;

        }

        /*
        |--------------------------------------------------------------------------
        | Orders Statistics
        |--------------------------------------------------------------------------
        */

        $completed_orders  = wc_orders_count( 'completed' );
        $processing_orders = wc_orders_count( 'processing' );
        $pending_orders    = wc_orders_count( 'pending' );

        $total_orders = (
            $completed_orders +
            $processing_orders +
            $pending_orders
        );

        /*
        |--------------------------------------------------------------------------
        | Revenue
        |--------------------------------------------------------------------------
        */

        $orders = wc_get_orders( array(
            'limit'  => -1,
            'status' => array( 'wc-completed' ),
        ) );

        $total_revenue = 0;

        foreach ( $orders as $order ) {

            $total_revenue += $order->get_total();

        }

        /*
        |--------------------------------------------------------------------------
        | Completion Rate
        |--------------------------------------------------------------------------
        */

        $completion_rate = 0;

        if ( $total_orders > 0 ) {

            $completion_rate = round(
                ( $completed_orders / $total_orders ) * 100
            );

        }

        /*
        |--------------------------------------------------------------------------
        | Latest Orders
        |--------------------------------------------------------------------------
        */

        $latest_orders = wc_get_orders( array(
            'limit'   => 5,
            'orderby' => 'date',
            'order'   => 'DESC',
        ) );

        ?>

        <style>

            .vendora-stats-grid{
                display:grid;
                grid-template-columns:repeat(2,1fr);
                gap:15px;
                margin-top:15px;
            }

            .vendora-stat-box{
                background:#fff;
                border-radius:12px;
                padding:20px;
                box-shadow:0 2px 10px rgba(0,0,0,0.08);
                border-left:5px solid #6c5ce7;
            }

            .vendora-stat-box h3{
                margin:0;
                font-size:14px;
                color:#666;
            }

            .vendora-stat-box p{
                margin-top:10px;
                font-size:28px;
                font-weight:bold;
                color:#111;
            }

            .vendora-progress{
                width:100%;
                height:12px;
                background:#eee;
                border-radius:20px;
                overflow:hidden;
                margin-top:10px;
            }

            .vendora-progress-bar{
                height:100%;
                background:#00b894;
            }

            .vendora-latest-orders{
                margin-top:25px;
            }

            .vendora-latest-orders table{
                width:100%;
                border-collapse:collapse;
            }

            .vendora-latest-orders th,
            .vendora-latest-orders td{
                padding:10px;
                border-bottom:1px solid #eee;
                text-align:left;
            }

        </style>


       
        <div style="margin-top:25px;">

            <h3>
                Completion Rate:
                <?php echo esc_html( $completion_rate ); ?>%
            </h3>

            <div class="vendora-progress">

                <div class="vendora-progress-bar"
                     style="width:<?php echo esc_attr( $completion_rate ); ?>%;">
                </div>

            </div>

        </div>

       <?php

/*
|--------------------------------------------------------------------------
| Monthly Orders Data
|--------------------------------------------------------------------------
*/

$monthly_orders = array();

for ( $i = 1; $i <= 12; $i++ ) {

    $start_date = date( 'Y-' . sprintf( '%02d', $i ) . '-01' );
    $end_date   = date( 'Y-' . sprintf( '%02d', $i ) . '-t' );

    $orders = wc_get_orders( array(
        'limit'        => -1,
        'date_created' => $start_date . '...' . $end_date,
    ) );

    $monthly_orders[] = count( $orders );

}

?>

<div class="vendora-analytics-wrapper">

    <!-- Pie Chart -->
    <div class="vendora-chart-card">

        <div class="vendora-chart-header">
            <h2>Orders Overview</h2>
            <span>Live Statistics</span>
        </div>

        <canvas id="vendoraPieChart"></canvas>

    </div>

    <!-- Line Chart -->
    <div class="vendora-chart-card">

        <div class="vendora-chart-header">
            <h2>Monthly Orders</h2>
            <span><?php echo date('Y'); ?> Analytics</span>
        </div>

        <canvas id="vendoraLineChart"></canvas>

    </div>

</div>

<style>

    .vendora-analytics-wrapper{

        display:grid;
        grid-template-columns:1fr 2fr;
        gap:25px;
        margin-top:35px;

    }

    .vendora-chart-card{

        position:relative;
        background:linear-gradient(
            145deg,
            #ffffff,
            #f7f8fc
        );

        border-radius:22px;

        padding:28px;

        box-shadow:
            0 10px 30px rgba(0,0,0,0.06),
            inset 0 1px 0 rgba(255,255,255,0.7);

        overflow:hidden;

        transition:0.3s ease;

    }

    .vendora-chart-card:hover{

        transform:translateY(-4px);

        box-shadow:
            0 18px 40px rgba(0,0,0,0.1);

    }

    .vendora-chart-card::before{

        content:'';

        position:absolute;

        top:0;
        left:0;

        width:100%;
        height:5px;

        background:linear-gradient(
            90deg,
            #6c5ce7,
            #00cec9
        );

    }

    .vendora-chart-header{

        display:flex;
        justify-content:space-between;
        align-items:center;

        margin-bottom:25px;

    }

    .vendora-chart-header h2{

        margin:0;

        font-size:20px;
        font-weight:700;

        color:#1e1e2f;

    }

    .vendora-chart-header span{

        background:#eef2ff;

        color:#6c5ce7;

        padding:6px 14px;

        border-radius:30px;

        font-size:12px;
        font-weight:600;

    }

    .vendora-chart-card canvas{

        width:100% !important;
        height:340px !important;

    }

    @media(max-width:1200px){

        .vendora-analytics-wrapper{

            grid-template-columns:1fr;

        }

    }

</style>

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>

document.addEventListener('DOMContentLoaded', function () {

    /*
    |--------------------------------------------------------------------------
    | Professional Doughnut Chart
    |--------------------------------------------------------------------------
    */

    const pieCtx = document
        .getElementById('vendoraPieChart')
        .getContext('2d');

    new Chart(pieCtx, {

        type: 'doughnut',

        data: {

            labels: [
                'Completed',
                'Processing',
                'Pending'
            ],

            datasets: [{

                data: [
                    <?php echo esc_js( $completed_orders ); ?>,
                    <?php echo esc_js( $processing_orders ); ?>,
                    <?php echo esc_js( $pending_orders ); ?>
                ],

                backgroundColor: [
                    '#00b894',
                    '#0984e3',
                    '#fdcb6e'
                ],

                hoverOffset: 15,

                borderWidth: 0

            }]

        },

        options: {

            responsive:true,

            cutout:'72%',

            plugins: {

                legend: {

                    position:'bottom',

                    labels: {

                        usePointStyle:true,
                        padding:20,
                        font:{
                            size:13,
                            weight:'600'
                        }

                    }

                }

            }

        }

    });

    /*
    |--------------------------------------------------------------------------
    | Professional Line Chart
    |--------------------------------------------------------------------------
    */

    const lineCtx = document
        .getElementById('vendoraLineChart')
        .getContext('2d');

    const gradient = lineCtx.createLinearGradient(
        0,
        0,
        0,
        400
    );

    gradient.addColorStop(0, 'rgba(108,92,231,0.35)');
    gradient.addColorStop(1, 'rgba(108,92,231,0.02)');

    new Chart(lineCtx, {

        type:'line',

        data:{

            labels:[
                'Jan',
                'Feb',
                'Mar',
                'Apr',
                'May',
                'Jun',
                'Jul',
                'Aug',
                'Sep',
                'Oct',
                'Nov',
                'Dec'
            ],

            datasets:[{

                label:'Orders',

                data:<?php echo wp_json_encode( $monthly_orders ); ?>,

                borderColor:'#6c5ce7',

                backgroundColor:gradient,

                fill:true,

                tension:0.45,

                borderWidth:4,

                pointRadius:5,

                pointHoverRadius:8,

                pointBackgroundColor:'#fff',

                pointBorderWidth:3,

                pointBorderColor:'#6c5ce7'

            }]

        },

        options:{

            responsive:true,

            maintainAspectRatio:false,

            interaction:{
                intersect:false,
                mode:'index'
            },

            plugins:{

                legend:{
                    display:false
                }

            },

            scales:{

                x:{

                    grid:{
                        display:false
                    },

                    ticks:{
                        color:'#555'
                    }

                },

                y:{

                    beginAtZero:true,

                    grid:{
                        color:'rgba(0,0,0,0.05)'
                    },

                    ticks:{
                        color:'#555'
                    }

                }

            }

        }

    });

});

</script>
        <?php
    }
}

if ( ! class_exists( 'VS_Reports' ) ) {
    class_alias( 'Vendora_Report', 'VS_Reports' );
}

/*
|--------------------------------------------------------------------------
| Run Class
|--------------------------------------------------------------------------
*/

Vendora_Report::init();

