<?php  
if( ! defined( 'ABSPATH' ) ) exit;
global $wpdb ?>
<h3 class="sync-entries-title">Financial Reports</h3>
<br>
<main class="sync_backend_reports">
    <div class="sync-report-field">
        <div class="select_report_drpdown">
            <select name="select_date" id="select_date" class="sync-dropdown">
                <option value="" selected disabled> --- Select Type of Report --- </option>
                <option value="weekly">Weekly Report</option>
                <option value="monthly">Monthly Report</option>
                <option value="quarterly">Quarterly Report</option>
                <option value="yearly">Yearly Report</option>
                <option value="all">Show All Data</option>
            </select> 
        </div>
        <div class="set_custom_date">
            <div class="from_date">
                <span class="from-date">From</span><input class="sync-calendar-from" readonly id="sync-from-date" data-toggle="datepicker" name="sync-from-date" ><span class="input-group-addon"><i class="fa fa-calendar fa-1x calendar"></i></span> 
            </div>
            <div class="to_date">
                <span class="to-date">To</span> <input class="sync-calendar-to" readonly id="sync-to-date" data-toggle="datepicker" name="sync-to-date" ><span class="input-group-addon"><i class="fa fa-calendar fa-1x calendar"></i></span> 
            </div>
            <div class="submit_btn_cont">
                <button type="button" id="submit_date_btn_car" class="submit btn btn-success">Go</button>
            </div>
        </div>
        
    </div>
    <br>
    <div class="table-report">
        <table class="table table-striped table-sm" >
            <thead>
                <tr style="text-align: center;">
                    <th scope="col">Car</th>
                    <th scope="col">Car Type</th>
                    <th scope="col">Booking</th>
                    <th scope="col">Completed</th>
                    <th scope="col">Cancelled</th>
                    <th scope="col">Price</th>
                    <th scope="col">Coupons Used</th>
                    <th scope="col">Earning</th>
                </tr>
            </thead>
            <tbody class="all_data">

                <?php 
                    $table_post = $wpdb->prefix . "posts";
                    $table_car_entries = $wpdb->prefix . "sync_rent_car_entries";
                    $table_payments = $wpdb->prefix . "sync_payments";
                    $result = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_status = 'publish' and post_type= 'easync_car_rental'");
                    $total_value = array();
                    if (!empty($result)) {
                        foreach ($result as $value => $key) {
                            $car_entries = $wpdb->get_results("select id, car_id from $table_car_entries where car_id= '".$key->ID."';");
                            $coupons_used = $wpdb->get_results("select coupon_id from $table_car_entries where car_id= '".$key->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $completed_booking = $wpdb->get_results("select car_id from $table_car_entries where car_id= '".$key->ID."' and status IN ('active', 'inactive');");
                            $cancelled_booking = $wpdb->get_results("select $table_payments.id from $table_car_entries inner join $table_payments on $table_car_entries.id = item_belongsto where item_cat = 'car' and payment_status = 'cancelled' and car_id= '".$key->ID."';");
                            
                            $meta = get_post_meta( $key->ID, 'easync_car', true );
                            $total_earning = array();
                            foreach ($car_entries as $each => $single) {
                                $earning = $wpdb->get_results("select payment_gross from $table_payments where payment_status = 'paid' AND item_cat = 'car' AND item_belongsto = '".$single->id."' ");
                                foreach ($earning as $earn_each => $single_earn) {
                                    $total_earning[] = $single_earn->payment_gross;
                                }
                            }
                            $total_earning = number_format(array_sum($total_earning), 2);
                            $total_value[] = $total_earning;

                    ?>
                        <tr style="text-align: center;">
                            <td><?php echo $meta['model']; ?></td>
                            <td><?php echo $meta['type']; ?></td>
                            <td><?php echo count($car_entries); ?></td>
                            <td><?php echo count($completed_booking); ?></td>
                            <td><?php echo count($cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .number_format($meta['price'], 2); ?></td>
                            <td><?php echo count($coupons_used); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$total_earning; ?></td>
                        </tr>
                    <?php       
                        }
                        $clean = str_replace(",", "", $total_value);
                        $total_values = number_format(array_sum($clean), 2);
                    ?>
                        <tr style="text-align: center; border-bottom: 1px solid lightgray;">
                            <td colspan="6"> </td>
                            <td><strong>TOTAL</strong></td>
                            <td><strong><?php echo easyncCurrency(). ' ' .$total_values; ?></strong></td>
                        </tr>
                    <?php
                    } else {
                    ?> 
                        <h4 style="text-align:center;"> --- No Data --- </h4>

                    <?php     
                    }
                    
                ?>
            </tbody>
            <tbody class="weekly_data" style="display:none;">
                <?php 
                    $table_post = $wpdb->prefix . "posts";
                    $table_payments = $wpdb->prefix . "sync_payments";
                    $table_car_entries = $wpdb->prefix . "sync_rent_car_entries";
                    $result_week = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_status = 'publish' and post_type= 'easync_car_rental'");
                    $weekly_total_value = array();
                    if (!empty($result_week)) {
                        foreach ($result_week as $value_week => $key_week) {
                            $car_weekly_bookings = $wpdb->get_results("SELECT id, car_id FROM $table_car_entries WHERE WEEK(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = WEEK(NOW()) and car_id = '".$key_week->ID."';");
                            $weekly_completed_booking = $wpdb->get_results("SELECT car_id FROM $table_car_entries WHERE WEEK(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = WEEK(NOW()) and car_id= '".$key_week->ID."' and status IN ('active', 'inactive');");
                            $weekly_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_car_entries WHERE WEEK(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = WEEK(NOW()) and car_id= '".$key_week->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $weekly_cancelled_booking = $wpdb->get_results("select $table_payments.id from $table_car_entries inner join $table_payments on $table_car_entries.id = item_belongsto where WEEK(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = WEEK(NOW()) and item_cat = 'car' and payment_status = 'cancelled' and car_id= '".$key_week->ID."';");

                            $meta = get_post_meta( $key_week->ID, 'easync_car', true );
                            $weekly_total_earning = array();
                            foreach ($car_weekly_bookings as $weekly_each => $weekly_single) {
                                $weekly_earning = $wpdb->get_results("select payment_gross from $table_payments where payment_status = 'paid' AND item_cat = 'car' AND item_belongsto = '".$weekly_single->id."' ");
                                foreach ($weekly_earning as $weekly_earn_each => $weekly_single_earn) {
                                    $weekly_total_earning[] = $weekly_single_earn->payment_gross;
                                }
                            }
                            $weekly_total_earning = number_format(array_sum($weekly_total_earning), 2);
                            $weekly_total_value[] = $weekly_total_earning;
                    ?>
                        <tr style="text-align: center;">
                            <td><?php echo $meta['model']; ?></td>
                            <td><?php echo $meta['type']; ?></td>
                            <td><?php echo count($car_weekly_bookings); ?></td>
                            <td><?php echo count($weekly_completed_booking); ?></td>
                            <td><?php echo count($weekly_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$meta['price']; ?></td>
                            <td><?php echo count($weekly_coupons_used); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$weekly_total_earning; ?></td>
                        </tr>
                    <?php       
                        }
                        $clean = str_replace(",", "", $weekly_total_value);
                        $weekly_total_values = number_format(array_sum($clean), 2);
                    ?>
                        <tr style="text-align: center; border-bottom: 1px solid lightgray;">
                            <td colspan="6"> </td>
                            <td><strong>TOTAL</strong></td>
                            <td><strong><?php echo easyncCurrency(). ' ' .$weekly_total_values; ?></strong></td>
                        </tr>
                    <?php
                    } else {
                    ?> 
                        <h4 style="text-align:center;"> --- No Data --- </h4>

                    <?php     
                    }
                    
                ?>
            </tbody>
            <tbody class="monthly_data" style="display:none;">
                <?php 
                    $table_post = $wpdb->prefix . "posts";
                    $table_payments = $wpdb->prefix . "sync_payments";
                    $table_car_entries = $wpdb->prefix . "sync_rent_car_entries";
                    $result_monthly = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_status = 'publish' and post_type= 'easync_car_rental'");
                    $monthly_total_value = array();
                    if (!empty($result_monthly)) {
                        foreach ($result_monthly as $value_monthly => $key_monthly) {
                            $car_monthly_bookings = $wpdb->get_results("SELECT id, car_id FROM $table_car_entries where MONTH(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = MONTH(NOW()) AND YEAR(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = YEAR(NOW()) AND car_id = '".$key_monthly->ID."';");
                            $monthly_completed_booking = $wpdb->get_results("SELECT car_id FROM $table_car_entries WHERE MONTH(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = MONTH(NOW()) and car_id= '".$key_monthly->ID."' and status IN ('active', 'inactive');");
                            $monthly_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_car_entries WHERE MONTH(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = MONTH(NOW()) and car_id= '".$key_monthly->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $monthly_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id from $table_car_entries inner join $table_payments on $table_car_entries.id = item_belongsto where MONTH(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = MONTH(NOW()) AND YEAR(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = YEAR(NOW()) and item_cat = 'car' and payment_status = 'cancelled' and car_id= '".$key_monthly->ID."';");

                            $meta = get_post_meta( $key_monthly->ID, 'easync_car', true );
                            $monthly_total_earning = array();
                            foreach ($car_monthly_bookings as $monthly_each => $monthly_single) {
                                $monthly_earning = $wpdb->get_results("select payment_gross from $table_payments where payment_status = 'paid' AND item_cat = 'car' AND item_belongsto = '".$monthly_single->id."' ");
                                foreach ($monthly_earning as $monthly_earn_each => $monthly_single_earn) {
                                    $monthly_total_earning[] = $monthly_single_earn->payment_gross;
                                }
                            }
                            $monthly_total_earning = number_format(array_sum($monthly_total_earning), 2);
                            $monthly_total_value[] = $monthly_total_earning;
                    ?>
                        <tr style="text-align: center;">
                            <td><?php echo $meta['model']; ?></td>
                            <td><?php echo $meta['type']; ?></td>
                            <td><?php echo count($car_monthly_bookings); ?></td>
                            <td><?php echo count($monthly_completed_booking); ?></td>
                            <td><?php echo count($monthly_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$meta['price']; ?></td>
                            <td><?php echo count($monthly_coupons_used); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$monthly_total_earning; ?></td>
                        </tr>
                    <?php       
                        }
                        $clean = str_replace(",", "", $monthly_total_value);
                        $monthly_total_values = number_format(array_sum($clean), 2);
                    ?>
                        <tr style="text-align: center; border-bottom: 1px solid lightgray;">
                            <td colspan="6"> </td>
                            <td><strong>TOTAL</strong></td>
                            <td><strong><?php echo easyncCurrency(). ' ' .$monthly_total_values; ?></strong></td>
                        </tr>
                    <?php
                    } else {
                    ?> 
                        <h4 style="text-align:center;"> --- No Data --- </h4>

                    <?php     
                    }
                    
                ?>
            </tbody>
            <tbody class="quarter_data" style="display:none;">
                <?php 
                    $table_post = $wpdb->prefix . "posts";
                    $table_payments = $wpdb->prefix . "sync_payments";
                    $table_car_entries = $wpdb->prefix . "sync_rent_car_entries";
                    $result_quarter = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_status = 'publish' and post_type= 'easync_car_rental'");
                    $quarter_total_value = array();
                    if (!empty($result_quarter)) {
                        foreach ($result_quarter as $value_quarter => $key_quarter) {
                            $car_quarter_bookings = $wpdb->get_results("SELECT id, car_id FROM $table_car_entries where QUARTER(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = QUARTER(now())  AND car_id = '".$key_quarter->ID."';");
                            $quarter_completed_booking = $wpdb->get_results("SELECT car_id FROM $table_car_entries WHERE QUARTER(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = QUARTER(NOW()) and car_id= '".$key_quarter->ID."' and status IN ('active', 'inactive');");
                            $quarter_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_car_entries WHERE QUARTER(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = QUARTER(NOW()) and car_id= '".$key_quarter->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $quarter_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id from $table_car_entries inner join $table_payments on $table_car_entries.id = item_belongsto where QUARTER(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = QUARTER(NOW()) and item_cat = 'car' and payment_status = 'cancelled' and car_id= '".$key_quarter->ID."';");

                            $meta = get_post_meta( $key_quarter->ID, 'easync_car', true );
                            $quarter_total_earning = array();
                            foreach ($car_quarter_bookings as $quarter_each => $quarter_single) {
                                $quarter_earning = $wpdb->get_results("select payment_gross from $table_payments where payment_status = 'paid' AND item_cat = 'car' AND item_belongsto = '".$quarter_single->id."' ");
                                foreach ($quarter_earning as $quarter_earn_each => $quarter_single_earn) {
                                    $quarter_total_earning[] = $quarter_single_earn->payment_gross;
                                }
                            }
                            $quarter_total_earning = number_format(array_sum($quarter_total_earning), 2);
                            $quarter_total_value[] = $quarter_total_earning;
                    ?>
                        <tr style="text-align: center;">
                            <td><?php echo $meta['model']; ?></td>
                            <td><?php echo $meta['type']; ?></td>
                            <td><?php echo count($car_quarter_bookings); ?></td>
                            <td><?php echo count($quarter_completed_booking); ?></td>
                            <td><?php echo count($quarter_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$meta['price']; ?></td>
                            <td><?php echo count($quarter_coupons_used); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$quarter_total_earning; ?></td>
                        </tr>
                    <?php       
                        }
                        $clean = str_replace(",", "", $quarter_total_value);
                        $quarter_total_values = number_format(array_sum($clean), 2);
                    ?>
                        <tr style="text-align: center; border-bottom: 1px solid lightgray;">
                            <td colspan="6"> </td>
                            <td><strong>TOTAL</strong></td>
                            <td><strong><?php echo easyncCurrency(). ' ' .$quarter_total_values; ?></strong></td>
                        </tr>
                    <?php
                    } else {
                    ?> 
                        <h4 style="text-align:center;"> --- No Data --- </h4>

                    <?php     
                    }
                    
                ?>
            </tbody>
            <tbody class="yearly_data" style="display:none;">
                <?php 
                    $table_post = $wpdb->prefix . "posts";
                    $table_payments = $wpdb->prefix . "sync_payments";
                    $table_car_entries = $wpdb->prefix . "sync_rent_car_entries";
                    $result_yearly = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_status = 'publish' and post_type= 'easync_car_rental'");
                    $yearly_total_value = array();
                    if (!empty($result_yearly)) {
                        foreach ($result_yearly as $value_yearly => $key_yearly) {
                            $car_yearly_bookings = $wpdb->get_results("SELECT id, car_id FROM $table_car_entries where YEAR(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = YEAR(now())  AND car_id = '".$key_yearly->ID."';");
                            $yearly_completed_booking = $wpdb->get_results("SELECT car_id FROM $table_car_entries WHERE YEAR(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = YEAR(NOW()) and car_id= '".$key_yearly->ID."' and status IN ('active', 'inactive');");
                            $yearly_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_car_entries WHERE YEAR(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = YEAR(NOW()) and car_id= '".$key_yearly->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $yearly_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id from $table_car_entries inner join $table_payments on $table_car_entries.id = item_belongsto where YEAR(CONCAT(SUBSTRING(pick_date, 7, 4), '-', SUBSTRING(pick_date, 1, 2), '-', SUBSTRING(pick_date, 4, 2))) = YEAR(NOW()) and item_cat = 'car' and payment_status = 'cancelled' and car_id= '".$key_yearly->ID."';");

                            $meta = get_post_meta( $key_yearly->ID, 'easync_car', true );
                            $yearly_total_earning = array();
                            foreach ($car_yearly_bookings as $yearly_each => $yearly_single) {
                                $yearly_earning = $wpdb->get_results("select payment_gross from $table_payments where payment_status = 'paid' AND item_cat = 'car' AND item_belongsto = '".$yearly_single->id."' ");
                                foreach ($yearly_earning as $yearly_earn_each => $yearly_single_earn) {
                                    $yearly_total_earning[] = $yearly_single_earn->payment_gross;
                                }
                            }
                            $yearly_total_earning = number_format(array_sum($yearly_total_earning), 2);
                            $yearly_total_value[] = $yearly_total_earning;

                            
                    ?>
                        <tr style="text-align: center;">
                            <td><?php echo $meta['model']; ?></td>
                            <td><?php echo $meta['type']; ?></td>
                            <td><?php echo count($car_yearly_bookings); ?></td>
                            <td><?php echo count($yearly_completed_booking); ?></td>
                            <td><?php echo count($yearly_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$meta['price']; ?></td>
                            <td><?php echo count($yearly_coupons_used); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .$yearly_total_earning; ?></td>
                        </tr>
                    <?php       
                        }
                        $clean = str_replace(",", "", $yearly_total_value);
                        $yearly_total_values = number_format(array_sum($clean), 2);
                    ?>
                        <tr style="text-align: center; border-bottom: 1px solid lightgray;">
                            <td colspan="6"> </td>
                            <td><strong>TOTAL</strong></td>
                            <td><strong><?php echo easyncCurrency(). ' ' .$yearly_total_values; ?></strong></td>
                        </tr>
                    <?php
                    } else {
                    ?> 
                        <h4 style="text-align:center;"> --- No Data --- </h4>

                    <?php     
                    }
                    
                ?>
            </tbody>
            <tbody class="custom_data_car" style="display:none;">
               
            </tbody>

        </table>
    </div>

    <br>
    <div class="graph_label">
        <div class="pieChart_bookings">
            <h4>All Bookings</h4>
        </div>
        <div class="pieChart_completed">
            <h4>Completed Bookings</h4>
        </div>
        <div class="pieChart_completed">
            <h4>Cancelled Bookings</h4>
        </div>
        <div class="pieChart_earnings">
            <h4>Earnings</h4>
        </div>
    </div>               
    <div class="chart_all_display">
        <div class="pieChart_bookings">
            <canvas id="pieChart_bookings_display"></canvas>
        </div>
        <div class="pieChart_completed">
            <canvas id="pieChart_completed_display"></canvas>
            <div class="if_empty_completed" style="display:none; width: 100%; text-align: center;">
                <h4>No Graphical Data to display.</h4>
            </div>
        </div>
        <div class="pieChart_cancelled">
            <canvas id="pieChart_cancelled_display"></canvas>
            <div class="if_empty_cancelled" style="display:none; width: 100%; text-align: center;">
                <h4>No Graphical Data to display.</h4>
            </div>
        </div>
        <div class="pieChart_earnings">
            <canvas id="pieChart_earnings_display"></canvas>
            <div class="if_empty_earnings" style="display:none; width: 100%; text-align: center;">
                <h4>No Graphical Data to display.</h4>
            </div>
        </div>
    </div>

    <div class="if_empty" style="display:none; width: 100%; text-align: center;">
        <h3>No Graphical Data to display.</h3>
    </div>
    
</main>

<script>
    
    let myPieChart;
    let myPieChart_earnings;
    let myPieChart_completed;
    let myPieChart_cancelled;
    
    jQuery(document).ready( function () {

        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "show_graphs_all_bookings_car"},
            success: function(dataa){

                var length = dataa.length-1;
                var labelss = new Array();
                for (var i = 0; i < length; i++) {
                    labelss.push(dataa[i]);
                }
                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                if (sum == 0) {
                    jQuery('.if_empty').show();
                    jQuery('.chart_all_display').hide();
                    jQuery('.graph_label').hide();
                }
                else {

                    let data = {
                        labels: labelss,
                        datasets: [{
                        data: dataa[dataa.length-1],
                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                        }]
                    };

                    let config = {
                        type: 'pie',
                        data,
                        options: {
                            responsive: true,

                        }
                    };

                    myPieChart = new Chart(
                        document.getElementById("pieChart_bookings_display"),
                        config
                    );

                    jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_all_completed_car"},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_completed').show();
                                jQuery('.pieChart_completed_display').hide();
                            }
                            else {

                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_completed = new Chart(
                                    document.getElementById("pieChart_completed_display"),
                                    config
                                );
                            }
                        }

                    });

                    jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_all_cancelled_car"},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_cancelled').show();
                                jQuery('.pieChart_cancelled_display').hide();
                            }
                            else {

                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_cancelled = new Chart(
                                    document.getElementById("pieChart_cancelled_display"),
                                    config
                                );
                            }
                        }

                    });

                    jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_all_earnings_car"},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_earnings').show();
                                jQuery('.pieChart_earnings_display').hide();
                                jQuery('.graph_label').hide();
                            }
                            else {
                    
                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_earnings = new Chart(
                                    document.getElementById("pieChart_earnings_display"),
                                    config
                                );
                            }
                        }
  
                    });
                    
                }
            }

        });
    });
    

    jQuery("#select_date").change(function () {
        var selected_report = jQuery(this).val();

        if(selected_report == 'weekly') {
            jQuery(".weekly_data").show();
            jQuery(".all_data").hide();
            jQuery(".monthly_data").hide(); 
            jQuery(".quarter_data").hide(); 
            jQuery(".yearly_data").hide();
            jQuery(".custom_data_car").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            jQuery('.graph_label').show();
            jQuery('.if_empty_completed').hide();
            jQuery('.if_empty_cancelled').hide();
            jQuery('.if_empty_earnings').hide();

            if (myPieChart instanceof Chart) {
                myPieChart.destroy();
            }
            if (myPieChart_completed instanceof Chart) {
                myPieChart_completed.destroy();
            }
            if (myPieChart_cancelled instanceof Chart) {
                myPieChart_cancelled.destroy();
            }
            if (myPieChart_earnings instanceof Chart) {
                myPieChart_earnings.destroy();
            }

            jQuery.ajax({
                type: "POST",
                dataType: "json",
                url: easync_admin_ajax_directory.ajaxurl,
                data: { action: "show_graphs_weekly_bookings_car"},
                success: function(dataa){
                    var length = dataa.length-1;
                    var labelss = new Array();
                    for (var i = 0; i < length; i++) {
                        labelss.push(dataa[i]);
                    }
                    var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                    if (sum == 0) {
                        jQuery('.if_empty').show();
                        jQuery('.chart_all_display').hide();
                        jQuery('.graph_label').hide();
                    }
                    else {

                        let data = {
                            labels: labelss,
                            datasets: [{
                            data: dataa[dataa.length-1],
                            backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                            }]
                        };

                        let config = {
                            type: 'pie',
                            data,
                            options: {
                                responsive: true,

                            }
                        };

                        myPieChart = new Chart(
                            document.getElementById("pieChart_bookings_display"),
                            config
                        );

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_weekly_completed_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_completed').show();
                                    jQuery('.pieChart_completed_display').hide();
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_completed = new Chart(
                                        document.getElementById("pieChart_completed_display"),
                                        config
                                    );
                                }
                            }

                        });

                        jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_weekly_cancelled_car"},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_cancelled').show();
                                jQuery('.pieChart_cancelled_display').hide();
                            }
                            else {

                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_cancelled = new Chart(
                                    document.getElementById("pieChart_cancelled_display"),
                                    config
                                );
                            }
                        }

                    });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_weekly_earnings_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_earnings').show();
                                    jQuery('.pieChart_earnings_display').hide();
                                }
                                else {
                        
                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_earnings = new Chart(
                                        document.getElementById("pieChart_earnings_display"),
                                        config
                                    );
                                }
                            }

                            
                        });
                        
                    }
                }

            });

        }

        if(selected_report == 'monthly') {
            jQuery(".weekly_data").hide();
            jQuery(".all_data").hide();
            jQuery(".monthly_data").show(); 
            jQuery(".quarter_data").hide(); 
            jQuery(".yearly_data").hide();
            jQuery(".custom_data_car").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            jQuery('.graph_label').show();
            jQuery('.if_empty_completed').hide();
            jQuery('.if_empty_cancelled').hide();
            jQuery('.if_empty_earnings').hide();

            if (myPieChart instanceof Chart) {
                myPieChart.destroy();
            }
            if (myPieChart_completed instanceof Chart) {
                myPieChart_completed.destroy();
            }
            if (myPieChart_cancelled instanceof Chart) {
                myPieChart_cancelled.destroy();
            }
            if (myPieChart_earnings instanceof Chart) {
                myPieChart_earnings.destroy();
            }

            jQuery.ajax({
                type: "POST",
                dataType: "json",
                url: easync_admin_ajax_directory.ajaxurl,
                data: { action: "show_graphs_monthly_bookings_car"},
                success: function(dataa){
                    var length = dataa.length-1;
                    var labelss = new Array();
                    for (var i = 0; i < length; i++) {
                        labelss.push(dataa[i]);
                    }
                    var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                    if (sum == 0) {
                        jQuery('.if_empty').show();
                        jQuery('.chart_all_display').hide();
                        jQuery('.graph_label').hide();
                    }
                    else {

                        let data = {
                            labels: labelss,
                            datasets: [{
                            data: dataa[dataa.length-1],
                            backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                            }]
                        };

                        let config = {
                            type: 'pie',
                            data,
                            options: {
                                responsive: true,

                            }
                        };

                        myPieChart = new Chart(
                            document.getElementById("pieChart_bookings_display"),
                            config
                        );

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_monthly_completed_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_completed').show();
                                    jQuery('.pieChart_completed_display').hide();
                                }
                                else {

                                    

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_completed = new Chart(
                                        document.getElementById("pieChart_completed_display"),
                                        config
                                    );
                                }
                            }

                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_monthly_cancelled_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_cancelled').show();
                                    jQuery('.pieChart_cancelled_display').hide();
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_cancelled = new Chart(
                                        document.getElementById("pieChart_cancelled_display"),
                                        config
                                    );
                                }
                            }

                        });


                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_monthly_earnings_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_earnings').show();
                                    jQuery('.pieChart_earnings_display').hide();
                                }
                                else {
                        
                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_earnings = new Chart(
                                        document.getElementById("pieChart_earnings_display"),
                                        config
                                    );
                                }
                            }

                            
                        });
                        
                    }
                }

            });

        }

        if(selected_report == 'quarterly') {
            jQuery(".weekly_data").hide();
            jQuery(".all_data").hide();
            jQuery(".monthly_data").hide(); 
            jQuery(".quarter_data").show(); 
            jQuery(".yearly_data").hide();
            jQuery(".custom_data_car").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            jQuery('.graph_label').show();
            jQuery('.if_empty_completed').hide();
            jQuery('.if_empty_cancelled').hide();
            jQuery('.if_empty_earnings').hide();

            if (myPieChart instanceof Chart) {
                myPieChart.destroy();
            }
            if (myPieChart_completed instanceof Chart) {
                myPieChart_completed.destroy();
            }
            if (myPieChart_cancelled instanceof Chart) {
                myPieChart_cancelled.destroy();
            }
            if (myPieChart_earnings instanceof Chart) {
                myPieChart_earnings.destroy();
            }

            jQuery.ajax({
                type: "POST",
                dataType: "json",
                url: easync_admin_ajax_directory.ajaxurl,
                data: { action: "show_graphs_quarterly_bookings_car"},
                success: function(dataa){
                    var length = dataa.length-1;
                    var labelss = new Array();
                    for (var i = 0; i < length; i++) {
                        labelss.push(dataa[i]);
                    }
                    var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                    if (sum == 0) {
                        jQuery('.if_empty').show();
                        jQuery('.chart_all_display').hide();
                        jQuery('.graph_label').hide();
                    }
                    else {



                        let data = {
                            labels: labelss,
                            datasets: [{
                            data: dataa[dataa.length-1],
                            backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                            }]
                        };

                        let config = {
                            type: 'pie',
                            data,
                            options: {
                                responsive: true,

                            }
                        };

                        myPieChart = new Chart(
                            document.getElementById("pieChart_bookings_display"),
                            config
                        );

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_quarterly_completed_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_completed').show();
                                    jQuery('.pieChart_completed_display').hide();
                                }
                                else {

                                    

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_completed = new Chart(
                                        document.getElementById("pieChart_completed_display"),
                                        config
                                    );
                                }
                            }

                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_quarterly_cancelled_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_cancelled').show();
                                    jQuery('.pieChart_cancelled_display').hide();
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_cancelled = new Chart(
                                        document.getElementById("pieChart_cancelled_display"),
                                        config
                                    );
                                }
                            }

                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_quarterly_earnings_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_earnings').show();
                                    jQuery('.pieChart_earnings_display').hide();
                                }
                                else {
                        
                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_earnings = new Chart(
                                        document.getElementById("pieChart_earnings_display"),
                                        config
                                    );
                                }
                            }

                            
                        });
                        
                    }
                }

            });

        }

        if(selected_report == 'yearly') {
            jQuery(".weekly_data").hide();
            jQuery(".all_data").hide();
            jQuery(".monthly_data").hide(); 
            jQuery(".quarter_data").hide(); 
            jQuery(".yearly_data").show();
            jQuery(".custom_data_car").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            jQuery('.graph_label').show();
            jQuery('.if_empty_completed').hide();
            jQuery('.if_empty_cancelled').hide();
            jQuery('.if_empty_earnings').hide();

            if (myPieChart instanceof Chart) {
                myPieChart.destroy();
            }
            if (myPieChart_cancelled instanceof Chart) {
                myPieChart_cancelled.destroy();
            }
            if (myPieChart_completed instanceof Chart) {
                myPieChart_completed.destroy();
            }
            if (myPieChart_earnings instanceof Chart) {
                myPieChart_earnings.destroy();
            }

            jQuery.ajax({
                type: "POST",
                dataType: "json",
                url: easync_admin_ajax_directory.ajaxurl,
                data: { action: "show_graphs_yearly_bookings_car"},
                success: function(dataa){
                    var length = dataa.length-1;
                    var labelss = new Array();
                    for (var i = 0; i < length; i++) {
                        labelss.push(dataa[i]);
                    }
                    var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                    if (sum == 0) {
                        jQuery('.if_empty').show();
                        jQuery('.chart_all_display').hide();
                        jQuery('.graph_label').hide();
                    }
                    else {

                        let data = {
                            labels: labelss,
                            datasets: [{
                            data: dataa[dataa.length-1],
                            backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                            }]
                        };

                        let config = {
                            type: 'pie',
                            data,
                            options: {
                                responsive: true,

                            }
                        };

                        myPieChart = new Chart(
                            document.getElementById("pieChart_bookings_display"),
                            config
                        );

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_yearly_completed_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_completed').show();
                                    jQuery('.pieChart_completed_display').hide();
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_completed = new Chart(
                                        document.getElementById("pieChart_completed_display"),
                                        config
                                    );
                                }
                            }

                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_yearly_cancelled_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_cancelled').show();
                                    jQuery('.pieChart_cancelled_display').hide();
                                    
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_cancelled = new Chart(
                                        document.getElementById("pieChart_cancelled_display"),
                                        config
                                    );
                                }
                            }
                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_yearly_earnings_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_earnings').show();
                                    jQuery('.pieChart_earnings_display').hide();
                                    jQuery('.graph_label').hide();
                                }
                                else {
                        
                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_earnings = new Chart(
                                        document.getElementById("pieChart_earnings_display"),
                                        config
                                    );
                                }
                            }

                            
                        });
                        
                    }
                }

            });
        }

        if(selected_report == 'all') {
            jQuery(".weekly_data").hide();
            jQuery(".all_data").show();
            jQuery(".monthly_data").hide(); 
            jQuery(".quarter_data").hide(); 
            jQuery(".yearly_data").hide();
            jQuery(".custom_data_car").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            jQuery('.graph_label').show();
            jQuery('.if_empty_completed').hide();
            jQuery('.if_empty_cancelled').hide();
            jQuery('.if_empty_earnings').hide();

            if (myPieChart instanceof Chart) {
                myPieChart.destroy();
            }
            if (myPieChart_completed instanceof Chart) {
                myPieChart_completed.destroy();
            }
            if (myPieChart_cancelled instanceof Chart) {
                myPieChart_cancelled.destroy();
            }
            if (myPieChart_earnings instanceof Chart) {
                myPieChart_earnings.destroy();
            }

            jQuery.ajax({
                type: "POST",
                dataType: "json",
                url: easync_admin_ajax_directory.ajaxurl,
                data: { action: "show_graphs_all_bookings_car"},
                success: function(dataa){
                    var length = dataa.length-1;
                    var labelss = new Array();
                    for (var i = 0; i < length; i++) {
                        labelss.push(dataa[i]);
                    }
                    var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                    if (sum == 0) {
                        jQuery('.if_empty').show();
                        jQuery('.chart_all_display').hide();
                        jQuery('.graph_label').hide();
                    }
                    else {

                        let data = {
                            labels: labelss,
                            datasets: [{
                            data: dataa[dataa.length-1],
                            backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                            }]
                        };

                        let config = {
                            type: 'pie',
                            data,
                            options: {
                                responsive: true,

                            }
                        };

                        myPieChart = new Chart(
                            document.getElementById("pieChart_bookings_display"),
                            config
                        );

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_all_completed_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_completed').show();
                                    jQuery('.pieChart_completed_display').hide();
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_completed = new Chart(
                                        document.getElementById("pieChart_completed_display"),
                                        config
                                    );
                                }
                            }

                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_all_cancelled_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_cancelled').show();
                                    jQuery('.pieChart_cancelled_display').hide();
                                }
                                else {

                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_cancelled = new Chart(
                                        document.getElementById("pieChart_cancelled_display"),
                                        config
                                    );
                                }
                            }
                        });

                        jQuery.ajax({
                            type: "POST",
                            dataType: "json",
                            url: easync_admin_ajax_directory.ajaxurl,
                            data: { action: "show_graphs_all_earnings_car"},
                            success: function(dataa){
                                var length = dataa.length-1;
                                var labelss = new Array();
                                for (var i = 0; i < length; i++) {
                                    labelss.push(dataa[i]);
                                }
                                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                                if (sum == 0) {
                                    jQuery('.if_empty_earnings').show();
                                    jQuery('.pieChart_earnings_display').hide();
                                }
                                else {
                        
                                    let data = {
                                        labels: labelss,
                                        datasets: [{
                                        data: dataa[dataa.length-1],
                                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                        }]
                                    };

                                    let config = {
                                        type: 'pie',
                                        data,
                                        options: {
                                            responsive: true,

                                        }
                                    };

                                    myPieChart_earnings = new Chart(
                                        document.getElementById("pieChart_earnings_display"),
                                        config
                                    );
                                }
                            }

                            
                        });
                        
                    }
                }

            });

        }
    
    });



    jQuery('#submit_date_btn_car').click( function () {
        var from_datee = jQuery('#sync-from-date').val();
        var to_datee = jQuery('#sync-to-date').val();

        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "display_custom_data_car", from_date: from_datee, to_date: to_datee },
            success: function(data){
                jQuery(".weekly_data").hide();
                jQuery(".all_data").hide();
                jQuery(".monthly_data").hide(); 
                jQuery(".quarter_data").hide(); 
                jQuery(".yearly_data").hide();
                jQuery(".custom_data_car").show();
                jQuery("#sync-from-date").val("");
                jQuery("#sync-to-date").val("");
                jQuery(".chart_all_display").show();
                jQuery(".custom_data_car").empty();
                jQuery(".custom_data_car").append(data);
                jQuery('.if_empty').hide();
                jQuery('.graph_label').show();
                jQuery('.if_empty_completed').hide();
                jQuery('.if_empty_cancelled').hide();
                jQuery('.if_empty_earnings').hide();

            }
        });

        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "show_graphs_custom_bookings_car", from_date: from_datee, to_date: to_datee},
            success: function(dataa){

                if (myPieChart instanceof Chart) {
                    myPieChart.destroy();
                }
                if (myPieChart_completed instanceof Chart) {
                    myPieChart_completed.destroy();
                }
                if (myPieChart_cancelled instanceof Chart) {
                    myPieChart_cancelled.destroy();
                }
                if (myPieChart_earnings instanceof Chart) {
                    myPieChart_earnings.destroy();
                }

                var length = dataa.length-1;
                var labelss = new Array();
                for (var i = 0; i < length; i++) {
                    labelss.push(dataa[i]);
                }
                var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                if (sum == 0) {
                    jQuery('.if_empty').show();
                    jQuery('.chart_all_display').hide();
                    jQuery('.graph_label').hide();
                }
                else {

                    let data = {
                        labels: labelss,
                        datasets: [{
                        data: dataa[dataa.length-1],
                        backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                        }]
                    };

                    let config = {
                        type: 'pie',
                        data,
                        options: {
                            responsive: true,

                        }
                    };

                    myPieChart = new Chart(
                        document.getElementById("pieChart_bookings_display"),
                        config
                    );

                    jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_custom_completed_car", from_date: from_datee, to_date: to_datee},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_completed').show();
                                jQuery('.pieChart_completed_display').hide();
                            }
                            else {

                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_completed = new Chart(
                                    document.getElementById("pieChart_completed_display"),
                                    config
                                );
                            }
                        }
                    });

                    jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_custom_cancelled_car", from_date: from_datee, to_date: to_datee},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_cancelled').show();
                                jQuery('.pieChart_cancelled_display').hide();
                            }
                            else {

                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_cancelled = new Chart(
                                    document.getElementById("pieChart_cancelled_display"),
                                    config
                                );
                            }
                        }
                    });

                    jQuery.ajax({
                        type: "POST",
                        dataType: "json",
                        url: easync_admin_ajax_directory.ajaxurl,
                        data: { action: "show_graphs_custom_earnings_car", from_date: from_datee, to_date: to_datee},
                        success: function(dataa){
                            var length = dataa.length-1;
                            var labelss = new Array();
                            for (var i = 0; i < length; i++) {
                                labelss.push(dataa[i]);
                            }
                            var sum = dataa[dataa.length-1].reduce((a, b) => a + b, 0);
                            if (sum == 0) {
                                jQuery('.if_empty_earnings').show();
                                jQuery('.pieChart_earnings_display').hide();
                            }
                            else {
                    
                                let data = {
                                    labels: labelss,
                                    datasets: [{
                                    data: dataa[dataa.length-1],
                                    backgroundColor: ["#FF7F00", "#FFFF00", "#FF0000", "#CC99C9", "#9400D3", "#4B0082", "#0000FF", "#00FF00", "#9EC1CF", "#9EE09E"]
                                    }]
                                };

                                let config = {
                                    type: 'pie',
                                    data,
                                    options: {
                                        responsive: true,

                                    }
                                };

                                myPieChart_earnings = new Chart(
                                    document.getElementById("pieChart_earnings_display"),
                                    config
                                );
                            }
                        }

                        
                    });
                    
                }
            }
        });
    });

</script>

