<?php
require_once __DIR__ . '/header.php';

// Redirect if already logged in
if (isLoggedIn()) {
    if ($_SESSION['user_type'] == 'admin') {
        redirect('admin/index.php');
    } elseif ($_SESSION['user_type'] == 'seller') {
        redirect('seller/index.php');
    } else {
        redirect('dashboard.php');
    }
}

$error = '';
$success = '';

if (isset($_SESSION['flash_success'])) {
    $success = $_SESSION['flash_success'];
    unset($_SESSION['flash_success']);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $mobile = sanitize($_POST['mobile']);
    $password = $_POST['password'];
    
    // Check if user exists
    $user = $db->select("SELECT * FROM users WHERE mobile = ?", [$mobile]);
    
    if (empty($user)) {
        $error = $lang->translate('mobile_not_registered', 'এই মোবাইল নম্বরটি নিবন্ধিত নয়। অনুগ্রহ করে প্রথমে রেজিস্টার করুন।');
    } elseif (array_key_exists('deleted_at', $user[0]) && !empty($user[0]['deleted_at'])) {
        // Soft-deleted by an admin: the row is still there, but the account is closed
        $error = $lang->translate('account_deleted', 'এই অ্যাকাউন্টটি বন্ধ করে দেওয়া হয়েছে। সাহায্যের জন্য সাপোর্টে যোগাযোগ করুন।');
    } else {
        // Check if the account is approved (status = 1)
        if ($user[0]['status'] == 0) {
            $error = $lang->translate('account_pending', 'আপনার অ্যাকাউন্টটি এখনও অ্যাডমিন দ্বারা অনুমোদিত হয়নি।');
        } 
        // Verify Password
        elseif (password_verify($password, $user[0]['password'])) {
            $_SESSION['user_id'] = $user[0]['id'];
            $_SESSION['user_name'] = $user[0]['name'];
            $_SESSION['user_mobile'] = $user[0]['mobile'];
            $_SESSION['user_type'] = $user[0]['user_type']; 
            
            // Role Based Redirection
            if ($user[0]['user_type'] == 'admin') {
                redirect('admin/index.php');
            } elseif ($user[0]['user_type'] == 'seller') {
                redirect('seller/index.php');
            } else {
                redirect('dashboard.php');
            }
        } else {
            $error = $lang->translate('wrong_password', 'পাসওয়ার্ড ভুল!');
        }
    }
}
?>

<div class="min-h-screen bg-gray-50 flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-md w-full space-y-8 bg-white rounded-2xl shadow-xl p-8">
        <div class="text-center">
            <img src="<?php echo $company_logo; ?>" alt="Logo" class="h-16 w-auto mx-auto mb-4">
            
            <h2 class="text-3xl font-extrabold text-gray-900"><?php echo $lang->translate('login', 'লগইন'); ?></h2>
            <p class="mt-2 text-sm text-gray-600">
                <?php echo $lang->translate('or', 'অথবা'); ?> 
                <a href="register.php" class="font-medium text-blue-600 hover:text-blue-500">
                    <?php echo $lang->translate('create_new_account', 'নতুন অ্যাকাউন্ট তৈরি করুন'); ?>
                </a>
            </p>
        </div>
        
        <?php if ($error): ?>
        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">
            <span class="block sm:inline"><?php echo $error; ?></span>
        </div>
        <?php endif; ?>
        
        <?php if ($success): ?>
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative" role="alert">
            <span class="block sm:inline"><?php echo $success; ?></span>
        </div>
        <?php endif; ?>
        
        <form class="mt-8 space-y-6" method="POST">
            <div class="space-y-4">
                <div>
                    <label for="mobile" class="block text-sm font-medium text-gray-700 mb-2"><?php echo $lang->translate('mobile_number', 'মোবাইল নম্বর'); ?></label>
                    <input id="mobile" name="mobile" type="tel" required 
                           class="appearance-none rounded-lg relative block w-full px-3 py-3 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           placeholder="<?php echo $lang->translate('enter_10_digit', '১০ সংখ্যার নম্বর দিন'); ?>">
                </div>

                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700 mb-2"><?php echo $lang->translate('password', 'পাসওয়ার্ড'); ?></label>
                    <input id="password" name="password" type="password" required 
                           class="appearance-none rounded-lg relative block w-full px-3 py-3 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                           placeholder="<?php echo $lang->translate('enter_password', 'পাসওয়ার্ড দিন'); ?>">
                    <div class="flex justify-end mt-2">
                        <a href="forgot-password.php" class="text-sm font-medium text-blue-600 hover:text-blue-500"><?php echo $lang->translate('forgot_password', 'পাসওয়ার্ড ভুলে গেছেন?'); ?></a>
                    </div>
                </div>
            </div>
            
            <div>
                <button type="submit" 
                        class="group relative w-full flex justify-center py-3 px-4 border border-transparent text-sm font-medium rounded-lg text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                    <?php echo $lang->translate('login', 'লগইন'); ?>
                </button>
            </div>
        </form>
    </div>
</div>

<?php require_once __DIR__ . '/footer.php'; ?>