<?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.coaching_call.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

$coach_temp_id = $member->GetCoaching_callList();

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

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

    if (!empty($coaching_callid)) {

        $pdf_flag = 1;

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

        $coaching_call_class = new Coaching_call();
        $coaching_call_list = $coaching_call_class->GetList([
            ["coaching_callId IN ($coaching_callid)"]
        ]);
    }

    if ($pdf_flag) {

        class MYPDF extends TCPDF {}

        if (!empty($coaching_call_list)) {
            foreach ($coaching_call_list as $coaching_list) {
                include '../coach/includes/pdf_files/coaching_call_pdf.php';
            }
        }
    }
}

$fcv_array[] = ["is_show", "=", 1];
$coachingcall_list_id = $member->GetCoaching_callList($fcv_array);

$sno = 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;
}
/*.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("coaching_call_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">Coaching Call Notes</h1>
                        <p class="page-header-subtitle">View coaching call notes and key takeaways</p>
                    </div>
                </div>
            </header>

            <div class="body-content">

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

                    <?php if (!empty($coachingcall_list_id)) { ?>
                        <?php foreach ($coachingcall_list_id as $coaching_call_id) { ?>
                            <!-- Coaching Call 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">Coaching Call <?php echo htmlspecialchars($coaching_call_id->filename); ?></h3>
                                    <p class="link-card-desc">View your coaching call notes</p>
                                </div>
                                <div class="link-card-doc-actions">
                                    <a href="member_coaching_call_list.php?type=print&coaching_callid=<?php echo (int)$coaching_call_id->coaching_callId; ?>&member_id=<?php echo htmlspecialchars($member_id); ?>&file=coaching_call_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_coaching_call_list.php?type=print&coaching_callid=<?php echo (int)$coaching_call_id->coaching_callId; ?>&member_id=<?php echo htmlspecialchars($member_id); ?>&file=coaching_call_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 coaching calls 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');
?>