<?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_hotel" 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">Room</th>
                    <th scope="col">Bookings</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_payments = $wpdb->prefix . "sync_payments";
                    $table_hotel_entries = $wpdb->prefix . "sync_hotel_entries";
                    $result = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_type= 'easync_hotel_room' AND post_status = 'publish'");
                    $total_value = array();
                    $total_values = "";
                    if (!empty($result)) {
                        foreach ($result as $value => $key) {
                            $hotel_entries = $wpdb->get_results("select id, room_id from $table_hotel_entries where room_id= '".$key->ID."';");
                            $coupons_used = $wpdb->get_results("select coupon_id from $table_hotel_entries where room_id= '".$key->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $completed_booking = $wpdb->get_results("select room_id from $table_hotel_entries where room_id= '".$key->ID."' and status IN ('active', 'inactive');");
                            $cancelled_booking = $wpdb->get_results("select $table_payments.id from $table_hotel_entries inner join $table_payments on $table_hotel_entries.id = item_belongsto where payment_status = 'cancelled' and room_id= '".$key->ID."';");
                            
                            $meta = get_post_meta( $key->ID, 'easync_hotel', true );
                            $total_earning = array();
                            $total = array();
                            foreach ($hotel_entries as $each => $single) {
                                $earning = $wpdb->get_results("select payment_gross from $table_payments where payment_status = 'paid' AND item_cat = 'hotel' AND item_belongsto = '".$single->id."' ");
                                foreach ($earning as $earn_each => $single_earn) {
                                    $total_earning[] = ($single_earn->payment_gross);
                                }
                            }
                            $total = number_format(array_sum($total_earning), 2);
                            $total_value[] = $total;
                            
                            
                    ?>
                        <tr style="text-align: center;">
                            <td><?php echo $key->post_title; ?></td>
                            <td><?php echo count($hotel_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; ?></td>
                        </tr>
                        
                    <?php       
                        }
                        $clean = str_replace(",", "", $total_value);
                        $total_values = number_format(array_sum($clean), 2);
                        // echo var_dump($total_values);
                        
                    ?>
                        <tr style="text-align: center; border-bottom: 1px solid lightgray;;">
                            <td colspan="5"> </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_hotel_entries = $wpdb->prefix . "sync_hotel_entries";
                    $result_week = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_type= 'easync_hotel_room' AND post_status = 'publish'");
                    $weekly_total_value = array();
                    
                    if (!empty($result_week)) {
                        foreach ($result_week as $value_week => $key_week) {
                            
                            $hotel_weekly_bookings = $wpdb->get_results("SELECT id, room_id FROM $table_hotel_entries WHERE WEEK(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = WEEK(NOW()) and room_id = '".$key_week->ID."';");
                            $weekly_completed_booking = $wpdb->get_results("SELECT room_id FROM $table_hotel_entries WHERE WEEK(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = WEEK(NOW()) and room_id= '".$key_week->ID."' and status IN ('active', 'inactive');");
                            $weekly_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_hotel_entries WHERE WEEK(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = WEEK(NOW()) and room_id= '".$key_week->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $weekly_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id from $table_hotel_entries inner join $table_payments on $table_hotel_entries.id = item_belongsto WHERE WEEK(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = WEEK(NOW()) AND room_id= '".$key_week->ID."' and payment_status = 'cancelled';");

                            $meta = get_post_meta( $key_week->ID, 'easync_hotel', true );
                            $weekly_total_earning = array();
                            foreach ($hotel_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 = 'hotel' 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 $key_week->post_title; ?></td>
                            <td><?php echo count($hotel_weekly_bookings); ?></td>
                            <td><?php echo count($weekly_completed_booking); ?></td>
                            <td><?php echo count($weekly_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .number_format($meta['price'], 2); ?></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="5"> </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_hotel_entries = $wpdb->prefix . "sync_hotel_entries";
                    $result_monthly = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_type= 'easync_hotel_room' AND post_status = 'publish'");
                    $monthly_total_value = array();
                    if (!empty($result_monthly)) {
                        foreach ($result_monthly as $value_monthly => $key_monthly) {
                            $hotel_monthly_bookings = $wpdb->get_results("SELECT id, room_id FROM $table_hotel_entries where MONTH(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = MONTH(NOW()) AND YEAR(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = YEAR(NOW()) AND room_id = '".$key_monthly->ID."';");
                            $monthly_completed_booking = $wpdb->get_results("SELECT room_id FROM $table_hotel_entries WHERE MONTH(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = MONTH(NOW()) and room_id= '".$key_monthly->ID."' and status IN ('active', 'inactive');");
                            $monthly_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_hotel_entries WHERE MONTH(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = MONTH(NOW()) and room_id= '".$key_monthly->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $monthly_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id from $table_payments inner join $table_hotel_entries on item_belongsto = $table_hotel_entries.id WHERE MONTH(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = MONTH(NOW()) AND payment_status = 'cancelled' AND room_id= '".$key_monthly->ID."';");
                            $meta = get_post_meta( $key_monthly->ID, 'easync_hotel', true );
                            $monthly_total_earning = array();
                            foreach ($hotel_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 = 'hotel' 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 $key_monthly->post_title; ?></td>
                            <td><?php echo count($hotel_monthly_bookings); ?></td>
                            <td><?php echo count($monthly_completed_booking); ?></td>
                            <td><?php echo count($monthly_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .number_format($meta['price'], 2); ?></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="5"> </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_hotel_entries = $wpdb->prefix . "sync_hotel_entries";
                    $result_quarter = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_type= 'easync_hotel_room' AND post_status = 'publish'");
                    $quarter_total_value = array();
                    if (!empty($result_quarter)) {
                        foreach ($result_quarter as $value_quarter => $key_quarter) {
                            $hotel_quarter_bookings = $wpdb->get_results("SELECT id, room_id FROM $table_hotel_entries where QUARTER(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = QUARTER(now()) AND room_id = '".$key_quarter->ID."';");
                            $quarter_completed_booking = $wpdb->get_results("SELECT room_id FROM $table_hotel_entries WHERE QUARTER(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = QUARTER(NOW()) and room_id= '".$key_quarter->ID."' and status IN ('active', 'inactive');");
                            $quarter_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_hotel_entries WHERE QUARTER(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = QUARTER(NOW()) and room_id= '".$key_quarter->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $quarter_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id from $table_hotel_entries inner join $table_payments on $table_hotel_entries.id = item_belongsto WHERE QUARTER(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = QUARTER(NOW()) and payment_status = 'cancelled' AND room_id= '".$key_quarter->ID."';");
                            $meta = get_post_meta( $key_quarter->ID, 'easync_hotel', true );
                            $quarter_total_earning = array();
                            foreach ($hotel_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 = 'hotel' 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 $key_quarter->post_title; ?></td>
                            <td><?php echo count($hotel_quarter_bookings); ?></td>
                            <td><?php echo count($quarter_completed_booking); ?></td>
                            <td><?php echo count($quarter_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .number_format($meta['price'], 2); ?></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="5"> </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_hotel_entries = $wpdb->prefix . "sync_hotel_entries";
                    $result_yearly = $wpdb->get_results("select ID, post_title, post_date from $table_post where post_type= 'easync_hotel_room' AND post_status = 'publish'");
                    $yearly_total_value = array();
                    $count = "";
                    if (!empty($result_yearly)) {
                        foreach ($result_yearly as $value_yearly => $key_yearly) {
                            $hotel_yearly_bookings = $wpdb->get_results("SELECT id, room_id FROM easync_demo.wp_sync_hotel_entries where YEAR(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = YEAR(now())  AND room_id = '".$key_yearly->ID."';");
                            $yearly_completed_booking = $wpdb->get_results("SELECT room_id FROM $table_hotel_entries WHERE YEAR(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = YEAR(NOW()) and room_id= '".$key_yearly->ID."' and status IN ('active', 'inactive');");
                            $yearly_coupons_used = $wpdb->get_results("SELECT coupon_id FROM $table_hotel_entries WHERE YEAR(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = YEAR(NOW()) and room_id= '".$key_yearly->ID."' and (status != 'cancelled' AND coupon_id != '0');");
                            $yearly_cancelled_booking = $wpdb->get_results("SELECT $table_payments.id as cancelled_bookings from $table_hotel_entries inner join $table_payments on $table_hotel_entries.id = item_belongsto WHERE YEAR(CONCAT(SUBSTRING(arrival_date, 7, 4), '-', SUBSTRING(arrival_date, 1, 2), '-', SUBSTRING(arrival_date, 4, 2))) = YEAR(NOW()) and payment_status = 'cancelled' AND room_id= '".$key_yearly->ID."';");
                            $meta = get_post_meta( $key_yearly->ID, 'easync_hotel', true );
                            $yearly_total_earning = array();
                            foreach ($hotel_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 = 'hotel' 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 $key_yearly->post_title; ?></td>
                            <td><?php echo count($hotel_yearly_bookings); ?></td>
                            <td><?php echo count($yearly_completed_booking); ?></td>
                            <td><?php echo count($yearly_cancelled_booking); ?></td>
                            <td><?php echo easyncCurrency(). ' ' .number_format($meta['price'], 2); ?></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="5"> </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" style="display:none;">
                
            </tbody>

        </table>
    </div>
    <br>
    <div class="chart_all_display">
        <div class="pieChart_bookings">
            <h4>All Bookings</h4>
            <canvas id="pieChart_bookings_display"></canvas>
        </div>
        <div class="pieChart_completed">
            <h4>Completed Bookings</h4>
            <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">
            <h4>Cancelled Bookings</h4>
            <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">
            <h4>Earnings</h4>
            <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_completed;
    let myPieChart_cancelled;
    let myPieChart_earnings;
    jQuery(document).ready( function () {
        
        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "show_graphs_all_bookings"},
            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"},
                        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"},
                        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"},
                        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("#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").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            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"},
                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();
                    }
                    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"},
                            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"},
                            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"},
                            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").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            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"},
                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"},
                            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: "graph_monthly_cancelled"},
                            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"},
                            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").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            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"},
                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"},
                            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"},
                            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"},
                            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").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            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_yearly_bookings"},
                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"},
                            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: "graphs_yearly_cancelled"},
                            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"},
                            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 == 'all') {
            jQuery(".weekly_data").hide();
            jQuery(".all_data").show();
            jQuery(".monthly_data").hide(); 
            jQuery(".quarter_data").hide(); 
            jQuery(".yearly_data").hide();
            jQuery(".custom_data").hide();
            jQuery("#sync-from-date").val("");
            jQuery("#sync-to-date").val("");
            jQuery(".chart_all_display").show();
            jQuery('.if_empty').hide();
            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"},
                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"},
                            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"},
                        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"},
                            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_hotel').click( function () {
        var from_datee = jQuery('#sync-from-date').val();
        var to_datee = jQuery('#sync-to-date').val();

        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: "display_custom_data_hotel", 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").show();
                jQuery("#sync-from-date").val("");
                jQuery("#sync-to-date").val("");
                jQuery(".chart_all_display").show();
                jQuery(".custom_data").empty();
                jQuery(".custom_data").append(data);
                jQuery('.if_empty').hide();
                jQuery('.graph_label').show();

            }
        });

        jQuery.ajax({
            type: "POST",
            dataType: "json",
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "show_graphs_custom_bookings", 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').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", 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", 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", 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>

