<?php include_once "./Configs.php"; ?>
<!DOCTYPE html>
<html lang="en">
<?php
if(isset($_GET['edit']) ){
$titulo="Edit contact";      
$sql="SELECT * FROM amr_contacts WHERE id='".$_GET['edit']."' ORDER BY creacion DESC LIMIT 1;"; 
$ejecutarquery=mysqli_query($link,$sql); 
if(mysqli_num_rows($ejecutarquery)>0){
    $row=mysqli_fetch_assoc($ejecutarquery);
}else{
    $titulo.= " - Id not found! Create a new one instead";
}    
}else{
$titulo="New contact";    
}
include_once "./Head.php"; ?>
    <!-- body start AQUI va el theme mode-->
<?php 
if($usuario["layout"] == 'horizontal'){
?>    
    <body class="loading" data-layout-mode="horizontal" data-layout='{"mode": "<?php echo $usuario["theme"]; ?>", "width": "fluid", "menuPosition": "fixed", "topbar": {"color": "dark"}, "showRightSidebarOnPageLoad": false}'>    
<?php 
}else{
?>    
    <body class="loading" data-layout='{"mode": "<?php echo $usuario["theme"]; ?>", "width": "fluid", "menuPosition": "fixed", "sidebar": { "color": "<?php echo $usuario["theme"]; ?>", "size": "default", "showuser": true}, "topbar": {"color": "light"}, "showRightSidebarOnPageLoad": true}'>
<?php 
}
?>    
        <div id="wrapper">
<?php  include_once "./Header.php"; ?>
<?php  include_once "./Menu.php"; ?>
            <div class="content-page">
                <div class="content">

                    <!-- Start Content-->
                    <div class="container-fluid">

                        <!-- start page title -->
<div class="row">
    <div class="col-12">
        <div class="page-title-box page-title-box-alt">
            <div class="page-title-right">
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Adminto</a></li>
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Tables</a></li>
                    <li class="breadcrumb-item active">Editable</li>
                </ol>
            </div>
            <h4 class="page-title"><?php echo $titulo; ?></h4>
            <p><?php if(isset($warning) ){echo $warning;} ?></p>
        </div>
    </div>
</div>     
<!-- end page title --> 
                        
                                                                  
                        <div class="row">
                            <div class="col-12">
                                <div class="card">
                                    <div class="card-body">
                                        <!--<h5 class="mt-0">New product</h5>-->
                                        <p class="sub-header">Fill in the form and click the "Create" button.</p>
                                        <form method="post" enctype="multipart/form-data"> 
                                            <div class="row">
                                                <div class="col-lg-6">
                                                    <div class="form-floating mb-3">
                                                        <input type="hidden" name="idcambiar" value="<?php if(isset($_GET["edit"]) ){echo $_GET['edit'];}?>">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="nombre" required value='<?php if(isset($row['nombre']) ){echo $row['nombre'];}?>'>
                                                        <label for="floatingPassword">Name</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <select class="form-select" id="floatingSelect" aria-label="Floating label select example" name="empresa" required>
                                                            </select>
                                                        <label for="floatingSelect">Company</label>
                                                        <?php if(isset($row['empresa']) ){echo '<script>jQuery(document).ready(function ($) { setTimeout(function() { $("select[name=empresa]").val('.$row['empresa'].');  }, 1000); });</script>';}?>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="puesto" value='<?php if(isset($row['puesto']) ){echo $row['puesto'];}?>'>
                                                        <label for="floatingPassword">Position</label>
                                                    </div>
                                                    <!--<div class="form-floating mb-3">
                                                        <textarea class="form-control" placeholder="description" id="floatingTextarea2"
                                                                    style="height: 100px" name="descripcion" required></textarea>
                                                        <label for="floatingTextarea2">Description</label>
                                                    </div>-->
                                                    <div class="form-floating mb-3">
                                                        <input type="email" class="form-control" id="floatingPassword" placeholder="Name" name="mail" required value='<?php if(isset($row['mail']) ){echo $row['mail'];}?>'>
                                                        <label for="floatingPassword">E-mail</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="tel" value='<?php if(isset($row['tel']) ){echo $row['tel'];}?>'>
                                                        <label for="floatingPassword">Phone</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="file" accept=".jpg,.png" class="form-control" id="floatingPassword" placeholder="file" name="file" style="padding-top: 35px;">
                                                        <label for="floatingPassword">Profile picture</label>
                                                        <input type="hidden" value="nothing" name="img">
                                                    </div>  
                                                </div>
                                                <div class="col-lg-6">
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="direccion" value='<?php if(isset($row['direccion']) ){echo $row['direccion'];}?>'>
                                                        <label for="floatingPassword">Address</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="ciudad" value='<?php if(isset($row['ciudad']) ){echo $row['ciudad'];}?>'>
                                                        <label for="floatingPassword">City</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="estado" value='<?php if(isset($row['estado']) ){echo $row['estado'];}?>'>
                                                        <label for="floatingPassword">State</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="pais" value='<?php if(isset($row['pais']) ){echo $row['pais'];}?>'>
                                                        <label for="floatingPassword">Country</label>
                                                    </div>
                                                    <div class="form-floating mb-3">
                                                        <input type="text" class="form-control" id="floatingPassword" placeholder="Name" name="cp" value='<?php if(isset($row['cp']) ){echo $row['cp'];}?>'>
                                                        <label for="floatingPassword">Zipcode</label>
                                                    </div>
                                                    <div class="d-grid">
                                                        <button type="submit" name="botonnuevo" class="btn btn-success waves-effect waves-light" value="contacto"><?php if(isset($_GET['edit']) ){echo "Update";}else{echo "Create";}?></button>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div> <!-- end card-body -->
                                </div> <!-- end card -->                               
    <script>jQuery(document).ready(function ($) { 
        /*Reemplaza textos por selects /**/
        <?php echo empreJS(); ?>
        $("select[name=empresa]").each(function() {//o p o span etc...
            let valor = $(this).html();
  //          if(valor.substr(0,7) == "<select"){valor=$(this).children().val();}
            let fila = $(this).attr("fila");
            let columna = $(this).attr("columna");

            let select = `<select class="form-select" fila="`+fila+`" columna="`+columna+`">
            <option disabled selected>----</option>
            `;
            for (var i = 0,  l1 = empre.length; i < l1; i++) {
                    select += '<option value="'+empre[i][0]+'" ';
                    if(empre[i][0]*1===valor*1){
                        select += 'selected';
                    }
                    select += '>'+empre[i][1]+'</option>';
            }
            select += '</select>';
            $(this).html(select);
        });        
    });</script>
                            </div> <!-- end col -->
                        </div> <!-- end row -->

                                
                    </div> <!-- container -->

                </div> <!-- content -->
<?php  include_once "./Footer.php"; ?>
        <!-- Table Editable plugin-->
        <script src="./assets/libs/jquery-tabledit/jquery.tabledit.min.js"></script>
        <!-- Table editable init-->
        <!--<script src="./assets/js/pages/tabledit.init.js"></script>-->


        <!-- third party js - Para las tablas que hacen el PDF-->
        <script src="./assets/libs/datatables.net/js/jquery.dataTables.min.js"></script>
        <script src="./assets/libs/datatables.net-bs5/js/dataTables.bootstrap5.min.js"></script>
        <script src="./assets/libs/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
        <script src="./assets/libs/datatables.net-responsive-bs5/js/responsive.bootstrap5.min.js"></script>
        <script src="./assets/libs/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
        <script src="./assets/libs/datatables.net-buttons-bs5/js/buttons.bootstrap5.min.js"></script>
        <script src="./assets/libs/datatables.net-buttons/js/buttons.html5.min.js"></script>
        <script src="./assets/libs/datatables.net-buttons/js/buttons.flash.min.js"></script>
        <script src="./assets/libs/datatables.net-buttons/js/buttons.print.min.js"></script>
        <script src="./assets/libs/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
        <script src="./assets/libs/datatables.net-select/js/dataTables.select.min.js"></script>
        <script src="./assets/libs/pdfmake/build/pdfmake.min.js"></script>
        <script src="./assets/libs/pdfmake/build/vfs_fonts.js"></script>
        <!-- third party js ends -->

        <!-- Datatables init -->
        <script src="./assets/js/pages/datatables.init.js"></script>
                
            </div>
        </div>        
    </body>
</html>