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

// Redirect if already logged in
if(isset($_SESSION['customer_login'])){
    header("Location: pin_pass.php");
    exit();
}

if(isset($_POST['submitBtn'])){
    $account_number = mysqli_real_escape_string($con, $_POST['uname']);
    $password_raw = $_POST['pwd'];

    $sql = "SELECT id, account_number, password, verified, email FROM users WHERE account_number='$account_number' LIMIT 1";
    $result = mysqli_query($con, $sql);

    if(mysqli_num_rows($result) == 1){
        $row = mysqli_fetch_assoc($result);
        if(password_verify($password_raw, $row['password'])){
            if($row['verified'] == 0){
                $_SESSION['verify_email'] = $row['email'];
                $error = "Account not verified. <a href='register.php' style='color:inherit; font-weight:bold;'>Verify now</a>";
            } else {
                $_SESSION['customer_login'] = true;
                $_SESSION['cust_id'] = $row['id'];
                $_SESSION['account_number'] = $row['account_number'];
                header("Location: pin_pass.php");
                exit();
            }
        } else {
            $error = "Invalid password. Please try again.";
        }
    } else {
        $error = "Account number not found.";
    }
}

include 'h_header.php'; 
?>

<style>  
body{  
    font-family: 'Inter', sans-serif;  
    background: linear-gradient(135deg,#eef2f7,#f8fbff);  
}  
  
.page-wrap{  
    min-height:100vh;  
    display:flex;  
    justify-content:center;  
    align-items:flex-start;  
    padding-top:80px;  
    padding-bottom: 40px;
}  
  
.card{  
    width:100%;  
    max-width:420px;  
    background:#fff;  
    padding:25px;  
    border-radius:16px;  
    box-shadow:0 10px 30px rgba(0,0,0,0.08);  
}  
  
.header{  
    display:flex;  
    justify-content:space-between;  
    align-items:center;  
    margin-bottom:20px;  
}  
  
/* This matches the 'login-btn' class in your registration script */
.open-account-btn{  
    background:#f1f5f9;  
    color:#0a1f44;  
    padding:10px 15px;  
    border-radius:8px;  
    text-decoration:none;
    font-size: 14px;
    font-weight: 600;
}  
  
input{  
    width:100%;  
    padding:14px;  
    margin-bottom:12px;  
    border-radius:10px;  
    border:1px solid #ddd;  
    box-sizing: border-box; 
    font-size: 15px;
    background-color: #fff;
    font-style: italic; /* Matching the placeholder style in your image */
}  
  
input:focus{  
    border-color:#1565d8;  
    box-shadow:0 0 0 2px rgba(21,101,216,0.1);  
    outline:none;  
}  
  
button{  
    width:100%;  
    padding:14px;  
    background:#1565d8;  
    color:#fff;  
    border:none;  
    border-radius:10px;  
    font-weight:600;  
    cursor: pointer;
    margin-top: 5px;
    font-size: 16px;
}  
  
button:hover{background:#0d47a1;}  
  
.alert{padding:10px;margin-bottom:15px;border-radius:8px;}  
.error{background:#ffe5e5; color: #d32f2f; border: 1px solid #fecaca;}

/* Labels matching the login screenshot */
.field-label {
    display: block;
    text-align: left;
    font-weight: 700;
    color: #475569;
    font-size: 13px;
    margin-bottom: 8px;
}

h2 { 
    text-align: left; 
    color: #0a1f44; 
    font-weight: 800; 
    margin-bottom: 5px; 
}

p.subtitle { 
    text-align: left; 
    color: #64748b; 
    font-size: 14px; 
    margin-bottom: 25px; 
}
</style>  
  
<div class="page-wrap">  
    <div class="card">  
  
        <div class="header">  
           <!------ <img src="assets/logo.png" height="40" alt="Logo">------->  
            <a href="register.php" class="open-account-btn">Open Account</a>  
        </div>  

        <h2>Welcome Back</h2>
        <p class="subtitle">Please enter your account details to continue.</p>
        
        <?php if(isset($error)) echo "<div class='alert error'>$error</div>"; ?>
        
        <form method="post">
            <label class="field-label">Account Number</label>
            <input type="text" name="uname" placeholder="Enter Account Number" required>  
            
            <label class="field-label">Password</label>
            <input type="password" name="pwd" placeholder="••••••••" required>  
            
            <button type="submit" name="submitBtn">Secure Login</button>  
        </form>

        <div style="margin-top: 20px; text-align: center;">
            <a href="#" style="color: #1565d8; text-decoration: none; font-size: 14px; font-weight: 600;">Forgot password?</a>
        </div>
  
    </div>  
</div>  
  
<?php include 'h_footer.php'; ?>
