EinsatzOnline/resources/js/resource_management_vehicle...

142 lines
6.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$( document ).ready(function() {
VehicleModule.get_vehicle_categories();
VehicleModule.get_vehicles();
$(".vehicle_detailed_view_submit").on("click", VehicleModule.save_vehicle);
});
VehicleModule = ( function() {
var vehicle_list = [];
var get_vehicle_categories = function(){
$.ajax({
type: "GET",
url: "/api/resources/vehicles/categories",
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
success: function (data) {
if(is_ok(data)) {
$(data).each(function(){
$(".vehicle_categories_select").append("<option data-category-id=\""+this.id+"\">"+this.name+"</option>");
});
}
}
});
};
var get_vehicles = function(){
$.ajax({
type: "GET",
url: "/api/resources/vehicles?entries=20&page=0",
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
success: function (data) {
if(is_ok(data)) {
vehicle_list = data;
$(data).each(function(){
var is_operational = "";
if(this.is_operational === true){
is_operational = "✓";
}else{
is_operational = "×"
}
$("#vehicle_list_tbody").append("<tr class=\"vehicle_list_select_tr\" data-vehicle-id=\""+this.entity_id+"\">\n" +
" <td><input type=\"checkbox\" data-vehicle-id=\""+this.entity_id+"\"></td>\n" +
" <td>"+this.identifier+"</td>\n" +
" <td>"+this.numberplate+"</td>\n" +
" <td>"+is_operational+"</td>\n"+
" </tr>");
});
$(".vehicle_list_select_tr").unbind("click").on("click", VehicleModule.load_vehicle_details);
}
}
});
};
var load_vehicle_details = function(){
var tr = this;
$("#vehicle_detailed_view > input").val("");
$("#vehicle_detailed_view").show();
$(vehicle_list).each(function(){
if(this.entity_id === $(tr).data("vehicle-id")){
$("#vehicle_detailed_view_identifier_input").val(this.identifier);
$("#vehicle_detailed_view_numberplate_input").val(this.numberplate);
$("#vehicle_detailed_view_description_input").val(this.description);
if(this.is_operational){
$("#vehicle_detailed_view_is_operational_input").attr("checked", true);
}
$("#vehicle_detailed_view_admissable_total_weight_input").val(this.admissible_total_weight);
$("#vehicle_detailed_view_entity_id").val(this.entity_id);
var entity_id = this.entity_id;
$.ajax({
type: "GET",
url: "/api/info/caller/permissions?permission=modules.scheduler.appointments.view&entity_id="+entity_id,
contentType: 'application/json',
timeout: 3000,
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
success: function (data) {
if(is_ok(data)) {
console.log(data);
if(data){
$(".scheduler").show();
$(".scheduler_add_appointment_entity_id").val(entity_id);
Scheduler.list_appointments();
}
}
}
});
}
});
};
var save_vehicle = function(){
var vehicle = new Object();
vehicle.identifier = $("#vehicle_detailed_view_identifier_input").val();
if($("#vehicle_detailed_view_numberplate_input").val().length > 0){
vehicle.numberplate = $("#vehicle_detailed_view_numberplate_input").val();
}
if($("#vehicle_detailed_view_description_input").val().length > 0){
vehicle.description = $("#vehicle_detailed_view_description_input").val();
}
if($("#vehicle_detailed_view_is_operational_input").prop('checked')){
vehicle.is_operational = true;
}else{
vehicle.is_operational = false;
}
if($("#vehicle_detailed_view_admissable_total_weight_input").val().length > 0){
//vehicle.admissible_total_weight = $("#vehicle_detailed_view_admissable_total_weight_input").val();
}
if($("#vehicle_detailed_view_category_input option:selected").data("category-id") !== null){
vehicle.vehicle_category = $("#vehicle_detailed_view_category_input option:selected").data("category-id");
}
vehicle.entity_id = $("#vehicle_detailed_view_entity_id").val();
console.log(vehicle);
$.ajax({
type: "PUT",
url: "/api/resources/vehicles/"+vehicle.entity_id,
contentType: 'application/json',
timeout: 3000,
data: JSON.stringify(vehicle),
error: function () {
alert("Verbindung zum Server unterbrochen!");
},
success: function (data) {
if(is_ok(data)) {
alert("OK!");
}
}
});
};
return{
get_vehicle_categories: get_vehicle_categories,
get_vehicles: get_vehicles,
load_vehicle_details: load_vehicle_details,
save_vehicle: save_vehicle,
};
})();