<!--
/* Coded by Ing. Ulises Morales Ramirez */
-->
<?php
include 'conexion.php';

$sql = "SELECT * FROM Inicial";
$result0 = $conn->query($sql);
$sql = "SELECT * FROM Preescolar";
$result1 = $conn->query($sql);
$sql = "SELECT * FROM Primaria";
$result2 = $conn->query($sql);
$sql = "SELECT * FROM Secundaria";
$result3 = $conn->query($sql);
$sql = "SELECT * FROM Media";
$result4 = $conn->query($sql);
$sql = "SELECT * FROM Superior";
$result5 = $conn->query($sql);

/*
if ($result->num_rows > 0) {
	// output data of each row
	while($row = $result->fetch_assoc()) {
		echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
	}
} else {
	echo "0 results";
}
$conn->close();*/
?>
<!DOCTYPE html>
<html>



<head>


    <title>Mi Huella por la Paz - Yucatán</title>

    <!-- Favicon -->
    <link rel="shortcut icon" href="favicon.ico?v2">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=0">
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Lato"/>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css"
          integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="ownStyle.css" media="screen"/>


</head>

<body onload="getLocation()">

<!--div class="text-center" style="z-index: -1"><img src="images/header.png" width="800px"></div-->
<div class="ripple-background">
    <div class="circle xxlarge shade1"></div>
    <div class="circle xlarge shade2"></div>
    <div class="circle large shade3"></div>
    <div class="circle mediun shade4"></div>
    <div class="circle small shade5"></div>
</div>


<div class="container col-md-12" style="margin-top: 10%">

    <div class="d-flex justify-content-center  col-md-12 col-sm-12 col-xs-12">
        <div class="user_card">
            <div class="d-flex justify-content-center">
                <div class="brand_logo_container">
                    <img src="images/logo_geo.png" class=" visible-md brand_logo " alt="Logo">
                </div>
                <div class="brand_logo_container1">
                    <img src="images/g980.png" class=" hidden visible-xs visible-sm brand_logo1 " alt="Logo">
                </div>
                <div class="brand_logo_container2">
                    <img src="images/g1010.png" class="visible-md brand_logo1 " alt="Logo">
                </div>
            </div>
            <div class=" justify-content-center container form_container">

                <form method="post" action="lenguaje.php">

                    <div class="input-group">
                        <div class="input-group-append">
                            <span class="input-group-text"><i class="fas fa-search"></i></span>
                        </div>
                        <input type="text" autocomplete="on" list="Escuelas" name="escuela" id="input_escuela"
                               class="form-control custom-select" value=""
                               placeholder="Seleccione la clave o nombre de su escuela" required ">
                        <input type="hidden" name="clave_escuela" id="clave_escuela">
                        <input type="hidden" name="latitude" id="latitude" >
                        <input type="hidden" name="longitude" id="longitude" >
                    </div>

                    <datalist id="Escuelas">
						<?php
						while ($row = $result0->fetch_assoc()) {
							echo '<option value="' . utf8_encode($row["CV_CCT"]) . '">' . $row["NOMBRECT"]. ' <br>(' . $row["C_NOM_LOC"]. ')</option>';
							//echo '<option value="'.utf8_encode($row["NOMBRECT"]).'">de '.utf8_encode($row["C_NOM_LOC"]).' <br>clave: '.utf8_encode($row["CV_CCT"]).'</option>';
						}
						while ($row = $result1->fetch_assoc()) {
							echo '<option value="' . utf8_encode($row["CV_CCT"]) . '">' . $row["NOMBRECT"] . ' <br>(' . $row["C_NOM_LOC"] . ')</option>';
							//echo '<option value="'.utf8_encode($row["NOMBRECT"]).'">de '.utf8_encode($row["C_NOM_LOC"]).' <br>clave: '.utf8_encode($row["CV_CCT"]).'</option>';
						}
						while ($row = $result2->fetch_assoc()) {
							echo '<option value="' . utf8_encode($row["CV_CCT"]) . '">' . $row["NOMBRECT"] . ' <br>(' . $row["C_NOM_LOC"] . ')</option>';
						}
						while ($row = $result3->fetch_assoc()) {
							echo '<option value="' . utf8_encode($row["CV_CCT"]) . '">' . $row["NOMBRECT"] . ' <br>(' . $row["C_NOM_LOC"] . ')</option>';
						}
						while ($row = $result4->fetch_assoc()) {
							echo '<option value="' . utf8_encode($row["CV_CCT"]) . '">' .$row["NOMBRECT"] . ' <br>(' . $row["LOCALIDAD"] . ')</option>';
						}
						while ($row = $result5->fetch_assoc()) {
							//echo '<option value="'.utf8_encode($row["CV_CCT"]).'">'.utf8_encode($row["NOMBRECT"]).'</option>';
							echo '<option value="' . utf8_encode($row["CV_CCT"]) . '">' . $row["NOMBRECT"] . ' <br>(' . $row["LOCALIDAD"] . ')</option>';
						}
						?>
                    </datalist>

				<?php
				if (isset($_GET['err'])) {
					echo "<div style='margin:10px;' class=\"alert alert-danger alert-dismissible \"> <strong>Seleccione una escuela de la lista!</strong> </div>";
				}
				?>                    

                    <div class="" style="color: white;">
                        <div class="mt-4">
                            <div class="d-flex text-center justify-content-center links" id="titulo1">
                            </div>
                        </div>
                        <div class="mt-4">
                            <div class="d-flex text-center justify-content-center links">
                                <h1 id="nombre_escuela"></h1>
                            </div>
                        </div>
                        <div class="mt-4">
                            <div class="d-flex text-center justify-content-center links" id="titulo2">
                            </div>
                        </div>
                        <div class="mt-4">
                            <div class="d-flex text-center justify-content-center links">
                                <h3 id="clave"></h3>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex justify-content-center mt-3 login_container">
                        <button type="submit" id="next" class="btn login_btn" disabled>Siguiente</button>
                    </div>
                </form>
            </div>
        </div>

    </div>
    <div class="text-center" style="margin:40px;">
        <img class="img-fluid img-responsive" src="images/Logo-Horizontal-V2-blanco.png" width="200"><br>

            <div class="text-center" style="margin:40px;">
                <img class="img-fluid img-responsive" src="images/g122.png" width="300">
            </div>

    </div>
        <script>
            $("#input_escuela").bind('input', function () {
                var dato = $('#input_escuela').val();
                $('#clave_escuela').val($('#input_escuela').val());
                document.getElementById('clave_escuela').value = $('#input_escuela').val();
                var valor = checkExists($('#input_escuela').val());
                //alert(valor);
                var lastword = valor.split(" ").pop();
                $('#nombre_escuela').empty();
                $('#clave').empty();
                $('#titulo1').empty();
                $('#titulo2').empty();
                valor = valor.replace(')', '');
                var porciones = valor.split(' (');

                document.getElementById('input_escuela').value = porciones[0];
                $('#nombre_escuela').append('' + porciones[0]);
                $('#titulo1').append('<h1><b>Escuela: </b></h1>');
                $('#titulo2').append('<h3><b>Localidad:</b></h3>');
                $('#clave').append(porciones[1]);

                $.post("ajax_post.php",
                    {
                        dato: dato
                    },
                    function (data, status) {
                        if (data == "1") {
                            $('#next').attr("disabled", false);
                        } else {
                            $('#next').attr("disabled", true);
                        }
                    });

            });

            function checkExists(inputValue) {
                var x = document.getElementById("Escuelas");
                var i;
                var flag;
                for (i = 0; i < x.options.length; i++) {
                    if (inputValue == x.options[i].value) {
                        flag = x.options[i].text;
                    }

                }
                return flag;
            }

            function getLocation() {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(showPosition);
                } else {
                    x.innerHTML = "Geolocation is not supported by this browser.";
                }
            }

            function showPosition(position) {
                $("#latitude").val(position.coords.latitude);
                $("#longitude").val(position.coords.longitude);
            }

        </script>

</body>
</html>