<?php 
session_start(); // Starting Session
include('/home1/pinnadb7/public_html/coach/functions.php');
include("/home1/pinnadb7/public_html/coach/objects/class.member.php");
include("configuration.php");
// include '../coach/objects/class.baseline_details.php';
include '/home1/pinnadb7/public_html/coach/objects/class.baseline_measurement.php';
include '/home1/pinnadb7/public_html/coach/objects/class.baseline_screen.php';
include '/home1/pinnadb7/public_html/coach/objects/class.baseline_bodycomposition.php';


//to define from and to date between 2 weeks
$chart_from_date = date('m/d/y', strtotime('-2 weeks'));
$chart_to_date = date('m/d/y');
//defining array
$selected_dates = array('x');
$selected_dates_grid = array();
$height_dates = array();
$weight_dates = array();
$bf_percnt_dates = array();
$lean_mass_dates = array();
$total_fat_dates = array();
$height = array('Height / Inches');
$weight = array('Weight / LBS');
$bf_percnt = array('Body Fat %');
$lean_mass = array('Lean Mass / LBS');
$total_fat = array('Total FAT / LBS');
$baseline_bodycomposition_data = array();

if($_POST['submit'] || $pdf_file == 'pdf_file') {
  $chart_from_date = ($_POST['chart_from_date'] !='' && $_POST['chart_from_date'] != '01/01/70' && $_POST['chart_from_date'] != '00/00/00')?date('m/d/y',strtotime($_POST['chart_from_date'])):$chart_from_date;
  $chart_to_date = ($_POST['chart_to_date'] !='' && $_POST['chart_to_date'] != '01/01/70' && $_POST['chart_to_date'] != '00/00/00')?date('m/d/y',strtotime($_POST['chart_to_date'])):$chart_to_date;
}
$member_id = $_SESSION['memberid'];
$member = new Member();
$member->memberId=$member_id;
$member_details = $member->Get($member_id);

//getting baseline details
$baseline_detail_ids = $member->GetBaseline_detailsList();
foreach ($baseline_detail_ids as $key => $baseline_value) {
  $baseline_details = new Baseline_details();
  $baseline_details->baseline_detailsId = $baseline_value->baseline_detailsId;
  $baseline_bodycomposition = $baseline_details->GetBaseline_bodycompositionList(array(array('cmp_date','>=', date('Y-m-d',strtotime($chart_from_date))),array('cmp_date','<=', date('Y-m-d',strtotime($chart_to_date)))),'cmp_date',true);
  $baseline_bodycomposition_data = array_merge($baseline_bodycomposition_data,$baseline_bodycomposition);
}

$arr = 0;
for( $i=strtotime($chart_from_date); $i<=strtotime($chart_to_date); ) {
  $selected_dates[]=date('m/d/y',$i);
  $selected_dates_grid[$arr]['value']=date('m/d/y',$i);
  $height[] = '';
  $weight[] = '';
  $bf_percnt[] = '';
  $lean_mass[] = '';
  $total_fat[] = '';
  $i=strtotime('+1 day',$i);
  $arr++;
}

for( $j=0; $j<count($baseline_bodycomposition_data); $j++ ) {
  if( in_array(date('m/d/y',strtotime($baseline_bodycomposition_data[$j]->cmp_date)),$selected_dates) ) {
    $key = array_search(date('m/d/y',strtotime($baseline_bodycomposition_data[$j]->cmp_date)), $selected_dates); 
    $height[$key] = $baseline_bodycomposition_data[$j]->cmp_height;
    $weight[$key] = $baseline_bodycomposition_data[$j]->cmp_weight;
    $bf_percnt[$key] = $baseline_bodycomposition_data[$j]->cmp_body_fat;
    $lean_mass[$key] = $baseline_bodycomposition_data[$j]->cmp_lean_bm;
    $total_fat[$key] = $baseline_bodycomposition_data[$j]->cmp_fat_weight;
  }
}
//removing empty data
$height_dates = $selected_dates;
$weight_dates = $selected_dates;
$bf_percnt_dates = $selected_dates;
$lean_mass_dates = $selected_dates;
$total_fat_dates = $selected_dates;

for($k=0;$k<count($selected_dates);$k++) {
  if($height[$k] == ''){
    unset($height[$k]);
    unset($height_dates[$k]);
  }
  if($weight[$k] == ''){
    unset($weight[$k]);
    unset($weight_dates[$k]);
  }
  if($bf_percnt[$k] == ''){
    unset($bf_percnt[$k]);
    unset($bf_percnt_dates[$k]);
  }
  if($lean_mass[$k] == ''){
    unset($lean_mass[$k]);
    unset($lean_mass_dates[$k]);
  }
  if($total_fat[$k] == ''){
    unset($total_fat[$k]);
    unset($total_fat_dates[$k]);
  }
}
$height = array_values($height);
$height_dates = array_values($height_dates);
$weight = array_values($weight);
$weight_dates = array_values($weight_dates);
$bf_percnt = array_values($bf_percnt);
$bf_percnt_dates = array_values($bf_percnt_dates);
$lean_mass = array_values($lean_mass);
$lean_mass_dates = array_values($lean_mass_dates);
$total_fat = array_values($total_fat);
$total_fat_dates = array_values($total_fat_dates);

$no_data = 0;
if(count($height_dates) <= 1 || count($weight_dates) <= 1 || count($bf_percnt_dates) <= 1 || count($lean_mass_dates) <= 1 || count($total_fat_dates) <= 1 ) {
  $no_data = 1;
}
?>

<!DOCTYPE html>
<html>
<head>
  <title><?=$title?></title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link href="css/style.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" type="text/css" href="../coach/css/c3.min.css">
  <link href="../coach/css/jquery.datetimepicker.min.css" rel="stylesheet" type="text/css">
  <link href="../coach/css/bootstrap.css" rel="stylesheet" type="text/css">
</head>
<style type="text/css">
  #chart1 {
    width: 100%;
    min-height: 400px;
  }
  #daily_log_id { width: 75%; margin: 5px 0px 52px 0px; }
  .container {
    padding-bottom: 20px;
  }
      .success { background-color: #ccffcc; }
      #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>
<body>
<div class="container">
  <div id="profile">
    <label><img height="100" style="border:none;"  src="http://pinnacle-fitness.com/wp-content/uploads/2014/08/Pinnacle-Fitness-NewLogo.jpg"></label></br></br></br>
    <div id="profile"></br></div>
    <b id="homepage"><a href="member_homepage.php">Daily Log</a></b>&nbsp;&nbsp;|&nbsp;&nbsp;
    <b id="homepage"><a href="baseline_chart.php">Baseline Details Chart</a></b>&nbsp;&nbsp;|&nbsp;&nbsp;
    <b id="homepage"><a href="member_profile.php">Profile</a></b>&nbsp;&nbsp;|&nbsp;&nbsp; 
    <b id="homepage"><a href="https://meetme.so/ChristopherMorel" target="_new">Schedule Coaching Call</a></b>&nbsp;&nbsp;|&nbsp;&nbsp; 
    <b id="logout"><a href="logout.php">Log Out</a></b>
  </div>
  <div style=""><p style="text-align: center;font-weight: bold;"> Baseline Charts </p></div>
  <div>  
    <form method="POST">
      <table style="width:60%;" border="0">   
        <tr>   
          <td>Start Date:</td>
          <td>
            <input type="text" maxlength="10" class="datepicker"  name="chart_from_date"  class="text text-upper" value="<?php echo $chart_from_date;?>" placeholder="YYYY-MM-DD"/>
          <td> 
          <td>
            End Date:<br/>                   
          </td>
          <td>
            <input type="text" maxlength="10" class="datepicker"  name="chart_to_date"  class="text text-upper" value="<?php echo $chart_to_date;?>" placeholder="YYYY-MM-DD"/>
          <td> 
          <td>
            <input type="submit" name="submit" value="Submit" class="submit"/>
                       
          </td>
        </tr> 
      </table>
    </form>    
  </div>
  <br/>
  <br/>
 
  <div id="chart_display" style="">
    <div style="font-size: 18px;"><b>Chart:</b></div>
    <?php if($no_data == 0) { ?>
      <div id="chart1"></div><br/>
      <div id="chart2"></div> <br/>
      <div id ="chart3"></div><br/>
      <div id ="chart4"></div><br/>
      <div id ="chart5"></div><br/><!--  style="height:500px;"-->
    <?php } else { if($pdf_file == '') { ?>
      <div style="padding-left:20px;font-weight:bold;color: red;">Please change the date criteria as the selected date range has no data available to display</div>
    <?php } } ?>
  </div>  
  </div>
<script language="javascript" type="text/javascript" src="../coach/js/jquery.js"></script>
<script src="../coach/js/jquery.datetimepicker.full.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="../coach/js/c3.min.js"></script>

  <script type="text/javascript">  
    
  var selected_dates = <?php echo json_encode($selected_dates); ?>;
  var selected_dates_grid = <?php echo json_encode($selected_dates_grid); ?>;
  var weight = <?php echo json_encode($weight); ?>;
  var weight_dates = <?php echo json_encode($weight_dates); ?>;
  var bf_percnt = <?php echo json_encode($bf_percnt); ?>;
  var bf_percnt_dates = <?php echo json_encode($bf_percnt_dates); ?>;
  var lean_mass = <?php echo json_encode($lean_mass); ?>;
  var lean_mass_dates = <?php echo json_encode($lean_mass_dates); ?>;
  var total_fat = <?php echo json_encode($total_fat); ?>;
  var total_fat_dates = <?php echo json_encode($total_fat_dates); ?>;
  var height = <?php echo json_encode($height); ?>;
  var height_dates = <?php echo json_encode($height_dates); ?>;
  var no_data = <?php echo $no_data; ?>;

  if(no_data == 0) {
    var chart1 = c3.generate({
      bindto: '#chart1',
      data: {
        x: 'x',
        columns: [height_dates,height],
        labels: true,
        type: 'line',
        colors: {
          'Height / Inches': '#eeb422',
        },
      },
      point: {
        r: 5
      },
      grid: {
        x: {
          //lines: selected_dates_grid,
        },
        y: {
          show: true,
        }
      },
      axis: {
        x: {
          label: {
            position: 'outer-center'
          },
          type: 'category',
        },
        y: {
          label: {
            text: 'Height / Inches',
            position: 'outer-middle'
          },
          show: true,
        }
      },
      padding: {
        top: 10,
        bottom: 10
      }
    });
  }
  if(no_data == 0) {
    var chart2 = c3.generate({
      bindto: '#chart2',
      data: {
        x: 'x',
        columns: [weight_dates,weight],
        labels: true,
        type: 'line'
      },
      point: {
        r: 5
      },
      grid: {
        x: {
          ////lines: selected_dates_grid,
        },
        y: {
          show: true,
        }
      },
      axis: {
        x: {
          label: {
            position: 'outer-center'
          },
          type: 'category'
        },
        y: {      
          label: {
            text: 'Weight / LBS',
            position: 'outer-middle'
          },
        }
      },
      padding: {
        top: 10,
        bottom: 10
      }  
    });
    }
  if(no_data == 0) {
    var chart3 = c3.generate({
      bindto: '#chart3',
      data: {
        x: 'x',
        columns: [ bf_percnt_dates,bf_percnt ],
        labels: true,
        type: 'line',
        colors: {
          'Body Fat %': '#cd0000'
        }
      },
      point: {
        r: 5
      },
      grid: {
        x: {
          //lines: selected_dates_grid,
        },
        y: {
          show: true,
        }
      },
      axis: {
        x: {
          label: {
            position: 'outer-center'
          },
          type: 'category'
        },
        y: {
          label: {
            text: 'Body Fat %',
            position: 'outer-middle'
          },
        }
      },
      padding: {
        top: 10,
        bottom: 10
      }
    });
  }
  if(no_data == 0) {
    var chart4 = c3.generate({
      bindto: '#chart4',
      data: {
        x: 'x',
        columns: [lean_mass_dates,lean_mass ],
        labels: true,
        type: 'line',
        colors: {
          'Lean Mass / LBS': '#9acd32'
        }
      },
      point: {
        r: 5
      },
      grid: {
        x: {
          //lines: selected_dates_grid,
        },
        y: {
          show: true,
        }
      },
      axis: {
        x: {
          label: {
            position: 'outer-center'
          },
          type: 'category'
        },
        y: {
          label: {
            text: 'Lean Mass / LBS',
            position: 'outer-middle'
          },
        } 
      },
      padding: {
        top: 10,
        bottom: 10
      }
    });
  }
  if(no_data == 0) {
    var chart5 = c3.generate({
      bindto: '#chart5',
      data: {
        x: 'x',
        columns: [ total_fat_dates, total_fat ],
        labels: true,
        type: 'line',
        colors: {
          'Total FAT / LBS': '#483d8b'
        }
      },
      point: {
        r: 5
      },
      grid: {
        x: {
          //lines: selected_dates_grid,
        },
        y: {
          show: true,
        }
      },
      axis: {
        x: {
          label: {
            position: 'outer-center'
          },
          type: 'category'
        },
        y: {
          label: {
            text: 'Total Fat / LBS',
            position: 'outer-middle'
          }
        }
      },
      padding: {
        top: 10,
        bottom: 10
      }   
    });
  }

  $('.c3-legend-item text').css('font-size','16px');
  $('.c3-legend-item text').css('font-weight','bold');
  $('.c3-legend-item-tile').attr('stroke-width','14');
  $('.c3-text').css('font-size','24px');
  $('.c3-shapes path').css('stroke-width','2.5px');
  $('.c3-grid line').css('stroke','#aaa');
  d3.select('#chart1 .c3-title').style('font-size', '18px').style('fill', '#0000bb');
 
  jQuery(document).ready(function() {
    $('.datepicker').datetimepicker(
    {
      timepicker:false,
      format:'Y-m-d',
      formatDate:'Y-m-d',
    });
  });
  </script>
</body>
</html>