<?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 user .
if ($do == 'insertTransport') {

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

    if ($transportObj->name == '' || $transportObj->period == '' || $transportObj->code == '') {
        $status = 'failed';
        $reson = '1';
        $msg = '���� ��� ���� ������';
    } else {
        //$tran = new ots ();
        $tran = R::dispense('bus');
        $tran->name = $transportObj->name;
        $tran->comment = $transportObj->comment;
        $tran->period = $transportObj->period;
        $tran->code = $transportObj->code;
        $tran->lat = $transportObj->lat;
        $tran->lng = $transportObj->lng;
        $tran->phone = $transportObj->phone;

        $id = R::store($tran);
        //$id = ots::insert('bus', $tran, false);
        if (!empty($id)) {
            $status = 'success';
            $reson = '0';
            $msg = '�� ����� �����';
        }
    }

    $arrRes = array("status" => $status, "reson" => $reson, "id" => $id);
    echo json_encode($arrRes);
}
//#
elseif ($do == 'updateTransport') {
    $id = $_GET['id'];
    $transport = $_GET['transport'];
    //# convert json to array .
    $transportObj = json_decode($transport);

    if ($transportObj->name == '' || $transportObj->period == '' || $transportObj->code == '') {
        $status = 'failed';
        $reson = '1';
        $msg = '���� ��� ���� ������';
    } else {
        //$tran = new ots ();
        $tran = R::load('bus', $id);
        $tran->name = $transportObj->name;
        $tran->comment = $transportObj->comment;
        $tran->period = $transportObj->period;
        $tran->code = $transportObj->code;
        $tran->lat = $transportObj->lat;
        $tran->lng = $transportObj->lng;
        $tran->phone = $transportObj->phone;

        R::store($tran);
        //ots::update('bus', 'id = ' . $id . ' ', $tran, false);
        if (!empty($id)) {
            $status = 'success';
            $reson = '0';
            $msg = '�� ����� �����';
        }
    }

    $arrRes = array("status" => $status, "reson" => $reson, "id" => $id);
    echo json_encode($arrRes);
}
//#
elseif ($do == 'updateUserLocation') {
    $id = $_GET['id'];
    $updateLocation = $_GET['updateLocation'];
    //# convert json to array .
    $locationObj = json_decode($updateLocation);

    //$tran = new ots ();
    $tran = R::load('bus', $id);
    $tran->lat = $locationObj->lat;
    $tran->lng = $locationObj->lng;
    R::store($tran);
    //ots::update('bus', 'id = ' . $id . ' ', $tran, false);
}
//#
elseif ($do == 'userData') {
    $id = $_GET['id'];

    //$userData = ots::load('bus', 'where id = ' . $id);
    $userData = R::load('bus', $id);
    $arr = array(
        'name' => $userData['name'],
        'comment' => $userData['comment'],
        'period' => $userData['period'],
        'code' => $userData['code'],
        'lat' => $userData['lat'],
        'lng' => $userData['lng'],
    );

    echo json_encode($arr);
}
//#
elseif ($do == 'getContacts') {
    $id = 1;

    //$userData = ots::load('bus', 'where id = ' . $id);
    $userData = R::load('contacts', $id);
    $arr = array(
        'mobile' => $userData['mobile'],
        'phone' => $userData['phone'],
        'facelink' => $userData['facelink'],
        'whatsapp' => $userData['whatsapp'],
    );

    echo json_encode($arr);
}
//#
elseif ($do == 'checkCode') {
    $code = $_GET['code'];
    $sQuery = "SELECT * FROM bus where code = '" . $code . "'";
    //$userData = R::getAll($sQuery);
    $countResult = R::count('bus', ' code = "' . $code . '"');

    if ($countResult > 0) {
        $res = '0';
        $status = 'success';
    } else {
        $res = '1';
        $status = 'failed';
    }

    $arr = array('res' => $res, 'status' => $status);
    echo json_encode($arr);
}
//#
elseif ($do == 'getTransportData') {
    $code = $_GET['code'];
    $sQuery = "SELECT * FROM bus where code = '" . $code . "'";
    $userData = R::getAll($sQuery);

    foreach ($userData as $val) {


        $arr[] = array(
            'id' => $val['id'],
            'name' => $val['name'],
            'comment' => $val['comment'],
            'period' => $val['period'],
            'code' => $val['code'],
            'lat' => $val['lat'],
            'lng' => $val['lng'],
            'phone' => $val['phone'],
        );
    }

    echo json_encode($arr);
}
//#
elseif ($do == 'checkDeviceId') {

    $devid = $_GET['devid'];

    $countResult = R::count('deviceids', ' devid = "' . $devid . '"');

    if ($countResult > 0) {
  
    } else {
        $msg = R::dispense('deviceids');
        $msg->devid = $devid;
        $id = R::store($msg);
    }
   
}
//#
elseif ($do == 'getBusLocation') {

    $id = $_GET['id'];

    $tran = R::load('bus', $id);
    $arr = array(
        'id' => $id,
        'name' => $tran['name'],
        'comment' => $tran['comment'],
        'period' => $tran['period'],
        'code' => $tran['code'],
        'lat' => $tran['lat'],
        'lng' => $tran['lng'],
    );

    echo json_encode($arr);
}
//#
elseif ($do == 'getAllHomeImgs') {
    $code = $_GET['code'];
    //$sQuery = "SELECT * FROM slider where default_bus = 1 and code = '" . $code . "'";
    $sQuery = "SELECT * FROM slider where default_bus = 1 ";
    $dSliderData = R::getAll($sQuery);

    foreach ($dSliderData as $val) {

        //$imgPath = 'localhost/bus_admin/admin_panel/../upload/m_Slider/' . $val['image'];
        $imgPath = 'https://bus.gt4host.com/bus_admin/admin_panel/../upload/m_Slider/' . $val['image'];
        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'bus_id' => $val['bus_id'],
            'content' => $val['content'],
            'link' => $val['link'],
            'title_en' => $val['title_en'],
            'content_en' => $val['content_en'],
            'imgPath' => $imgPath,
            'hide' => $val['hide'],
            'default_bus' => $val['default_bus'],
        );
    }

    echo json_encode($arr);
}
//#
elseif ($do == 'getAllImgsOneBus') {
    $bus_id = $_GET['bus_id'];
    $sQuery = "SELECT * FROM slider where default_bus = 0 and bus_id = " . $bus_id . " ";
    $dSliderData = R::getAll($sQuery);

    foreach ($dSliderData as $val) {

        $imgPath = 'https://bus.gt4host.com/bus_admin/admin_panel/../upload/m_Slider/' . $val['image'];
        //$imgPath = 'localhost/bus_admin/admin_panel/../upload/m_Slider/' . $val['image'];
        $arr[] = array(
            'id' => $val['id'],
            'title' => $val['title'],
            'bus_id' => $val['bus_id'],
            'content' => $val['content'],
            'link' => $val['link'],
            'title_en' => $val['title_en'],
            'content_en' => $val['content_en'],
            'imgPath' => $imgPath,
            'hide' => $val['hide'],
            'default_bus' => $val['default_bus'],
        );
    }

    echo json_encode($arr);
}

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

    foreach ($allNews as $val) {

        $imgPath = 'https://bus.gt4host.com/bus_admin/admin_panel/../upload/news/' . $val['image'];
        //$imgPath = 'localhost/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 = 'https://bus.gt4host.com/bus_admin/admin_panel/../upload/news/' . $val['image'];
        //$imgPath = 'localhost/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 page by id
//1:مجلس الإدارة
//2:عن التطبيق
//3:مواعيد الباصات
elseif ($do == 'loadPage') {
    $pageid = $_GET['pageid'];
    $sQuery = "SELECT * FROM page where id = " . $pageid;
    $allPage = R::getAll($sQuery);

    foreach ($allPage as $val) {

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

    echo json_encode($arr);
}

//# Notifications
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);
}


//# contact
elseif ($do == 'insertContact') {

    $name = filter_input(INPUT_POST, 'name');
    $title = filter_input(INPUT_POST, 'title');
    $email = filter_input(INPUT_POST, 'email');
    $phone = filter_input(INPUT_POST, 'phone');
    $msgxx = filter_input(INPUT_POST, 'msgs');
    $type = filter_input(INPUT_POST, 'type');
    //$image = filter_input(INPUT_POST, 'image');
    $image = $_POST['image'];

    $folder = "../../upload/contactmessage/";
    $imgflag = uploadBase64($image, $folder);
    if ($name == '' || $title == '' || $msgxx == '' || $type == '') {
        $status = 'failed';
        $reson = '1';
        $msgxa = 'أدخل البيانات المطلوبة';
    } else {
        $msg = R::dispense('contactmsg');
        $msg->name = $name;
        $msg->title = $title;
        $msg->email = $email;
        $msg->phone = $phone;
        $msg->msg = $msgxx;
        $msg->type = $type; // 1:شكوى-- 
        $msg->image = $imgflag;

        $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';
            $msgxa = 'تم إرسال الرسالة بنجاح';
        }
    }

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

//////############################ start functions .
function uploadBase64($img, $folder) {


     $img = str_replace('data:image/jpeg;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $dataxx = base64_decode($img);
    $fileName = uniqid() . '.jpeg';
    $file = $folder . $fileName;
    $success = file_put_contents($file, $dataxx);
    $res = $success ? $fileName : 'Unable to save the file.';

    return $res;
    


}
