<?php
session_start(['gc_maxlifetime' => 14400, 'cookie_httponly' => true]); // Starting Session
include('member_session.php');
include('../coach/functions.php');

include("../coach/objects/class.member.php");
include '../coach/objects/food_journal.class.php';
include '../coach/objects/foodjournal_files.class.php';

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

$food_journal = new Food_journal();


if ($type == 'delete') {
    $food_id = (int)$_GET['food_journalId'];
    $food_journal->Get($food_id); //for delete function 
    if ($food_journal->Delete(false)) { //for delete function
        $message = "<p style='text-align:center;color:green;font-weight:Bold;'>Food Journal Deleted Successfully</p>";
    }
}
if (isset($_GET['history']) && $_GET['history'] == 1) {
    if (isset($_POST['from_date']) && $_POST['from_date'] && isset($_POST['to_date']) && $_POST['to_date']) {
        $fcv_array[] = ["date", ">=", date('Y-m-d 00:00:00', strtotime($_POST['from_date']))];
        $fcv_array[] = ["date", "<=", date('Y-m-d 23:59:59', strtotime($_POST['to_date']))];
    }
} else {
    $fcv_array[] = ["date", ">=", date('Y-m-d 00:00:00')];
    $fcv_array[] = ["date", "<=", date('Y-m-d 23:59:59')];
}

$fcv_array[] = ["member_id", "=", $_SESSION['memberid']];
$food_list_id = $food_journal->GetList($fcv_array, 'food_journalId', false);

$foodJournalFiles = new Foodjournal_files();
$sno = 1;
include('member-header.php');
?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.app-layout {
width: 100%;
}
.action-card:hover {
text-decoration: none;
}

.photo-food-empty-state-title {
padding: 0;
}
.page-header-title, .page-header-subtitle {
padding: 0;
}
    /*.success { background-color: #ccffcc; }*/

    /*#daily_log_id { width: 100%; 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;*/
    /*}*/

    /*@media (max-width: 568px) {*/
    /*    #notesLabel{*/
    /*        display: none*/
    /*    }*/
    /*}*/
    /*@media (max-width: 768px) {*/
    /*    #daily_log_id { width: 90%; margin: 5px 0px 52px 0px; }*/
    /*    ::-webkit-scrollbar {*/
    /*        -webkit-appearance: none;*/
    /*    }*/

    /*    ::-webkit-scrollbar:vertical {*/
    /*        width: 10px;*/
    /*    }*/

    /*    ::-webkit-scrollbar:horizontal {*/
    /*        height: 10px;*/
    /*    }*/

    /*    ::-webkit-scrollbar-thumb {*/
    /*        background-color: #ccc;*/
    /*        border-radius: 10px;*/
    /*        border: 2px solid #eee;*/
    /*    }*/

    /*    ::-webkit-scrollbar-track { */
    /*        background-color: #eee; */
    /*    }*/
    /*}      */
    /*#main-header{*/
    /*    z-index: 9999*/
    /*}*/
    /*table tbody tr td span {*/
    /*    color: #000*/
    /*}*/

    /*#table{*/
    /*    margin: 0 0 15px -18px;*/
    /*}*/
    /*.container1 select {*/
    /*    padding: 10px;*/
    /*    background: #ffffff;*/
    /*    border: 1px solid #CCCCCC;*/
    /*    border-radius: 3px;*/
    /*    margin: 0 3px;*/
    /*}*/
    /*input[type=submit] {*/
    /*    width: 100%;*/
    /*    background-color: #FFBC00;*/
    /*    color: #fff;*/
    /*    border: 2px solid #FFCB00;*/
    /*    padding: 7px;*/
    /*    font-size: 18px;*/
    /*    cursor: pointer;*/
    /*    border-radius: 5px;*/
    /*    margin-bottom: 15px;*/
    /*}*/
    /*.submit {*/
    /*    width: 100%;*/
    /*    background-color: gray;*/
    /*    color: #fff;*/
    /*    border: 2px solid gray;*/
    /*    padding: 7px;*/
    /*    font-size: 18px;*/
    /*    cursor: pointer;*/
    /*    border-radius: 5px;*/
    /*    margin-bottom: 15px;*/
    /*}*/
    .action-card-text {
color: var(--textPrimary, #000000)!important;
}
</style>

<script src="../coach/js/jquery-1.11.0.min.js"></script>
<script>
    function showHide(id) {
        var isVisible = $("#notes_" + id).is(":visible");
        $('.notes').hide();
        $("#notes_" + id + ' td').attr("data-th", "Notes");
        if (isVisible) {
            $("#notes_" + id).fadeOut();
        } else {
            $("#notes_" + id).fadeIn();
        }
    }
    $(document).ready(function () {
        $("a[rel=example_group]").fancybox({
            'transitionIn': 'none',
            'transitionOut': 'none',
            'titlePosition': 'over',
            'titleFormat': function (title, currentArray, currentIndex, currentOpts) {
                return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
            }
        });
    });
    function noImage() {
        alert("Image not found")
    }
    function deleteFn(id) {
        if (confirm("Are you sure want to delete this entry?")) {
            window.location.href = "foodjournal_list.php?type=delete&food_journalId=" + id;
        }
    }
    function reset_list() {
        window.location.href = "foodjournal_list.php?history=1";
    }
</script>


<link rel="stylesheet" href="../coach/css/lightbox.min.css">
<script src="../coach/js/jquery.datetimepicker.full.js"></script>

    <!-- 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 -->
        <div id="sidebar-placeholder"><?php include('menu.php'); ?></div>

<?php if ($foodJournal == 0) { ?>
    <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">Photo Food Journal</h1>
                        <p class="page-header-subtitle">Track your meals with photo entries</p>
                    </div>
                </div>
            </header>

            <div class="body-content">

                <!-- Body Content -->
                <div class="photo-food-page-body">

                    <!-- Action Cards Row -->
                    <div class="action-cards-row">
                        <a href="food_journal.php" class="action-card">
                            <svg width="32" height="32" viewBox="0 0 32 32" fill="none"
                                xmlns="http://www.w3.org/2000/svg">
                                <path
                                    d="M18.6627 5.33337C19.1437 5.33337 19.6158 5.46349 20.0289 5.70996C20.4421 5.95642 20.7808 6.31005 21.0093 6.73337L21.6573 7.93337C21.8858 8.3567 22.2246 8.71033 22.6377 8.95679C23.0508 9.20326 23.5229 9.33338 24.004 9.33337H26.6667C27.3739 9.33337 28.0522 9.61433 28.5523 10.1144C29.0524 10.6145 29.3333 11.2928 29.3333 12V24C29.3333 24.7073 29.0524 25.3856 28.5523 25.8857C28.0522 26.3858 27.3739 26.6667 26.6667 26.6667H5.33332C4.62608 26.6667 3.9478 26.3858 3.4477 25.8857C2.94761 25.3856 2.66666 24.7073 2.66666 24V12C2.66666 11.2928 2.94761 10.6145 3.4477 10.1144C3.9478 9.61433 4.62608 9.33337 5.33332 9.33337H7.99599C8.47655 9.3334 8.94819 9.20356 9.36103 8.9576C9.77387 8.71163 10.1126 8.35867 10.3413 7.93604L10.9933 6.73071C11.2221 6.30808 11.5608 5.95512 11.9736 5.70915C12.3865 5.46318 12.8581 5.33335 13.3387 5.33337H18.6627Z"
                                    stroke="black" stroke-width="1.33333" stroke-linecap="round"
                                    stroke-linejoin="round" />
                                <path
                                    d="M16 21.3334C18.2091 21.3334 20 19.5425 20 17.3334C20 15.1242 18.2091 13.3334 16 13.3334C13.7909 13.3334 12 15.1242 12 17.3334C12 19.5425 13.7909 21.3334 16 21.3334Z"
                                    stroke="black" stroke-width="1.33333" stroke-linecap="round"
                                    stroke-linejoin="round" />
                            </svg>

                            <span class="action-card-text">Add Food Entry</span>
                            <svg class="action-card-arrow" width="24" height="24" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg">
                                <path d="M9 18L15 12L9 6" stroke="#6B7280" stroke-width="1.33333" stroke-linecap="round"
                                    stroke-linejoin="round" />
                            </svg>

                        </a>
                        <?php if (isset($_GET['history']) && $_GET['history'] == 1) { ?>
                            <a href="foodjournal_list.php" class="action-card">
                        <?php } else { ?>
                            <a href="foodjournal_list.php?history=1" class="action-card">
                        <?php } ?>
                            <svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M4 16C4 18.3734 4.70379 20.6935 6.02236 22.6668C7.34094 24.6402 9.21509 26.1783 11.4078 27.0866C13.6005 27.9948 16.0133 28.2324 18.3411 27.7694C20.6689 27.3064 22.8071 26.1635 24.4853 24.4853C26.1635 22.8071 27.3064 20.6689 27.7694 18.3411C28.2324 16.0133 27.9948 13.6005 27.0866 11.4078C26.1783 9.21509 24.6402 7.34094 22.6668 6.02236C20.6935 4.70379 18.3734 4 16 4C12.6453 4.01262 9.42529 5.32163 7.01333 7.65333L4 10.6667" stroke="black" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
                                <path d="M4 4V10.6667H10.6667" stroke="black" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
                                <path d="M16 9.33337V16L21.3333 18.6667" stroke="black" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
                            </svg>

                            <span class="action-card-text">
                                <?php if (isset($_GET['history']) && $_GET['history'] == 1) { ?>
                                    Hide Journal History
                                <?php } else { ?>
                                    Journal History
                                <?php } ?>
                            </span>
                            <svg class="action-card-arrow" width="24" height="24" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg">
                                <path d="M9 18L15 12L9 6" stroke="#6B7280" stroke-width="1.33333" stroke-linecap="round"
                                    stroke-linejoin="round" />
                            </svg>
                        </a>
                    </div>

                    <?php if (isset($_GET['history']) && $_GET['history'] == 1) { ?>
                        <div>  
                            <form method="POST" action="foodjournal_list.php?history=1">
                                <div class="row">
                                    <div class="form-group row" style="margin-top: 5px;">
                                        <div class="col-md-3"><label for="start_date">Start Date:</label></div>
                                        <div class="col-md-9">
                                            <div class="container1">
                                                <input type="text" id="from_date" name="from_date" ata-field="date" autocomplete="off" class="form-control" value="<?php echo isset($_POST['from_date']) && $_POST['from_date'] ? date('Y-m-d', strtotime($_POST['from_date'])) : date('Y-m-d'); ?>" readonly class="text" style="display: none;">
                                            </div>
                                        </div>
                                        <div id="dtBox"></div>
                                        <div class="col-md-1"></div>
                                    </div>
                                    <div class="form-group row" style="margin-top: 5px;">
                                        <div class="col-md-3"><label for="end_date"> End Date:</label></div>
                                        <div class="col-md-9"> 
                                            <div class="container1">
                                                <input type="text" id="to_date" name="to_date" ata-field="date" autocomplete="off" class="form-control" value="<?php echo isset($_POST['to_date']) && $_POST['to_date'] ? date('Y-m-d', strtotime($_POST['to_date'])) : date('Y-m-d'); ?>" readonly class="text" style="display: none;">
                                            </div>
                                        </div>
                                        <div class="col-md-1"></div>
                                    </div>
                                    <div class="form-group row" style="margin-top: 2px;">
                                        <div class="col-xs-6 col-sm-6 col-md-6"><input type="submit" name="submit"  value="Submit" class="submit"/></div>
                                        <div class="col-xs-6 col-sm-6 col-md-6"><input type="button" name="submit"  value="Reset" class="submit" onclick="reset_list()"/></div>
                                        <div class="col-md-1"></div>
                                    </div>
                                </div>
                            </form>    
                        </div>
                    <?php } ?>

                    <div class="text-upper"><?php print (isset($message) ? $message : ""); ?></div>

                    <?php if (count($food_list_id) == 0) { ?>
                        <!-- Empty State -->
                        <div class="photo-food-empty-state-card">
                            <div class="photo-food-empty-state-icon">
                                <svg width="65" height="65" viewBox="0 0 65 65" fill="none"
                                    xmlns="http://www.w3.org/2000/svg">
                                    <path
                                        d="M51.4583 8.125H13.5417C10.5501 8.125 8.125 10.5501 8.125 13.5417V51.4583C8.125 54.4499 10.5501 56.875 13.5417 56.875H51.4583C54.4499 56.875 56.875 54.4499 56.875 51.4583V13.5417C56.875 10.5501 54.4499 8.125 51.4583 8.125Z"
                                        stroke="black" stroke-width="1.91185" stroke-linecap="round"
                                        stroke-linejoin="round" />
                                    <path
                                        d="M24.375 29.7917C27.3665 29.7917 29.7916 27.3666 29.7916 24.375C29.7916 21.3835 27.3665 18.9584 24.375 18.9584C21.3834 18.9584 18.9583 21.3835 18.9583 24.375C18.9583 27.3666 21.3834 29.7917 24.375 29.7917Z"
                                        stroke="black" stroke-width="1.91185" stroke-linecap="round"
                                        stroke-linejoin="round" />
                                    <path
                                        d="M56.875 40.625L48.5171 32.2671C47.5013 31.2516 46.1238 30.6812 44.6875 30.6812C43.2512 30.6812 41.8737 31.2516 40.8579 32.2671L16.25 56.875"
                                        stroke="black" stroke-width="1.91185" stroke-linecap="round"
                                        stroke-linejoin="round" />
                                </svg>

                            </div>
                            <h3 class="photo-food-empty-state-title">No Food Journal Entries Yet</h3>
                            <p class="photo-food-empty-state-subtitle">Start By Adding Your First Food Photo</p>
                        </div>
                    <?php } else { ?>

                        <div id="daily_log_id">
                            <div id="profile"></br></div>

                            <table id="table">
                                <thead>
                                <th>Meal Title</th>
                                <th>Date</th>
                                <th>Action</th>
                                <th style="display: none"></th>
                                </thead>
                                <tbody>
                                    <?php
                                    $i = 0;
                                    foreach ($food_list_id as $food_list) {
                                        $images = [];
                                        $params = array(array("foodjournal_id = " . $food_list->food_journalId . ""));
                                        $images = $foodJournalFiles->GetList($params, 'foodjournal_filesid', false);
                                        ?>
                                        <tr>
                                            <td><?php
                                                echo htmlspecialchars($food_list->category, ENT_QUOTES, 'UTF-8');
                                                $sno++
                                                ?></td>
                                            <td><?php echo (isset($food_list->date) && $food_list->date != '0000-00-00 00:00:00') ? date('m/d/Y h:i A', strtotime($food_list->date)) : ""; ?></td>
                                            <td>
                                                <i title="View/Hide Notes" class="fa fa-sticky-note-o" aria-hidden="true" onclick="showHide(<?= $i ?>)" style="font-size: 20px; cursor: pointer"></i>
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a <?php if ($images) { ?> class="example-image-link"  href="./images/foods/<?= $images[0]->file_name ?>"  data-lightbox="example-set<?= $i ?>" <?php } else { ?> onclick="noImage()" <?php } ?>  style="cursor: pointer; text-decoration: none" title="Images for <?= htmlspecialchars($food_list->category, ENT_QUOTES, 'UTF-8') ?>">
                                                    <i class="fa fa-image" style="font-size:20px;"></i>
                                                </a>
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="food_journal_update.php?food_journalId=<?= $food_list->food_journalId; ?>" title="Edit"><i class="fa fa-edit" style="font-size:20px"></i></a>
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a  onclick="deleteFn(<?= $food_list->food_journalId; ?>)" style="cursor: pointer" title="Delete"><i class="fa fa-remove" style="font-size:20px; color: red"></i></a>
                                            </td>
                                        </tr>
                                        <tr class="notes" style="display: none"  id="notes_<?= $i ?>">
                                            <td colspan="6" data-th="Notes"><p><b id="notesLabel">Notes:</b> <?php echo nl2br(htmlspecialchars($food_list->notes, ENT_QUOTES, 'UTF-8')); ?></p></td>
                                        </tr>
                                        <tr style="display: none">
                                            <?php
                                            if ($images) {
                                                $j = 0;
                                                foreach ($images as $image) {
                                                    if ($j > 0) {
                                                        ?>
                                                        <td><a class="example-image-link" style="text-decoration: none" href="./images/foods/<?= $image->file_name ?>" data-lightbox="example-set<?= $i ?>"><img class="example-image" src="./images/foods/<?= $image->file_name ?>" /></a></td>
                                                                <?php
                                                            }
                                                            $j++;
                                                        }
                                                    }
                                                    ?>
                                        </tr>
                                        <?php
                                        $i++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </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>

<script src="../coach/js/lightbox-plus-jquery.min.js"></script>

<script>
    $(function () {
        lightbox.option({
            'resizeDuration': 400,
            'maxHeight': 500,
            'maxWidth': 500,
            'positionFromTop': 150,
            fitImagesInViewport: true,
            disableScrolling: true
        })
        $('#from_date').dropdownDatepicker({
            defaultDateFormat: 'yyyy-mm-dd',
            displayFormat: 'mdy',
            submitFormat: 'yyyy-mm-dd'
        });
        $('#to_date').dropdownDatepicker({
            defaultDateFormat: 'yyyy-mm-dd',
            displayFormat: 'mdy',
            submitFormat: 'yyyy-mm-dd'
        });
    });

    $(document).ready(function () {
        $('#table').basictable();
    });
</script>
<!-- jQuery First -->
<script src="../coach/js/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<!-- Or from a CDN -->
<script src="../coach/js/jquery-dropdown-datepicker.min.js"></script>

<link rel="stylesheet" type="text/css" href="../coach/css/style.css" />
<link rel="stylesheet" type="text/css" href="../coach/css/basictable.css" />
<script type="text/javascript" src="../coach/js/jquery.basictable.min.js"></script>

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