<?php
session_start(['gc_maxlifetime' => 14400, 'cookie_httponly' => true]);

include('member_session.php');
include('../coach/functions.php');
include('../coach/includes/phpmailer.php');
include('../coach/includes/TCPDF/tcpdf.php');
include("../coach/objects/class.member.php");

$member_id = $_SESSION['memberid'] ?? null;

$type = $_POST['type'] ?? $_GET['type'] ?? "";
$file = $_POST['file'] ?? $_GET['file'] ?? "";

$member = new Member();
$member->memberId = $member_id;

// Post data
$post_from_date = $_POST['log_from_date'] ?? "";

// Default week logic
$log_from_date = (date('w') == '1') ? date('Y-m-d') : date('Y-m-d', strtotime('last monday'));
$log_to_date   = (date('w') == '0') ? date('Y-m-d') : date('Y-m-d', strtotime('next sunday'));

$days = [
    'sun' => [], 'mon' => [], 'tue' => [], 'wed' => [],
    'thu' => [], 'fri' => [], 'sat' => []
];

$meal_scor_tot = 0;
$activi_scor_tot = 0;

if (!empty($post_from_date) && $type === 'pdf_file') {

    $log_from_date = date('Y-m-d', strtotime($post_from_date));
    $log_to_date   = date('Y-m-d', strtotime($log_from_date . ' +6 days'));

    // Fetching details
    $memberData = new Member();
    $memberData->memberId = $member_id;

    $member = $memberData->Get($member_id);
    $meals_per_day = $member->meals_per_day ?? 0;

    $accountability_chart_data = $memberData->GetAccountability_chartList([
        ['log_date', '>=', $log_from_date],
        ['log_date', '<=', $log_to_date]
    ]);
}

if ($type === 'pdf_file') {

    class MYPDF extends TCPDF {}

    include '../coach/includes/pdf_files/accountability.php';
}

include('member-header.php');
?>

<style>
.container1 select {
    padding: 10px;
    background: #ffffff;
    border: 1px solid #CCCCCC;
    border-radius: 3px;
    margin: 0 3px;
}
</style>

<?php include('menu.php'); ?>

<?php if (!empty($remoteClient) && $remoteClient == 1 && !empty($remote_client) && in_array("accountability_charts", $remote_client)) { ?>
<script>
    window.location.href = "member_homepage.php";
</script>
<?php } ?>

<div>
    <br><br>
    <p style="text-align: center;font-weight: bold;">Accountability Charts</p>
</div>

<div>
<form target="_blank" method="POST" id="accountability_form_print" name="accountability_form_print"
      action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8'); ?>">

    <div class="row">
        <div class="form-group row" style="margin-top: 5px;">
            <div class="col-md-3">
                <label for="start_date">Select Date:</label>
            </div>

            <div class="col-md-9">
                <div class="container1">
                    <input type="text"
                           id="log_from_date"
                           name="log_from_date"
                           data-field="date"
                           autocomplete="off"
                           class="form-control"
                           value="<?php echo !empty($log_from_date) ? date('Y-m-d', strtotime($log_from_date)) : date('Y-m-d'); ?>"
                           readonly
                           style="display: none;">
                </div>
            </div>
        </div>

        <div class="form-group row" style="margin-top: 2px;">
            <div class="col-md-12">
                <input type="submit" name="submit" class="btn btn-default" value="Print"/>
            </div>
        </div>
    </div>

    <input type="hidden" name="type" value="pdf_file">
    <input type="hidden" name="file" value="accountability_pdf_print">
    <input type="hidden" name="member_id" value="<?php echo htmlspecialchars($member_id); ?>">

</form>
</div>

<script src="../coach/js/jquery.js"></script>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="../coach/js/c3.min.js"></script>

<script>
$(function () {
    $('#log_from_date').dropdownDatepicker({
        defaultDateFormat: 'yyyy-mm-dd',
        displayFormat: 'mdy',
        submitFormat: 'yyyy-mm-dd'
    });
});
</script>

<script src="../coach/js/jquery-dropdown-datepicker.min.js"></script>

<?php include('member-footer.php'); ?>