<?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 as
 $consulta = $db->query("SELECT * FROM tb_configuracion_odepreciacion ORDER BY id DESC LIMIT 1");
 $od = $db->fetch_array($consulta);
?>
<?php
  if(isset($_POST['depreciaciones'])){
   if(empty($errors)){
      $af0045   = remove_junk($db->escape($_POST['af0045']));
      $af0047   = remove_junk($db->escape($_POST['af0047']));
      $af0473   = remove_junk($db->escape($_POST['af0473']));
      $af0474 = remove_junk($db->escape($_POST['af0474']));
      $af0477 = remove_junk($db->escape($_POST['af0477']));
      $edificaciones = remove_junk($db->escape($_POST['edificaciones']));
      $anio = remove_junk($db->escape($_POST['anio']));
      $mes = remove_junk($db->escape($_POST['mes']));
      $resultado = ($af0045 + $af0047 + $af0473 + $af0474 + $af0477 + $edificaciones)/10;
      $created_at=date("Y-m-d H:i:s");

      
      $query = "INSERT INTO tb_configuracion_odepreciacion (";
      $query .="af0045, af0047, af0473, af0474, af0477, edificaciones, resultado, anio, mes, created_at";
      $query .=") VALUES (";
      $query .=" '{$af0045}', '{$af0047}', '{$af0473}', '{$af0474}', '{$af0477}', '{$edificaciones}', '{$resultado}', '{$anio}', '{$mes}', '{$created_at}'";
      $query .=")";
      if($db->query($query)){
        //sucess
        $session->msg('s'," La Configuración de DEPRECIACIONES ha sido Actualizada");
        redirect('depreciaciones.php', false);
      } else {
        //failed
        $session->msg('d',' No se pudo Actualizar Configuración de DEPRECIACIONES');
        redirect('depreciaciones.php', false);
      }
   }else {
   $session->msg("d", $errors);
    redirect('depreciaciones.php',false);
 }
}
?>
<?php include_once('../../layouts/header.php'); ?>
<div class="col-md-12">
   <?php echo display_msg($msg); ?>
 </div>
 <form method="post" action="depreciaciones.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 OTRAS DEPRECIACION</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">
                               <option value="" disabled>Seleccione...</option>      
                                <?php
                                    $year = date("Y");
                                    for ($i=2022; $i<=$year; $i++) {
                                        $selected = ($od['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">
                            <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 = ($od['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="af0045" class="col-sm-6 col-form-label text-primary">AF-0045</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="af0045" name="af0045" placeholder="number" step="any" value="<?php echo  (double)$od['af0045'];?>">
                          </div>
                        </div>
                      </div>
                     </div>
                     <div class="row mb-3">
                      <div class="col-12 col-lg-12">
                        <div class="form-group row">
                          <label for="af0047" class="col-sm-6 col-form-label text-primary">AF-0047</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="af0047" name="af0047" placeholder="number" step="any" value="<?php echo  (double)$od['af0047'];?>">
                          </div>
                        </div>
                      </div>
                     </div>
                     <div class="row mb-3">
                      <div class="col-12 col-lg-12">
                        <div class="form-group row">
                          <label for="af0473" class="col-sm-6 col-form-label text-primary">AF-0473</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="af0473" name="af0473" placeholder="number" step="any" value="<?php echo  (double)$od['af0473'];?>">
                          </div>
                        </div>
                      </div>
                     </div>
                     <div class="row mb-3">
                      <div class="col-12 col-lg-12">
                        <div class="form-group row">
                          <label for="af0474" class="col-sm-6 col-form-label text-primary">AF-0474</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="af0474" name="af0474" placeholder="number" step="any" value="<?php echo  (double)$od['af0474'];?>">
                          </div>
                        </div>
                      </div>
                     </div>
                     <div class="row mb-3">
                      <div class="col-12 col-lg-12">
                        <div class="form-group row">
                          <label for="af0477" class="col-sm-6 col-form-label text-primary">AF-0477</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="af0477" name="af0477" placeholder="number" step="any" value="<?php echo  (double)$od['af0477'];?>">
                          </div>
                        </div>
                      </div>
                     </div>
                  
                  <div class="row mb-3">
                      <div class="col-12 col-lg-12">
                        <div class="form-group row">
                          <label for="edificaciones" class="col-sm-6 col-form-label text-primary">EDIFICACIONES</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="edificaciones" name="edificaciones" placeholder="number" step="any" value="<?php echo  (double)$od['edificaciones'];?>">
                          </div>
                        </div>
                      </div>
                     </div>
                  
                  <div class="row mb-3">
                      <div class="col-12 col-lg-12">
                        <div class="form-group row">
                          <label for="resultado" class="col-sm-6 col-form-label text-primary">RESULTADO</label>
                          <div class="col-sm-6">
                            <input type="number" class="form-control" id="resultado" name="resultado" placeholder="number" step="any" value="<?php echo  (double)$od['resultado'];?>" 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="depreciaciones" id="depreciaciones">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_od.js"></script>