<?php

session_start();
ob_start();
//the global file operation
$do = isset($_GET['do']) ? $_GET['do'] : "";
$ajaxDoArr = array("add2SimpleReturn");

include("initiateStaticSessionCommingWithCurl.php");
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
    array_push($ajaxDoArr, $do);
}


if (!in_array($do, $ajaxDoArr)) {
    //the global file operation
    include("../public/impOpreation.php");
}
//Onlineproductcat
require_once('../models/dao/OnlineproductcatDAO.class.php');
require_once('../models/dto/Onlineproductcat.class.php');
require_once('../models/mysql/OnlineproductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/OnlineproductcatMySqlExtDAO.class.php');

// get the config file
include_once("../public/config.php");
include("../library/uploadImages.php");


require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');




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

$langs = $_SESSION['erp_lang'];

//include_once("../views/languages/$langs/productCat/productCat.php");
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

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

  Controller Name :- productCatController

  OPERTATION in Controller

  1-display add form
  2- add in productCat tble
  4-display sucess or error
  5-display show and delete
  6-delete from productCat tbl
  7-display edit
  8- update productCat tbl
  ======================== */

//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

    } else {
        $smarty->display("header.html");
    }
}

//here goes the instances and general variables
//Productcat
$onlineProductCat = new Onlineproductcat();
$onlineProductCatDAO = new OnlineproductcatMySqlDAO();
$onlineProductCatEX = new OnlineproductcatMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

$today = date("Y-m-d");
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check


    $allParents = getProductCatParents();
    $smarty->assign("categories", $allParents);

    $smarty->assign("customProductCat", 1);
    $smarty->display("onlineproductCatview/add.html");
} elseif ($do == "add") {

    $flag = $_GET['flag'];
    try {
        add();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        //echo $e;
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "show") {
    $allData = $onlineProductCatDAO->queryByIsdel(0);
    $smarty->assign("allData", $allData);
    $catId = $_REQUEST['catId'];
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    if (isset($catId) && !empty($catId) && $catId != '-1') {
        showByCatId($catId);
    } else {
        show();
    }
    $smarty->display("onlineproductCatview/show.html");
    $smarty->assign('customUsergroup', 1);

    $smarty->assign("customCheck", 1);
} elseif ($do == "deleteFinaly") { //tempdelete
    try {
        $productCatid = (int) $_GET['id'];
        deleteFinaly($productCatid);

        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "edit") { //edit  productCat
    $productCatid = $_GET['id'];
    $allParents = getProductCatParents($productCatid);
    $smarty->assign("allParents", $allParents);
    $parentid = $_GET['parentid'];
    if ($parentid) {
        $productCatparent = $onlineProductCatDAO->load($parentid);
        $smarty->assign("productCatparent", $productCatparent);
    }
    $smarty->assign("parentid", $parentid);
    $smarty->assign("categories", $allParents);

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

    $smarty->assign("customProductCat", 1);
    $smarty->display("onlineproductCatview/edit.html");
} elseif ($do == "update") { //edit update
    try {
        update();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success');
            echo json_encode($data);
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = array('status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error');
            echo json_encode($data);
        } else {
            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("customValidation", 1);

//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

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

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

function getProductCatParents($productCatid = 0) {
    global $onlineProductCatEX;

    //$allParents = $productCatExt->getProductCatParents();
    $allParents = $onlineProductCatEX->getCategoriesWithoutProducts($productCatid);
    return $allParents;
}

// add in productCat tbl
function add() {
    global $onlineProductCat;
    global $onlineProductCatDAO;

    $title = $_POST['title'];
    $titleEn = $_POST['titleEn'];
    $description = $_POST['description'];
    $descriptionEn = $_POST['descriptionEn'];
    $parent = (int) $_POST['parent'];

    if (empty($parent) || $parent == -1) {
        $parent = 0;
    }



    $onlineProductCat->parentid = $parent;
    $onlineProductCat->title = $title;
    $onlineProductCat->titleEn = $titleEn;
    $onlineProductCat->description = $description;
    $onlineProductCat->descriptionEn = $descriptionEn;
    $onlineProductCat->sysDate = date("Y-m-d H:i:s");
    $onlineProductCat->userid = $_SESSION['userid'];
    $onlineProductCat->isdel = 0;

    $handle = new upload($_FILES['logo']);
    $image = uploadImages2($handle, '../views/default/images/cat_image');
    $onlineProductCat->image = $image;
    //print_r($productCat);
    $productCatId = $onlineProductCatDAO->insert($onlineProductCat);

    return $productCatId;
}

//show
function show() {
    global $onlineProductCatEX;
    global $smarty;

    $productCatData = $onlineProductCatEX->queryAllForShow();
    $smarty->assign('productCatData', $productCatData);
}

function showByCatId($catId) {
    global $onlineProductCatEX;
    global $smarty;

    $productCatData = $onlineProductCatEX->loadExt($catId);
    $smarty->assign('productCatData', $productCatData);
}

function edit() {
    global $onlineProductCatDAO;

    $productCatid = $_GET['id'];
    $productCatData = $onlineProductCatDAO->load($productCatid);
    return $productCatData;
}

function update() {
    global $onlineProductCatDAO;

    $productCatId = $_POST['productCatId'];
    $productCat = $onlineProductCatDAO->load($productCatId);
    $title = $_POST['title'];
    $titleEn = $_POST['titleEn'];
    $description = $_POST['description'];
    $descriptionEn = $_POST['descriptionEn'];
    $parent = (int) $_POST['parent'];

    if (empty($parent) || $parent == -1) {
        $parent = 0;
    }

    $productCat->parentid = $parent;
    $productCat->title = $title;
    $productCat->titleEn = $titleEn;
    $productCat->description = $description;
    $productCat->descriptionEn = $descriptionEn;
    $productCat->sysDate = date("Y-m-d H:i:s");
    $productCat->userId = $_SESSION['userid'];

    $handle = new upload($_FILES['logo']);
    $image = updateImagesWithoutresiz($handle, "oldlogo", '../views/default/images/cat_image');
    $productCat->image = $image;
    $onlineProductCatDAO->update($productCat);
}

function deleteFinaly($productCatid) {
    global $onlineProductCatDAO;
    global $onlineProductCatEX;

    $prodactcatdata = $onlineProductCatEX->queryByParentExt($productCatid);
    if (count($prodactcatdata) > 0) {
        foreach ($prodactcatdata as $myprodactcatdata) {
            deleteFinaly($myprodactcatdata->id);
        }
    }
    $delcatdata = $onlineProductCatDAO->delete($productCatid);
}
