<?php

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

if (! in_array($do, $ajaxDoArr)) {
    include "../public/impOpreation.php";
}

include_once "../public/config.php";
include "../library/newUplode/uploadfunctions.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/success.php";
include_once "../views/languages/$langs/error.php";

if (! in_array($do, $ajaxDoArr)) {
    if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

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

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

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);

$myStoreEx = new StoreMySqlExtDAO();

$today = date("Y-m-d");
//check and use the condition that suite this action
if (empty($do)) {
    $smarty->assign("customproductShape", 1);
    $smarty->display("productShapeView/add.html");
} elseif ($do == "add") {
    try {
        add();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = ['status' => 1, 'message' => 'تمت العمليه بنجاح', 'message_en' => 'Success'];
            echo json_encode($data);
        } else {
            header("location:?do=success");
        }
    } catch (Exception $e) {
        //echo $e;
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            $data = ['status' => 2, 'message' => 'حدث خطأ', 'message_en' => 'Error'];
            echo json_encode($data);
        } else {
            header("location:?do=error");
        }
    }
} elseif ($do == "show") {
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    
    $showData = R::getAll('SELECT productshape.*, user.employeeName username
            from productshape
            LEFT JOIN user ON user.userid = productshape.userid
            where isdel = 0 order by productshape.id desc');
    $smarty->assign('showData', $showData);

    $smarty->display("productShapeView/show.html");
    $smarty->assign("customproductShape", 1);
    $smarty->assign("customCheck", 1);
} elseif ($do == "del") {
    try {
        $id = (int) $_GET['id'];
        deleteFinaly($id);

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

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

// add in productCat tbl
function add(){
    $proshape = $_POST['shape'];
    $name = $_POST['name'];
    if($proshape == 'box') {
        $stripno = $_POST['stripno'];
        $stripcapno = $_POST['stripcapno'];
        $capno = 0;
    } else {
        $capno = $_POST['capno'];
        $stripno = 0;
        $stripcapno = 0;
    }

    $shape = R::dispense('productshape');
    $shape->name   = $name;
    $shape->shape   = $proshape;
    $shape->stripno   = $stripno;
    $shape->stripcapno   = $stripcapno;
    $shape->capno   = $capno;
    $shape->sysdate    = date("Y-m-d H:i:s");
    $shape->updateDate = date("Y-m-d H:i:s");
    $shape->userid     = $_SESSION['userid'];
    $shape->isdel      = 0;
    $id = R::store($shape);

    return $id;
}

function update()
{
    $id       = $_POST['id'];
    $proshape = $_POST['shape'];
    $name = $_POST['name'];
    if($proshape == 'box') {
        $stripno = $_POST['stripno'];
        $stripcapno = $_POST['stripcapno'];
        $capno = 0;
    } else {
        $capno = $_POST['capno'];
        $stripno = 0;
        $stripcapno = 0;
    }
    $shape = R::load('productshape', $id);
    $shape->name   = $name;
    $shape->shape   = $proshape;
    $shape->stripno   = $stripno;
    $shape->stripcapno   = $stripcapno;
    $shape->capno   = $capno;
    $shape->updateDate = date("Y-m-d H:i:s");
    $shape->userid = $_SESSION['userid'];
    $id = R::store($shape);
    return $id;
}

function deleteFinaly($id)
{
    $shape = R::load('productshape', $id);
    $shape->isdel = 1;
    R::store($shape);
}

