<?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/meal_plans.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'] : "");

$mealplans = new Meal_plans();

if ($type == 'print') {
    $mealplan = $mealplans->Get((int)$_GET['meal_plansId']);
    $doc_path = str_replace("file://", "", "../coach/includes/pdf_files/pdf/" . $mealplan->file);
    $view_exts = array(
        'image/jpg',
        'image/jpeg',
        'image/png',
        'application/pdf',
        'application/doc',
        'application/xls'
    );

    $file_disposition = ($file === 'meal_plans_download')
        ? 'attachment'
        : (in_array($mealplan->file_type, $view_exts) ? 'inline' : 'attachment');
    $file_name = preg_replace("/[^a-zA-Z0-9-_. ]+/", "", $mealplan->file);

    header('Cache-Control: public');
    header('Content-Type: ' . $mealplan->file_type);
    header('Content-Disposition: ' . $file_disposition . ';filename="' . $file_name . '"');
    readfile($doc_path);
}

$fcv_array[] = ["is_show", "=", 1];
$fcv_array[] = ["member_id", "=", $_SESSION['memberid']];

$mealplan_list_id = $mealplans->GetList($fcv_array, 'meal_plansId', false);
$sno = 1;
include('member-header.php');
?>
<style>
    .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 include('menu.php'); ?>
<?php if ($remoteClient == 1 && in_array("meal_plan_list", $remote_client)) { ?>
    <script>
        window.location.href = "member_homepage.php";
    </script>
<?php } ?>
<div id="daily_log_id">
    <div id="profile"></br></div>
    <div style="text-align: center;" align="">
        <p style="font-weight: bold;text-transform: uppercase;">Meal Plans</p>

        <table width='100%' cellspacing="5" cellpadding="5">
            <?php
            if (count($mealplan_list_id) != 0) {
                foreach ($mealplan_list_id as $meal_plan_id) {
                    ?>
                    <tr>
                        <td width="5%"><?php echo $sno; ?></td>
                        <td width="35%"> 
                            Meal Plans <?=
                            $meal_plan_id->title;
                            $sno++
                            ?>
                        </td>  

                        <td width="25%">
                            <?php if ($meal_plan_id->file) { ?>
                                <label><a target="_blank" href="meal_plan_list.php?type=print&meal_plansId=<?php echo $meal_plan_id->meal_plansId; ?>&member_id=<?php echo isset($member_id) ? $member_id : ""; ?>&file=meal_plans_print" title="Click to View"><strong>View</strong></a></label>
                                &nbsp;|&nbsp;
                                <label><a target="_blank" href="meal_plan_list.php?type=print&meal_plansId=<?php echo $meal_plan_id->meal_plansId; ?>&member_id=<?php echo isset($member_id) ? $member_id : ""; ?>&file=meal_plans_download" title="Click to Download"><strong>Download</strong></a></label>
                            <?php } ?>
                        </td>

                    </tr>
                    <?php
                }
            } else {
                ?>
                <tr>
                    <td colspan="3" width="100%" style="padding-top: 10px; color: red;">No documents available right now.</td>
                </tr>
            <?php } ?>
        </table>

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