<?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';
include '../coach/objects/baseline_bodycomposition.class.php';
$page_action = $_POST['action'] ?? '';

$memberid = $_SESSION['memberid'];
?>
<?php
include('member-header.php');

$category = array(
    "Breakfast" => "Breakfast",
    "Lunch" => "Lunch",
    "Dinner" => "Dinner",
    "Snack" => "Snack",
    "Extras" => "Extras"
);
$time = array(
    "12:00 AM" => "12:00 AM",
    "12:30 AM" => "12:30 AM",
    "01:00 AM" => "01:00 AM",
    "01:30 AM" => "01:30 AM",
    "02:00 AM" => "02:00 AM",
    "02:30 AM" => "02:30 AM",
    "03:00 AM" => "03:00 AM",
    "03:30 AM" => "03:30 AM",
    "04:00 AM" => "04:00 AM",
    "04:30 AM" => "04:30 AM",
    "05:00 AM" => "05:00 AM",
    "05:30 AM" => "05:30 AM",
    "06:00 AM" => "06:00 AM",
    "06:30 AM" => "06:30 AM",
    "07:00 AM" => "07:00 AM",
    "07:30 AM" => "07:30 AM",
    "08:00 AM" => "08:00 AM",
    "08:30 AM" => "08:30 AM",
    "09:00 AM" => "09:00 AM",
    "09:30 AM" => "09:30 AM",
    "10:00 AM" => "10:00 AM",
    "10:30 AM" => "10:30 AM",
    "11:00 AM" => "11:00 AM",
    "11:30 AM" => "11:30 AM",
    "12:00 PM" => "12:00 PM",
    "12:30 PM" => "12:30 PM",
    "01:00 PM" => "01:00 PM",
    "01:30 PM" => "01:30 PM",
    "02:00 PM" => "02:00 PM",
    "02:30 PM" => "02:30 PM",
    "03:00 PM" => "03:00 PM",
    "03:30 PM" => "03:30 PM",
    "04:00 PM" => "04:00 PM",
    "04:30 PM" => "04:30 PM",
    "05:00 PM" => "05:00 PM",
    "05:30 PM" => "05:30 PM",
    "06:00 PM" => "06:00 PM",
    "06:30 PM" => "06:30 PM",
    "07:00 PM" => "07:00 PM",
    "07:30 PM" => "07:30 PM",
    "08:00 PM" => "08:00 PM",
    "08:30 PM" => "08:30 PM",
    "09:00 PM" => "09:00 PM",
    "09:30 PM" => "09:30 PM",
    "10:00 PM" => "10:00 PM",
    "10:30 PM" => "10:30 PM",
    "11:00 PM" => "11:00 PM",
    "11:30 PM" => "11:30 PM"
);
?>
<style>
    .error { color: red; }
    .success { background-color: #ccffcc; }

    #daily_log_id { width: 100%; margin: 5px 0px 32px 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;
    }


    #files {width: 95%}
    #files1 {width: 95%}

    @media (max-width: 768px) {
        #daily_log_id { width: 90%; margin: 5px 0px 52px 0px; }
        #files { width: 90% }
        #files1 { width: 90% }
    }    

    @media (max-width: 593px) {
        #time {
            margin-top: 10px;
        }
    } 

    @media (max-width: 384px) {
        .day {
            margin-top: 5px !important
        }
    } 

    @media (max-width: 275px) {
        .month {
            margin-top: 5px !important
        }
    } 

    .text_area {
        width: 99.5%;
        padding: 10px;
        margin-top: 8px;
        border: 1px solid #ccc;
        padding-left: 5px;
        font-size: 16px;
        font-family: raleway
    }
    .btn-success {
        color: #fff;
        background-color: #5cb85c;
        border-color: #5cb85c;
    }
    input[type=submit] {
        width: 100%;
    }

    .entry-content tr td, body.et-pb-preview #main-content .container tr td {
        padding: 6px 6px;
        border-top: 1px solid #eee;
    }

    #addNew table tr td {
        border: none
    }
    .bodyCompo table tr td {
        border: none
    }
</style>

<?php include('menu.php'); ?>
<?php
$message = '';
$body_composition = new Baseline_bodycomposition();

if (!empty($_POST['bm'])) {
    $new = 0;
    $save_ok = true;
    foreach ($_POST['bm'] as $id => $data) {
        if ($data['date']) {
            if ($id > 0) {
                $body_composition->Get($id);
                $body_composition->updated_on = date('Y-m-d H:i:s');
            } if ($_POST['add_new'] == 1) {
                $new = 1;
                $body_composition->member_id = $memberid;
                $body_composition->created_on = date('Y-m-d H:i:s');
                $body_composition->updated_on = date('Y-m-d H:i:s');
            }
            $body_composition->date = date('Y-m-d', strtotime($data['date']));
            $body_composition->weight = $data['weight'];
            $body_composition->body_fat = $data['body_fat'];
            if ($body_composition->Save() === false) {
                $save_ok = false;
            }
        }
    }
    $message = $save_ok ? success_text() : 'Unable to save. Please try again.';
}

$param = array(array("member_id = " . $memberid . ""));
$body_comp_list = $body_composition->GetList($param, 'baseline_bodycompositionid', false, 3);
?>

<?php if (in_array("body_composition", $remote_client) && $remoteClient != 1) { ?>
    <script>
        window.location.href = "member_homepage.php";
    </script>
<?php } ?>
<div id="daily_log_id">
    <b><br>
        <p style="text-align: center;font-weight: bold;"><div class="success" ><?php
            if ($message) {
                echo $message;
            } else {
                ?>Body Composition<?php } ?></div></p>
    </b><div></br></div>
    <p style="font-weight: bold;text-align: center;">
        <input type="button" id="add_new" class="btn btn-primary" value="Add" style="cursor: pointer">
        <input type="button" id="add_new" class="btn btn-success click_submit" value="Submit" style="cursor: pointer">
    </p>
    <form method="POST" name="baseline" action="<?= htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') ?>">
        <div class="row" style="margin: 0px">
            <input type="hidden" class="add_new" name="add_new" value="<?php echo count($body_comp_list) == 0 ? 1 : 0 ?>">
            <div class="col-xs-12" <?php if (empty($body_comp_list)) { ?> style="display: block;"<?php } else { ?> style="display:none" <?php } ?> id="addNew">
                <table style="border: none">
                    <tr>
                        <td>DATE</td>
                        <td>
                            <div class="container1">
                                <input type="text" name="bm[0][date]" id="date0" maxlength="50" class="text date"  data-field="date" readonly style="display: none;"/><br/>
                            </div>
                            <p id="date_error0" style="color: red; margin: 0px;"></p>
                        </td>
                    </tr>
                    <tr>
                        <td>WEIGHT</td>
                        <td>
                            <input type="text" id="weight0" placeholder="0.00" class="text" name="bm[0][weight]" value="">
                            <p id="weight_error0" style="color: red; margin: 0px;"></p>
                        </td>
                    </tr>
                    <tr>
                        <td>% BODY FAT</td>
                        <td>
                            <input type="text" id="body_fat0" placeholder="0.00" class="text" name="bm[0][body_fat]" value="">
                            <p id="body_fat_error0" style="color: red; margin: 0px;"></p>
                        </td>
                    </tr>
                </table>
            </div>
            <?php
            $key = 1;
            foreach ($body_comp_list as $sn => $val) {
                ?>
                <div class="col-xs-12 bodyCompo" id="compID<?php echo $key; ?>">
                    <table style="border: none">
                        <tr>
                            <?php if ($key == 1) { ?><td class="comp_field">DATE</td><?php } ?>
                            <td>
                                <div class="container1">
                                    <input type="text" name="bm[<?php echo $val->baseline_bodycompositionId; ?>][date]" id="date<?php echo $key; ?>" value="<?php echo $val->date != '0000-00-00' ? date('Y-m-d', strtotime($val->date)) : ""; ?>" maxlength="50" class="text date"  data-field="date" readonly style="display: none;"/><br/>
                                </div>
                                <p id="date_error<?php echo $key; ?>" style="color: red; margin: 0px;"></p>
                            </td>
                        </tr>

                        <tr>
                            <?php if ($key == 1) { ?><td class="comp_field">WEIGHT</td><?php } ?>
                            <td>
                                <input type="text" id="weight<?php echo $key; ?>" placeholder="0.00" class="text" name="bm[<?php echo $val->baseline_bodycompositionId; ?>][weight]" value="<?php echo $val->weight; ?>">
                                <p id="weight_error<?php echo $key; ?>" style="color: red; margin: 0px;"></p>
                            </td>
                        </tr>
                        <tr>
                            <?php if ($key == 1) { ?><td class="comp_field">% BODY FAT</td><?php } ?>
                            <td>
                                <input type="text" id="body_fat<?php echo $key; ?>" placeholder="0.00" class="text" name="bm[<?php echo $val->baseline_bodycompositionId; ?>][body_fat]" value="<?php echo $val->body_fat; ?>">
                                <p id="body_fat_error<?php echo $key; ?>" style="color: red; margin: 0px;"></p>
                            </td>
                        </tr>

                    </table>
                </div>
                <?php
                $key++;
            }
            ?>
        </div>
        <input type="submit" style="display: none" name="submit" id="submit" value="Submit" class="submit"/>
    </form>
</div>
<script src="../coach/js/jquery.js"></script>
<script>
        $(function () {
            $('.date').dropdownDatepicker({
                defaultDateFormat: 'yyyy-mm-dd',
                displayFormat: 'mdy',
                submitFormat: 'yyyy-mm-dd'
            });
        });

        var body_count = '<?php echo count($body_comp_list); ?>';

        $("#add_new").click(function () {
            $('#addNew').show();
            if (body_count > 2) {
                $('#compID' + body_count).hide();
            }
            $('.comp_field').hide();
            $('.add_new').val(1);
        });

        $(".click_submit").on('click', function (event) {
            $("#submit").click();
        })
</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>
<style>
    .container1 select {
        padding: 10px;
        background: #ffffff;
        border: 1px solid #CCCCCC;
        border-radius: 3px;
        margin: 0 3px;
    }
</style>
<?php
include('member-footer.php');
?>
