<?php
session_start();

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

include("../conexion.php");

$id = intval($_GET['id']);

$sql = "SELECT * FROM cliente WHERE id_cliente = $id";
$resultado = mysqli_query($conexion, $sql);
$fila = mysqli_fetch_assoc($resultado);

if (!$fila) {
    header("Location: listar.php");
    exit();
}

$error = isset($_GET['error']) ? $_GET['error'] : '';
?>

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editar Cliente</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
    :root {
        --primary: #EC407A;
        --primary-dark: #D81B60;
        --primary-light: #FCE4EC;
    }
    
    body {
        background: linear-gradient(135deg, #FFF5F7 0%, #FCE4EC 100%);
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        padding: 20px;
    }
    
    .card-modern {
        background: white;
        border-radius: 24px;
        padding: 40px 45px;
        max-width: 700px;
        width: 100%;
        box-shadow: 0 20px 60px rgba(236, 64, 122, 0.15);
        border: 1px solid rgba(236, 64, 122, 0.05);
        transition: all 0.3s ease;
        margin: 0 auto;
    }
    .card-modern:hover {
        box-shadow: 0 25px 70px rgba(236, 64, 122, 0.2);
        transform: translateY(-2px);
    }
    
    .card-header-custom {
        text-align: center;
        margin-bottom: 35px;
        padding-bottom: 25px;
        border-bottom: 2px solid #f0f0f0;
        position: relative;
    }
    .card-header-custom .icon-circle {
        width: 70px;
        height: 70px;
        background: linear-gradient(135deg, #EC407A, #D81B60);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 15px;
        font-size: 28px;
        color: white;
        box-shadow: 0 8px 25px rgba(236, 64, 122, 0.3);
    }
    .card-header-custom h2 {
        font-weight: 700;
        color: #2d3748;
        font-size: 1.8rem;
        margin: 0;
    }
    .card-header-custom p {
        color: #718096;
        font-size: 0.95rem;
        margin: 5px 0 0 0;
    }
    
    .form-group-custom {
        margin-bottom: 20px;
    }
    .form-group-custom .form-label {
        font-weight: 600;
        color: #2d3748;
        font-size: 0.9rem;
        margin-bottom: 6px;
        display: flex;
        align-items: center;
        gap: 8px;
    }
    .form-group-custom .form-label i {
        color: #EC407A;
        width: 18px;
    }
    .form-group-custom .form-label .required-star {
        color: #EC407A;
        font-weight: 700;
    }
    
    .form-group-custom .form-control {
        border-radius: 12px;
        border: 2px solid #e8ecf1;
        padding: 12px 16px;
        transition: all 0.3s ease;
        background: #fafbfc;
        font-size: 0.95rem;
    }
    .form-group-custom .form-control:focus {
        border-color: #EC407A;
        background: white;
        box-shadow: 0 0 0 4px rgba(236, 64, 122, 0.12);
    }
    .form-group-custom .form-control::placeholder {
        color: #b0b8c4;
        font-size: 0.9rem;
    }
    
    .form-group-custom .input-icon-wrapper {
        position: relative;
    }
    .form-group-custom .input-icon-wrapper i {
        position: absolute;
        left: 14px;
        top: 50%;
        transform: translateY(-50%);
        color: #a0aec0;
        font-size: 1rem;
        transition: all 0.3s ease;
        pointer-events: none;
    }
    .form-group-custom .input-icon-wrapper .form-control {
        padding-left: 42px;
    }
    .form-group-custom .input-icon-wrapper .form-control:focus + i {
        color: #EC407A;
    }
    
    .btn-group-custom {
        display: flex;
        gap: 12px;
        margin-top: 30px;
        flex-wrap: wrap;
    }
    
    .btn-primary-custom {
        background: linear-gradient(135deg, #EC407A, #D81B60);
        border: none;
        padding: 14px 35px;
        border-radius: 12px;
        font-weight: 600;
        transition: all 0.3s ease;
        color: white;
        font-size: 0.95rem;
        letter-spacing: 0.3px;
        box-shadow: 0 4px 15px rgba(236, 64, 122, 0.3);
        flex: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
    }
    .btn-primary-custom:hover {
        background: linear-gradient(135deg, #D81B60, #c2185b);
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(236, 64, 122, 0.4);
        color: white;
    }
    
    .btn-secondary-custom {
        border-radius: 12px;
        padding: 14px 30px;
        font-weight: 600;
        transition: all 0.3s ease;
        border: 2px solid #e2e8f0;
        background: white;
        color: #4a5568;
        flex: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        text-decoration: none;
    }
    .btn-secondary-custom:hover {
        background: #f7fafc;
        border-color: #cbd5e0;
        transform: translateY(-2px);
        color: #2d3748;
    }
    
    .row-custom {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 18px;
    }
    
    .help-text {
        font-size: 0.8rem;
        color: #a0aec0;
        margin-top: 4px;
        display: flex;
        align-items: center;
        gap: 4px;
    }
    .help-text i {
        font-size: 0.7rem;
    }
    
    .info-box {
        background: linear-gradient(135deg, #fdf2f8, #fce4ec);
        border-radius: 14px;
        padding: 16px 20px;
        border-left: 4px solid #EC407A;
        margin-bottom: 25px;
        display: flex;
        align-items: center;
        gap: 12px;
        flex-wrap: wrap;
    }
    .info-box i {
        color: #EC407A;
        font-size: 1.3rem;
    }
    .info-box .info-text {
        color: #4a5568;
        font-size: 0.92rem;
        margin: 0;
    }
    .info-box .info-text strong {
        color: #2d3748;
    }
    .info-box .badge-id {
        background: rgba(236, 64, 122, 0.15);
        color: #D81B60;
        padding: 4px 14px;
        border-radius: 20px;
        font-weight: 600;
        font-size: 0.85rem;
        margin-left: auto;
    }
    
    .alert-modern {
        border-radius: 14px;
        padding: 16px 20px;
        border: none;
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 20px;
        animation: slideDown 0.4s ease;
    }
    .alert-modern.alert-danger {
        background: #fff5f5;
        border-left: 4px solid #e53e3e;
        color: #c53030;
    }
    .alert-modern.alert-danger i {
        color: #e53e3e;
        font-size: 1.2rem;
    }
    .alert-modern.alert-success {
        background: #f0fff4;
        border-left: 4px solid #38a169;
        color: #276749;
    }
    .alert-modern .btn-close {
        margin-left: auto;
    }

    @keyframes slideDown {
        from { opacity: 0; transform: translateY(-10px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .page-header-custom {
        margin-bottom: 28px;
    }
    .page-header-custom h2 {
        font-weight: 700;
        font-size: 1.7rem;
        color: #2d3748;
        display: flex;
        align-items: center;
        gap: 12px;
    }
    .page-header-custom h2 i {
        color: #EC407A;
        background: #fce4ec;
        padding: 10px;
        border-radius: 14px;
        font-size: 1.4rem;
    }
    .page-header-custom .btn-back {
        border-radius: 12px;
        padding: 10px 24px;
        font-weight: 500;
        transition: all 0.3s ease;
        background: #f7fafc;
        border: 2px solid #e2e8f0;
        color: #4a5568;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }
    .page-header-custom .btn-back:hover {
        background: #edf2f7;
        transform: translateY(-2px);
        color: #2d3748;
    }

    @media (max-width: 600px) {
        .card-modern {
            padding: 25px 20px;
        }
        .row-custom {
            grid-template-columns: 1fr;
        }
        .btn-group-custom {
            flex-direction: column;
        }
        .btn-primary-custom, .btn-secondary-custom {
            flex: none;
            width: 100%;
        }
        .card-header-custom h2 {
            font-size: 1.4rem;
        }
        .info-box {
            flex-direction: column;
            text-align: center;
        }
        .info-box .badge-id {
            margin-left: 0;
        }
    }
</style>
</head>
<body>

<div class="card-modern">
    <!-- Header -->
    <div class="card-header-custom">
        <div class="icon-circle">
            <i class="fas fa-user-edit"></i>
        </div>
        <h2><i class="fas fa-user-edit me-2" style="color:#EC407A;"></i>Editar Cliente</h2>
        <p>Actualiza los datos del cliente</p>
    </div>

    <!-- Info Box -->
    <div class="info-box">
        <i class="fas fa-info-circle"></i>
        <span class="info-text">
            <strong>Editando cliente:</strong> <?php echo htmlspecialchars($fila['nombre_razon']); ?>
        </span>
        <span class="badge-id">
            <i class="fas fa-hashtag"></i> ID #<?php echo $fila['id_cliente']; ?>
        </span>
    </div>

    <!-- Mensajes de error -->
    <?php if($error == 'dni_existente'): ?>
    <div class="alert-modern alert-danger">
        <i class="fas fa-exclamation-circle"></i>
        <span>El <strong>DNI/RUC</strong> ya está registrado por otro cliente.</span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <?php if($error == 'email_existente'): ?>
    <div class="alert-modern alert-danger">
        <i class="fas fa-exclamation-circle"></i>
        <span>El <strong>correo electrónico</strong> ya está registrado por otro cliente.</span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <?php if($error == 'campos_vacios'): ?>
    <div class="alert-modern alert-danger">
        <i class="fas fa-exclamation-triangle"></i>
        <span>Por favor, <strong>completa todos los campos</strong> del formulario.</span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <?php if($error == 'email_invalido'): ?>
    <div class="alert-modern alert-danger">
        <i class="fas fa-envelope"></i>
        <span>El <strong>correo electrónico</strong> no es válido.</span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <?php if($error == 'error_actualizacion'): ?>
    <div class="alert-modern alert-danger">
        <i class="fas fa-exclamation-triangle"></i>
        <span><strong>Error</strong> al actualizar el cliente. Intenta nuevamente.</span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    <?php endif; ?>

    <!-- Formulario -->
    <form action="actualizar.php" method="POST">
        <input type="hidden" name="id_cliente" value="<?php echo $fila['id_cliente']; ?>">

        <!-- DNI/RUC -->
        <div class="form-group-custom">
            <label class="form-label">
                <i class="fas fa-id-card"></i> DNI  <span class="required-star">*</span>
            </label>
            <div class="input-icon-wrapper">
                <i class="fas fa-id-card"></i>
                <input type="text" class="form-control" name="dni_ruc" 
                       value="<?php echo htmlspecialchars($fila['dni_ruc']); ?>" required>
            </div>
        </div>

        <!-- Nombre / Razón Social -->
        <div class="form-group-custom">
            <label class="form-label">
                <i class="fas fa-user"></i> Nombre  <span class="required-star">*</span>
            </label>
            <div class="input-icon-wrapper">
                <i class="fas fa-user"></i>
                <input type="text" class="form-control" name="nombre_razon" 
                       value="<?php echo htmlspecialchars($fila['nombre_razon']); ?>" required>
            </div>
        </div>

        <!-- Dirección + Teléfono -->
        <div class="row-custom">
            <div class="form-group-custom">
                <label class="form-label">
                    <i class="fas fa-map-marker-alt"></i> Direccion <span class="required-star">*</span>
                </label>
                <div class="input-icon-wrapper">
                    <i class="fas fa-map-marker-alt"></i>
                    <input type="text" class="form-control" name="direccion" 
                           value="<?php echo htmlspecialchars($fila['direccion']); ?>" required>
                </div>
            </div>

            <div class="form-group-custom">
                <label class="form-label">
                    <i class="fas fa-phone"></i> Telefono <span class="required-star">*</span>
                </label>
                <div class="input-icon-wrapper">
                    <i class="fas fa-phone"></i>
                    <input type="text" class="form-control" name="telefono" 
                           value="<?php echo htmlspecialchars($fila['telefono']); ?>" required>
                </div>
            </div>
        </div>

        <!-- Email -->
        <div class="form-group-custom">
            <label class="form-label">
                <i class="fas fa-envelope"></i> Correo Electrónico <span class="required-star">*</span>
            </label>
            <div class="input-icon-wrapper">
                <i class="fas fa-envelope"></i>
                <input type="email" class="form-control" name="email" 
                       value="<?php echo htmlspecialchars($fila['email']); ?>" required>
            </div>
        </div>

        <!-- Botones -->
        <div class="btn-group-custom">
            <button type="submit" class="btn-primary-custom">
                <i class="fas fa-save"></i> Actualizar Cliente
            </button>
            <a href="listar.php" class="btn-secondary-custom">
                <i class="fas fa-times"></i> Cancelar
            </a>
        </div>
    </form>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
