<?php
session_start(['gc_maxlifetime' => 14400, 'cookie_httponly' => true]); // Starting Session
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");
include("../coach/objects/class.training_template_details.php");
include("../coach/objects/class.homework_details.php");
include("../coach/objects/class.weekly_schedule_details.php");
include("../coach/objects/class.cardio_phase.php");
include'../coach/objects/class.smr_details.php';
include '../coach/objects/class.activation_stabilization_details.php';
include '../coach/objects/class.dynamic_movement_prep_details.php';
include '../coach/objects/class.power_saq_fullbody_details.php';
include '../coach/objects/class.flexibility_mobility_details.php';
include '../coach/objects/class.metabolic_conditioning_details.php';
include '../coach/objects/class.exercises_details.php';
include '../coach/objects/class.weights_reps_details.php';
include '../coach/objects/class.baseline_measurement.php';
include '../coach/objects/class.baseline_screen.php';
include '../coach/objects/class.baseline_bodycomposition.php';

$member_id = $_SESSION['memberid'];
$type = (isset($_POST['type']) && $_POST['type'] != "") ? $_POST['type'] : (isset($_GET['type']) && $_GET['type'] != '' ? $_GET['type'] : "");
$file = (isset($_POST['file']) && $_POST['file'] != "") ? $_POST['file'] : (isset($_GET['file']) && $_GET['file'] != '' ? $_GET['file'] : "");

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

if ($type == 'send') {
    //post data
    $mail_subject = isset($_POST['mail_subject']) ? $_POST['mail_subject'] : "";
    $mail_body = isset($_POST['mail_body']) ? $_POST['mail_body'] : "";
    $baseline_ids = (isset($_POST['baseline_ids']) && $_POST['baseline_ids'] != "") ? $_POST['baseline_ids'] : ((isset($_GET['baseline_ids']) && $_GET['baseline_ids'] != '') ? array($_GET['baseline_ids']) : []);
    $training_ids = (isset($_POST['training_ids']) && $_POST['training_ids'] != "") ? $_POST['training_ids'] : ((isset($_GET['training_ids']) && $_GET['training_ids'] != '') ? array($_GET['training_ids']) : []);
    $homework_ids = (isset($_POST['homework_ids']) && $_POST['homework_ids'] != "") ? $_POST['homework_ids'] : ((isset($_GET['homework_ids']) && $_GET['homework_ids'] != '') ? array($_GET['homework_ids']) : []);
    $schedule_ids = (isset($_POST['schedule_ids']) && $_POST['schedule_ids'] != "") ? $_POST['schedule_ids'] : ((isset($_GET['schedule_ids']) && $_GET['schedule_ids'] != '') ? array($_GET['schedule_ids']) : []);

    // SECURITY FIX: sanitise all ID arrays
    $baseline_ids = array_filter(array_map('intval', (array)$baseline_ids));
    $training_ids = array_filter(array_map('intval', (array)$training_ids));
    $homework_ids = array_filter(array_map('intval', (array)$homework_ids));
    $schedule_ids = array_filter(array_map('intval', (array)$schedule_ids));
    $cardio_ids = (isset($_POST['cardio_ids']) && $_POST['cardio_ids'] != "") ? $_POST['cardio_ids'] : ((isset($_GET['cardio_ids']) && $_GET['cardio_ids'] != '') ? array($_GET['cardio_ids']) : []);

    if (empty($file)) {
        //MAIL functions
        $mail = new PHPMailer();
        if ($login_session == 'chris') {
            $from_address = _fromEmail_;
            $from_name = ucfirst("chris");
        } else if ($login_session == 'coach') {
            $from_address = _fromEmail_;
            $from_name = ucfirst("Coach");
        }

        $mail->From = $from_address;
        $mail->FromName = $from_name;
        $mail->Subject = $mail_subject;

        $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
        $mail->Body = $mail_body;
        $mail->AddAddress(trim($email_to));
    }

    //fetching homework details
    if (count($homework_ids) != 0) {
        $pdf_flag = 1;
        $hw_ar = 0;
        while (isset($homework_ids[$hw_ar]) && $homework_ids[$hw_ar] != '') {
            $val = $homework_ids[$hw_ar];
            for ($i = intval($val); $i < ($val + 6); $i++) {
                $homework_ids_ar[] = $i;
            }
            $hw_ar++;
        }
        $homework_class = new Homework_details();
        $homework_list = $homework_class->GetList(array(array("homework_detailsId IN (" . implode(",", $homework_ids_ar) . ")")));
        $enabled_videolinks = "";
        foreach ($homework_list as $hw_value) {
            $enabled_videolinks = $hw_value->enabled_videolinks;
            $internal_notes[] = $hw_value->internal_notes;
            $corrective_details_arr['corrective_exercise'] = $hw_value->corrective_exercise;
            $corrective_details_arr['corrective_sets'] = $hw_value->corrective_sets;
            $corrective_details_arr['corrective_reps'] = $hw_value->corrective_reps;
            $corrective_details_arr['corrective_rest'] = $hw_value->corrective_rest;
            $corrective_details_arr['corrective_notes'] = $hw_value->corrective_notes;
            $strength_details_arr['strength_exercise'] = $hw_value->strength_exercise;
            $strength_details_arr['strength_sets'] = $hw_value->strength_sets;
            $strength_details_arr['strength_reps'] = $hw_value->strength_reps;
            $strength_details_arr['strength_rest'] = $hw_value->strength_rest;
            $strength_details_arr['strength_notes'] = $hw_value->strength_notes;
            $flexibility_details_arr['flexibility_exercise'] = $hw_value->flexibility_exercise;
            $flexibility_details_arr['flexibility_sets'] = $hw_value->flexibility_sets;
            $flexibility_details_arr['flexibility_reps'] = $hw_value->flexibility_reps;
            $flexibility_details_arr['flexibility_rest'] = $hw_value->flexibility_rest;
            $flexibility_details_arr['flexibility_notes'] = $hw_value->flexibility_notes;
            $corrective_details_arr['filename'] = $hw_value->filename;
            $corrective_details[] = $corrective_details_arr;
            $strength_details[] = $strength_details_arr;
            $flexibility_details[] = $flexibility_details_arr;
        }
    }

    if ($pdf_flag != 0) {
        //creating pdf files        
        class MYPDF extends TCPDF {
            
        }

        //for homework details template
        if (count($homework_ids) != 0) {
            $i = 1;
            $j = 0;
            $count = 1;
            for ($hw_pg = 0; $hw_pg < count($homework_ids); $hw_pg++) {
                include '../coach/includes/pdf_files/homework_pdf.php';
                $j = $i;
                $count++;
            }
        }
    }
    if (empty($file)) {
        if ($mail->Send()) {
            $message = "Mail Sent Succcessfully";
        }
    }
}

$homework_temp_id = $member->GetHomework_detailsList();

$sno = 1;
$hw_det = 1;
$hw_det1 = 1;

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

<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>
<style>
       
.app-layout {
width: 100%;
}
.link-card-title {
padding: 0;
}
.link-card:hover {
text-decoration: none;
}
.page-header-title, .page-header-subtitle {
padding: 0;
}

</style>

    <!-- Mobile Top Bar (logo centered) -->
    <div class="mobile-top-bar">
        <img src="./assets/images/logo.png" alt="Brand Logo">
    </div>

    <div class="app-layout">
        <!-- Desktop Sidebar -->
      <?php include('menu.php'); ?>

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

        <!-- Main Content -->
        <main class="main-content">
            <!-- Page Header -->
            <header class="page-header">
                <div class="page-header-meta">
                    <div>
                        <h1 class="page-header-title">Homework</h1>
                        <p class="page-header-subtitle">View your homework assignments and exercises</p>
                    </div>
                </div>
            </header>

            <div class="body-content">

                <!-- Body Content -->
                <div class="tp-cards-grid">

                    <?php
                    $has_data = false;
                    if (count($homework_temp_id) != 0) {
                        foreach ($homework_temp_id as $home_work_id) {
                            if ($hw_det1 == $hw_det) {
                                $hw_det = $hw_det + 6;
                                if ($home_work_id->is_show == 1) {
                                    $has_data = true;
                                    ?>
                                    <!-- Homework Card -->
                                    <div class="link-card link-card-doc">
                                        <div class="link-card-icon">
                                            <svg width="40" height="40" viewBox="0 0 40 40" fill="none"
                                                xmlns="http://www.w3.org/2000/svg">
                                                <path
                                                    d="M9.99996 36.6668C9.1159 36.6668 8.26806 36.3156 7.64294 35.6905C7.01782 35.0654 6.66663 34.2176 6.66663 33.3335V6.66683C6.66663 5.78278 7.01782 4.93493 7.64294 4.30981C8.26806 3.68469 9.1159 3.3335 9.99996 3.3335H23.3333C23.8609 3.33265 24.3834 3.43617 24.8708 3.63812C25.3583 3.84006 25.8009 4.13643 26.1733 4.51017L32.1533 10.4902C32.528 10.8627 32.8252 11.3057 33.0278 11.7938C33.2303 12.2818 33.3342 12.8051 33.3333 13.3335V33.3335C33.3333 34.2176 32.9821 35.0654 32.357 35.6905C31.7319 36.3156 30.884 36.6668 30 36.6668H9.99996Z"
                                                    stroke="black" stroke-width="1.33333" stroke-linecap="round"
                                                    stroke-linejoin="round" />
                                                <path
                                                    d="M23.3334 3.3335V11.6668C23.3334 12.1089 23.509 12.5328 23.8215 12.8453C24.1341 13.1579 24.558 13.3335 25 13.3335H33.3334"
                                                    stroke="black" stroke-width="1.33333" stroke-linecap="round"
                                                    stroke-linejoin="round" />
                                                <path d="M16.6667 15H13.3334" stroke="black" stroke-width="1.33333"
                                                    stroke-linecap="round" stroke-linejoin="round" />
                                                <path d="M26.6667 21.6665H13.3334" stroke="black" stroke-width="1.33333"
                                                    stroke-linecap="round" stroke-linejoin="round" />
                                                <path d="M26.6667 28.3335H13.3334" stroke="black" stroke-width="1.33333"
                                                    stroke-linecap="round" stroke-linejoin="round" />
                                            </svg>
                                        </div>
                                        <div class="link-card-content">
                                            <h3 class="link-card-title">Homework <?php echo htmlspecialchars($home_work_id->filename); ?></h3>
                                            <p class="link-card-desc">View your homework assignment</p>
                                        </div>
                                        <div class="link-card-doc-actions">
                                            <a href="homework-mom.php?homework_ids=<?php echo $home_work_id->homework_detailsId; ?>&member_id=<?php echo isset($member_id) ? $member_id : ""; ?>"
                                               target="_blank" class="doc-btn doc-btn-view" title="View">
                                                <i class="fa fa-eye"></i><span>View</span>
                                            </a>
                                            <a href="member_homework_details_list.php?type=send&homework_ids=<?php echo $home_work_id->homework_detailsId; ?>&member_id=<?php echo isset($member_id) ? $member_id : ""; ?>&file=homework_pdf"
                                               target="_blank" class="doc-btn doc-btn-download" title="Download PDF">
                                                <i class="fa fa-download"></i><span>Download</span>
                                            </a>
                                        </div>
                                    </div>
                                    <?php
                                }
                            } $hw_det1++;
                        }
                    }
                    if (!$has_data) {
                        ?>
                        <!-- Empty State -->
                        <div class="link-card empty-state">
                            <div class="link-card-content">
                                <h3 class="link-card-title">No documents available</h3>
                                <p class="link-card-desc">No homework assignments found right now.</p>
                            </div>
                        </div>
                    <?php } ?>

                </div>

            </div>
        </main>
    </div>

    <!-- Mobile Bottom Navigation -->
    <div id="mobile-nav-placeholder"></div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <script src="./js/layout.js"></script>

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