<?php

if (!class_exists('AHACHAT_WP_ANALYTICS')): /****/
    class AHACHAT_WP_ANALYTICS {
        public function __construct() {
        ?>

<?php
global $wpdb;
//statistical
$ahachat_table_statistical = AHACHAT_ANALYTICS_TABLE;
$ahachat_table_analystic_order = AHACHAT_ANALYTICS_FB_TABLE;
$ahachat_date_statistical=date('Y-m-d',time());
$ahachat_array_month=array(1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec');
//print_r($ahachat_array_month);
if(isset($_GET['month']) && isset($_GET['year'])){
    $month=(int)$_GET['month'];
    $year=(int)$_GET['year'];
    $month_current=date('m',time());
    $year_current=date('Y',time());
  /*  
    if($month==$month_current && $year==$year_current){
        $day=date('d',time());
    }else{
        $day=$number_day_in_month=cal_days_in_month(CAL_GREGORIAN, $month, $year); // function cal_days_in_month of php
    }
  */
    $day = date('t', mktime(0, 0, 0, $month, 1, $year));
}else{
//    $day=date('d',time());
    $month=date('m',time());
    $year=date('Y',time());
    $day = date('t', mktime(0, 0, 0, $month, 1, $year));
}
foreach ($ahachat_array_month as $key => $value) {
    if($month== $key)
    {
        $month_abbreviations=$value;
        break;
    }
}
?>

<div class="wrap">
<h1 class="wp-heading-inline"> <?php _e("Analytics",AHACHAT_WP);?></h1>
</div>
    <div style="width:70%;float: left;">
            <div style="float: right;padding: 0px 0px 15px 0;">
                <?php $link_url_statis=add_query_arg(array('page'=>'ahachat-analytics'),admin_url('admin.php')); ?>
                    <input type="hidden" id="ahachat_href_statistical" value="<?php echo $link_url_statis; ?>">
                    <select name="ahachat_statistical_month" id="ahachat_statistical_month">
                        <?php foreach ($ahachat_array_month as $key => $value) {?>
                                <option <?php if($key==$month) echo "selected='selected'"; ?> value="<?php echo $key ?>"><?php echo Aha__Month_In_Year($value); ?></option>
                        <?php } ?> 
                    </select>
                    <select name="ahachat_statistical_year" id="ahachat_statistical_year">
                        <?php for($i=2020;$i<=date('Y',time());$i++) { ?>
                                 <option <?php if($i==$year) echo "selected='selected'"; ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
                        <?php } ?>
                    </select>
            </div>
            
            <div class="ahachat_analystic_container ahachat_analystic_container_fix_css">
            <?php 
                $ahachat_row_user_order_cartback=$wpdb->get_results("SELECT * FROM $ahachat_table_analystic_order WHERE month='".$month."' AND year='".$year."' ");

                $ahachat_revenue_recovered=0;
                $ahachat_count_orders_recovered=0;
                $count_message_recipient=0;
                if(count($ahachat_row_user_order_cartback)>0){
                    foreach ($ahachat_row_user_order_cartback as $key => $value) {
                        $first_reminder=unserialize($value->first_reminder);
                        $second_reminder=unserialize($value->second_reminder);
                        $third_reminder=unserialize($value->third_reminder);

                        $_first_revenue=isset($first_reminder['revenue']) ? $first_reminder['revenue'] : 0;
                       
                        $_second_revenue=isset($second_reminder['revenue']) ? $second_reminder['revenue'] : 0;
                        $_third_revenue=isset($third_reminder['revenue']) ? $third_reminder['revenue'] : 0;
                        $_first_purchases=isset($first_reminder['purchases']) ? $first_reminder['purchases'] : 0;
                        $_second_purchases=isset($second_reminder['purchases']) ? $second_reminder['purchases'] : 0;
                        $_third_purchases=isset($third_reminder['purchases']) ? $third_reminder['purchases'] : 0;
                        $ahachat_revenue_recovered=$ahachat_revenue_recovered+($_first_revenue+$_second_revenue+$_third_revenue);
                        $ahachat_count_orders_recovered=$ahachat_count_orders_recovered+($_first_purchases+$_second_purchases+$_third_purchases);
                    }
                }
                $user_add_to_cart=$wpdb->get_results("SELECT * FROM $ahachat_table_statistical WHERE month='".$month."' AND year='".$year."' ");
                
                if(count($user_add_to_cart)>0){
                    foreach ($user_add_to_cart as $key => $value) {
                        $count_message_recipient=$count_message_recipient+$value->send_user;
                    }
                }
            ?>
                    <ul class="ahachat_analystic_li">
                        <li><div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span>$<?php echo $ahachat_revenue_recovered; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">revenue recovered</span></div>
                            </div>
                        </li>
                        <li>
                            <div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span><?php echo $ahachat_count_orders_recovered; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">orders recovered</span></div>
                            </div>
                        </li>
                        <li><div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span><?php echo $count_message_recipient; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">message sent</span></div>
                            </div></li>
                    </ul>
            </div>
            <br/>
            <?php
                $row_coupon_shortcode_user=$wpdb->get_results("SELECT * FROM $ahachat_table_statistical WHERE month='".$month."' AND year='".$year."' ");
                $coupon_users=0;$shortcode_users=0;$coupon_sent=0;$shortcode_sent=0;
                if(count($row_coupon_shortcode_user)>0){
                    foreach ($row_coupon_shortcode_user as $key => $value) {
                        $coupon_users=$coupon_users+$value->coupon_user;
                        $coupon_sent=$coupon_sent+$value->coupon_sent;
                        $shortcode_users=$shortcode_users+$value->shortcode_user;
                        $shortcode_sent=$shortcode_sent+$value->shortcode_sent;
                    }
                }
            ?>
            <div class="ahachat_analystic_container ahachat_analystic_container_fix_css" style="margin: 20px 0;float: left; width: 100%;">
            
                    <ul class="ahachat_analystic_li">
                        <li style="width: 25%"><div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span><?php echo $coupon_users; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">Coupon users</span></div>
                            </div>
                        </li>
                        <li style="width: 25%">
                            <div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span><?php echo $coupon_sent; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">Coupon sent</span></div>
                            </div>
                        </li>
                        <li style="width: 25%"><div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span><?php echo $shortcode_users; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">Shortcode users</span></div>
                            </div></li>

                        <li style="width: 24%"><div class="ahachat_analystic_parent">
                                <div class="ahachat_analystic_child_on"><span><?php echo $shortcode_sent; ?></span></div>
                                <div class="ahachat_analystic_child_under"><span style="font-size:16px;text-transform: uppercase;">Shortcode sent</span></div>
                            </div></li>

                    </ul>
            </div>

            <?php 
                $row_table_analystic=$wpdb->get_results("SELECT * FROM $ahachat_table_analystic_order WHERE month='".$month."' AND year='".$year."' ");
                $send_first=0;$send_second=0;$send_third=0;
                $click_first=0;$click_second=0;$click_third=0;$purchases_first=0;$purchases_second=0;$purchases_third=0;$revenue_first=0;$revenue_second=0;$revenue_third=0;
                
                if(count($row_table_analystic)>0){

                    foreach ($row_table_analystic as $key => $analytics_reminder) {
                        $firt_reminder=unserialize($analytics_reminder->first_reminder);
                        $second_reminder=unserialize($analytics_reminder->second_reminder);
                        $third_reminder=unserialize($analytics_reminder->third_reminder);
                        
                        $first_click=(isset($firt_reminder['click']) && !empty($firt_reminder['click'])) ? (double)$firt_reminder['click'] : 0;
                        $second_click=(isset($second_reminder['click']) && !empty($second_reminder['click'])) ? (double)$second_reminder['click'] : 0;
                        $third_click=(isset($third_reminder['click']) && !empty($third_reminder['click'])) ? (double)$third_reminder['click'] : 0;
                        $first_purchases=(isset($firt_reminder['purchases']) && !empty($firt_reminder['purchases'])) ? (double)$firt_reminder['purchases'] : 0;
                        $second_purchases=(isset($second_reminder['purchases']) && !empty($second_reminder['purchases'])) ? (double)$second_reminder['purchases'] : 0;
                        $third_purchases=(isset($third_reminder['purchases']) && !empty($third_reminder['purchases'])) ? (double)$third_reminder['purchases'] : 0;
                        $firt_revenue=(isset($firt_reminder['revenue']) && !empty($firt_reminder['revenue'])) ? (double)$firt_reminder['revenue'] : 0;
                        $second_revenue=(isset($second_reminder['revenue']) && !empty($second_reminder['revenue'])) ? (double)$second_reminder['revenue'] : 0;
                        $third_revenue=(isset($third_reminder['revenue']) && !empty($third_reminder['revenue'])) ? (double)$third_reminder['revenue'] : 0;

                        $click_first=$click_first+$first_click;$purchases_first=$purchases_first+$first_purchases;$revenue_first=$revenue_first+$firt_revenue;
                        $click_second=$click_second+$second_click;$purchases_second=$purchases_second+$second_purchases;$revenue_second=$revenue_second+$second_revenue;
                        $click_third=$click_third+$third_click;$purchases_third=$purchases_third+$third_purchases;$revenue_third=$revenue_third+$third_revenue;

                        
                    }
                }

               
                $row_table_statistical=$wpdb->get_results("SELECT * FROM $ahachat_table_statistical WHERE month='".$month."' AND year='".$year."' ");
                if(count($row_table_statistical)>0){
                    foreach ($row_table_statistical as $key => $statistical_reminder) {
                        $send_first=$send_first+$statistical_reminder->first_reminder_sent;
                        $send_second=$send_second+$statistical_reminder->second_reminder_sent;
                        $send_third=$send_third+$statistical_reminder->third_reminder_sent;
                    }
                }
            ?>
            <div id="ahachat_table_reminder" class="ahachat_table_reminder_fix_css">
                <table id="table_reminder">
                    <tr class="tr_reminder title">
                        <td class="td_reminder">Message</td>
                        <td class="td_reminder">Sent</td>
                        <td class="td_reminder">Click</td>
                        <td class="td_reminder">Purchases</td>
                        <td class="td_reminder">Revenue</td>
                    </tr>
                    <tr class="tr_reminder">
                    	<?php
                    		$ahachat_first_reminder=get_option('ahachat_first_reminder');
                    		$ahachat_second_reminder=get_option('ahachat_second_reminder');
                    		$ahachat_third_reminder=get_option('ahachat_third_reminder');
                    	?>
                        <td class="td_reminder">
                            <ul class="ul_first_col">
                                <li class="li_first_col">First Reminder</li><?php echo $ahachat_first_reminder['hour']; ?> hour after
                            </ul>
                        </td>
                        <td class="td_reminder"><?php echo $send_first; ?></td>
                        <td class="td_reminder"><?php echo $click_first;?></td>
                        <td class="td_reminder"><?php echo $purchases_first;?></td>
                        <td class="td_reminder"><?php echo $revenue_first;?></td>
                    </tr>
                    <tr class="tr_reminder">
                        <td class="td_reminder">
                            <ul class="ul_first_col">
                                <li class="li_first_col">Second Reminder</li><?php echo $ahachat_second_reminder['hour']; ?> hour after
                            </ul>
                        </td>
                        <td class="td_reminder"><?php echo $send_second; ?></td>
                        <td class="td_reminder"><?php echo $click_second;?></td>
                        <td class="td_reminder"><?php echo $purchases_second;?></td>
                        <td class="td_reminder"><?php echo $revenue_second;?></td>
                    </tr>
                    <tr class="tr_reminder">
                        <td class="td_reminder"><ul class="ul_first_col">
                                <li class="li_first_col">Third Reminder</li><?php echo $ahachat_third_reminder['hour']; ?> hour after
                            </ul></td>
                        <td class="td_reminder"><?php echo $send_third; ?></td>
                        <td class="td_reminder"><?php echo $click_third;?> </td>
                        <td class="td_reminder"><?php echo $purchases_third;?></td>
                        <td class="td_reminder"><?php echo $revenue_third;?></td>
                    </tr>
                   
                    
                </table>
            </div>
			<br/>

            <canvas id="canvas"></canvas>
    </div>
    <div style="width: 28%;float:right;margin-right: 10px;height:  300px;">
        <style type="text/css">
            .statistics-box.with-icon{
                text-align: center;padding: 8px 20px;
                font-size: 16px;
            }
            .ahachat_short_coupon_title{
                color: #ffffff;
            }
        </style>
        <?php
            $ahachat_count_message_sent=0;
            $user_statistical=$wpdb->get_results("SELECT * FROM $ahachat_table_statistical");

            if(count($user_statistical)>0){
                foreach ($user_statistical as $key => $value) {
                    $ahachat_count_message_sent=$ahachat_count_message_sent+$value->send_user;
                }
            }

            $ahachat_row_user_order_cartback=$wpdb->get_results("SELECT * FROM $ahachat_table_analystic_order");

            $ahachat_revenue_recovered=0;
            $ahachat_count_orders_recovered=0;

        if(count($ahachat_row_user_order_cartback)>0){
            foreach ($ahachat_row_user_order_cartback as $key => $value) {
                $first_reminder=unserialize($value->first_reminder);
                $second_reminder=unserialize($value->second_reminder);
                $third_reminder=unserialize($value->third_reminder);

                $_first_revenue=isset($first_reminder['revenue']) ? $first_reminder['revenue'] : 0;

                $_second_revenue=isset($second_reminder['revenue']) ? $second_reminder['revenue'] : 0;
                $_third_revenue=isset($third_reminder['revenue']) ? $third_reminder['revenue'] : 0;
                $_first_purchases=isset($first_reminder['purchases']) ? $first_reminder['purchases'] : 0;
                $_second_purchases=isset($second_reminder['purchases']) ? $second_reminder['purchases'] : 0;
                $_third_purchases=isset($third_reminder['purchases']) ? $third_reminder['purchases'] : 0;
                $ahachat_revenue_recovered=$ahachat_revenue_recovered+($_first_revenue+$_second_revenue+$_third_revenue);
                $ahachat_count_orders_recovered=$ahachat_count_orders_recovered+($_first_purchases+$_second_purchases+$_third_purchases);
            }
        }
        ?>
        <h2 style="font-size: 20px;font-weight: bold;text-align: center;text-transform: uppercase;">ALL-TIME TOTAL VALUE</h2>
        <div class="box-content bg-success text-white">
            <div style="background-color: #bd7a96;" class="statistics-box with-icon">
                <h3 class="ahachat_short_coupon_title">REVENUE RECOVERED</h3>
                <h3 class="counter"><b>$<?php echo $ahachat_revenue_recovered; ?></b></h3>
            </div>
            <div style="background-color: #bf916a;" class="statistics-box with-icon">
                <h3 class="ahachat_short_coupon_title">ORDERS RECOVERED</h3>
                <h2 class="counter"><b><?php echo $ahachat_count_orders_recovered; ?></b></h2>
            </div>
            <div style="background-color: #86bd92;" class="statistics-box with-icon">
               <h3 class="ahachat_short_coupon_title">MESSAGE SENT</h3>
                <h2 class="counter"><b><?php echo $ahachat_count_message_sent; ?></b></h2>
            </div>
        </div>
    </div> 
    <?php 
        $array_day=array();
        $array_number_user_add_cart=array();
        $array_number_user_sent=array();
        for($i=1;$i<=$day;$i++){
            array_push($array_day, $i);
            $user_add_to_cart=$wpdb->get_row("SELECT * FROM $ahachat_table_statistical WHERE day='".$i."' AND month='".$month."' AND year='".$year."' ");
                      if(empty($user_add_to_cart)):
                         array_push($array_number_user_add_cart, 0);
                         array_push($array_number_user_sent, 0);
                      else:
                         $user_test=$user_add_to_cart->user_test;
                         $send_user=$user_add_to_cart->send_user;
                         array_push($array_number_user_add_cart, $user_test);
                         array_push($array_number_user_sent, $send_user);
                      endif;
        }
        $number_user_add_to_cart=implode(',', $array_number_user_add_cart);
        $number_message_user_sent=implode(',', $array_number_user_sent);
    ?>
    <script>
        window.onload = function() {
            var monthInYear = '<?php echo Aha__Month_In_Year($month_abbreviations);?> <?php echo $year; ?>';
            var config = {
                type: 'line',
                data: {
                    labels: [<?php for($i=1;$i<=$day;$i++){echo $i.",";}?>],
                    datasets: [{
                        label: "Number of  Add-to-cart users",
                        backgroundColor: window.chartColors.red,
                        borderColor: window.chartColors.red,
                        data: [
                            <?php echo $number_user_add_to_cart; ?>
                        ],
                        fill: false,
                    }, {
                        label: "Number of messages sent",
                        fill: false,
                        backgroundColor: window.chartColors.blue,
                        borderColor: window.chartColors.blue,
                        data: [
                            <?php echo $number_message_user_sent; ?>
                        ],
                    }]
                },
                options: {
                    responsive: true,
                    title:{
                        display:true,
                        text:'Statistics Chart'
                    },
                    tooltips: {
                        enabled: true,
                        mode: 'single',
                        callbacks: {
                            title: function(tooltipItems, data) {
                                return tooltipItems[0].xLabel + ' ' + monthInYear;
                            }
                        }
                    },
                    hover: {
                        mode: 'nearest',
                        intersect: true
                    },
                    scales: {
                        xAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: monthInYear
                            }
                        }],
                        yAxes: [{
                            display: true,
                            scaleLabel: {
                                display: true,
                                labelString: 'Number Add-to-cart / Messages sent users'
                            }
                        }]
                    }
                }
            };
            var ctx = document.getElementById("canvas").getContext("2d");
            window.myLine = new Chart(ctx, config);
        };
    </script>
<style>
    .tooltip-chart {
        text-align: center;
    }

</style>
<?php
            }
        }
    endif;
    new AHACHAT_WP_ANALYTICS();
 ?>