<?php include_once('header.php') ;

if(!empty($_POST['mid']))
$mid=$_POST['mid'];
else if(!empty($_GET['mid']))
$mid=$_GET['mid'];
else
$mid='';

if(!empty($_POST['username']))
$pass=add_child();

$latest=array();
if($mid!='')
$latest=get_child($mid,'','');
?>


  <div class="layout-overlay" id="layoutOverlay"></div>

  <main class="main-content kids-form-page">
    <section class="page-head" style="display:none">
      <div>
        <h1>Add child</h1>
      </div>
    </section>

    <section class="content-card form-card">
      <div class="content-card__head">
        <div>
          <h2>Add Child</h2>
        </div>
      </div>

      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="form-validation" class="needs-validation" novalidate>
         <div id="formFeedback" class="alert d-none mb-3" role="alert"></div>
    
      <div class="row g-3 g-lg-4 form-grid">
        <div class="col-12 col-md-12">
          <label class="form-label" for="child_name">Child Name</label>
          <input type="text" class="form-control" name="child_name" id="child_name" value="<?php if(!empty($latest[0])) echo $latest[0]['child_name']; ?>" required>
          <div class="invalid-feedback">Child Name Required.</div>
        </div>
        
        <div class="col-12 col-md-12">
          <label class="form-label" for="child_age">Child Age Group</label>
          <select class="form-select" name="child_age" id="child_age" onChange="get_group(this.value,'');" required>
            <option value="" selected disabled>Select Age Group</option>
              <?php
                  for($i = 7; $i < 19; $i += 2){
                    $next = $i + 2;
               ?>
                  <option value="<?php echo $i.'-'.$next; ?>"> <?php echo $i.'-'.$next; ?> years </option>
               <?php
                  }
               ?>
         </select>
           <div class="invalid-feedback">Child Age Group Required.</div>
        </div>
        
        <div class="col-12 col-md-12">
          <label class="form-label" for="grade">Grade</label>
          <select class="form-select" name="grade" id="grade" required>
              <option value="" selected disabled>Select Grade</option>
          </select>
          <div class="invalid-feedback">Grade Required.</div>
        </div>
        
        <div class="col-12 col-md-12">
          <label class="form-label" for="username">Username</label>
          <input type="text" class="form-control" name="username" value="<?php if(!empty($latest[0])) echo $latest[0]['username']; ?>" id="username" required>
          <div class="invalid-feedback">Username Required.</div>
        </div>
        <?php if(empty($mid)){ ?>
        <div class="col-12 col-md-12">
          <label class="form-label" for="child_name">Password</label>
          <input type="password" class="form-control" name="password" id="password" value="<?php if(!empty($latest[0])) echo $latest[0]['password']; ?>" required>
          <div class="invalid-feedback">Password Required.</div>
        </div>
        <?php } ?>
        <div class="col-12 form-actions">
        	<input name="mid" type="hidden" id="mid" value="<?php echo $mid; ?>" />
            <input name="page_index" type="hidden" id="page_index" value="<?php echo $page_index; ?>" />
          <button type="submit" class="btn btn-primary rounded-pill px-3">Submit</button>
        </div>
      </div>
      </form>
    </section>
  </main>
</div>

<script>
$('#child_age').val('<?php echo $latest[0]['child_age']; ?>');
get_group('<?php echo $latest[0]['child_age']; ?>','<?php echo $latest[0]['grade']; ?>');
$('#grade').val('<?php echo $latest[0]['grade']; ?>');
</script>

<?php include_once('footer.php') ?>