<?php

include 'core/init.php';
include 'includes/overall/header_add_new_vehicle.php';
if (empty($_POST) === false) {
    $required_fields = array('vehicle_number', 'vehicle_type', 'stolen_status');
    foreach ($_POST as $key => $value) {
        if (empty($value) && in_array($key, $required_fields) === true) {
            $errors[] = 'Fields marked with an asterisk are required';
            break 1;
        }
    }
}
if (empty($errors) === true && empty($_POST) === false) {
    if (vehicle_exists($_POST['vehicle_number']) === true) {
        $errors[] = 'Sorry, the vehicle_number \'' . $_POST['vehicle_number'] . '\' already exists.';
    } else {
        if (preg_match("/\\s/", $_POST['vehicle_number']) == true) {
            $errors[] = 'Your vehicle_number must not contain any spaces.';
        } else {
            if (strlen($_POST['vehicle_number']) < 10) {
                $errors[] = 'Your vehicle number must be atleast 10 letters long.';
            } else {
                if (!preg_match("/^[0-9A-Z]*\$/", $_POST['vehicle_number'])) {
                    $errors[] = 'Wrong Vehicle Number format.';
                }
            }
        }
    }
}
?>
        $errors[] = 'Vehicle_number must not contain any spaces.';
    }
    if (strlen($first) < 10) {
        $errors[] = 'Vehicle number must be atleast 10 letters long.';
    }
    if (!preg_match("/^[0-9A-Z]*\$/", $first)) {
        $errors[] = 'Wrong Vehicle Number format.';
    }
}
?>


<?php 
if (empty($errors) === true) {
    $vehicle_number = $first;
    $vehicle_exists = vehicle_exists($vehicle_number);
    if ($vehicle_exists === false) {
        $errors[] = 'Vehicle Number ' . $vehicle_number . ' registered. Bill Manually!!!';
    } else {
        stolen_alert($vehicle_number, $user_data);
        $balance = balance($vehicle_number, $user_data);
        if ($balance === false) {
            $errors[] = 'Vehicle Number' . $vehicle_number . 'balance is low. Bill Manually!!!';
            $data1 = array();
            $data1 = mysql_fetch_assoc(mysql_query("SELECT * FROM `vehicle` WHERE `vehicle_number` = '{$vehicle_number}'"));
            $vehicle_owner_id = $data1['vehicle_owner_id'];
            $data2 = array();
            $data2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `user_id` = '{$vehicle_owner_id}'"));
            $email = $data2['email'];
            $body = "Your Current Balance is " . $data2['balance'] . " which is low. Recharge Urgently.";
            $sub = "Toll Booth Transaction";