<?php

session_start();
ob_start();
header('Content-Type: text/html; charset=utf-8');
$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)) {
    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();

$today = date("Y-m-d");
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    $smarty->display("tenderAnnouncementView/add.html");
} elseif ($do == "add") {
    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;
        die();
        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") {
   
    
    $sql = "SELECT t.*, u.employeename 
            FROM tenderannouncement t
            LEFT JOIN user u ON t.userid = u.userid 
            WHERE t.isdel = 0 
            ORDER BY t.id DESC";
    $showData = R::getAll($sql);
    $smarty->assign('showData', $showData);

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

        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  announcement
    $id = $_GET['id'];
    
    $showData = R::load('tenderannouncement', $id);
    $smarty->assign('showData', $showData);

    $smarty->display("tenderAnnouncementView/edit.html");
} elseif ($do == "view") { //edit  announcement
    $id = $_GET['id'];
    
    $showData = R::load('tenderannouncement', $id);
    $smarty->assign('showData', $showData);

    $smarty->display("tenderAnnouncementView/view.html");
} elseif ($do == "adreport") { //edit  announcement
     
   $sql = "SELECT * FROM tenderannouncement WHERE isdel = 0 ORDER BY id DESC";
    $allTenders = R::getAll($sql);

    $smarty->assign('allTenders', $allTenders);
    $smarty->display("tenderAnnouncementView/report.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
  ================================ */

// add in tenderannouncement tbl
function add() {
    $name = $_POST['name'];
    $description = $_POST['description'];
    $terms = $_POST['terms'];
    $notes = $_POST['notes'];
    $side = $_POST['side'];
    $status = $_POST['status'];
    $activity = $_POST['activity'];
    $subdate = $_POST['subdate'];
    $opening = $_POST['openingdate'];
    $annoucedate = $_POST['annoucedate'];
    $insurance = $_POST['insurance'];
    $bigprice = $_POST['bigprice'];
    $tinyprice = $_POST['tinyprice'];
    $nature = $_POST['nature'];
    if($nature=='other'){
        $nature = $_POST['other_nature'];
    }
    $type = $_POST['type'];
    $evaluation = (int) $_POST['evaluation'];
    $accept = (int) $_POST['accept'];
    $offers = (int) $_POST['offers'];
  
    
    $rdispense = R::dispense('tenderannouncement');
    $rdispense->name = $name;
    $rdispense->description = $description;
    $rdispense->terms = $terms;
    $rdispense->notes = $notes;
    $rdispense->the_issuing_entity = $side;
    $rdispense->status = $status;
    $rdispense->divisible = $accept;
    $rdispense->alternative_offers = $offers;
    $rdispense->activity = $activity;
    $rdispense->sub_date = $subdate;
    $rdispense->opening_date = $opening;
    $rdispense->annoucement_date = $annoucedate;
    $rdispense->insurance = $insurance;
    $rdispense->terms_price_for_big_companies = $bigprice;
    $rdispense->terms_price_for_small_companies = $tinyprice;
    $rdispense->process_nature = $nature;
    $rdispense->process_type = $type;
    $rdispense->evaluation_system = $evaluation;
    $rdispense->sys_date = date("Y-m-d H:i:s");
    $rdispense->userid = $_SESSION['userid'];
    $rdispense->isdel = 0;
    $id = R::store($rdispense);
    return $id;
}

function update() {

    $id = $_POST['id'];
    $name = $_POST['name'];
    $description = $_POST['description'];
    $terms = $_POST['terms'];
    $notes = $_POST['notes'];
    $side = $_POST['side'];
    $status = $_POST['status'];
    $activity = $_POST['activity'];
    $subdate = $_POST['subdate'];
    $opening = $_POST['openingdate'];
    $annoucedate = $_POST['annoucedate'];
    $insurance = $_POST['insurance'];
    $bigprice = $_POST['bigprice'];
    $tinyprice = $_POST['tinyprice'];
    $nature = $_POST['nature'];
    if($nature=='other'){
        $nature = $_POST['other_nature'];
    }
    $type = $_POST['type'];
    $evaluation = (int) $_POST['evaluation'];
    $accept = (int) $_POST['accept'];
    $offers = (int) $_POST['offers'];
  
    $rupdate = R::load('tenderannouncement', $id);
    $rupdate->name = $name;
    $rupdate->description = $description;
    $rupdate->terms = $terms;
    $rupdate->notes = $notes;
    $rupdate->the_issuing_entity = $side;
    $rupdate->status = $status;
    $rupdate->divisible = $accept;
    $rupdate->alternative_offers = $offers;
    $rupdate->activity = $activity;
    $rupdate->sub_date = $subdate;
    $rupdate->opening_date = $opening;
    $rupdate->annoucement_date = $annoucedate;
    $rupdate->insurance = $insurance;
    $rupdate->terms_price_for_big_companies = $bigprice;
    $rupdate->terms_price_for_small_companies = $tinyprice;
    $rupdate->process_nature = $nature;
    $rupdate->process_type = $type;
    $rupdate->evaluation_system = $evaluation;
    $rupdate->sys_date = date("Y-m-d H:i:s");
    $rupdate->userid = $_SESSION['userid'];
    $rupdate->isdel = 0;
    $id = R::store($rupdate);
}

function deleteFinaly($id) { 
    
    $rupdates = R::load('tenderannouncement', $id);
    
    if ($rupdates->id > 0) {
        $rupdates->isdel = 1;
        R::store($rupdates);
    }
}