<?php

//the global file operation
include("../public/impOpreation.php");


//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");



//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");


//user
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');


//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');



//get the do the action
$do = $_GET['do'];


/* ======================

  Controller Name :- userCTRL

  OPERTATION in Controller

  1-display add form
  2- add in user tble
  3-display sucess or error
  4-display show form
  5-delete from user tbl
  6-edit in user data

  ======================== */


//here the global templates
$smarty->display("header.html");



//here goes the instances and general variables
//user
$myUser = new User();
$myUserRecord = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();

//usergroup
$myUserGroup = new Usergroup();
$myUserGroupRecord = new UsergroupMySqlDAO();
$myUserGroupEx = new UsergroupMySqlExtDAO();



//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");

    $UserGroupData = loadusergroup();
    $smarty->assign("UserGroupData", $UserGroupData);

    $smarty->display("userview/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");

    $UserGroupData = loadusergroup();
    $smarty->assign("UserGroupData", $UserGroupData);

    $usergroupId = $_REQUEST['usergroupid'];

    if (isset($usergroupId) && $usergroupId != "-1") {
        $groupData = $myUserGroupRecord->load($usergroupId);
        $message = "المجموعة: " . $groupData->usergroupname;
        $smarty->assign("message", $message);

        showByUsergroupId($usergroupId);
    } else {
        showAll();
    }

    $smarty->display("userview/show.html");
} elseif ($do == "delete") {
    include_once("../public/authentication.php");
    try {
        delete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "deleteFinaly") {
    include_once("../public/authentication.php");
    try {
        deleteFinaly();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    include_once("../public/authentication.php");

    $loadData = edit();
    $smarty->assign("loadData", $loadData);

    $UserGroupData = loadusergroup();
    $smarty->assign("UserGroupData", $UserGroupData);

    //here the smarty templates
    $smarty->display("userview/edit.html");
} elseif ($do == "update") {
    //here the permssion check
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
$smarty->assign("customUser", 1);
//here the global templates
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */

// select from usergroup tbl
function loadusergroup() {

    //to use the variable out side the funcion
    global $myUserGroupRecord;

    // select all data from usergroup tbl
    $UserGroupData = $myUserGroupRecord->queryByConditions(0);

    return $UserGroupData;
}

//select all save data
function loadSave() {

    //to use the variable out side the funcion
    global $mySaveRecord;

    //load all save data
    $saveData = $mySaveRecord->queryByConditions(0);

    return $saveData;
}

//select all save data
function loadStore() {

    //to use the variable out side the funcion
    global $StoreDAO;

    //load all save data
    $StoreData = $StoreDAO->queryByConditions(0);

    return $StoreData;
}

// add in user tbl
function add() {

    //to use the variable out side the funcion
    global $myUser;
    global $myUserRecord;


    $userName = $_POST['username'];
    $passWord = $_POST['password'];
    $userstoreid = 0; //$_POST['userstoreid'];
    $employeeName = $_POST['employeename'];
    $usergroupId = $_POST['usergroupid'];
    $saveId = 0; //$_POST['saveid'];

    $awardrate = 0; //$_POST["awardrate"];

    $searchinonesave = $_POST["searchinonesave"];
    $branchid = 0; //$_POST["branchid"];
    $question = $_POST["question"];
    $answer = $_POST["answer"];


    $myUser->searchinonesave = $searchinonesave;

    //print_r($userstoreid.'xxxxxxxxxxxxxxxx');
    //insert data in user tbl

    if ($branchid == all) {
        $myUser->branchId = -1;
    } else {
        $myUser->branchId = $branchid;
    }

    $myUser->currentdate = date("Y-m-d");
    $myUser->conditions = 0;
    $myUser->employeename = $employeeName;
    $myUser->password = $passWord;
    $myUser->username = $userName;
    $myUser->usergroupid = $usergroupId;
    $myUser->saveid = $saveId;
    $myUser->userstoreid = $userstoreid;
    $myUser->awardrate = $awardrate;

    $myUser->question = $question;
    $myUser->answer = $answer;


    print_r($myUser);
    $myUserRecord->insert($myUser);
}

// show the form
function showByUsergroupId($usergroupId) {
    //to use the variable out side the funcion

    global $myUserRecord;
    global $myUserEx;

    global $smarty;


    // get the all colums number
    $userData = $myUserEx->queryWithGroupIdAndUserId($_SESSION['userid'], $usergroupId);

    // assign your db results to the template
    $smarty->assign('userData', $userData);
}

// show the form
function showAll() {
    //to use the variable out side the funcion

    global $myUserRecord;
    global $myUserEx;

    global $smarty;

    //select all data from user tbl
    $userData = $myUserEx->queryAllWithoutSessionUser($_SESSION['userid']);

    // assign your db results to the template
    $smarty->assign('userData', $userData);
}

//update tempdelete in user tbl
function delete() {
    //to use the variable out side the funcion
    global $myUser;
    global $myUserEx;

    //get variable
    $conditions = $_GET['conditions'];
    $userId = $_GET['userid'];

    if ($conditions == 1) {
        $myUser->conditions = 0;
    } else {
        $myUser->conditions = 1;
    }

    //update conditions in user tbl
    $myUser->userid = $userId;
    $myUser->currentdate = date("Y-m-d");

    $myUserEx->updateConditions($myUser);
}

//finaly deleted from user tbl
function deleteFinaly() {
    //to use the variable out side the funcion
    global $myUserRecord;

    //get variable
    $userId = $_GET['userid'];

    //delete from user tbl
    $myUserRecord->delete($userId);
}

// edit in table
function edit() {
    //to use the variable out side the funcion
    global $myUserRecord;
    global $myUserGroupRecord;

    //get variable
    $userId = $_GET['userid'];

    //load data by usergroupid
    $loadData = $myUserRecord->load($userId);
    //	print_r("<pre>");
    //print_r($loadData);
    //print_r("<pre>");
    return $loadData;
}

// update table
function update() {
    //to use the variable out side the funcion
    global $myUser;
    global $myUserRecord;
    global $myUserEx;
    global $smarty;


    $userName = $_POST['username'];
    $passWord = $_POST['password'];
    $userstoreid = $_POST['userstoreid'];
    $employeeName = $_POST['employeename'];
    $usergroupId = $_POST['usergroupid'];
    $saveId = $_POST['saveid'];
    $userId = $_POST['userid'];
    $currentDate = $_POST['currentdate'];
    $conditions = $_POST['conditions'];
    $awardrate = $_POST["awardrate"];
    $branchid = $_POST["branchid"];

    $question = $_POST["question"];
    $answer = $_POST["answer"];

    $searchinonesave = $_POST["searchinonesave"];
    $myUser->searchinonesave = $searchinonesave;

    if ($branchid == all) {
        $myUser->branchId = -1;
    } else {
        $myUser->branchId = $branchid;
    }
    //insert data in user tbl
    $myUser->currentdate = $currentDate;
    $myUser->awardrate = $awardrate;
    $myUser->conditions = $conditions;
    $myUser->employeename = $employeeName;
    $myUser->password = $passWord;
    $myUser->username = $userName;
    $myUser->usergroupid = $usergroupId;
    $myUser->saveid = $saveId;
    $myUser->branchId = $branchid;
    $myUser->userid = $userId;
    $myUser->userstoreid = $userstoreid;

    $myUser->question = $question;
    $myUser->answer = $answer;

    $myUserRecord->update($myUser);

    /*     * ******************************** fatma younis ********************************************** */
    $validationResult = $myUserEx->validationUser($userName, $passWord);

    //if there no result in the arry send it to the login form
    if (count($validationResult) <= 0) {
        // the login form is empty
        $smarty->display("login.html");
    } else {
        /* $_SESSION['userid'] = $validationResult->userid;
          $_SESSION['usergroupid']=$validationResult->usergroupid;
          $_SESSION['username']=$validationResult->username;
          $_SESSION['usergroupname']=$validationResult->usergroupname;
          $_SESSION['saveid']=$validationResult->saveid;
          $_SESSION['employeename']=$validationResult->employeename;
          $_SESSION['searchinonesave']=$validationResult->searchinonesave;
         */ $_SESSION['branchId'] = $validationResult->branchId;

        /* $_SESSION['dbname'] = $db; */
    }
    /*     * ****************************************************************************** */
}

?>