<?php
session_start();
require 'config.php';
if(!isset($_SESSION["usuario"])){header("Location: login.php");exit;}

$ret = $_GET['ret'] ?? 'dashboard.php?vista=clientes';
$msg = '';

if($_SERVER['REQUEST_METHOD']==='POST'){
    $nom  = trim($_POST['nombre_cliente_completo'] ?? '');
    $tel  = trim($_POST['telefono'] ?? '');
    $email= trim($_POST['email_cliente'] ?? '');
    $emp  = trim($_POST['empresa_ref_cliente'] ?? '');
    $suc  = $_SESSION['sucursal'];
    $crea = $_SESSION['usuario'];
    if($nom){
        $pdo->prepare("INSERT INTO clientes_tec (nombre_cliente_completo,telefono,email_cliente,empresa_ref_cliente,sucursal,creado_por,fecha_alta_cliente) VALUES (?,?,?,?,?,?,NOW())")
            ->execute([$nom,$tel,$email,$emp,$suc,$crea]);
        header("Location: nueva_orden.php");exit;
    } else {
        $msg = "El nombre es obligatorio.";
    }
}
?><!DOCTYPE html>
<html lang="es"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Nuevo Cliente</title>
<link href="https://fonts.googleapis.com/css2?family=Exo+2:wght@600;700;800&family=Inter:wght@400;500&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:"Inter",sans-serif;background:#EBF5FF;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:20px}
.card{background:#fff;border-radius:16px;border:1px solid #C5DDF9;padding:36px 36px 30px;width:100%;max-width:480px;box-shadow:0 4px 24px rgba(21,101,192,.1)}
h2{font-family:"Exo 2",sans-serif;font-size:20px;font-weight:800;color:#1565C0;margin-bottom:6px}
p{font-size:13px;color:#5A7FA8;margin-bottom:24px}
.fg{margin-bottom:16px}
label{display:block;font-family:"Exo 2",sans-serif;font-size:10px;font-weight:700;letter-spacing:1.5px;text-transform:uppercase;color:#5A7FA8;margin-bottom:6px}
input{width:100%;padding:10px 13px;background:#F5FAFF;border:1.5px solid #C5DDF9;border-radius:8px;font-size:14px;outline:none;transition:border-color .2s}
input:focus{border-color:#1E88E5;background:#fff}
.btns{display:flex;gap:10px;margin-top:20px}
.btn-g{flex:1;padding:12px;background:linear-gradient(135deg,#1565C0,#1E88E5);border:none;border-radius:8px;color:#fff;font-family:"Exo 2",sans-serif;font-size:14px;font-weight:700;cursor:pointer;letter-spacing:1px}
.btn-c{padding:12px 20px;border-radius:8px;border:1.5px solid #C5DDF9;background:#fff;color:#5A7FA8;font-family:"Exo 2",sans-serif;font-size:13px;font-weight:600;text-decoration:none;display:flex;align-items:center}
.err{background:#FFEBEE;border:1px solid #FFCDD2;border-radius:8px;padding:10px 14px;color:#C62828;font-size:13px;margin-bottom:16px}
</style></head><body>
<div class="card">
  <h2><i class="fas fa-user-plus" style="color:#29B6F6;margin-right:8px"></i>Nuevo Cliente</h2>
  <p>Complete los datos para registrar al cliente.</p>
  <?php if($msg): ?><div class="err"><?= htmlspecialchars($msg) ?></div><?php endif; ?>
  <form method="POST">
    <div class="fg"><label>Nombre Completo <span style="color:red">*</span></label><input type="text" name="nombre_cliente_completo" required placeholder="Nombre y apellidos"></div>
    <div class="fg"><label>Teléfono</label><input type="text" name="telefono" placeholder="614-000-0000"></div>
    <div class="fg"><label>Email</label><input type="email" name="email_cliente" placeholder="cliente@correo.com"></div>
    <div class="fg"><label>Empresa / Referencia</label><input type="text" name="empresa_ref_cliente" placeholder="Empresa o cómo nos conoció"></div>
    <div class="btns">
      <button type="submit" class="btn-g"><i class="fas fa-save" style="margin-right:6px"></i>Guardar Cliente</button>
      <a href="<?= htmlspecialchars($ret) ?>" class="btn-c"><i class="fas fa-times" style="margin-right:6px"></i>Cancelar</a>
    </div>
  </form>
</div>
<?php include __DIR__."/theme.php"; ?>
</body></html>
