<?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
include("../public/include_dao.php");
//properities
require_once('../models/dao/PropertiesDAO.class.php');
require_once('../models/dto/Propertie.class.php');
require_once('../models/mysql/PropertiesMySqlDAO.class.php');
require_once('../models/mysql/ext/PropertiesMySqlExtDAO.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');
//Relusergroupproperties
require_once('../models/dao/RelusergrouppropertiesDAO.class.php');
require_once('../models/dto/Relusergrouppropertie.class.php');
require_once('../models/mysql/RelusergrouppropertiesMySqlDAO.class.php');
require_once('../models/mysql/ext/RelusergrouppropertiesMySqlExtDAO.class.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');

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

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

  Controller Name :- usergroupCTRL

  OPERTATION in Controller

  1-load property data from properities tble
  2-display add form
  2- add in usergroup and relusergroupproperties tble
  3-display sucess or error
  4-display show form
  5-delete from usergroup tble
  6-edit in usergroup data

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

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

//here goes the instances and general variables
$myProperitiesRecord = new PropertiesMySqlDAO();

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

$myRelusergroup = new Relusergrouppropertie();
$myRelusergroupRecord = new RelusergrouppropertiesMySqlDAO();
$myRelusergroupEx = new RelusergrouppropertiesMySqlExtDAO();

$myUserRecord = new UserMySqlDAO();

//check and use the condetion that suite this action
if (empty($do)) {
    $data = loadproperity();

    $smarty->assign("properityData", $data[0]);
    $smarty->assign("properityNum", $data[1]);

    /* $mantanceData = propmantance();
      print_r($mantanceData); */
    //here the smarty templates
    $smarty->display("usergroupview/add.html");
} elseif ($do == "add") {
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    $userGroupData = show();
    $smarty->assign("userGroupData", $userGroupData);
    $smarty->display("usergroupview/show.html");

    $smarty->assign('customUsergroup', 1);
    $smarty->assign('customCheck', 1);
} elseif ($do == "execute") {
    try {
        executeOperation();
        $userGroupData = show();
        $smarty->assign("userGroupData", $userGroupData);
        $smarty->display("usergroupview/show.html");

        $smarty->assign('customUsergroup', 1);
        $smarty->assign('customCheck', 1);
    } catch (Exception $e) {
        //echo $e;
        $smarty->display("error.html");
    }
} elseif ($do == "returndelete") {//returndelete
    include_once("../public/authentication.php");
    try {
        returndelete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "tempdelete") {//tempdelete
    include_once("../public/authentication.php");
    try {
        $note = tempdelete();
        if ($note != "success") {
            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "deleteFinaly") {
    try {
        $note = deleteFinaly();
        if ($note != "success") {
            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        echo $e;
        //header("location:?do=error");
    }
} elseif ($do == "edit") {
    $editData = edit();
    /* print_r('<pre>');
      print_r($editData[1]); */
    $smarty->assign("loadData", $editData[0]);
    //print_r('<pre>');
    $smarty->assign("properityData", $editData[1]);
    $smarty->assign("properityNum", $editData[2]);

    $smarty->display("usergroupview/edit.html");
} elseif ($do == "update") {
    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "editprint") {
    $editData = edit();
    $smarty->assign("loadData", $editData[0]);
    $smarty->assign("properityData", $editData[1]);
    $smarty->assign("properityNum", $editData[2]);

    $smarty->display("usergroupview/editprint.html");
} elseif ($do == "sucess") {
    $smarty->display("succes.html");
} elseif ($do == "error") {
    $smarty->display("error.html");
}

//here the global templates
$smarty->assign('customValidation', 1);

$smarty->display("footer.html");

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

// select from properity tbl
function loadproperity() {
    global $myProperitiesRecord;

    // select all data from properity tbl
    $properityData = $myProperitiesRecord->queryAll();
    //get count of selected Data
    $properityNum = count($properityData);
    return array($properityData, $properityNum);
}

function propmantance() {
    global $myProperitiesRecord;

    // select all data from properity tbl
    $mantanceData = $myProperitiesRecord->queryByPropertyparent(164);

    return $mantanceData;
}

// add in usergroup tbl
function add() {
    global $myUserGroup;
    global $myUserGroupRecord;
    global $myRelusergroup;
    global $myRelusergroupRecord;

    $userGroupName = $_POST['usergroupname'];
    $userGroupDescription = $_POST['usergroupdescription'];
    $startPage = $_POST['startpage'];
    $level = $_POST['level'];


    $savedailydate = $_POST['savedailydate'];
    $hidemenu = $_POST['hidemenu'];
    $hidecat = $_POST['hidecat'];


    $itr = $_POST['itr'];


    $clientnegative = $_POST['clientnegative'];
    $suplliernegative = $_POST['suplliernegative'];
    $storenegative = $_POST['storenegative'];
    $priceupdate = $_POST['priceupdate'];
    $directDiscount = $_POST['directDiscount'];
    $percentageDiscount = $_POST['percentageDiscount'];

    //insert data in usergroup tbl
    $myUserGroup->priceupdate = $priceupdate;
    $myUserGroup->directDiscount = $directDiscount;
    $myUserGroup->percentageDiscount = $percentageDiscount;

    $myUserGroup->clientnegative = $clientnegative;
    $myUserGroup->suplliernegative = $suplliernegative;
    $myUserGroup->storenegative = $storenegative;

    $myUserGroup->currentdate = date("Y-m-d");
    $myUserGroup->conditions = 0;
    $myUserGroup->usergroupname = $userGroupName;
    $myUserGroup->startpage = $startPage;
    $myUserGroup->level = $level;
    $myUserGroup->savedailydate = $savedailydate;
    $myUserGroup->usergroupdescription = $userGroupDescription;
    $myUserGroup->hidemenu = $hidemenu;
    $myUserGroup->hidecat = $hidecat;

    $userGroupId = $myUserGroupRecord->insert($myUserGroup);

    //insert data in relusergroupproperties tbl
    $h = 1;
    for ($h = 1; $h <= $itr; $h++) {
        $propertyValue = $_POST['propertydefault' . $h . ''];
        $propertyId = $_POST['propertyid' . $h . ''];

        $myRelusergroup->propertyid = $propertyId;
        $myRelusergroup->propertyvalue = $propertyValue;
        $myRelusergroup->usergroupid = $userGroupId;

        $myRelusergroupRecord->insert($myRelusergroup);
    }
}

// show the form
function show() {
    global $myUserGroupEx;

    //select all data from usergroup tbl
    $userGroupData = $myUserGroupEx->queryAllwithOrder();
    return $userGroupData;
}

function executeOperation() {
    global $myUserGroupRecord;
    global $myUserGroupEx;
    global $smarty;
    global $outputString;

    $operationType = $_POST['operation'];

    $choosedItemArr = $_POST['choosedItem'];
    if (count($choosedItemArr) > 0) {
        $i = 1;
        foreach ($choosedItemArr as $usergroupid) {
            if ($operationType == '1') {//tempdelete
                $note = tempdelete($usergroupid);
            } elseif ($operationType == "2") {//returndelete
                $note = returndelete($usergroupid);
            } elseif ($operationType == "3") {//deleteFinaly
                $note = deleteFinaly($usergroupid);
            }

            if ($note != "success") {
                //get usergroup name
                $usergroupData = $myUserGroupRecord->load($usergroupid);
                if (count($usergroupData) > 0) {
                    $usergroupname = $usergroupData->usergroupname;
                    $outputString .= $usergroupname . ": " . $note . "<br/>";
                }
            } else {
                $outputString .= "تمت العملية بنجاح";
            }
            $i++;
        }
        $smarty->assign("outputString", $outputString);
    }
}

function tempdelete($usergroupid = 0) {
    global $myUserGroupEx;
    global $myUserGroupRecord;

    if (empty($usergroupid) || $usergroupid == 0) {
        $usergroupid = $_GET['id'];
    }
    if (!empty($usergroupid)) {
        try {
            //temp delete from usergroup tbl
            $myUserGroupEx->deletetemp($usergroupid);
            $note = "success";
        } catch (Exception $e) {
            $note = "حدث خطأ أثناء إتمام العملية";
        }
    } else {
        $note = "حدث خطأ أثناء إتمام العملية";
    }
    return $note;
}

function returndelete($usergroupid = 0) {
    global $myUserGroupEx;
    $note;
    if (empty($usergroupid) || $usergroupid == 0) {
        $usergroupid = $_GET['id'];
    }

    if (!empty($usergroupid)) {
        try {
            //return delete from usergroup tbl
            $myUserGroupEx->returndelete($usergroupid);
            $note = "success";
        } catch (Exception $e) {
            $note = "حدث خطأ أثناء إتمام العملية";
        }
    } else {
        $note = "حدث خطأ أثناء إتمام العملية";
    }
    return $note;
}

//finaly deleted from usergroup tbl
function deleteFinaly($userGroupId = 0) {
    global $myUserGroupRecord;
    global $myRelusergroupRecord;
    global $myUserRecord;
    $note;

    if (empty($userGroupId) || $userGroupId == 0) {
        //get variable
        $userGroupId = $_GET['id'];
    }

    if (!empty($userGroupId)) {
        //check if find usergroupid in user tbl
        $userValidation = $myUserRecord->queryByUsergroupid($userGroupId);
        if (count($userValidation) <= 0) {
            try {
                //delete from usergroup tbl and relusergroupproperties tbl
                $myRelusergroupRecord->deleteByUsergroupid($userGroupId);
                $myUserGroupRecord->delete($userGroupId);
                $note = "success";
            } catch (Exception $e) {
                $note = "حدث خطأ أثناء إتمام العملية";
            }
        } else {
            $note = "لا يمكنك حذف هذه المجموعة إلا بعد حذف المستخدم المرتبط به";
        }
    } else {
        $note = "حدث خطأ أثناء إتمام العملية";
    }
    return $note;
}

// edit in table
function edit() {
    global $myUserGroup;
    global $myUserGroupRecord;
    global $myRelusergroupEx;

    //get variable
    $userGroupId = $_GET['id'];
    //load data by usergroupid
    $loadData = $myUserGroupRecord->load($userGroupId);


    // select all data from properity and relusergroupproperties tbl by usergroupid
    $properityData = $myRelusergroupEx->queryAllwithUsergroupid($userGroupId);
    //get count of selected Data
    $properityNum = count($properityData);
    return array($loadData, $properityData, $properityNum);
}

// update table
function update() {
    global $myUserGroup;
    global $myUserGroupRecord;
    global $myRelusergroup;
    global $myRelusergroupRecord;

    //post variable
    $conditions = $_POST['delete'];
    $userGroupId = $_POST['usergroupid'];
    $userGroupName = $_POST['usergroupname'];
    $userGroupDescription = $_POST['usergroupdescription'];
    $today = $_POST['today'];
    $startPage = $_POST['startpage'];
    $level = $_POST['level'];

    $savedailydate = $_POST['savedailydate'];
    $hidemenu = $_POST['hidemenu'];
    $hidecat = $_POST['hidecat'];

    $itr = $_POST['itr'];

    $clientnegative = $_POST['clientnegative'];
    $suplliernegative = $_POST['suplliernegative'];
    $storenegative = $_POST['storenegative'];

    $priceupdate = $_POST['priceupdate'];
    $directDiscount = $_POST['directDiscount'];
    $percentageDiscount = $_POST['percentageDiscount'];

    //insert data in usergroup tbl
    $myUserGroup->priceupdate = $priceupdate;
    $myUserGroup->directDiscount = $directDiscount;
    $myUserGroup->percentageDiscount = $percentageDiscount;


    $myUserGroup->clientnegative = $clientnegative;
    $myUserGroup->suplliernegative = $suplliernegative;
    $myUserGroup->storenegative = $storenegative;

    $myUserGroup->currentdate = $today;
    $myUserGroup->conditions = $conditions;
    $myUserGroup->usergroupname = $userGroupName;
    $myUserGroup->usergroupid = $userGroupId;
    $myUserGroup->startpage = $startPage;
    $myUserGroup->level = $level;
    $myUserGroup->savedailydate = $savedailydate;
    $myUserGroup->usergroupdescription = $userGroupDescription;
    $myUserGroup->hidemenu = $hidemenu;
    $myUserGroup->hidecat = $hidecat;


    $myUserGroupRecord->update($myUserGroup);

    //update data in relusergroupproperties tbl
    $h = 1;
    for ($h = 1; $h <= $itr; $h++) {
        $propertyValue = $_POST['propertydefault' . $h . ''];
        $propertyId = $_POST['propertyid' . $h . ''];
        $relusergrouppropertiesId = $_POST['relusergrouppropertiesid' . $h . ''];

        $myRelusergroup->propertyid = $propertyId;
        $myRelusergroup->propertyvalue = $propertyValue;
        $myRelusergroup->usergroupid = $userGroupId;
        $myRelusergroup->relusergrouppropertiesid = $relusergrouppropertiesId;

        $myRelusergroupRecord->update($myRelusergroup);
    }
}

?>