<?php
// session_start();

include_once 'UsersModel.php';
include_once '../../db/Config.php';
// include_once '../../../resource/sessions.php';

$UsersObj = new UsersModel();
/*
     * Controller file 
     * 
     */
$controlid = filter_input(INPUT_POST, 'status', FILTER_UNSAFE_RAW);
$firstname = filter_input(INPUT_POST, 'firstname', FILTER_UNSAFE_RAW);
$lastname = filter_input(INPUT_POST, 'lastname', FILTER_UNSAFE_RAW);
$jobtitle = filter_input(INPUT_POST, 'jobtitle', FILTER_UNSAFE_RAW);

$usertype = filter_input(INPUT_POST, 'usertype', FILTER_UNSAFE_RAW);

$usergroup = filter_input(INPUT_POST, 'usergroup', FILTER_UNSAFE_RAW);
$tel = filter_input(INPUT_POST, 'tel', FILTER_UNSAFE_RAW);
$dpimage = filter_input(INPUT_POST, 'dpimage', FILTER_UNSAFE_RAW);
$coverimage = filter_input(INPUT_POST, 'coverimage', FILTER_UNSAFE_RAW);
$email = filter_input(INPUT_POST, 'email', FILTER_UNSAFE_RAW);
$username = filter_input(INPUT_POST, 'username', FILTER_UNSAFE_RAW);
$password = filter_input(INPUT_POST, 'password', FILTER_UNSAFE_RAW);
$status = filter_input(INPUT_POST, 'userstatus', FILTER_UNSAFE_RAW);
$ebay_store = filter_input(INPUT_POST, 'ebay_store', FILTER_UNSAFE_RAW);
$amazon_store = filter_input(INPUT_POST, 'amazon_store', FILTER_UNSAFE_RAW);
$dnp = filter_input(INPUT_POST, 'dnp', FILTER_UNSAFE_RAW);
$fbm = filter_input(INPUT_POST, 'fbm', FILTER_UNSAFE_RAW);
$address = filter_input(INPUT_POST, 'address', FILTER_UNSAFE_RAW);





if ($controlid == "loginuser") {
    $username = filter_input(INPUT_POST, 'userid', FILTER_UNSAFE_RAW);
    $password = filter_input(INPUT_POST, 'securecode', FILTER_UNSAFE_RAW);

    //reCAPTCHA validation
    // if (isset($_POST['g-recaptcha-response'])) {

    //     require('../../recaptcha/src/autoload.php');
    //     $secretkey = "6Ldp3X4fAAAAACcKmNC7rDxyb7VCQXdtbqaMD6R6";
    //     $recaptcha = new \ReCaptcha\ReCaptcha($secretkey);

    //     $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

    //     if (!$resp->isSuccess()) {
    //         echo "2| Sorry, Captcha Validation Required!";
    //     } else {
            if ($UsersObj->loginUser($username, $password)) {
                if($UsersObj->getusertype()==101){
                     echo "2|101";
                }
                else{
                echo "1|Success Login";
                }
            } else {
                echo "2|" . $UsersObj->getResult();
            }
    //     }

    //     //echo "2|testing";
    // } else {
    //     echo "2|No captcha";
    // }
}


//*****************Add Users*****************************
if ($controlid == "addUsers") {
    if (!isSession()) {
        echo "2|Session Expired. Please Login Afresh";
    } else {

        $UsersObj->initializeUsers($firstname, $lastname, $usertype, $tel, $email, $username, $password, $ebay_store,$amazon_store,$dnp,$fbm);
        if ($UsersObj->addNewUsers()) {
            echo "1|" . $UsersObj->getResult();
        } else {
            echo "2|" . $UsersObj->getResult();
        }
    }
}
//*****************Edit/Update Users*****************************
if ($controlid == "editUsers") {
    if (!isSession()) {
        echo "2|Session Expired. Please Login Afresh";
    } else {
        $id = filter_input(INPUT_POST, 'id', FILTER_UNSAFE_RAW);
        $UsersObj->initializeUsers($firstname, $lastname, $usertype, $tel, $email, $username, $password, $ebay_store,$amazon_store,$dnp,$fbm);
        if ($UsersObj->updateUsers($id)) {
            echo "1|" . $UsersObj->getResult();
        } else {
            echo "2|Error Ocurred.Try again:" . $UsersObj->getResult();
        }
    }
}
if ($controlid == "reset_pswd") {
    if (!isSession()) {
        echo "2|Session Expired. Please Login Afresh";
    } else {
        $pswd_n = filter_input(INPUT_POST, 'pswd_new', FILTER_UNSAFE_RAW);
        $pswd_repeat = filter_input(INPUT_POST, 'pswd_repeat', FILTER_UNSAFE_RAW);
        $id = filter_input(INPUT_POST, 'userid', FILTER_UNSAFE_RAW);

        $userid = !empty($id) ? $id : $_SESSION['userid_brand'];
        if (!empty($userid)) {

            if ($pswd_n != $pswd_repeat) {
                echo "2|Sorry, Passwords entered do not match";
            } else if ($UsersObj->resetPassword($userid, $pswd_n)) {
                echo "1|" . $UsersObj->getResult();
            } else {
                echo "2|" . $UsersObj->getResult();
            }
        } else {
            echo "2|Sorry, Could not reset password at the moment. Login Afresh or Try again";
        }
    }
}
if ($controlid == "delete") {
    if (!isSession()) {
        echo "2|Session Expired. Please Login Afresh";
    } else {
        $id = filter_input(INPUT_POST, 'id', FILTER_UNSAFE_RAW);
        if ($UsersObj->deleteUsers($id)) {
            echo "1|" . $UsersObj->getResult();
        } else {
            echo "2|Failure: " . $UsersObj->getResult();
        }
    }
}

//returnaddress
    if ($controlid == "returnaddress") {
        if ($UsersObj->returnAddress($address)) {
            
            
            echo "1|Address Submitted";
            
        } else {
            echo "2|Error Ocurred.Try again:";
            
        }
    }
function isSession()
{
    session_start();
    if (!isset($_SESSION["userid_brand"])) {
        echo "2|Session Expired. Please Login Afresh";
        return false;
    }
    return true;
}
