<?php
  $page_title = 'CONFIGURACION';
  require_once('../../includes/load.php');
?>
<?php
// Checkin What level user has permission to view this page
 page_require_level(1);

//Consulta de la base de datos para obterner el ultimo registro del epps
 $consulta = $db->query("SELECT * FROM tb_configuracion_epps ORDER BY id DESC LIMIT 1");
 $epps = $db->fetch_array($consulta);
?>
<?php
  if(isset($_POST['epps'])){
   if(empty($errors)){
      $pantalon   = remove_junk($db->escape($_POST['pantalon']));
      $polo   = remove_junk($db->escape($_POST['polo']));
      $chompa   = remove_junk($db->escape($_POST['chompa']));
      $lente = remove_junk($db->escape($_POST['lente']));
      $mascarilla = remove_junk($db->escape($_POST['mascarilla']));
      $zapato = remove_junk($db->escape($_POST['zapato']));
      $alcohol = remove_junk($db->escape($_POST['alcohol']));
      $anio = remove_junk($db->escape($_POST['anio']));
      $mes = remove_junk($db->escape($_POST['mes']));
      $total = $pantalon/6 + $polo/6 + $chompa/6 + $lente/6 + $mascarilla/6 + $zapato/6 + $alcohol;
      $created_at=date("Y-m-d H:i:s");

      
      $query = "INSERT INTO tb_configuracion_epps (";
      $query .="pantalon, polo, chompa, lente, mascarilla, zapato, alcohol, total, anio, mes, created_at";
      $query .=") VALUES (";
      $query .=" '{$pantalon}', '{$polo}', '{$chompa}', '{$lente}', '{$mascarilla}', '{$zapato}', '{$alcohol}', '{$total}', '{$anio}', '{$mes}', '{$created_at}'";
      $query .=")";
      if($db->query($query)){
        //sucess
        $session->msg('s'," La Configuración de EPPS ha sido Actualizada");
        redirect('epps.php', false);
      } else {
        //failed
        $session->msg('d',' No se pudo Actualizar Configuración de EPPS');
        redirect('epps.php', false);
      }
   }else {
   $session->msg("d", $errors);
    redirect('epps.php',false);
 }
}
?>
<?php include_once('../../layouts/header.php'); ?>
<div class="col-md-12">
   <?php echo display_msg($msg); ?>
 </div>
  <form method="post" action="epps.php">
  <div class="col-12 px-4 pt-3">
    <div class="container-fluid">
      <div class="row d-flex justify-content-center">
        <div class="col-6">
          <div class="card">
            <div class="card-header">
              <h5 class="card-title mb-0">CONFIGURACIÓN EPPS</h5>
            </div>
            <div class="card-body">
              <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="anio" class="col-sm-6 col-form-label text-primary">AÑO</label>
                    <div class="col-sm-6">
                      <select class="custom-select-confi is-requerido" id="anio" name="anio" required>
                        <option value="" disabled>Seleccione...</option>      
                          <?php
                              $year = date("Y");
                              for ($i=2022; $i<=$year; $i++) {
                                  $selected = ($epps['anio'] == $i) ? 'selected' : ''; // Verifica si este mes debe ser seleccionado
                                  echo '<option ' . $selected . ' value="' . $i . '">'.$i.'</option>';
                              }
                          ?>               
                    </select>
                    </div>
                  </div>
                </div>
               </div>
               <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="mes" class="col-sm-6 col-form-label text-primary">MES</label>
                    <div class="col-sm-6">
                      <select class="custom-select-confi is-requerido" id="mes" name="mes" required>
                      <option value="" disabled>Seleccione...</option>      
                          <?php
                            for ($i = 1; $i <= 12; $i++) {
                                $value = ($i < 10) ? '0' . $i : $i;
                                switch ($value) {
                                   case '1': $mes = 'Enero';break;
                                   case '2': $mes = 'Febrero';break;
                                   case '3': $mes = 'Marzo';break;
                                   case '4': $mes = 'Abril';break;
                                   case '5': $mes = 'Mayo';break;
                                   case '6': $mes = 'Junio';break;
                                   case '7': $mes = 'Julio';break;
                                   case '8': $mes = 'Agosto';break;
                                   case '9': $mes = 'Septiembre';break;
                                   case '10': $mes = 'Octubre';break;
                                   case '11': $mes = 'Noviembre';break;
                                   case '12': $mes = 'Diciembre';break;
                                   default: $mes = 'Non'; break;
                                }
                                $selected = ($epps['mes'] == $i) ? 'selected' : ''; // Verifica si este mes debe ser seleccionado
                                echo '<option ' . $selected . ' value="' . $i . '">' . $mes . '</option>';
                            }
                            ?>
                    </select>
                    </div>
                  </div>
                </div>
               </div>
              <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="polo" class="col-sm-6 col-form-label text-primary">POLO</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="polo" name="polo" placeholder="number" value="<?php echo  (double)$epps['polo'];?>" step="any">
                    </div>
                  </div>
                </div>
               </div>
               <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="chompa" class="col-sm-6 col-form-label text-primary">CHOMPA</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="chompa" name="chompa" placeholder="number" value="<?php echo  (double)$epps['chompa'];?>" step="any">
                    </div>
                  </div>
                </div>
               </div>
               <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="lente" class="col-sm-6 col-form-label text-primary">LENTE</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="lente" name="lente" placeholder="number" value="<?php echo  (double)$epps['lente'];?>" step="any">
                    </div>
                  </div>
                </div> 
               </div>
               <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="pantalon" class="col-sm-6 col-form-label text-primary">PANTALON</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="pantalon" name="pantalon" placeholder="number" value="<?php echo  (double)$epps['pantalon'];?>" step="any">
                    </div>
                  </div>
                </div> 
               </div>
               <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="mascarilla" class="col-sm-6 col-form-label text-primary">MASCARILLA</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="mascarilla" name="mascarilla" placeholder="number" value="<?php echo  (double)$epps['mascarilla'];?>" step="any">
                    </div>
                  </div>
                </div>
               </div>
               <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="zapato" class="col-sm-6 col-form-label text-primary">ZAPATO</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="zapato" name="zapato" placeholder="number" value="<?php echo  (double)$epps['zapato'];?>" step="any">
                    </div>
                  </div>
                </div>
               </div>
            
            <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="alcohol" class="col-sm-6 col-form-label text-primary">ALCOHOL</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="alcohol" name="alcohol" placeholder="number" value="<?php echo  (double)$epps['alcohol'];?>" step="any">
                    </div>
                  </div>
                </div>
               </div>
            
            <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    <label for="total" class="col-sm-6 col-form-label text-primary">TOTAL</label>
                    <div class="col-sm-6">
                      <input type="number" class="form-control" id="total" name="total" placeholder="number" value="<?php echo  (double)$epps['total'];?>" step="any" disabled>
                    </div>
                  </div>
                </div>
               
            </div>

            <div class="row mb-3">
                <div class="col-12 col-lg-12">
                  <div class="form-group row">
                    
                    <div class="col-sm-12">
                        <button type="submit" class="btn btn-block btn-success" name="epps">Actualizar</button>
                    </div>
                  </div>
                </div>
               
            </div>
          </div>

         
        </div>
      </div>
    </div>
  </div>
</div>
</form>

<?php include_once('../../layouts/footer.php'); ?>
<script type="text/javascript" src="js/configuracion_epps.js"></script>