<?php
session_start();
include 'assets/includes/db.php';
include 'assets/includes/config.php';

if ($_ERRORS) {
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
}

if (isset($_SESSION['loggedin']) && $_SESSION['loggedin']) {
    header('location: dashboard.php');
}

if (isset($_POST['login'])) {
    $sql = 'SELECT * FROM profile WHERE username = "' . $_POST['username'] . '";';
    $result = $sqlite3->query($sql);

    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $id = $row['id'];
        $username = $row['username'];
        $password = $row['password'];
        $profile_name = $row['profile_name'];
        $avatar_url = $row['avatar_url'];
    }

    if (isset($id)) {
        if ($password == $_POST['password']) {
            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['profile_name'] = $_POST['profile_name'];
            $_SESSION['avatar_url'] = $_POST['avatar_url'];
            
            if (($_POST['username'] == 'admin') && ($_POST['password'] == 'admin')) {
                $_SESSION['loggedin'] = true;
                header('location: profile_edit.php');
            } else {
                $_SESSION['loggedin'] = true;
                header('location: ' . ($DASHBOARD ? 'dashboard.php' : 'profile_edit.php'));
            }
        } else {
            $error = "Contraseña incorrecta";
        }
    } else {
        $error = "Usuario no encontrado";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Xciptv Panel - Login</title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', sans-serif;
        }

        body {
            background: #1a1a1a;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
        }

        .login-container {
            width: 100%;
            max-width: 380px;
            background: #2d2d2d;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
            border: 1px solid #3d3d3d;
        }

        .title {
            text-align: center;
            margin-bottom: 35px;
            color: #fff;
            font-size: 28px;
            font-weight: 600;
            letter-spacing: 0.5px;
            text-transform: uppercase;
        }

        .title span {
            color: #0066ff;
        }

        .error-message {
            background: rgba(255, 68, 68, 0.1);
            color: #ff4444;
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
            text-align: center;
            border: 1px solid rgba(255, 68, 68, 0.2);
        }

        .input-group {
            margin-bottom: 24px;
            position: relative;
        }

        .input-group label {
            display: block;
            margin-bottom: 8px;
            color: #b3b3b3;
            font-size: 14px;
            font-weight: 500;
        }

        .input-group input {
            width: 100%;
            padding: 12px 16px;
            background: #1d1d1d;
            border: 1px solid #3d3d3d;
            border-radius: 8px;
            color: #fff;
            font-size: 15px;
            transition: all 0.3s ease;
        }

        .input-group input:focus {
            border-color: #0066ff;
            outline: none;
            box-shadow: 0 0 0 3px rgba(0, 102, 255, 0.1);
        }

        .login-btn {
            width: 100%;
            padding: 14px;
            background: #0066ff;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 500;
            transition: all 0.3s ease;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .login-btn:hover {
            background: #0052cc;
            transform: translateY(-1px);
        }

        .login-btn:active {
            transform: translateY(1px);
        }

        .footer {
            text-align: center;
            margin-top: 30px;
            color: #666;
            font-size: 14px;
        }

        @media (max-width: 480px) {
            .login-container {
                margin: 20px;
                padding: 30px;
            }
            
            .title {
                font-size: 24px;
            }
        }

        .input-group input:hover {
            border-color: #4d4d4d;
        }

        ::placeholder {
            color: #666;
        }

        ::selection {
            background: rgba(0, 102, 255, 0.2);
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1 class="title">X<span>ciptv</span> Panel</h1>

        <?php if (isset($error)): ?>
            <div class="error-message">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form method="POST">
            <div class="input-group">
                <label for="username">Username</label>
                <input type="text" 
                       id="username" 
                       name="username" 
                       placeholder="Enter your username"
                       required 
                       autocomplete="off">
            </div>

            <div class="input-group">
                <label for="password">Password</label>
                <input type="password" 
                       id="password" 
                       name="password" 
                       placeholder="Enter your password"
                       required>
            </div>

            <button type="submit" name="login" class="login-btn">
                Sign In
            </button>
        </form>

        <?php if ($MasterAPP_BRANDING): ?>
            <div class="footer">
                <?php echo date('Y'); ?> © Rebrands Mx
            </div>
        <?php endif; ?>
    </div>
</body>
</html>