<?php 
session_start();
include '_inc/dbconn.php';

// Check if user is logged in
if(!isset($_SESSION['customer_login'])) {
    header('location:index.php');
    exit();
}

$cust_id = $_SESSION['cust_id'];

// Handle Pin Submission
if(isset($_POST['submitPin'])){
    $inputpin = mysqli_real_escape_string($con, $_POST['inpin']);
    
    /* LOGIC FIX: 
       1. Table name changed to 'users'
       2. Column name changed to 'account_pin' per your SQL dump
    */
    $sql = "SELECT id, account_pin, notify_status FROM users WHERE id='$cust_id' LIMIT 1";
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));
    $user = mysqli_fetch_assoc($result);
    
    if($user && $inputpin === $user['account_pin']) {
        
        // Check for Account Status (Using notify_status as a proxy for Dormant if applicable)
        // or check if you have a specific 'status' column.
        if(isset($user['status']) && $user['status'] == 'Dormant'){
            header('location:dormant.php');
            exit();
        } else {
            // Success: Direct to Dashboard
            header('location:customer_account_summary.php'); 
            exit();
        }
    } else {
        // Fail: Invalid PIN
        echo "<script>alert('The Pin you have entered is incorrect.');</script>";
    }
}

/* DATA FETCHING FOR PAGE DISPLAY
   Ensuring variables match the 'users' table columns
*/
$sql_display = "SELECT * FROM users WHERE id='$cust_id' OR account_number='$cust_id' LIMIT 1";
$res_display = mysqli_query($con, $sql_display) or die(mysqli_error($con));
$rws = mysqli_fetch_assoc($res_display);

$name            = $rws['fullname'];       
$account_no      = $rws['account_number']; 
$email           = $rws['email'];          
$account_balance = $rws['balance'];        
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Nexus Vault | PIN Verification</title>

    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        :root {
            --primary: #0066cc;
            --secondary: #00d4ff;
            --bg-dark: #0a192f;
            --glass: rgba(255, 255, 255, 0.98);
        }

        body {
            font-family: 'Inter', sans-serif;
            background: var(--bg-dark);
            margin: 0;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .bg-gradient {
            position: fixed;
            top: 0; left: 0; width: 100%; height: 100%;
            background: linear-gradient(135deg, #0a192f 0%, #112240 100%);
            z-index: -1;
        }

        .login-wrapper {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 40px 20px;
        }

        .login-card {
            background: var(--glass);
            width: 100%;
            max-width: 400px;
            padding: 40px;
            border-radius: 24px;
            box-shadow: 0 25px 50px rgba(0,0,0,0.5);
            text-align: center;
        }

        .logo-area { margin-bottom: 30px; }
        .logo-area img { height: 45px; }

        .security-icon {
            font-size: 40px;
            color: var(--primary);
            margin-bottom: 20px;
        }

        h2 { font-weight: 800; color: #0a192f; margin: 0 0 10px 0; font-size: 24px; }
        p.subtitle { color: #64748b; margin-bottom: 30px; font-size: 14px; line-height: 1.5; }

        .form-group { margin-bottom: 25px; position: relative; text-align: center; }
        
        /* Modern PIN Input Styling */
        .pin-input {
            width: 100%;
            padding: 16px;
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            box-sizing: border-box;
            font-size: 24px;
            text-align: center;
            letter-spacing: 15px;
            font-weight: bold;
            transition: all 0.3s ease;
            background: #f8fafc;
            color: #0a192f;
        }

        .pin-input:focus {
            border-color: var(--primary);
            background: #fff;
            outline: none;
            box-shadow: 0 0 0 4px rgba(0, 102, 204, 0.1);
        }

        .btn-verify {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, var(--primary) 0%, #004499 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-weight: 700;
            font-size: 16px;
            cursor: pointer;
            transition: 0.3s;
            box-shadow: 0 10px 20px rgba(0, 102, 204, 0.2);
        }

        .btn-verify:hover {
            transform: translateY(-2px);
            box-shadow: 0 15px 25px rgba(0, 102, 204, 0.3);
        }

        .footer {
            padding: 25px;
            text-align: center;
            color: rgba(255,255,255,0.5);
            font-size: 13px;
        }

        /* Error state styling */
        .alert-error {
            background: #fff1f2;
            color: #be123c;
            padding: 12px;
            border-radius: 10px;
            margin-bottom: 20px;
            font-size: 13px;
            border: 1px solid #ffe4e6;
        }
    </style>
</head>
<body>
    <div class="bg-gradient"></div>

    <div class="login-wrapper">
        <div class="login-card">
            <div class="logo-area">
                <a href="index.php"><img src="images/logo.png" alt="Nexus Vault Logo"></a>
            </div>

            <div class="security-icon">
                <i class="fas fa-user-shield"></i>
            </div>
            
            <h2>PIN Verification</h2>
            <p class="subtitle">For your security, please enter your 4-digit transaction PIN to proceed.</p>

            <?php if(isset($error)): ?>
                <div class="alert-error">
                    <i class="fas fa-exclamation-triangle"></i> <?php echo $error; ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST">
                <div class="form-group">
                    <input type="password" 
                           name="inpin" 
                           class="pin-input" 
                           placeholder="••••" 
                           maxlength="4" 
                           pattern="\d*" 
                           inputmode="numeric" 
                           autocomplete="one-time-code"
                           required>
                </div>
                
                <button type="submit" name="submitPin" class="btn-verify">
                    Authorize Session
                </button>
            </form>
        </div>
    </div>

    <footer class="footer">
        &copy; 2026 Nexus Vault Secure Systems. <br>
        <span style="opacity: 0.7; font-size: 11px;">End-to-End Encrypted Verification.</span>
    </footer>
</body>
</html>
