<?php
session_start();
error_reporting(0);

// Check if already logged in
if (isset($_SESSION['agent_id'])) {
    header("Location: dashboard.php");
    exit();
}

include_once('../include/db_config.php');
include_once('../lib/Agent.class.php');

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $phone = $_POST['phone'];
    $password = $_POST['password'];
    
    if (!empty($phone) && !empty($password)) {
        $agent = new Agent();
        $result = $agent->verifyLogin($phone, $password);
        
        if ($result['success']) {
            $_SESSION['agent_id'] = $result['agent']['id'];
            $_SESSION['agent_name'] = $result['agent']['agent_name'];
            $_SESSION['agent_code'] = $result['agent']['agent_code'];
            
            // Generate simple token for API calls
            $_SESSION['agent_token'] = md5($result['agent']['id'] . $result['agent']['phone']);
            
            header("Location: dashboard.php");
            exit();
        } else {
            $error = $result['message'];
        }
    } else {
        $error = 'Phone and password are required';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Agent Login - MikhMon</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="agent.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
            overflow: hidden;
        }
        .login-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .login-header h1 {
            font-size: 24px;
            margin-bottom: 5px;
        }
        .login-header p {
            opacity: 0.9;
            font-size: 14px;
        }
        .login-body {
            padding: 30px;
        }
        .input-icon {
            position: relative;
        }
        .input-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #999;
        }
        .input-icon .form-control {
            padding-left: 40px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h1><i class="fa fa-users"></i> Agent Login</h1>
            <p>MikhMon Agent/Reseller System</p>
        </div>
        
        <div class="login-body">
            <?php if ($error): ?>
            <div class="alert alert-danger">
                <i class="fa fa-exclamation-circle"></i> <?= $error; ?>
            </div>
            <?php endif; ?>
            
            <form method="POST">
                <div class="form-group">
                    <label>Phone Number</label>
                    <div class="input-icon">
                        <i class="fa fa-phone"></i>
                        <input type="text" name="phone" class="form-control" placeholder="Enter your phone number" required>
                    </div>
                </div>
                
                <div class="form-group">
                    <label>Password</label>
                    <div class="input-icon">
                        <i class="fa fa-lock"></i>
                        <input type="password" name="password" class="form-control" placeholder="Enter your password" required>
                    </div>
                </div>
                
                <button type="submit" class="btn">
                    <i class="fa fa-sign-in"></i> Login
                </button>
            </form>
        </div>
    </div>
</body>
</html>