<?php
session_start();
include ('conexion/config.php');
include ('php/funciones.php');
include ('php/funciones_clientes.php');

if (CheckSesion($_SESSION, 'emailUser') == "1") {

    $session_var = VariablesSession($_SESSION, ['nameUser', 'emailUser', 'UserAdmin']);
    ?>
    <!DOCTYPE html>
    <html lang="es">

    <?php include ('php/head.php'); ?>

    <body>

        <div class='dashboard'>
            <div class="dashboard-nav2">
                <header>        
                    <button class="open-button2" onclick="openFormCrear()">Nuevo Cliente</button>
                </header>
            </div>
            <div class="dashboard-nav">

                <nav class="dashboard-nav-list">

                    <?php CargarListaClientes($con, $_GET["busq"]); ?>
                    <!--
                    <a href="#" class="dashboard-nav-item">
                        <i class="fas fa-home"></i>
                        Home 
                    </a>
                    
                    <a href="#" class="dashboard-nav-item active">
                        <i class="fas fa-tachometer-alt"></i>
                        dashboard
                    </a>
                    
                    <a href="#" class="dashboard-nav-item">
                        <i class="fas fa-file-upload"></i>
                        Upload 
                    </a>

                    <div class='dashboard-nav-dropdown'>
                        <a href="#!" class="dashboard-nav-item dashboard-nav-dropdown-toggle">
                            <i class="fas fa-photo-video"></i>
                            Media 
                        </a>
                            <div class='dashboard-nav-dropdown-menu'>
                                <a href="#" class="dashboard-nav-dropdown-item">All</a>
                                <a href="#" class="dashboard-nav-dropdown-item">Recent</a>
                                <a href="#"	class="dashboard-nav-dropdown-item">Images</a>
                                <a href="#" class="dashboard-nav-dropdown-item">Video</a>
                            </div>
                    </div>


                    <div class="nav-item-divider"></div>
                    <a href="#" class="dashboard-nav-item"><i class="fas fa-sign-out-alt"></i> Salir Sesión </a>
-->
                </nav>
            </div>
            <div class='dashboard-app'>

                <header class='dashboard-toolbar'>
                    <a href="#!" class="menu-toggle menu-desktop">
                        <i class="fas fa-bars"></i>
                    </a>

                    <div class="">

                        <div class="menu-resposive-brown">
                            <ul>
                                <li>
                                    <?php
                                    $texto_admin = VarBoolIntToTexto($session_var[2], " (A)", "");
                                    echo $session_var[0] . $texto_admin;
                                    ?>
                                </li>
                                <li>
                                    <a href="login/salirSesion.php?email=<?php echo $session_var[1]; ?>">
                                        <i class="fas fa-sign-out-alt"></i>
                                        Salir
                                    </a>
                                </li>
                                <li>
                                    <form action="clientes.php" class="">
                                        <input type="text" placeholder="" name="busq" value="" required>
                                        <button type="submit" class="btn">Buscar</button>
                                    </form>
                                </li>
                                <li>
                                    <a href="#!" class="menu-toggle menu-mobil">
                                        <i class="fas fa-bars"> Ver Clientes</i>
                                    </a>
                                </li>
                            </ul>


                        </div>
                    </div>

                </header>

                <div class='dashboard-content'>
                    <div class='container'>
                        <div class='card'>
                            <div class='card-header'>
                                <h1>
                                    <?php
                                    $cliente = GetInfoCliente($con, $_GET["id"]);

                                    if ($cliente['cli_id'] != "") {
                                        echo $cliente['cli_nombre'];
                                    } else {
                                        echo "Selecciona un cliente";
                                    }
                                    ?>
                                </h1>
                            </div>
                            <div class='card-body'>

                                <?php

                                if ($cliente['cli_id'] != "") {
                                    PutInfoCliente('Id', $cliente['cli_id']);
                                    PutInfoCliente('Nombre', $cliente['cli_nombre']);
                                    PutInfoCliente('Dirección', $cliente['cli_direccion']);
                                    PutInfoCliente('Población', $cliente['cli_poblacion']);
                                    PutInfoCliente('CP', $cliente['cli_cp']);
                                    PutInfoCliente('Teléfono', $cliente['cli_tfno']);

                                    echo '<button class="open-button" onclick="openForm()">Editar</button>';
                                }
                                ?>
                                <hr>
                            </div>
                            <div class='card-body'>
                                <?php
                                GetIDContactosCliente($con, $_GET["id"]);                                
                                ?>                                
                            </div>

                            <div class="form-popup" id="myForm">
                                <form action="editar_cliente.php" class="form-container">
                                    <h1>Editar</h1>

                                    <input id="Id" name="Id" type="hidden" value="<?php echo $cliente['cli_id']; ?>" />

                                    <label for="nombre"><b>Nombre</b></label>
                                    <input type="text" placeholder="<?php echo $cliente['cli_nombre']; ?>" name="nombre"
                                        value="<?php echo $cliente['cli_nombre']; ?>" required>
                                    <label for="direccion"><b>Dirección</b></label>
                                    <input type="text" placeholder="<?php echo $cliente['cli_direccion']; ?>"
                                        name="direccion" value="<?php echo $cliente['cli_direccion']; ?>">
                                    <label for="poblacion"><b>Población</b></label>
                                    <input type="text" placeholder="<?php echo $cliente['cli_poblacion']; ?>"
                                        name="poblacion" value="<?php echo $cliente['cli_poblacion']; ?>">
                                    <label for="cp"><b>CP</b></label>
                                    <input type="text" placeholder="<?php echo $cliente['cli_cp']; ?>" name="cp"
                                        value="<?php echo $cliente['cli_cp']; ?>">
                                    <label for="telefono"><b>Teléfono</b></label>
                                    <input type="text" placeholder="<?php echo $cliente['cli_tfno']; ?>" name="telefono"
                                        value="<?php echo $cliente['cli_tfno']; ?>">


                                    <button type="submit" class="btn">Editar</button>
                                    <button type="button" class="btn cancel" onclick="closeForm()">Cerrar</button>
                                </form>
                            </div>

                            <div class="form-popup" id="myFormCrear">
                                <form action="añadir_cliente.php" class="form-container">
                                    <h1>Nuevo Cliente</h1>                                    

                                    <label for="nombre"><b>Nombre</b></label>
                                    <input type="text" placeholder="" name="nombre"
                                        value="" required>
                                    <label for="direccion"><b>Dirección</b></label>
                                    <input type="text" placeholder=""
                                        name="direccion" value="">
                                    <label for="poblacion"><b>Población</b></label>
                                    <input type="text" placeholder=""
                                        name="poblacion" value="">
                                    <label for="cp"><b>CP</b></label>
                                    <input type="text" placeholder="" name="cp"
                                        value="">
                                    <label for="telefono"><b>Teléfono</b></label>
                                    <input type="text" placeholder="" name="telefono"
                                        value="">


                                    <button type="submit" class="btn">Editar</button>
                                    <button type="button" class="btn cancel" onclick="closeFormCrear()">Cerrar</button>
                                </form>
                            </div>


                            


                            <?php
                            PutFormContactoClientes($con, $_GET["id"]);
                            ?>

                            <div class="form-popup" id="myFormContactoCrear">
                                <form action="añadir_contacto.php" class="form-container">
                                    <h1>Crear Contacto</h1>
                                    <input id="IdCli" name="IdCli" type="hidden" value="<?php echo $_GET["id"];?>" />

                                    <label for="nombre"><b>Nombre</b></label>
                                    <input type="text" placeholder="" name="nombre" value="" required>
                                    <label for="movil"><b>Movil</b></label>
                                    <input type="text" placeholder="" name="movil" value="">
                                    <label for="email"><b>Email</b></label>
                                    <input type="text" placeholder="" name="email" value="">

                                    <button type="submit" class="btn">Editar</button>
                                    <button type="button" class="btn cancel"
                                        onclick="closeForm3()">Cerrar</button>
                                </form>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script>
        <script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js'></script>
        <script src="assets/js/script.js"></script>


    </body>

    </html>

    <?php

} else {
    header("location:login/login.php");
}


