<?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");
include '../coach/objects/class.email_notes.php';

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

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

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

$pdf_flag = 0; // ✅ fix warning

$email_temp_id = $member->GetEmail_notesList();

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

    $email_notesid = $_POST['email_notesid'] ?? $_GET['email_notesid'] ?? null;

    if (!empty($email_notesid)) {

        $pdf_flag = 1;

        // sanitize
        $email_notesid = (int)$email_notesid;

        $email_notes_class = new Email_notes();
        $email_notes_list = $email_notes_class->GetList([
            ["email_notesId IN ($email_notesid)"]
        ]);
    }

    if ($pdf_flag) {

        class MYPDF extends TCPDF {}

        if (!empty($email_notes_list)) {
            foreach ($email_notes_list as $email_list) {
                include '../coach/includes/pdf_files/email_notes_pdf.php';
            }
        }
    }
}

// filter
$fcv_array = [["is_show", "=", 1]];

$emailnotes_list_id = $member->GetEmail_notesList($fcv_array);

$sno = 1;

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

<style>
.app-layout {
width: 100%;
}
.link-card-title {
padding: 0;
}
.link-card:hover {
text-decoration: none;
}
.page-header-title, .page-header-subtitle {
padding: 0;
}
    /*.success { background-color: #ccffcc; }*/

    /*#daily_log_id { width: 75%; margin: 5px 0px 52px 0px; }*/

    /*.success {*/
    /*    background-color: #4592e0;*/
    /*    height: 25px;*/
    /*    text-align: center;*/
    /*    color: #ffffff;*/
    /*}*/

    /*#accountability_chart_form label.error { color: red; }*/

    /*#thd {*/
    /*    text-align: center;*/
    /*    font-weight: bold;*/
    /*}*/

    /*td {*/
    /*    padding: 10px 15px;*/
    /*}*/

    /*.text-center {*/
    /*    text-align: center;*/
    /*}*/

    /*.text-upper {*/
    /*    text-transform: uppercase;*/
    /*}*/

    /*.mandatory {*/
    /*    color: red;*/
    /*    font-size: 14px;*/
    /*}*/

    /*.btn {*/
    /*    background-color: #c0c0c0;*/
    /*}*/

    /*@media (max-width: 768px) {*/
    /*    #daily_log_id {*/
    /*        width: 90%;*/
    /*        margin: 5px 0px 52px 0px;*/
    /*    }*/
    /*}*/
</style>



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

    <!-- 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'); ?>

        <!-- Main Content -->
        <main class="main-content">
            <!-- Page Header -->
            <header class="page-header">
                <div class="page-header-meta">
                    <div>
                        <h1 class="page-header-title">Email Notes</h1>
                        <p class="page-header-subtitle">View key training program notes</p>
                    </div>
                </div>
            </header>

            <div class="body-content">

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

                    <?php if (!empty($emailnotes_list_id)) { ?>
                        <?php foreach ($emailnotes_list_id as $email_notes_id) { ?>
                            <!-- Email Note 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="M36.6667 11.6667L21.6817 21.2117C21.1732 21.5071 20.5956 21.6627 20.0075 21.6627C19.4195 21.6627 18.8419 21.5071 18.3334 21.2117L3.33337 11.6667"
                                            stroke="black" stroke-width="1.33333" stroke-linecap="round"
                                            stroke-linejoin="round" />
                                        <path
                                            d="M33.3334 6.66675H6.66671C4.82576 6.66675 3.33337 8.15913 3.33337 10.0001V30.0001C3.33337 31.841 4.82576 33.3334 6.66671 33.3334H33.3334C35.1743 33.3334 36.6667 31.841 36.6667 30.0001V10.0001C36.6667 8.15913 35.1743 6.66675 33.3334 6.66675Z"
                                            stroke="black" stroke-width="1.33333" stroke-linecap="round"
                                            stroke-linejoin="round" />
                                    </svg>
                                </div>
                                <div class="link-card-content">
                                    <h3 class="link-card-title">Email Notes <?php echo htmlspecialchars($email_notes_id->filename); ?></h3>
                                    <p class="link-card-desc">View your email notes</p>
                                </div>
                                <div class="link-card-doc-actions">
                                    <a href="member_email_notes_list.php?type=print&email_notesid=<?php echo (int)$email_notes_id->email_notesId; ?>&member_id=<?php echo htmlspecialchars($member_id); ?>&file=email_notes_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_email_notes_list.php?type=print&email_notesid=<?php echo (int)$email_notes_id->email_notesId; ?>&member_id=<?php echo htmlspecialchars($member_id); ?>&file=email_notes_download"
                                       target="_blank" class="doc-btn doc-btn-download" title="Download PDF">
                                        <i class="fa fa-download"></i><span>Download</span>
                                    </a>
                                </div>
                            </div>
                        <?php } ?>
                    <?php } else { ?>
                        <!-- 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 email notes 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');
?>