<?php
ob_start();
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once($_SERVER['DOCUMENT_ROOT'].'/core/func.php');

global $uid, $BaseConnect, $user;

// Dəyişənlər
$login_param = isset($_GET['login']) ? number($_GET['login']) : 0;
$password_param = isset($_GET['password']) ? number($_GET['password']) : 0;
$start = isset($_GET['start']) ? number($_GET['start']) : 0;
$ref = isset($_GET['ref']) ? number($_GET['ref']) : 0;
$agreee = isset($_GET['agreee']) ? number($_GET['agreee']) : 0;
$exit = isset($_GET['exit']) ? number($_GET['exit']) : 0;
$rules = isset($_GET['rules']) ? number($_GET['rules']) : 0;
$help = isset($_GET['help']) ? number($_GET['help']) : 0;

$r = rand(100000,900000);

// --- 1. QEYDİYYAT MƏNTİQİ ---
if($start == 1 && !$uid){
    
    $idRef = ($ref >= 1) ? $ref : 0;
    
    $rou = rand(100000,999999);
    $rous = rand(100000,999999);
    
    $password_hash = md5(md5($rous)); 
    $hash = md5(md5($rou));      
    $login = 'Боец_'.$rou;      
    $ip = checking($_SERVER['REMOTE_ADDR']);
    $time_now = time();

    try {
        // DÜZƏLİŞ: `email` sütununu bura əlavə etdim
        $stmt = $BaseConnect->prepare("
            INSERT INTO `users` SET
            `login` = ?,
            `password` = ?,
            `hash_password` = ?,
            `email` = ?, 
            `time_reg` = ?,
            `time_on` = ?,
            `ip` = ?,
            `class` = ?,
            `sex` = ?,
            `start` = ?,
            `ref` = ?
        ");
        
        $result = $stmt->execute(array(
            $login,
            $password_hash,
            $hash,
            '', // Email üçün boş dəyər göndəririk ki, xəta verməsin
            $time_now,
            $time_now,
            $ip,
            1, 
            1, 
            1, 
            $idRef
        ));
        
        $new_uid = $BaseConnect->lastInsertId();

        if ($new_uid > 0) {
            // Uğurlu olduqda
            UserEnter($new_uid, $password_hash);
            unset($_SESSION['u']);
            
            // Xəta yoxdursa, birbaşa yönləndiririk
            header("Location:/?r=".$r);
            exit();

        } else {
            $errorInfo = $stmt->errorInfo();
            die("BAZA XƏTASI: " . print_r($errorInfo, true));
        }

    } catch(PDOException $e) {
        die("PDO Xətası: " . $e->getMessage());
    }
}

// --- HTML BAŞLIQ ---
head("Королевство Лионес",$uid);

// --- YÖNLƏNDİRMƏLƏR ---
if($agreee == 1){ require_once($_SERVER['DOCUMENT_ROOT'].'/about/agreee.php'); foot($uid); exit(); }
if($rules == 1 && $uid){ require_once($_SERVER['DOCUMENT_ROOT'].'/about/rules.php'); foot($uid); exit(); }
if($help == 1 && $uid){ require_once($_SERVER['DOCUMENT_ROOT'].'/about/help.php'); foot($uid); exit(); }
if($exit == 1 && $uid){ UserExit($uid); header('location:?'); exit(); }

// --- GİRİŞ FORMALARI VƏ OYUN ---
if(!$uid){
    if($login_param == 1){
        if(isset($_GET['enter'])){
            $login_in = checking($_POST['login']);
            $password_in = checking($_POST['password']);
            $password_in_hash = md5(md5($password_in));
            $quser = fch("SELECT `id`, `password` FROM `users` WHERE `login` = ? AND `password` = ? LIMIT 1", array($login_in, $password_in_hash));
            if($quser != null){ UserEnter($quser['id'], $quser['password']); header("Location:/?login=1&r=".$r); exit(); } 
            else { err("<span class = 'red'>Неверное имя героя или пароль</span>"); }
        }
        ?>
        <div class="tophead"><? echo logs(); ?>
        <form action="?login=1&enter&r=<?=$r;?>" method="post">
        Имя героя:<br /><input type="text" name="login" maxlength="20" /><br />
        Пароль:<br /><input type="password" name="password" maxlength="20" /><br /><br />
        <input class="landing-login-button" type="submit" value="Войти" /><br />
        </form></div>
        <a class="a" href="/?password=1"><u>Забыли пароль?</u></a>
        <? foot($uid); exit();
    }
    
    // Parol bərpa kodları (Qısa saxladım, əvvəlki kimi qalır)
    if($password_param == 1 || (isset($_GET['id']) && isset($_GET['hash']))) {
         // Buradakı kodları olduğu kimi saxla
    }

    // LANDING PAGE
    ?>
    <div class="main_container" style="height: 763px;">
 <div class="bgpanel">
 <div class="logo"><img src="/images/logo.png"></div>
 <div class="ca_label">Испытай судьбу<br>и стань великим рыцарем!</div>
<a class="enter_button" href="/?start=1&ref=<?=$ref?>"><img src="/images/button_green.png"><span>В бой!</span></a>
 <div class="vmmo_label"><span>Уже есть аккаунт ?</span><a href="/?login=1" class="landing-login-button">Войти</a></div>
 </div>
    </div>
    <?
} else {
    // OYUN DAXİLİ
    if($user['start'] <= 3){ require_once($_SERVER['DOCUMENT_ROOT'].'/templates/start.php'); foot($uid); } 
    elseif($user['start'] >= 4 && $user['start'] <= 9){ require_once($_SERVER['DOCUMENT_ROOT'].'/templates/start2.php'); foot($uid); } 
    else { require_once($_SERVER['DOCUMENT_ROOT'].'/templates/index.php'); foot($uid); }
}
?>