<?php

require_once __DIR__. '/../common.php';
// include_once __DIR__ . '/helpers/admin-common.php';
require_once __DIR__. '/helpers/SessionManagementService.php';
require_once __DIR__. '/helpers/UserService.php';
require_once __DIR__. '/models/UserModel.php';
require_once __DIR__. '/../my-optician-wsdl-service/MyOpticianWsdlService.php';
require_once __DIR__. '/../my-optician-wsdl-service/GlobalLoader.php';
global $service_url, $optician_id, $shops, $opticianService, $siteInfo, $siteId, $practitioners, $slots;
if(!isset($opticianService)){
	$opticianService = new MyOpticianWsdlService($service_url, $optician_id);
}
$sessionService = new SessionManagementService();
$userService = new UserService();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $userModel = $userService->validateLogin($username, $password);
    // print_r($userModel);
    // die;
    if ($userModel != null) {
        $sessionService->login($userModel);
        // echo $_SESSION['username'];
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Invalid credentials';
        header("Location: index.php?error=$error");
        // include_once( __DIR__ . '/index.php');
    }
} else {
    $error = "Invalid HTTP Method";
    header("Location: index.php?error=$error");
    exit;
}
?>