<?php

include('/home1/pinnadb7/public_html/portal/configuration.php');

$get_log_date = ($_POST['get_log_date'] !="")?$_POST['get_log_date']:(($_GET['get_log_date']!='')?$_GET['get_log_date']:date('d-m-Y'));
$member_id = ($_POST['member_id'] !="")?$_POST['member_id']:(($_GET['member_id']!='')?$_GET['member_id']:"");
$type = ($_POST['type']!="")?$_POST['type']:($_GET['type'] != ''?$_GET['type']:($get_log_date != ''?'edit':''));
$accountability_chart_id = ($_POST['accountability_chart_id']!="")?$_POST['accountability_chart_id']:(isset($_GET['accountability_chart_id'])?$_GET['accountability_chart_id']:"");
// SECURITY FIX: validate form_action against allowlist to prevent open redirect/XSS
$_raw_form_action = $_POST['form_action'] ?? $_GET['form_action'] ?? '';
$_allowed_pages = ['member_homepage.php', 'accountability_chart.php', 'accountability_chart_old.php'];
$form_action = in_array(basename($_raw_form_action), $_allowed_pages, true)
    ? basename($_raw_form_action)
    : htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8');
?>
<?php
$message = '';
if($type == 'update')
{
    $accountability_chart_add = new Accountability_chart();
    $member = new Member();
    $accountability_chart_add->memberId=$member_id;
    $member_det = $accountability_chart_add->GetMember();
    $accountability_chart_add->SetMember($member_det);
    $accountability_chart_add->log_date =$_POST['log_date'];
    $accountability_chart_add->breakfast =$_POST['breakfast'];
    $accountability_chart_add->snack1 =$_POST['snack1'];
    $accountability_chart_add->lunch =$_POST['lunch'];
    $accountability_chart_add->snack2 =$_POST['snack2'];
    $accountability_chart_add->dinner =$_POST['dinner'];
    $accountability_chart_add->snack3 =$_POST['snack3'];
    $accountability_chart_add->extras =$_POST['extras'];
    $accountability_chart_add->activity_level =$_POST['activity_level'];
    $accountability_chart_add->cardio =$_POST['cardio'];
    $accountability_chart_add->resistance =$_POST['resistance'];
    $accountability_chart_add->homework =$_POST['homework'];
    $accountability_chart_add->meal_score_totals =$_POST['meal_score_totals'];
    $accountability_chart_add->activity_total_score =$_POST['activity_total_score'];
    $accountability_chart_add->updated_on =date('Y-m-d H:i:s');

    if($_POST['accountability_chart_id']!= '') {
        $accountability_chart_add->accountability_chartId=$_POST['accountability_chart_id'];
    }
    if($accountability_chart_add->Save()) {
        $message = "<p style='text-align:center;color:green;font-weight:Bold;' >Accountability chart Updated Successfully</p>";
        $type="edit";
        $accountability_chart_id=$accountability_chart_add->accountability_chartId;
        $get_log_date=$accountability_chart_add->log_date;
    } else {
        $message = "<p style='text-align:center;color:red;font-weight:Bold;' >Accountability chart Not Updated.Please Try Again..</p>";
        $type="edit";
        $accountability_chart_id=$_POST['accountability_chart_id'];
        $get_log_date=$_POST['log_date'];
    }
}
if($type == 'save')
{
    $member = new Member();
    $accountability_chart_add = new Accountability_chart();
    $accountability_chart_add->memberId=$_SESSION['memberid'];

    $member_det = $accountability_chart_add->GetMember();
    $accountability_chart_add->SetMember($member_det);
    $accountability_chart_add->member_id =$_POST['member_id'];
    $accountability_chart_add->log_date =$_POST['log_date'];
    $accountability_chart_add->breakfast =$_POST['breakfast'];
    $accountability_chart_add->snack1 =$_POST['snack1'];
    $accountability_chart_add->lunch =$_POST['lunch'];
    $accountability_chart_add->snack2 =$_POST['snack2'];
    $accountability_chart_add->dinner =$_POST['dinner'];
    $accountability_chart_add->snack3 =$_POST['snack3'];
    $accountability_chart_add->extras =$_POST['extras'];
    $accountability_chart_add->activity_level=$_POST['activity_level'];
    $accountability_chart_add->cardio =$_POST['cardio'];
    $accountability_chart_add->resistance =$_POST['resistance'];
    $accountability_chart_add->homework =$_POST['homework'];
    $accountability_chart_add->created_on = date('Y-m-d H:i:s');
    $accountability_chart_add->updated_on = date('Y-m-d H:i:s');
    $accountability_chart_add->meal_score_totals =$_POST['meal_score_totals'];
    $accountability_chart_add->activity_total_score =$_POST['activity_total_score'];

    if($accountability_chart_add->save()) {        
      $accountability_chart_id=$accountability_chart_add->accountability_chartId;
      $get_log_date=$accountability_chart_add->log_date;
      $message = "<p style='text-align:center;color:green;font-weight:Bold;'>Accountability chart Saved Successfully</p>";
      $type="edit";
    } else {
      $message = "<p style='text-align:center;color:red;font-weight:Bold;'>Accountability chart Not Saved.Please Try Again..</p>";
      $type="add";
    }
}
if($type == 'edit' || $type == 'change_log_date')
{
  if($type == 'change_log_date') {
    include('member_session.php');
    include('../coach/functions.php');
    include('../coach/objects/class.member.php');
  }
  if($get_log_date) { 
    $memberData = new Member();
    $memberData->memberId=$_SESSION['memberid'];   
    $accountability_chart_data = $memberData->GetAccountability_chartList(array(array('log_date','=',$get_log_date)));
    $accountability_chart_list = $accountability_chart_data[0];
    $accountability_chart_id = $accountability_chart_list->accountability_chartId;    
  }
  
  if(!$get_log_date) {
    $accountability_chart = new Accountability_chart();
    $accountability_chart_list = $accountability_chart->Get($accountability_chart_id);
  }  
  
  $get_log_date = ($accountability_chart_list->log_date == '')?$get_log_date:$accountability_chart_list->log_date;
  $breakfast = $accountability_chart_list->breakfast;
  $snack1 =$accountability_chart_list->snack1;
  $lunch =$accountability_chart_list->lunch;
  $snack2 =$accountability_chart_list->snack2;
  $dinner =$accountability_chart_list->dinner;
  $snack3=$accountability_chart_list->snack3;
  $extras=$accountability_chart_list->extras;
  $activity_level = $accountability_chart_list->activity_level;
  $cardio = $accountability_chart_list->cardio;
  $resistance =$accountability_chart_list->resistance;
  $homework =$accountability_chart_list->homework;
  if($accountability_chart_id == '') {
    $type='';
  } else {
    $type='edit';
  }  
} else {
  $log_date = "";
  $breakfast = "";
  $snack1 ="";
  $lunch ="";
  $snack2 ="";
  $dinner ="";
  $snack3="";
  $extras="";
  $activity_level = "";
  $cardio = "";
  $resistance ="";
  $homework ="";
}
?>
<?php $form_fields='<div class="text-upper">'.$message.'</div>
    <div style="width:100%;"> 
      <input type="hidden" id="log_date_id" name="log_date" value="'.$get_log_date.'" />  
    </div>  
    <div class="row"></div>  
    <table style="width:100%" class="table">      
    <tbody>
      <tr><td colspan="2" class="text-center"><b>MEAL</b></td></tr>
      <tr>
        <td class="text-center">BREAKFAST <span class="mandatory">*</span></td>
        <td>
          <select name="breakfast" id="breakfast" class="custom-select">
            <option value="" ';$form_fields.=  ($breakfast == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.= ($breakfast == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($breakfast == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($breakfast == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($breakfast == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
          </select> 
          <br/> 
          <label id="breakfast-error" class="error" for="breakfast">&nbsp;</label>
        </td>
      </tr>
      <tr>  
        <td class="text-center">SNACK<span class="mandatory">*</span></td>
        <td>
          <select name="snack1" id="snack1" class="custom-select">
            <option value="" ';$form_fields.=  ($snack1 == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($snack1 == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($snack1 == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($snack1 == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($snack1 == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
          </select> 
          <br/> 
          <label id="snack1-error" class="error" for="snack1">&nbsp;</label>
        </td>
      </tr>
      <tr>                
        <td class="text-center">LUNCH <span class="mandatory">*</span></td>
        <td>
          <select name="lunch" id="lunch" class="custom-select">
            <option value="" ';$form_fields.=  ($lunch == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($lunch == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($lunch == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($lunch == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($lunch == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
          </select> 
          <br/> 
          <label id="lunch-error" class="error" for="lunch">&nbsp;</label>
        </td>
      </tr>
      <tr>  
          <td class="text-center">SNACK<span class="mandatory">*</span></td>
          <td>              
            <select name="snack2" id="snack2" class="custom-select">
              <option value="" ';$form_fields.=  ($snack2 == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($snack2 == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($snack2 == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($snack2 == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($snack2 == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
            </select> 
            <br/> 
            <label id="snack2-error" class="error" for="snack2">&nbsp;</label>        
          </td>
      </tr>
      <tr>  
          <td class="text-center">DINNER <span class="mandatory">*</span></td>
          <td>              
            <select name="dinner" id="dinner" class="custom-select">
              <option value="" ';$form_fields.=  ($dinner == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($dinner == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($dinner == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($dinner == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($dinner == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
            </select> 
            <br/> 
            <label id="dinner-error" class="error" for="dinner">&nbsp;</label>         
          </td>
      </tr>
      <tr>  
          <td class="text-center">SNACK<span class="mandatory">*</span></td>  
          <td>             
            <select name="snack3" id="snack3" class="custom-select">
              <option value="" ';$form_fields.=  ($snack3 == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($snack3 == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($snack3 == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($snack3 == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($snack3 == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
            </select> 
            <br/> 
            <label id="snack3-error" class="error" for="snack3">&nbsp;</label>         
          </td>              
      </tr>
      <tr>  
          <td class="text-center">EXTRAS</td>  
          <td>             
            <select name="extras" id="extras" class="custom-select">
              <option value="" ';$form_fields.=  ($extras == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($extras == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($extras == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($extras == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($extras == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
            </select> 
            <br/> 
            <label id="extras-error" class="error" for="extras">&nbsp;</label>         
          </td>              
      </tr>
      <tr>
        <td class="text-center" colspan="2"><b>ACTIVITY LEVEL</b><span class="mandatory"></span></td>
  			<!--<td><textarea maxlength="255" name="activity_level" id="activity_level"  class="text text-upper" rows="2" cols="25" maxlength="6" placeholder="">';$form_fields.=  (isset($activity_level)?$activity_level:"");$form_fields.='</textarea><br/><label id="activity_level-error" class="error" for="activity_level">&nbsp;</label></td>-->
      </tr>
      <tr>
        <td class="text-center">CARDIO</td>
        <td>             
          <select name="cardio" id="cardio" class="custom-select">
            <option value="" ';$form_fields.=  ($cardio == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($cardio == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($cardio == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($cardio == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($cardio == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
          </select> 
          <br/> 
          <label id="cardio-error" class="error" for="cardio">&nbsp;</label>         
        </td>
      </tr>
      <tr>  
        <td class="text-center">RESISTANCE</td>
        <td>            
            <select name="resistance" id="resistance" class="custom-select">
              <option value="" ';$form_fields.=  ($resistance == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($resistance == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($resistance == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($resistance == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($resistance == "4")?" selected":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
            </select> 
            <br/> 
            <label id="resistance-error" class="error" for="resistance">&nbsp;</label>         
          </td>
      </tr>
      <tr>                
        <td class="text-center">HOMEWORK</td>
        <td>          
            <select name="homework" id="homework" class="custom-select">
              <option value="" ';$form_fields.=  ($homework == "")?" selected":"";$form_fields.='>Select&nbsp;&nbsp;</option>
            <option value="1" ';$form_fields.=  ($homework == "1")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10003;</span></option>
            <option value="2" ';$form_fields.=  ($homework == "2")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10133;</span></option>
            <option value="3" ';$form_fields.=  ($homework == "3")?" selected":"";$form_fields.='><span style="font-size: 200%;">&#10134;</span></option>
            <option value="4" ';$form_fields.=  ($homework == "4")?" selected ":"";$form_fields.='><span style="font-size: 200%;">O</span></option>
            </select> 
            <br/> 
            <label id="homework-error" class="error" for="homework">&nbsp;</label>         
          </td>
      </tr>        
    </tbody>               
  </table>   
<div class="button">
 <table>
    <tr>
      <td>
        <input type="hidden" name="meal_score_totals" id="meal_score_totals" value="" />
        <input type="hidden" name="activity_total_score" id="activity_total_score" value="" />
        <input type="hidden" name="type" value="'; $form_fields.=($type == "")?"save":"update";$form_fields.='" />
        <input type="hidden" name="type" value="'; $form_fields.=($type == "")?"save":"update";$form_fields.='" />
        <input type="hidden" id="" name="member_id" value="'; $form_fields.= $_SESSION["memberid"]; $form_fields.='">
        <input type="hidden" name="accountability_chart_id" value="'; $form_fields.= $accountability_chart_id; $form_fields.='" />
        <input type="submit" name="submit" value="'; $form_fields.= ($type == "")?"Add":"Edit"; $form_fields.='" class="btn btn-default "/>
      </td>
    </tr>
  </table></div> ';
  echo $form_fields;
?>
