<?php
require_once '../connection/db.php';
require('../PHPMailer/EncrptyModel.php');

$d = date("Y-m-d");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');

    $passObj = new passEncrypt();
    $encPass = $passObj->twoPassEncrypt($password);

    try {

        $db = getDB();

        $stmt = $db->prepare("
            SELECT * 
            FROM meddolic_user_details 
            WHERE user_id = ? 
            AND password = ? 
            AND user_type = 1
            LIMIT 1
        ");

        $stmt->execute([$username, $encPass]);
        $val = $stmt->fetch();

        if (!$val) {
            ?>
            <script>
                alert("Incorrect Userid or Password Login!!!\nPlease Try Again With Valid Id Password!!!");
                window.top.location.href = "index.php";
            </script>
            <?php
        } else {

            $_SESSION['admin_id'] = $val['member_id'];
            $_SESSION['admin_user_id'] = $val['user_id'];
            $_SESSION['admin_password'] = $encPass;

            ?>
            <script>
                window.top.location.href = 'dashboard.php';
            </script>
            <?php
        }

    } catch (PDOException $e) {

        echo "Database Error: " . $e->getMessage();
    }
}
?>