<?php
	$seccion    = 'Clientes';
	$subseccion = 'Clientes';
	$position   = '../../';

	require_once($position.'../switch/access.php');
	require_once($position.'assets/global.php');
	require_once($position.'assets/session.php');
	require_once($position.'assets/funciones.php');

	if (isset($_GET['id'])){$tipoAccion="editar";}else{$tipoAccion="nuevo";}


	/* Usuario */
	if(isset($_POST['save'])== 'Ok'){

	  if (isset($_GET['id'])){ $Id	= $_GET['id']; }
		if(isset($_POST['nombre'])){ $nombre = addslashes(strip_tags($_POST['nombre'])); }
		if (isset($_POST['activo'])){ $activo	= addslashes(strip_tags($_POST['activo'])); }

		$now = now();

	    //-->Nuevo Usuario
	    if($_POST['action'] == 'nuevo'){

        //Save
        $Query = "
					INSERT INTO clientes (
						nombre, activo
					)VALUES (
						'$nombre', '$activo'
					)
				";
        $Result = mysqli_query($mysqli, $Query);
        $Id = mysqli_insert_id($mysqli);

        echo '<script type="text/javascript">document.location.href="index.php?id='.$Id.'&sv=1"</script>';

	    }
	    //END Nuevo Usuario

	    //-->Edita Usuario
	    if($_POST['action'] == 'editar'){

        //Update
        $Query = "
        	UPDATE clientes
					SET
						nombre = '$nombre',
						activo = '$activo'
					WHERE id = '$Id'
				";
        $Result = mysqli_query($mysqli, $Query);

        echo '<script type="text/javascript">document.location.href="index.php?id='.$Id.'&sv=1"</script>';

	    }
	    //END Edita Usuario

	}//End if save
	/* END Usuario  */

	//Query's

	if(isset($_GET['id'])){

	    //QrUsuario
	    $Query = "
				SELECT
					cli.id,
					cli.nombre,
					cli.activo
				FROM clientes cli
				WHERE cli.id = $_GET[id]
			";
	    $Result  = mysqli_query($mysqli, $Query);
	    $Cli   = mysqli_fetch_assoc($Result);

}

?>

<!DOCTYPE html>
<html lang="es_MX">
    <head>
        <meta charset="utf-8" />
        <title>Switch Point | <?php echo $seccion; if(isset($subseccion)) { echo " > ".$subseccion;} ?> </title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <!-- App favicon -->
        <link rel="shortcut icon" href="<?php echo $position; ?>assets/images/favicon.ico">

		<!-- App css -->
		<link href="<?php echo $position; ?>assets/css/bootstrap-corporate.min.css" rel="stylesheet" type="text/css" id="bs-default-stylesheet" />
		<link href="<?php echo $position; ?>assets/css/app-corporate.min.css" rel="stylesheet" type="text/css" id="app-default-stylesheet" />

		<link href="<?php echo $position; ?>assets/css/bootstrap-corporate-dark.min.css" rel="stylesheet" type="text/css" id="bs-dark-stylesheet" />
		<link href="<?php echo $position; ?>assets/css/app-corporate-dark.min.css" rel="stylesheet" type="text/css" id="app-dark-stylesheet" />
        <link href="<?php echo $position; ?>assets/css/style.css" rel="stylesheet" type="text/css" id="app-dark-stylesheet" />

        <!-- Sweet Alert-->
        <link href="<?php echo $position; ?>assets/libs/sweetalert2/sweetalert2.min.css" rel="stylesheet" type="text/css" />

        <!-- icons -->
		<link href="<?php echo $position; ?>assets/css/icons.min.css" rel="stylesheet" type="text/css" />

    </head>

    <body class="loading" data-layout='{"mode": "light", "width": "fluid", "menuPosition": "fixed", "sidebar": { "color": "dark", "size": "default", "showuser": false}, "topbar": {"color": "light"}, "showRightSidebarOnPageLoad": false}'>

        <!-- Pagina de inicio -->
        <div id="wrapper">

            <!-- ========== Inicio nav-izq ========== -->
            <?php include_once($position.'assets/nav.php'); ?>
            <!-- Fin nav-izq -->

            <!-- ============================================================== -->
            <!-- Incio de Contenido -->
            <!-- ============================================================== -->

            <div class="content-page">
                <div class="content">

                    <!-- Topbar -->
                    <?php include_once($position.'assets/topbar.php'); ?>
                    <!-- /Topbar -->

                    <!-- Content-->
                    <div class="container-fluid">
	                    <form method="post" enctype="multipart/form-data" class="parsley-examples" novalidate>

	                        <!-- page title -->
	                        <div class="row">
	                            <div class="col-12">
	                                <div class="page-title-box">
	                                    <h4 class="page-title"><?php echo $seccion; ?></h4>
	                                    <div class="page-title-right">
	                                        <ol class="breadcrumb m-0">
	                                            <li class="breadcrumb-item"><a href="<?php echo $dir; ?>inicio/">Inicio</a></li>
	                                            <li class="breadcrumb-item"><a href="<?php echo $dir; ?>tenderos/usuarios/"><?php echo $seccion; ?></a></li>
	                                            <li class="breadcrumb-item active"><?php echo $subseccion; ?></li>
	                                        </ol>
	                                    </div>
	                                </div>
	                            </div>
	                        </div>
	                        <!-- /page title -->

	                        <!-- secciones  -->
													<!-- Botones -->
													<div class="row">
														<div class="col-12 text-right">
															<button type="button" class="btn btn-white btn-sm" onclick="window.location.href='../'"><i class="fe-arrow-left"></i>&nbsp; Regresar</button>
															<!-- <button type="button" class="btn btn-white btn-sm" onclick="window.location.href='usuario.php'">Nuevo <i class="fe-user-plus"></i></button> -->
															<button type="submit" class="btn btn-primary btn-sm">Guardar <i class="fe-check-circle"></i></button>
														</div>
														<div class="col-12" style="height: 5px;">&nbsp;</div>
													</div>

													<!-- usuarios -->
	                        <div class="row">
	                          <div class="col-md-12">
	                            <div class="card">
	                              <div class="card-body">
	                                <h4 class="header-title">Información General</h4>

	                                <div class="form-row">
                                    <input type="hidden" class="form-control" name="save" value="Ok">
                                    <input type="hidden" class="form-control" name="action" value="<?php echo $tipoAccion; ?>">

																		<!-- Nombre -->
                                    <div class="form-group col-md-12">
                                      <label for="inputNo" class="col-form-label">Nombre<span class="text-danger">*</span></label>
                                      <input
																				type="text"
																				class="form-control"
																				id="inputNo"
																				name="nombre"
																				placeholder="Nombre"
																				value="<?php if(isset($Cli['id'])){ echo $Cli['nombre'];}else{ if(isset( $_GET['nombre'])){ echo $_GET['nombre'];}}?>" required>
                                    </div>
																		<!-- End Nombre -->

																		<!-- Activo -->
																		<div class="form-group col-md-12">
																			<label for="inputNo" class="col-form-label">Activo<span class="text-danger">*</span></label>
																			<select name="activo" class="form-control" required>
																					<option <?php if( $Cli['id']){ if( $Cli['activo'] == 1 ){ echo "selected='selected'"; } } ?> value="1">Activo</option>
																					<option <?php if( $Cli['id']){ if( $Cli['activo'] == 0 ){ echo "selected='selected'"; } } ?> value="0">Inactivo</option>
																			</select>
																		</div>
																		<!-- End Activo -->

	                              </div>
	                          	</div>
	                          </div>
	                        </div>
	                      </div>
	                      <!-- /secciones -->
	                    </form>
                    </div> <!-- /container -->

                </div> <!-- /content -->

                <!-- Footer -->
                <?php include_once($position.'assets/footer.php'); ?>
                <!-- /Footer -->

            </div>

            <!-- ============================================================== -->
            <!-- Fin de Contenido -->
            <!-- ============================================================== -->


        </div>
        <!--  /wrapper -->

        <!-- Vendor js -->
        <script src="<?php echo $position; ?>assets/js/vendor.min.js"></script>

        <!-- App js -->
        <script src="<?php echo $position; ?>assets/js/app.min.js"></script>

        <!-- Plugin js-->
        <script src="<?php echo $position; ?>assets/libs/parsleyjs/parsley.min.js"></script>
        <script src="<?php echo $position; ?>assets/libs/twitter-bootstrap-wizard/jquery.bootstrap.wizard.min.js"></script>

        <!-- Validation init js-->
        <script src="<?php echo $position; ?>assets/js/pages/form-validation.init.js"></script>

        <!-- Sweet Alerts js -->
        <script src="<?php echo $position; ?>assets/libs/sweetalert2/sweetalert2.min.js"></script>

        <!-- Sweet alert init js-->
        <script src="<?php echo $position; ?>assets/js/pages/sweet-alerts.init.js"></script>

        <!-- Plugins js mask -->
        <script src="<?php echo $position; ?>assets/libs/jquery-mask-plugin/jquery.mask.min.js"></script>
        <script src="<?php echo $position; ?>assets/libs/autonumeric/autoNumeric-min.js"></script>

        <!-- Init js mask-->
        <script src="<?php echo $position; ?>assets/js/pages/form-masks.init.js"></script>

        <!-- save -->
        <script>
        <?php if( $_GET['sv'] == 1 ){ ?>
        $( document ).ready(function(){
        Swal.fire({
            //position:"top-end",
            type:"success",
            title:"El Usuario se ha guardado correctamente.",
            showConfirmButton:!1,timer:5000
        });

        });
        <?php } ?>
        <?php if( $_GET['sv'] == 2 ){ ?>
        $( document ).ready(function(){
        Swal.fire({
            type:"error",
            title:"Oops...",
            text:"El No. Telefónico ya fue registrado con anterioridad, ingresa un nuevo teléfono de contacto",
            confirmButtonClass:"btn btn-confirm mt-2",
            //footer:'<a href="">Why do I have this issue?</a>'
        });
        });
        <?php } ?>
        </script>


    </body>
</html>
