<?php
	$Seccion      = 'Vehiculos';
	$NameSec      = 'Vehiculos';
	$SecLine      = array('Vehiculos', 'Editar');
	$Position   = '../';
	require_once($Position.'config/config.php');
  require_once($Position.'config/conexion.php');
  require_once($Position.'config/user.php');
  require_once($Position.'funciones/acces.php');
  require_once($Position.'funciones/logout.php');
  require_once($Position.'funciones/funciones.php');
  //require_once($Position.'funciones/extras.php');

	if ( $_POST['Save'] == "Ok" ) {

		$Vehiculo_Id = $_POST['Vehiculo_Id'];
		$TipoVehiculo    		= $_POST['TipoVehiculo'];
		$Placas 		= $_POST['Placas'];
		$Email     		= $_POST['Email'];
		// $Celular   = $_POST['Celular'];
		// $FechaAlta = CambioFechaCompleta($_POST['FechaAlta']);
		$Activo    		= $_POST['Activo'];

	}

	//Guardar Datos
	if ( $_POST['Save'] == "Ok"){

		//New
		if ( $_POST['Action'] == "New" ) {

			//Insert Usuario
	    $Query = "
				INSERT INTO PA_Vehiculos
		    (
					Vehiculo_Id,
					TipoVehiculo, Placas
				)VALUES(
					'$Vehiculo_Id',
					'$TipoVehiculo', '$Placas'
				)
			";
	    mysqli_query($mysqli, $Query );
	    $Id = mysqli_insert_id($mysqli);

		}
		//End New

		//Edit
		if ( $_POST['Save'] == "Ok" && $_POST['Id'] ) {

	    $Id = $_POST['Id'];
	    $Query = "
		    UPDATE PA_Vehiculos SET
			    TipoVehiculo  = '$TipoVehiculo',
			    Placas 				= '$Placas',
			    Activo    		= '$Activo'
		    WHERE Id = '$Id'
	    ";
	    mysqli_query($mysqli, $Query );
	    $Save = 'Ok';

		}
		//End Edit

		if( $_POST['Action'] == "New" && $Id ){
		  echo '<script type="text/javascript">document.location.href="edit.php?Id='.$Id.'&New=Ok"</script>';
		}

	}
	//End Guardar Datos

	//Conductor
	$Query = "
		SELECT
			usr.Id,
			usr.Vehiculo_Id,
			usr.TipoVehiculo, usr.Placas,
			usr.Activo
		FROM PA_Vehiculos usr
		WHERE usr.Id = '$_GET[Id]'
	";
	$Result = mysqli_query($mysqli, $Query );
	$Vehiculo = mysqli_fetch_assoc($Result);

?>

<!DOCTYPE html>
<html lang="en">

<?php require_once($Position.'head.php'); ?>

<body class="fix-sidebar fix-header card-no-border">

  <div class="preloader">
    <svg class="circular" viewBox="25 25 50 50">
    <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /> </svg>
  </div>

  <div id="main-wrapper">

    <?php require_once($Position.'header.php'); ?>
    <?php require_once($Position.'menu.php'); ?>

    <div class="page-wrapper">

      <div class="container-fluid">

        <form method="post" enctype="multipart/form-data" data-validate="parsley" role="form" id="FormPer">

          <div class="row page-titles page-titles-fixed">

              <?php require_once($Position.'title.php'); ?>

              <!-- Botones -->
              <div class="col-md-7 align-self-center text-right">
                <div class="btn-group" role="group" aria-label="Basic example">

									<!-- Btn Regresar -->
									<button
										type="button"
										class="btn btn-sm btn-secondary"
										onclick="window.location.href='index.php'"
									>
										Regresar
										<i class="fa fa-arrow-left m-l-10"></i>
									</button>
									<!-- End Btn Regresar -->

									<!-- Btn Guardar -->
									<button
										type="submit"
										class="btn btn-sm btn-success"
									>
										Guardar
										<i class="fa fa-check m-l-10"></i>
									</button>
									<!-- End Btn Guardar -->

                  <input type="hidden" name="Save" value="Ok">

                  <?php if( $Vehiculo['Id'] ){ ?>
										<input name="Id" type="hidden" id="Id" value="<?php echo $Vehiculo['Id']; ?>">
										<input name="Action" type="hidden" value="Edit">
									<?php }else{ ?>
										<input name="Action" type="hidden" value="New">
									<?php } ?>
                </div>
              </div>
              <!-- End Botones -->

          </div>

          <div class="row inner-fixed-content">
            <div class="col-12">

							<!-- Nav tabs -->
							<ul class="nav nav-tabs tabcontent-border" role="tablist">

								<!-- Información -->
								<li class="nav-item">
									<a class="nav-link active" data-toggle="tab" href="#Informacion" role="tab">
										<span class="hidden-sm-up"><i class="ti-user"></i></span>
										<span class="hidden-xs-down">Información</span>
									</a>
								</li>
								<!-- End Información -->

							</ul>
							<!-- End Nav tabs -->

							<!-- Tabs Content -->
							<div class="tab-content tabcontent-border">

								<!-- Tab Información -->
								<div class="tab-pane card-body active" id="Informacion" role="tabpanel">
									<h4 class="card-title">Información</h4>
									<hr />
									<div class="row">

										<!-- ID -->
										<div class="form-group col-sm-12">
											<label class="col-form-label">ID<span class="text-danger">*</span></label>
											<div class="">
												<input
													name="Vehiculo_Id"
													type="text"
													class="form-control"
													value="<?php echo $Vehiculo['Vehiculo_Id']; ?>"
													autocomplete="off"
													required="true"
												>
											</div>
										</div>
										<!-- End ID -->

										<!-- TipoVehiculo -->
										<div class="form-group col-sm-12">
											<label class="col-form-label">Tipo de Vehiculo<span class="text-danger">*</span></label>
											<div class="">
												<input
													name="TipoVehiculo"
													type="text"
													class="form-control"
													value="<?php echo $Vehiculo['TipoVehiculo']; ?>"
													autocomplete="off"
													required="true"
												>
											</div>
										</div>
										<!-- End TipoVehiculo -->

										<!-- Placas -->
										<div class="form-group col-sm-12">
											<label class="col-form-label">Placas<span class="text-danger">*</span></label>
											<div class="">
												<input
													name="Placas"
													type="text"
													class="form-control"
													value="<?php echo $Vehiculo['Placas'];?>"
													autocomplete="off"
													required="true"
												>
											</div>
										</div>
										<!-- End Placas -->

										<!-- Celular --
										<div class="form-group col-sm-12">
											<label class="col-form-label">Teléfonos Celular<span class="text-danger">*</span></label>
											<div class="">
												<input class="form-control" id="Celular" name="Celular" placeholder="Enter valid e-mail address" value="<?php echo $Vehiculo['Celular'];?>" autocomplete="off" parsley-trigger="change" required>
											</div>
										</div>
										<!-- End Celular -->

										<!-- Status --
										<div class="form-group col-sm-12">
											<label class="col-form-label">Status<span class="text-danger">*</span></label>
											<div class="">
												<select
													class="chosen-select"
													name="Activo"
													required
													data-validation-required-message="This field is required"
												>
													<option
														value="1"
														<?php
															if(
																( $Sta[Id] && $Vehiculo[Activo] == 1 )
																||
																( !$Vehiculo[Id] )
															){
														 		echo "selected='selected'";
															}
														?>
													>
														Activo
													</option>
													<option
														value="0"
														<?php
															if(
																$Sta[Id] && $Vehiculo[Activo] == 0
															){
														 		echo "selected='selected'";
															}
														?>
													>
														Inactivo
													</option>
												</select>
											</div>
										</div>
										<!-- End Status -->

									</div>
								</div>
								<!-- End Tab Información -->

							</div>
							<!-- End Tabs Content -->

            </div>
          </div>

        </form>
      </div>

      <?php //require_once($Position.'footer.php'); ?>

    </div>

  </div>

  <?php require_once($Position.'scripts.php'); ?>
  <script>

  </script>
</body>

</html>
