<?php
require_once 'classes/ConDB.php';

require_once 'classes/getErrorMsg.php';
require_once 'classes/checkDeviceId.php';

header('Content-type: application/json');
if (isset($_REQUEST['device_submit'])) {
    $db = new ConDB();
    
    $request = $db-> _cleanInputs($_REQUEST);
    // $request = $_REQUEST;
    
    
    extract($request);
   
    if ($device_id == '' || $description == '') {
        $msg = new getErrorMsg('7');
        $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
        echo json_encode($json);
        return false;
    } else {
        $dev_check = new checkDeviceId($device_id);
    }
    $inloop = false;
    if (!$dev_check->available) {
        $msg = new getErrorMsg('3');
        $inloop = true;
    }

    if ($inloop) {

        //header('Content-type: application/json');
        $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
        echo json_encode($json);
        return false;
    } else {

         $reg_dev_qry = "insert into device_table(device_id,description,device_lock,latidegress,latimins,latisecs,longdegress,longmins,longsecs,active) values('".$device_id."','".$description."','".isset($dev_lock)."',0,0,0,0,0,0,'".isset($active)."')";

        $resutl= mysql_query($reg_dev_qry, $db->conn);
        $inserted_id = mysql_insert_id();
        if (mysql_affected_rows() > 0) {

            $msg = new getErrorMsg('0');
            $json = array('DeviceId' => $inserted_id, 'statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
            echo json_encode($json);

            return false;
        } else {
//            echo $reg_user_qry;
            $msg = new getErrorMsg('9');
            $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
            echo json_encode($json);
//                echo $reg_user_qry;
            return false;
        }
    }

}
?>