<?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']) : []);
    $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();
        //$mail->IsHTML(true);   

        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 baseline details
    if (count($baseline_ids) != 0) {
        $pdf_flag = 1;
        $baseline_list_arr = $member_list->GetBaseline_detailsList(array(array("baseline_detailsId IN (" . implode(",", $baseline_ids) . ") AND")));
        $baseline_details = new Baseline_details();
        $baseline_details->baseline_detailsId = $baseline_list_arr[0]->baseline_detailsId;
        $msmt_details_arr = $baseline_details->GetBaseline_measurementList();
        $screen_details_arr = $baseline_details->GetBaseline_screenList();
        $bdycmp_details_arr = $baseline_details->GetBaseline_bodycompositionList();
        $baseline_list = $baseline_list_arr[0];
        $msmt_list1 = $msmt_details_arr[0];
        $msmt_list2 = $msmt_details_arr[1];
        $msmt_list3 = $msmt_details_arr[2];
        $screen_list1 = $screen_details_arr[0];
        $screen_list2 = $screen_details_arr[1];
        $screen_list3 = $screen_details_arr[2];
        $bdycmp_list1 = $bdycmp_details_arr[0];
        $bdycmp_list2 = $bdycmp_details_arr[1];
        $bdycmp_list3 = $bdycmp_details_arr[2];
        $msmt_list1->msmt_date = ($msmt_list1->msmt_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($msmt_list1->msmt_date));
        $msmt_list2->msmt_date = ($msmt_list2->msmt_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($msmt_list2->msmt_date));
        $msmt_list3->msmt_date = ($msmt_list3->msmt_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($msmt_list3->msmt_date));
        $screen_list1->scn_date = ($screen_list1->scn_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($screen_list1->scn_date));
        $screen_list2->scn_date = ($screen_list2->scn_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($screen_list2->scn_date));
        $screen_list3->scn_date = ($screen_list3->scn_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($screen_list3->scn_date));
        $bdycmp_list1->cmp_date = ($bdycmp_list1->cmp_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($bdycmp_list1->cmp_date));
        $bdycmp_list2->cmp_date = ($bdycmp_list2->cmp_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($bdycmp_list2->cmp_date));
        $bdycmp_list3->cmp_date = ($bdycmp_list3->cmp_date == "0000-00-00 00:00:00") ? "" : date("d-m-y", strtotime($bdycmp_list3->cmp_date));
    }

    $trainingtemp_details = [];
    //fetching training details
    if (count($training_ids) != 0) {
        $pdf_flag = 1;
        $trainingtemp_class = new Training_template_details();
        $trainingtemp_details = $trainingtemp_class->GetList(array(array("training_template_detailsid IN (" . implode(",", $training_ids) . ")")));
    }
    //fetching homework details
    if (count($homework_ids) != 0) {
        $pdf_flag = 1;
        $hw_ar = 0;
        while ($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) . ")")));

        foreach ($homework_list as $hw_value) {
            $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[] = $corrective_details_arr;
            $strength_details[] = $strength_details_arr;
            $flexibility_details[] = $flexibility_details_arr;
        }
    }
    if (count($schedule_ids) != 0) {
        $pdf_flag = 1;
        $sc_ar = 0;
        while (isset($schedule_ids[$sc_ar]) && $schedule_ids[$sc_ar] != '') {
            $val = $schedule_ids[$sc_ar];
            for ($i = intval($val); $i < ($val + 3); $i++) {
                $schedule_ids_ar[] = $i;
            }
            $sc_ar++;
        }
        $weekly_schedule_class = new Weekly_schedule_details();
        $weeklyschedule_list = $weekly_schedule_class->GetList(array(array("weekly_schedule_detailsId IN (" . implode(",", $schedule_ids_ar) . ")")));
    }
    if (count($cardio_ids) != 0) {
        $pdf_flag = 1;
        $cardio_class = new Cardio_phase();
        $cardio_phase_list = $cardio_class->GetList(array(array("cardio_phaseId IN (" . implode(",", $cardio_ids) . ")")));
    }

    if ($pdf_flag != 0) {

        //creating pdf files        
        class MYPDF extends TCPDF {
            
        }

        if (count($baseline_ids) != 0) {
            //included code to generating pdf file
            include '../coach/includes/pdf_files/baseline_pdf.php';
        }
        //for training template details
        if (count($trainingtemp_details) != 0) {
            $count = 1;
            foreach ($trainingtemp_details as $training_detail) {
                //included code to generating pdf file
                include '../coach/includes/pdf_files/trainingtemplate_pdf.php';
                $count++;
            }
        }
        //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++;
            }
        }
        //weekly schedule pdf page data
        if (count($schedule_ids) != 0) {
            $i = 1;
            $j = 0;
            $count = 1;
            for ($wksc = 0; $wksc < count($schedule_ids); $wksc++) {
                include '../coach/includes/pdf_files/weekly_schedule_pdf.php';
                $j = $i;
                $count++;
            }
        }
        //for cardio pdf page data
        if (count($cardio_ids) > 0) {
            $count = 1;
            foreach ($cardio_phase_list as $cardio_list) {
                include '../coach/includes/pdf_files/cardio_pdf.php';
                $count++;
            }
        }
    }
    if (empty($file)) {
        if ($mail->Send()) {
            $message = "Mail Sent Succcessfully";
        }
    }
}

$weekly_schedule_id = $member->GetWeekly_schedule_detailsList();

$sno = 1;
$ws_det = 1;
$ws_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("weekly_schedule_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">Weekly Schedule</h1>
                        <p class="page-header-subtitle">View your weekly training schedule</p>
                    </div>
                </div>
            </header>

            <div class="body-content">

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

                    <?php
                    $has_data = false;
                    if (count($weekly_schedule_id) != 0) {
                        foreach ($weekly_schedule_id as $weeklyschedule_id) {
                            if ($ws_det1 == $ws_det) {
                                $ws_det = $ws_det + 3;
                                if ($weeklyschedule_id->is_show == 1) {
                                    $has_data = true;
                                    ?>
                                    <!-- Weekly Schedule 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">Weekly Schedule <?php echo htmlspecialchars($weeklyschedule_id->filename); ?></h3>
                                            <p class="link-card-desc">View your weekly schedule</p>
                                        </div>
                                        <div class="link-card-doc-actions">
                                            <a href="member_weekly_schedule_list.php?type=send&schedule_ids=<?php echo $weeklyschedule_id->weekly_schedule_detailsId; ?>&member_id=<?php echo isset($member_id) ? $member_id : ""; ?>&file=weekly_schedule_pdf_print"
                                               target="_blank" class="doc-btn doc-btn-view" title="View PDF">
                                                <i class="fa fa-eye"></i><span>View</span>
                                            </a>
                                            <a href="member_weekly_schedule_list.php?type=send&schedule_ids=<?php echo $weeklyschedule_id->weekly_schedule_detailsId; ?>&member_id=<?php echo isset($member_id) ? $member_id : ""; ?>&file=weekly_schedule_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
                                    $sno++;
                                }
                            } $ws_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 weekly schedules 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');
?>