<?php
// var_dump($obj->getAllData("tbl_agent", [['ag_id','>',0]]));
// var_dump($obj->getAllData("tbl_agent"));

// var_dump($obj->getSingleData("tbl_agent", [['ag_id','>',0]]));
// var_dump($obj->getSingleData("tbl_agent"));

// var_dump($obj->totalCount("tbl_agent", [['ag_id','>',0]]));
// // var_dump($obj->totalCount("tbl_agent"));

$messageNotice = "";
$date = date('Y-m-d');
$offdates = '2030-12-12';
$bnotice = $obj->getSingleData("tbl_notice", [['user_id', '=', 0]]);
$totacntuser = $obj->totalCount("tbl_agent");
$totalcountuser = ($totacntuser) ? $totacntuser : 0;

if ($bnotice) {
    if ($bnotice['trail_date'] >= $date) {
        $offdates = $bnotice['trail_date'];
        $trairemaining = floor((strtotime($bnotice['trail_date']) - time()) / 86400);
        if ($trairemaining < 7) {
            $trairemaining = $trairemaining + 1;
            $messageNotice = "Trail period will be end within $trairemaining days. Before Inactive contact with support " . $bnotice['disconnected_message'];
        }
    } elseif (($bnotice['next_disconnected_date'] >= $date) && ($bnotice['free_customer_limit'] <= $totalcountuser)) {
        $offdates = $bnotice['next_disconnected_date'];
        $disremaining = floor((strtotime($bnotice['next_disconnected_date']) - time()) / 86400);
        if ($disremaining < 7) {
            $disremaining = $disremaining + 1;
            $messageNotice = "Please Pay your software bill within $disremaining days. Before Inactive contact with support " . $bnotice['disconnected_message'];
        }
    } elseif (($bnotice['next_disconnected_date'] < $date) && ($bnotice['free_customer_limit'] <= $totalcountuser)) {
        $offdates = $bnotice['next_disconnected_date'];
        $messageNotice = "Please Pay your software bill to active your account. Contact with support " . $bnotice['disconnected_message'];
        session_destroy();
        // header("location: login.php");
    }
}


$date = strtotime("$offdates 4:00 PM"); //provide disconnect date    
$remaining = $date - time();
$days_remaining = floor($remaining / 86400);
$days_rem_pos = abs($days_remaining);
$hours_remaining = floor(($remaining % 86400) / 3600);
$hours_remaining_pos = abs($hours_remaining);
if ($days_remaining <= 0  && $hours_remaining == 0 || $hours_remaining < 0) {
    $amethod = "";
    $etype = "hidden";
    $ptype = "hidden";
    $rtype = "hidden";
    $battr = "hidden";
} else {
    $amethod = "POST";
    $etype = "text";
    $ptype = "password";
    $rtype = "checkbox";
    $battr = "";
}

if (isset($_POST['login']) ? $_POST['login'] : NULL) {

    if ((!isset($_POST['username']) || empty($_POST['username'])) || (!isset($_POST['password']) || empty($_POST['password']))) {
        $obj->notificationStore("User Name or Password Must Not be empty", 'danger');
        header('Location: ?page=login');
        exit();
    }
    $username = isset($_POST['username']) ? $obj->validation($_POST['username']) : NULL;
    $password = isset($_POST['password']) ? $obj->validation($_POST['password']) : NULL;

    $username = stripcslashes(trim($username));
    $password = stripcslashes(trim($password));
    $remember = (isset($_POST['remember'])) ? true : false;

    $users =  $obj->login_check("vw_user_info", $username, md5($password));
    $re_users =  $obj->login_check("_reseller_createuser", $username, md5($password));

    if ($users) {
        $_SESSION["login"] = true;
        $_SESSION['userid'] = $users['UserId'];
        $_SESSION['username'] = $users['UserName'];
        $_SESSION['usertype'] = $users['UserType'];
        $_SESSION['userfullname'] = $users['FullName'];
        $_SESSION['userimage'] = $users['PhotoPath'];

        if ($remember) {
            setcookie("login", true, time() + (1 * 24 * 60 * 60)); // 1 days
            setcookie("userid", $users['UserId'], time() + (1 * 24 * 60 * 60));
            setcookie("username", $users['UserName'], time() + (1 * 24 * 60 * 60));
            setcookie("usertype", $users['UserType'], time() + (1 * 24 * 60 * 60));
            setcookie("userfullname", $users['FullName'], time() + (1 * 24 * 60 * 60));
            setcookie("userimage", $users['PhotoPath'], time() + (1 * 24 * 60 * 60));
        }
        // $obj->notificationStore("Login Success", 'success');
        header('Location: ?page=dashboard');
        
        
        // create activity log for delete payment info
        $obj->createActivityLog(
            $users['UserId'],
            '1',
            '1',
            null,
            null,
            null,
            null,
            $users['FullName'] . ' Logged In Successfully.',
            null,
            null,
            true,
            true,
            null
        );
    }elseif ($re_users) {

       $userId = (int)$re_users['UserId'];

        $userId = (int)$userId;

        $reAccess = $obj->details_by_cond("_re_useraccess", "UserId='$userId'");
       
        $_SESSION["login"] = true;
        $_SESSION['userid'] = $re_users['UserId'];
        $_SESSION['username'] = $re_users['UserName'];
        $_SESSION['usertype'] = $reAccess['UserType'];
        $_SESSION['userfullname'] = $re_users['FullName'];
        $_SESSION['userimage'] = $re_users['PhotoPath'];

        if ($remember) {
            setcookie("login", true, time() + (1 * 24 * 60 * 60)); // 1 days
            setcookie("userid", $re_users['UserId'], time() + (1 * 24 * 60 * 60));
            setcookie("username", $re_users['UserName'], time() + (1 * 24 * 60 * 60));
            setcookie("usertype", $reAccess['UserType'], time() + (1 * 24 * 60 * 60));
            setcookie("userfullname", $re_users['FullName'], time() + (1 * 24 * 60 * 60));
            setcookie("userimage", $re_users['PhotoPath'], time() + (1 * 24 * 60 * 60));
        }
        
        
        $result = $obj->resellerHistoryLog([
            're_id'        => $re_users['UserId'],
            'header'       => 'login',
            'descriptions' => 'Reseller Logged In Successfully.',
            'type'         => 'login',
        ]);
        
        // var_dump($result);
        
        // exit();
            
        // $obj->notificationStore("Login Success", 'success');
        header('Location: ?page=reseller-dashboard');
        
        
        // create activity log for delete payment info
        
    } else {
        $obj->notificationStore("Your User Credentials are Invalid", 'danger');
        header('Location: ?page=login');
        exit();
    }
}
