<?php
	$Seccion      = 'Expositores';
	$NameSec      = 'Expositores';
	$SecLine      = array('Expositores', 'Editar');
	$Position     = '../../';
	$UrlImgs      = 'images/expositores';
	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');
	require_once($Position.'funciones/idiomas.php');
  require_once($Position.'funciones/resize-class.php');

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

		$Nombre    = addslashes($_POST['Nombre']);
		$Apellidos = addslashes($_POST['Apellidos']);
		$Titulo    = addslashes($_POST['Titulo']);
		$Email     = addslashes($_POST['Email']);
		$Celular   = addslashes($_POST['Celular']);
		$Contenido = addslashes($_POST['Contenido']);
		$Activo    = $_POST['Activo'];

		$UsuarioDependiente	= $_POST['UsuarioDependiente'];

		// if( $_POST['Password'] == '0000000000' ){
	  //   $Pass     = '';
	  //   $PassEdit = '';
		// }else{
	  //   $Pass     = md5(addslashes($_POST['Password']));
	  //   $PassEdit = "Pass  = '$Pass',";
		// }
	}

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

		//Insert Usuario
    $Query = "
			INSERT INTO CMS_Expositores
	    (
				Email, Celular,
				Nombre, Apellidos,
				Titulo, Contenido,
				Activo
			)VALUES(
				'$Email', '$Celular',
				'$Nombre', '$Apellidos',
				'$Titulo', '$Contenido',
				'$Activo'
			)
		";
    mysqli_query($mysqli, $Query );
    $Id = mysqli_insert_id($mysqli);

	}

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

	    $Id = $_POST['Id'];
	    $Query = "
		    UPDATE CMS_Expositores SET
			    Nombre    = '$Nombre',
			    Apellidos = '$Apellidos',
					Titulo		= '$Titulo',
					Contenido	= '$Contenido',
					Email			= '$Email',
					Celular		= '$Celular',
			    Activo    = '$Activo'
		    WHERE Id  = '$Id'
	    ";
	    mysqli_query($mysqli, $Query );
	    $Save = 'Ok';

	}

	//Sube Imagen Desk
	$Imagen = "image";
	if(empty($_FILES[$Imagen]['name'])){}else{
		$_FILES[$Imagen]["name"];
		$allowedExts = array("JPEG", "jpeg", "jpg", "JPG");
		$temp = explode(".", $_FILES[$Imagen]["name"]);
		$extension = end($temp);
		if((
		($_FILES[$Imagen]["type"] == "image/jpeg")
		|| ($_FILES[$Imagen]["type"] == "image/jpg")
		|| ($_FILES[$Imagen]["type"] == "image/JPEG")
		|| ($_FILES[$Imagen]["type"] == "image/JPG")
		)
		&& ($_FILES[$Imagen]["size"] < 10000000)
		&& in_array($extension, $allowedExts)) {
		if ($_FILES[$Imagen]["error"] > 0) {
		}else{

			//RootSite del Sitio Seleccionado
			echo $ImageUp  = $RootSite.'/'.$UrlImgs."/".$Id.".jpg";
			$ImageUpm = $RootSite.'/'.$UrlImgs."/".$Id."m.jpg";

			if ( move_uploaded_file($_FILES[$Imagen]["tmp_name"], $ImageUp) ){

				//echo "Img Up";
				chmod($ImageUp, 0777);
				//Ajuste de tamaño
				$width = getWidth($ImageUp);
				$height = getHeight($ImageUp);
				$max_width = "700";
				if ($width > $max_width){
					$scale = $max_width/$width;
					$resizeObj = new resize($ImageUp);
					$resizeObj -> resizeImage(500, 700, 'crop');
					$resizeObj -> saveImage($ImageUp, 70);
				}else{
					$scale = 1;
					$resizeObj = new resize($ImageUp);
					$resizeObj -> resizeImage(500, 700, 'crop');
					$resizeObj -> saveImage($ImageUp, 70);
				}

				if ( copy($ImageUp, $ImageUpm) ){

					$resizeObj = new resize($ImageUpm);
					$resizeObj -> resizeImage(250, 350, 'crop');
					$resizeObj -> saveImage($ImageUpm, 70);

				}

			}else{
				//echo "Img Not<br>";
				print_r(error_get_last());
			}

		}}else{
		}
	}
	//End Sube Imagen Desk

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

	//Usuario
	$Query = "
		SELECT
			usr.Id,
			usr.Email,
			usr.Celular,
			usr.Nombre, usr.Apellidos,
			usr.Titulo, usr.Contenido,
			usr.Activo
		FROM CMS_Expositores usr
		WHERE usr.Id = '$_GET[Id]'
	";
	$Result = mysqli_query($mysqli, $Query );
	$Expo = 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( $Expo['Id'] ){ ?>
										<input name="Id" type="hidden" id="Id" value="<?php echo $Expo['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 -->

								<!-- Fotografía -->
								<li class="nav-item">
									<a class="nav-link" data-toggle="tab" href="#Foto" role="tab">
										<span class="hidden-sm-up"><i class="ti-user"></i></span>
										<span class="hidden-xs-down">Fotografía</span>
									</a>
								</li>
								<!-- End Fotografía -->

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

							</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">

									<div class="row">

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

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

										<!-- Apellidos -->
										<div class="form-group col-sm-6">
											<label class="col-form-label">Apellido(s)<span class="text-danger">*</span></label>
											<div class="">
												<input
													name="Apellidos"
													type="text"
													class="form-control"
													value="<?php echo $Expo['Apellidos'];?>"
													autocomplete="off"
													required="true"
												>
											</div>
										</div>
										<!-- End Apellidos -->

										<!-- 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" value="<?php echo $Expo['Celular'];?>" autocomplete="off" parsley-trigger="change">
											</div>
										</div>
										<!-- End Celular -->

										<!-- Email -->
										<div class="form-group col-sm-12">
											<label class="col-form-label">Correo Electrónico<span class="text-danger">*</span></label>
											<div class="">
												<input type="email" class="form-control" id="email" name="Email" value="<?php echo $Expo['Email'];?>" autocomplete="off" parsley-trigger="change">
											</div>
										</div>
										<!-- End Email -->

										<!-- 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] && $Expo[Activo] == 1 )
																||
																( !$Expo[Id] )
															){
														 		echo "selected='selected'";
															}
														?>
													>
														Activo
													</option>
													<option
														value="0"
														<?php
															if(
																$Sta[Id] && $Expo[Activo] == 0
															){
														 		echo "selected='selected'";
															}
														?>
													>
														Inactivo
													</option>
												</select>
											</div>
										</div>
										<!-- End Status -->

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

								<!-- Tab Foto -->
								<div class="tab-pane card-body" id="Foto" role="tabpanel">
									<div class="row">

										<!-- Img -->
										<div class="col-sm-3">
											<?php
												$Image = $RootSite.'/'.$UrlImgs.'/'.$Expo['Id'].'m.jpg';
												if (file_exists($Image)) {
											?>
												<div class="m-b-10">
													<img
														class="img-responsive"
														src="<?php echo $UrlSite.'/'.$UrlImgs.'/'.$Expo['Id'].'m.jpg'; ?>"
													>
												</div>
											<?php } ?>
										</div>
										<!-- End Img -->

										<!-- ImgIn -->
										<div class="col-sm-9">
											<label for="input-file-now-custom-1">
												La fotografía deberá estar en la siguiente resolución: 400 pxw x 700 pxh y 72 dpi;
												<strong>solo se acepta formato jpg.</strong>
											</label>
											<input name="image" type="file" accept="jpg" id="input-file-now-custom-1" class="dropify"/>
										</div>
										<!-- End ImgIn -->

									</div>
								</div>
								<!-- End Tab Foto -->

								<!-- Tab Semblanza -->
								<div class="tab-pane card-body" id="Semblanza" role="tabpanel">

									<div class="row">

										<!-- Semblanza -->
										<div class="form-group col-sm-12">
											<label class="col-form-label">Semblanza</label>
											<div class="">
												<textarea
													name="Contenido"
													class="form-control TextArea"
												><?php echo $Expo['Contenido'];?></textarea>
											</div>
										</div>
										<!-- End Titulo -->

									</div>

								</div>
								<!-- End Tab Semblanza -->

							</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>
