<?php

	$Position         = '../../';
	$Seccion          = 'Facturacion';
	$SectionTitle     = 'Logotipo';
	$SecLine          = array('Facturacion','Logotipo');
  $SeccionSis 			= '5';

	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/resize-class.php');

	//require_once($Position.'funciones-php/facturacion-datos-logo-guardar.php');

	if( $_POST['SaveLogo'] == 'Ok' ){

    $Action = addslashes($_POST['Action']);

    if( $Action == '1' ){


    }elseif( $Action == '2' ){

      $Id 			= $_POST['Id'];
			$UsarLogo = $_POST['UsarLogo'];

			//Update
			$Query = "
        UPDATE FacturasPerfiles
        SET
          UsarLogo = '$UsarLogo'
        WHERE Id = '$Id'
					AND Fk_Cuenta = '$Cuenta[Id]'
      ";
      $Result = mysqli_query($mysqli, $Query);

      //Guarda Imagen
      $Imagen = "image";
      if(empty($_FILES[$Imagen]['name'])){}else{
        $_FILES[$Imagen]["name"];
        $allowedExts = array("JPG", "jpg");
        $temp = explode(".", $_FILES[$Imagen]["name"]);
        $extension = end($temp);

        if( $extension == 'jpg' || $extension == 'JPG' ){

          //RootSite del Sitio Seleccionado
          $ImageUp = $RootSite.'/documentos/logos/'.$Id.'.jpg';

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

          //if ( copy($_FILES["image"]["tmp_name"], $ImageUp) ){
            //echo "Img Up";
            chmod($ImageUp, 0777);
            //Ajuste de tamaño
            $width = getWidth($ImageUp);
            $height = getHeight($ImageUp);
            $max_width = "1800";
            if ($width > $max_width){
              $scale = $max_width/$width;
              $resizeObj = new resize($ImageUp);
              $resizeObj -> resizeImage(400, 400, 'crop');
              $resizeObj -> saveImage($ImageUp, 75);
            }else{
              $scale = 1;
              $resizeObj = new resize($ImageUp);
              $resizeObj -> resizeImage(400, 400, 'crop');
              $resizeObj -> saveImage($ImageUp, 75);
            }
          }else{
            //echo "Img Not<br>";
            print_r(error_get_last());
          }

        }else{
        }
      }
      //End Guarda Imagen

    }

    //if( $IdPla ){
      $Saved = 'Ok';
    //}
  }


	//Perfil
  $Query = "
    SELECT
      per.Id,
      per.Fk_TipoPersona, per.Fk_Giro, per.Fk_RegimenFiscal, per.Fk_TamanoEmpresa,
      per.NombreComercial, per.NombreFiscal, per.Rfc, per.Email, per.Telefono,
			per.UsarLogo
    FROM FacturasPerfiles per
    WHERE per.Fk_Cuenta = '$Cuenta[Id]'
			AND per.Id = '$PerfilSel[Id]'
  ";
  $Result = mysqli_query($mysqli, $Query);
  $Per    = 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="row page-titles page-titles-fixed" style="margin: 0px;">

				<?php require_once($Position.'funciones-modulos/section-title.php'); ?>

				<!-- Botones -->
				<div class="col-md-7 align-self-center text-right">

					<div class="btn-group" role="group" aria-label="Basic example">

						<!-- Btn Save -->
						<button
							type="button"
							class="Btn-Save btn btn-sm btn-success"
							data-form="Logo"
						>
							Guardar <i class="fa fa-check m-l-5"></i>
						</button>
						<!-- End Btn Save -->

					</div>
				</div>
				<!-- End Botones -->
			</div>

			<!-- Box1 de Navegación -->
			<div class="P-t-InnerContet">

			  <!-- Menu Promoción -->
			  <div class="SubMenuOut">

			    <ul>
			      <?php
			        //MenuSeccion
			        $Query = "
			          SELECT
			            menu.Id, menu.Nombre, menu.Opc, menu.Modulo,
									menu.UrlDirecto,
			            menu.Form, menu.Pasos
			          FROM MenuSecciones menu
			          WHERE menu.Fk_SeccionSis = '$SeccionSis'
			            AND menu.Activo = '1'
			          ORDER BY menu.Orden
			        ";
			        $SubMenuItems = $mysqli->query($Query);
			        mysqli_data_seek($SubMenuItems, 0);
			        while($SubMenuItem = mysqli_fetch_array($SubMenuItems)) {
			          $ItemActivo = 0;
			      ?>
			        <li
			          data-opc="<?php echo $SubMenuItem[Opc]; ?>"
			          data-modulo="<?php echo $SubMenuItem[Modulo]; ?>"
			          data-form="<?php echo $SubMenuItem[Form]; ?>"
			          class="<?php
			            $SubMenuItem[Pasos];
			            $Pasos = explode(',',$SubMenuItem[Pasos]);
			            foreach($Pasos as $Paso){
			              if( $Paso == $Wizard[IdPaso] ){ $ItemActivo = 1; }
			            }
			            if( $ItemActivo == 1 ){ echo "OpcSubMenu active"; }
			          ?>"
			        >
								<?php if( $SubMenuItem[UrlDirecto] ){ ?>
									<a href="<?php echo $SubMenuItem[UrlDirecto]; ?>" style="color:#fff;">
								<?php } ?>
			          	<?php echo $SubMenuItem[Nombre]; ?>
								<?php if( $SubMenuItem[UrlDirecto] ){ ?>
									</a>
								<?php } ?>
							</li>
			      <?php } ?>
			    </ul>
			  </div>
			  <!-- End Menu Promoción -->

			  <!-- Box Interno -->
			  <div class="BoxInterno">
			    <div class="BoxInternoIn">

						<form
						  method="post"
						  enctype="multipart/form-data"
						  id="Form-Logo"
						>

						<input type="hidden" name="Save" value="Ok" />
						<input type="hidden" name="SaveLogo" value="Ok" />
						<input
					    type="hidden"
					    name="Action"
					    value="<?php if( $Per[Id] ){ echo '2'; }else{ echo '1'; } ?>"
					  />
					  <?php if($Per[Id]){ ?>
					    <input type="hidden" name="Id" value="<?php echo $Per[Id]; ?>" />
					  <?php } ?>

							<!-- Card -->
							<div class="card">
								<div class="card-body">
									<h4 class="card-title">Logotipo</h4>
									<hr>

									<div class="row">

										<!-- Imagen -->
										<div class="col-sm-4">

											<?php
				                $Image = $RootSite.'/documentos/logos/'.$Per['Id'].'.jpg';
				                if (file_exists($Image)) {
				              ?>
				                <div class="m-b-10">
				                  <img class="img-responsive" src="<?php
				                    $Image = $RootSite.'/documentos/logos/'.$Per['Id'].'.jpg';
				                    if (file_exists($Image)) {
				                      echo $UrlSite.'/documentos/logos/'.$Per['Id'].'.jpg';
				                    }else{
				                      //echo $UrlSite.'/images/platillos/0.jpg';
				                    }
				                  ?>">
				                </div>
				              <?php } ?>

										</div>
										<!-- End Imagen -->

										<div class="col-sm-8">

											<label for="input-file-now-custom-1">
				                Imagen de 400 px x 400 px; <strong>Solo se acepta formato jpg.</strong>
				              </label>

											<!-- Input Imagen -->
											<input
												accept=".jpg"
												name="image"
												type="file"
												id="input-file-now-custom-1"
												class="dropify"
											/>
											<!-- End Input Imagen -->

										</div>

										<!-- Usar Logo -->
										<div class="col-sm-6">
											<label>Usar logotipo en facturas</label>
											<div class="controls">
												<select
													name="UsarLogo"
													data-required="true"
													class="form-control chosen-select"
												>
													<option
														value="0"
														<?php if( $Per[UsarLogo] == 0 ){ echo "selected='selected'"; } ?>
													>
														No
													</option>
													<option
														value="1"
														<?php if( $Per[UsarLogo] == 1 ){ echo "selected='selected'"; } ?>
													>
														Si
													</option>
												</select>
											</div>
										</div>
										<!-- End Usar Logo -->

									</div>

								</div>
							</div>
							<!-- End Card -->

						</form>

			    </div>
			  </div>
			  <!-- End Box Interno -->

			</div>
			<!-- End Box1 de Navegación -->


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

    </div>

  </div>

  <?php require_once($Position.'scripts.php'); ?>
	<?php //require_once($Position.'funciones-js/perfil.php'); ?>
  <script>

	//Guarda
	$(document.body).on('click', '.Btn-Save' ,function(){
		var form = $(this).data('form');

		console.log('Formulario: '+form);

			//Formulario Logo
			if( form == 'Logo' ){

				console.log('FormLogo');

				formulario = '#Form-Logo';
				$(formulario).submit();

			}
			//End Formulario Logo

		});
		//End Guardar

  </script>
</body>

</html>
