<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:ignoreFile

use Morilog\Jalali\Jalalian;

$month_start_timestamp     = anbarak_make_datetime('first day of this month');
$month_end_timestamp       = anbarak_make_datetime('today 23:59:59');
$six_month_ago_timestamp   = anbarak_make_datetime('-6 months');


?>
<div class="stats text-sm 2xl:text-base">

    <div class="filter-form-wrapper box mb-3">
        <form action="" id="frm-report-stats" class="flex flex-col lg:flex-row gap-3 items-center flex-grow">
            <input type="hidden" name="action" value="anbarak_get_report_stats">
            <input type="hidden" name="security" value="<?php echo esc_attr(ANBARAK_AJAX_NONCE) ?>">
            <?php
            anbarak_get_template_part('report/filter-form-dates', [
                'date_from' => $month_start_timestamp,
                'date_to' => $month_end_timestamp,
                'predefined' => true,
                'selected_predefined_date' => 'month'
            ]);
            ?>
            <button type="submit" class="button button--primary"><?php esc_html_e('Apply filters', 'anbarak'); ?></button>
            <div class="ms-auto text-sm text-gray-500 px-2.5 py-1 whitespace-nowrap ltr flex items-center gap-1.5 dark:bg-[#1a1a1a] dark:text-gray-400" id="report-date-range">
                <span class="date-range-from"><?php echo esc_attr(anbarak_date_format('Y/m/d', $month_start_timestamp)); ?></span>
                <span class="text-gray-300">–</span>
                <span class="date-range-to"><?php echo esc_attr(anbarak_date_format('Y/m/d', $month_end_timestamp)); ?></span>
            </div>
        </form>
    </div>

    <div id="report-stats-holder" data-filter-form="frm-report-stats">
        <?php anbarak_render_filtered_stats($month_start_timestamp, $month_end_timestamp); ?>
    </div>

    <div class="w-full">
        <div id="overall-stat-chart" class="box mt-3 mb-2 h-[400px]"></div>
        <div id="sell-profit-chart" class="box mt-3 mb-2 h-[400px]"></div>
        <div id="last-six-month-stat-chart" class="box mt-3 mb-2 h-[400px]"></div>
    </div>
</div>

<div id="last-six-month-sales-all" hidden>
    <?php
    $six_month_sales = @anbarak_get_month_sales_prices($six_month_ago_timestamp, $month_end_timestamp);
    echo esc_html(wp_json_encode($six_month_sales));
    ?>
</div>
<div id="last-six-month-profit" hidden>
    <?php
    $six_month_profit = @anbarak_get_month_sales_profit($six_month_ago_timestamp, $month_end_timestamp);
    echo esc_html(wp_json_encode($six_month_profit));
    ?>
</div>
<div id="current-month-name" hidden>
    <?php echo esc_html(wp_json_encode(anbarak_date_format('Y', time())  . ' ' .  anbarak_date_format('F', time()))); ?>
</div>
<div id="month-sales-all" hidden>
    <?php
    $sales = @anbarak_get_month_sales_prices($month_start_timestamp, $month_end_timestamp);
    echo esc_html(wp_json_encode($sales));
    ?>
</div>
<div id="month-sales-online" hidden>
    <?php
    $sales = @anbarak_get_month_sales_prices($month_start_timestamp, $month_end_timestamp, 'online');
    echo esc_html(wp_json_encode($sales));
    ?>
</div>
<div id="month-sales-in-store" hidden>
    <?php
    $sales = @anbarak_get_month_sales_prices($month_start_timestamp, $month_end_timestamp, 'in-store');
    echo esc_html(wp_json_encode($sales));
    ?>
</div>
<div id="month-expense" hidden>
    <?php
    $sales = anbarak_get_month_expenses($month_start_timestamp, $month_end_timestamp);
    echo esc_html(wp_json_encode($sales));
    ?>
</div>
<div id="month-share" hidden>
    <?php
    //phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date 
    $the_day_end = mktime(23, 59, 59, date('n'), date('d'), date('Y'));
    $sales = array();
    $sales['online'] = count(anbarak_get_sales($month_start_timestamp, $the_day_end, 'online'));
    $sales['offline'] = count(anbarak_get_sales($month_start_timestamp, $the_day_end, 'in-store'));
    echo esc_html(wp_json_encode($sales));
    ?>
</div>
<div id="month-profit" hidden>
    <?php
    $profit = @anbarak_get_month_sales_profit($month_start_timestamp, $month_end_timestamp);
    echo esc_html(wp_json_encode($profit));
    ?>
</div>
