<?php

//include ('db.php');
//  String acrHeaders = request.getHeader("Access-Control-Request-Headers");
//  String acrMethod = request.getHeader("Access-Control-Request-Method");
//  response.setHeader("Access-Control-Allow-Origin", "*");
//  response.setHeader("Access-Control-Allow-Headers", acrHeaders);
//  response.setHeader("Access-Control-Allow-Methods", acrMethod);
require_once("../public/aw_config.php");

header("Access-Control-Allow-Origin: *");



$do = $_GET['do'];
//$do = 'checkCode';
//# insert new contact .
if ($do == 'insertContact') {

    $contact = $_GET['contact'];
    //# convert json to array .
    $contactObj = json_decode($contact);

    if ($contactObj->name == '' || $contactObj->title == '' || $contactObj->msg == '' || $contactObj->type == '') {
        $status = 'failed';
        $reson = '1';
        $msg = 'أدخل البيانات المطلوبة';
    } else {
        $msg = R::dispense('contactmsg');
        $msg->name = $contactObj->name;
        $msg->title = $contactObj->title;
        $msg->email = $contactObj->email;
        $msg->phone = $contactObj->phone;
        $msg->msg = $contactObj->msg;
        $msg->type = $contactObj->type; // 1:شكوى-- 2: طلب -- 3:مقترح
        $msg->image = $contactObj->image;

        $msg->mdate = date('Y-m-d H:i:s');
        $msg->read = 0;
        $msg->del = 0;

        $id = R::store($msg);
        if (!empty($id)) {
            $status = 'success';
            $reson = '0';
            $msg = 'تم إرسال الرسالة بنجاح';
        }
    }

    $arrRes = array("status" => $status, "reson" => $reson, "id" => $id);
    echo json_encode($arrRes);
}

//allNews
elseif ($do == 'getAllNews') {
    $sQuery = "SELECT * FROM news order by date desc";
    $allNews = R::getAll($sQuery);

    foreach ($allNews as $val) {

        $imgPath = 'http://bus.gt4host.com/bus_admin/admin_panel/../upload/news/' . $val['image'];
        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'imgPath' => $imgPath,
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

// last 10 News
elseif ($do == 'getAllNewsLimited') {
    $sQuery = "SELECT * FROM news order by date desc limit 10";
    $allNews = R::getAll($sQuery);

    foreach ($allNews as $val) {

        $imgPath = 'http://bus.gt4host.com/bus_admin/admin_panel/../upload/news/' . $val['image'];
        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'imgPath' => $imgPath,
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

//load news by id
elseif ($do == 'loadNews') {
    $newsid = $_GET['newsid'];
    $sQuery = "SELECT * FROM news where id = " . $newsid;
    $allNews = R::getAll($sQuery);

    foreach ($allNews as $val) {

        $imgPath = 'http://bus.gt4host.com/bus_admin/admin_panel/../upload/news/' . $val['image'];
        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'imgPath' => $imgPath,
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

//allNotification
elseif ($do == 'getAllNotification') {
    $sQuery = "SELECT * FROM notification order by date desc";
    $allNotification = R::getAll($sQuery);

    foreach ($allNotification as $val) {

        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

// last 10 Notification
elseif ($do == 'getAllNotificationLimited') {
    $sQuery = "SELECT * FROM notification order by date desc limit 10";
    $allNotification = R::getAll($sQuery);

    foreach ($allNotification as $val) {

        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

//load Notification by id
elseif ($do == 'loadNotification') {
    $notificationid = $_GET['notificationid'];
    $sQuery = "SELECT * FROM notification where id = " . $notificationid;
    $allNotification = R::getAll($sQuery);

    foreach ($allNotification as $val) {

        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

//load page by id
//1:مجلس الإدارة
//2:عن التطبيق
//3:مواعيد الباصات
elseif ($do == 'loadPage') {
    $pageid = $_GET['pageid'];
    $sQuery = "SELECT * FROM news where id = " . $pageid;
    $allPage = R::getAll($sQuery);

    foreach ($allPage as $val) {

        $imgPath = 'http://bus.gt4host.com/bus_admin/admin_panel/../upload/page/' . $val['image'];
        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'content' => $val['content'],
            'imgPath' => $imgPath,
            'date' => $val['date'],
        );
    }

    echo json_encode($arr);
}

















