<?php

session_start();
ob_start();
$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
     $query = '';
    $brEXist = R::getRow('select * from boardopenion limit 1');
    if(!empty($brEXist))
        $query = 'AND br.id NOT IN (SELECT request_details FROM boardopenion WHERE isdel = 0)';

    $sql = "SELECT br.id, ta.name as tender_name, m.member_name 
        FROM bidrequest br
        JOIN tenderannouncement ta ON br.op_id = ta.id
        JOIN memberships m ON br.member_id = m.id
        WHERE br.isdel = 0 ".$query;
    $results = R::getAll($sql);
    $smarty->assign('requests', $results);
    $smarty->display("boardOpenionView/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 
                bo.*, 
                ta.name as tender_name, 
                m.member_name,
                u.employeename 
            FROM boardopenion bo
            LEFT JOIN bidrequest br ON bo.request_details = br.id
            LEFT JOIN tenderannouncement ta ON br.op_id = ta.id
            LEFT JOIN memberships m ON br.member_id = m.id
            LEFT JOIN user u ON bo.userid = u.userid 
            WHERE bo.isdel = 0";
    $showData = R::getAll($sql);
    $smarty->assign('showData', $showData);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("boardOpenionView/show.html");
    $smarty->assign("customCheck", 1);
} 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 request
    $id = $_GET['id'];
    $showData = R::load('boardopenion', $id);
    $sql =$sql = "SELECT br.id, ta.name as tender_name, m.member_name 
        FROM bidrequest br
        JOIN tenderannouncement ta ON br.op_id = ta.id
        JOIN memberships m ON br.member_id = m.id
        WHERE br.isdel = 0 
        AND (
            br.id NOT IN (SELECT request_details FROM boardopenion WHERE isdel = 0) 
            OR br.id = $showData->request_details
        )";
            
    $requests = R::getAll($sql);
    
    $smarty->assign('showData', $showData);
    $smarty->assign('requests', $requests);
    $smarty->display("boardOpenionView/edit.html");

    } elseif ($do == "report") { 
    
    
    $sql = "SELECT 
                bo.*, 
                ta.name as tender_name, 
                m.member_name,
                u.employeename 
            FROM boardopenion bo
            LEFT JOIN bidrequest br ON bo.request_details = br.id
            LEFT JOIN tenderannouncement ta ON br.op_id = ta.id
            LEFT JOIN memberships m ON br.member_id = m.id
            LEFT JOIN user u ON bo.userid = u.userid 
            WHERE bo.isdel = 0 
            ORDER BY bo.id DESC";

    $showData = R::getAll($sql);
    $smarty->assign('showData', $showData);
    $smarty->display("boardOpenionView/report.html");

    } elseif ($do == "view") { //edit request
    $id = $_GET['id'];
    $showData = R::load('boardopenion', $id);
    $sql =$sql = "SELECT br.id, ta.name as tender_name, m.member_name 
        FROM bidrequest br
        JOIN tenderannouncement ta ON br.op_id = ta.id
        JOIN memberships m ON br.member_id = m.id
        WHERE br.isdel = 0 
        AND (
            br.id NOT IN (SELECT request_details FROM boardopenion WHERE isdel = 0) 
            OR br.id = $showData->request_details
        )";
            
    $requests = R::getAll($sql);
    
    $smarty->assign('showData', $showData);
    $smarty->assign('requests', $requests);
    $smarty->display("boardOpenionView/view.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() {
    $num = $_POST['num'];
    $image = uploadnew('image', False, 0, 0, 'boardOpenion');
    $openion = (int) $_POST['openion'];
    
    $rdispense = R::dispense('boardopenion');
    $rdispense->request_details = $num;
    $rdispense->image = $image;
    $rdispense->board_openion = $openion;
    $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'];
    $num = $_POST['num'];
    $image = uploadupdate('image', 'imageurl', False, 0, 0, 'boardOpenion');
    $openion = (int) $_POST['openion'];
    $rupdate = R::load('boardopenion', $id);
    $rupdate->request_details = $num;
    $rupdate->image = $image;
    $rupdate->board_openion = $openion;
    $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('boardopenion', $id);
    
    if ($rupdates->id > 0) {
        $rupdates->isdel = 1;
        R::store($rupdates);
    }
}