<?php
	$Seccion      = 'Sitios';
	$NameSec      = 'Sitios';
	$SecLine      = array('Configuración', 'Sitios');
	$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" ) {

		$Nombre    		= $_POST['Nombre'];
		$SelUrl     	= $_POST['Url'];
		$ScryptHead   = $_POST['ScryptHead'];
		$ScryptBody   = $_POST['ScryptBody'];
		$ScryptFooter = $_POST['ScryptFooter'];
		$Activo    		= $_POST['Activo'];

	}

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

    $Query = "
			INSERT INTO CMS_Sitios
	    (
				Nombre, Url, Activo,
				ScryptHead, ScryptBody, ScryptFooter
			)VALUES(
				'$Nombre',  '$SelUrl', '$Activo',
				'$ScryptHead', '$ScryptBody', '$ScryptFooter'
			)
		";
    // mysqli_query($mysqli, $Query );
    // $Id = mysqli_insert_id($mysqli);

	}

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

	    $Id             = $_POST['Id'];
	    $Query = "
		    UPDATE CMS_Sitios SET
			    Nombre    		= '$Nombre',
			    Url 					= '$SelUrl',
			    Activo    		= '$Activo',
					ScryptHead    = '$ScryptHead',
					ScryptBody    = '$ScryptBody',
					ScryptFooter  = '$ScryptFooter'
		    WHERE Id  = '$Id'
	    ";
	    mysqli_query($mysqli, $Query );
	    $Save = 'Ok';

	}

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

	//Administrador
	$Query = "
		SELECT
			sit.Id,
			sit.Nombre, sit.Url, sit.Activo,
			sit.ScryptHead, sit.ScryptBody, sit.ScryptFooter
		FROM CMS_Sitios sit
		WHERE sit.Id = '$_GET[Id]'
	";
	$Result = mysqli_query($mysqli, $Query );
	$Sitio = 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">
                  <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>
									<?php if( $Perms[42] ){ ?>
                  	<button type="button" class="btn btn-sm btn-secondary" onclick="window.location.href='edit.php'">Nuevo<i class="fa fa-plus m-l-10"></i></button>
									<?php } ?>
									<button type="submit" class="btn btn-sm btn-success">Guardar<i class="fa fa-check m-l-10"></i></button>
                  <input type="hidden" name="Save" value="Ok">
                  <?php if( $Sitio['Id'] ){ ?>
					<input name="Id" type="hidden" id="Id" value="<?php echo $Sitio['Id']; ?>">
				<?php }else{ ?>
				<input name="New" type="hidden" id="New" value="Ok">
				<?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">
								<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>
								<li class="nav-item">
									<a class="nav-link" data-toggle="tab" href="#Scrypts" role="tab">
										<span class="hidden-sm-up"><i class="ti-user"></i></span>
										<span class="hidden-xs-down">Scrypts</span>
									</a>
								</li>
							</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"><?php if( $Sitio['Id'] ){ echo "Editar"; }else{ echo "Alta de"; } ?> Sitio</h4>
									<hr />
									<div class="row">

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

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

										<!-- Status -->
										<div class="form-group col-sm-12">
											<label class="col-form-label">Activo<span class="text-danger">*</span></label>
											<div class="">
												<select
													class="chosen-select"
													name="Activo"
												>
													<option
														value="1"
														<?php if( $Sitio['Activo'] == '1' ){ echo "selected='selected'"; } ?>
													>
														Si
													</option>
													<option
														value="0"
														<?php if( $Sitio['Activo'] == '0' ){ echo "selected='selected'"; } ?>
													>
														No
													</option>
												</select>
											</div>
										</div>
										<!-- End Status -->

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

								<!-- Tab Permisos -->
								<div class="tab-pane card-body" id="Scrypts" role="tabpanel">
									<h4 class="card-title">Scrypts</h4>
									<hr />
									<div class="row">

										<!-- ScryptHead -->
										<div class="col-sm-12 form-group">
											<label>ScryptHead</label>
											<div>
												<textarea
													class="form-control"
													name="ScryptHead"
													style="height: 150px;"
												><?php echo $Sitio[ScryptHead]; ?></textarea>
											</div>
										</div>
										<!-- End ScryptHead -->

										<!-- ScryptBody -->
										<div class="col-sm-12 form-group">
											<label>ScryptBody</label>
											<div>
												<textarea
													class="form-control"
													name="ScryptBody"
													style="height: 150px;"
												><?php echo $Sitio[ScryptBody]; ?></textarea>
											</div>
										</div>
										<!-- End ScryptBody -->

										<!-- ScryptFooter -->
										<div class="col-sm-12 form-group">
											<label>ScryptFooter</label>
											<div>
												<textarea
													class="form-control"
													name="ScryptFooter"
													style="height: 150px;"
												><?php echo $Sitio[ScryptFooter]; ?></textarea>
											</div>
										</div>
										<!-- End ScryptFooter -->

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

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