<?php
// Confirmar sesión
session_start();

if (!isset($_SESSION['loggedin'])) {
    header('Location: index.php');
    exit;
}

include_once('conexion.php');

//function limpiar($data) {
//    return htmlspecialchars(trim($data));
//}

if (isset($_POST['guardar'])) {
    if (!empty($_POST['nomclr'])) {
        $nomclr = limpiar($_POST['nomclr']);
        $mailclr = limpiar($_POST['mailclr']);
        $telclr = limpiar($_POST['telclr']);
        
        $sql = "INSERT INTO cliente (nomclr, mailclr, telclr) VALUES ('$nomclr', '$mailclr', '$telclr')";
        if (mysqli_query($conexion, $sql)) {
            header('location:cliente.php');
        } else {
            echo "Error al registrar el cliente: " . mysqli_error($conexion);
        }
    } else {
        echo "<p style='color:red; text-align:center;'>Por favor, complete todos los campos requeridos.</p>";
    }
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" charset="UTF-8">
    <title>Registro de Clientes</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body class="loggedin">
    <nav class="navtop">
        <h1 style="color:white;">Registro de Clientes</h1>
        <table width="35%" border="0" style="width: 200px;">
            <tr>
                <td><a href="cliente.php" style="color:white;">Regresar</a></td>
                <td><a target="_top" href="inicio.php" style="color:white;"><i class="fas fa-user-circle"></i>Cerrar</a></td>
            </tr>
        </table>
    </nav>
    <br><br>
    <div id="nuevo">
        <form name="form1" method="post" enctype="multipart/form-data" action="">
            <table align="center">
                <tr>
                    <td><strong>Nombre del Cliente *</strong></td>
                    <td><input type="text" name="nomclr" autocomplete="off" required></td>
                </tr>
                <tr>
                    <td><strong>Correo Electrónico </strong></td>
                    <td><input type="text" name="mailclr" autocomplete="off" ></td>
                </tr>
                <tr>
                    <td><strong>Teléfono </strong></td>
                    <td><input type="text" name="telclr" autocomplete="off"></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <button type="submit" class="btn" name="guardar"><strong>Guardar</strong></button>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>
