<?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);
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;

        $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;

        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 == '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'],
            );
   
    }

    echo json_encode($arr);
}
//# 
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);
}





















