$(function() {
	 "use strict";
	  
 })
 
 function editVehicle(id){
	var obj = $('#modals');
	var form = "";
	$.ajax({
		url: "forms/vehicle.php",			
		type:'POST',
		data:{"vreg":id},
		async:false,
	    }).done(function(data){
	    	form= data;
	    });
		form = form.replace('{{title}}','Edit Vehicle');
		form = form.replace('{{btsubmit}}','Update');
		form = form.replace('{{id}}',id);

	 obj.html(form);
	 $( "#tabs" ).tabs();
	 obj.modal();
	
	 $(".chzn-select").chosen(); 
	 $('.timepicker-24').timepicker({
			minuteStep: 1,
			  showSeconds: false,
			  showMeridian: false,
			  defaultTime: "00:00"
	    });
	$(".datepicker").datepicker({format: 'yyyy-mm-dd'});
	$('#id_remark').wysihtml5();
}
 function showVehicle(id){
	var obj = $('#modals');
	var form = "";
	$.ajax({
		url: "forms/show_vehicle.php",			
		type:'POST',
		data:{"vreg":id},
		async:false,
	    }).done(function(data){
	    	form= data;
	    });
		
	 obj.html(form);
	 $( "#tabs" ).tabs();
	 obj.modal();
}
 function removeVehicle(id){
	var obj = $("#action_"+id);
	$.ajax({
		url:"ajaxviews/status_vehicle.php",
		type:"POST",
		data:{"vreg":id, "dirty": 1},
		async: false
	}).done(function(data){
		if(data['status'] == 'success'){
			var html = '<img class="action_restore" src="media/icon/restore-icon.png" id="recycle_vehicle" onclick="recycleVehicle(\''+id+'\')"/>';
			obj.html(html);
			$("#dirty_"+id).html('<img src="media/icon/icon-no.gif" />');
		}else{
			obj.addClass('error').html(data['msg']);
		}
	});
}

 function recycleVehicle(id){
	var obj = $("#action_"+id);
	$.ajax({
		url:"ajaxviews/status_vehicle.php",
		type:"POST",
		data:{"vreg":id, "dirty": 0},
		async: false
	}).done(function(data){
		if(data['status'] == 'success'){
			var html = '<img class="action_show" src="media/icon/details-icon.png" id="show_vehicle_details" onclick="showVehicle(\''+id+'\')"/>'+
	  		   '<img class="action_edit" src="media/icon/edit-icon.png" id="edit_vehicles" onclick="editVehicle(\''+id+'\')"/>'+
	           '<img class="action_delete" src="media/icon/delete-icon.png" id="remove_vehicle" onclick="removeVser(\''+id+'\')"/>';
			obj.html(html);
			$("#dirty_"+id).html('<img src="media/icon/icon-yes.gif" />');
		}else{
			obj.addClass('error').html(data['msg']);
		}
	});
}
 function listVehicle(){
	var tableData = "";
	var obj = $("#contentAdmin");
	$.ajax({
		url:"forms/vehicleTable.php",
		type:"POST",
		async: false
	}).done(function(data){
		tableData = data;
	});
	obj.html(tableData);
	$('#dataTable').dataTable({
		"bPaginate": true,
        "bFilter": true
      
    });
}

function addVehicle(){
	var obj = $('#modals');
	var form = "";
	$.ajax({
		url: "forms/vehicle.php",			
		type:'POST',
		async:false,
	    }).done(function(data){
	    	form= data;
	    });
		form = form.replace('{{title}}','Add Vehicle');
		form = form.replace('{{btsubmit}}','Add Vehicle');
		form = form.replace('{{id}}','');
		
	
	

	 obj.html(form);
	 $( "#tabs" ).tabs();
     obj.modal();
	
    
		$(".chzn-select").chosen(); 
		 $('.timepicker-24').timepicker({
				minuteStep: 1,
				  showSeconds: false,
				  showMeridian: false,
				  defaultTime: "00:00"
		    });
		$(".datepicker").datepicker({format: 'yyyy-mm-dd'});
		$('#id_remark').wysihtml5();
		
	 
}
function vehicleAction(id){
	"use strict";
	var frm = $("#frm_vehicle");

	frm.validate({
		ignore: [],
		rules: {
			imei: "required",
			name: "required",
			imsi: "required",
			tank_size: "required",
			max_speed: "required",
			max_temp: "required",
			"owner[]": "required",
			subscription_amt: "required"	
			}
		});
	
	if(frm.valid()){
	
		var base64img = $(".fileinput-preview img").attr('src');
		var d = new Date();
		var filename = d.getTime()+".jpg";
		$("#id_driver_dp").val(filename)
		$.ajax({//post driver_dp
		    type: "POST",
		    url: "ajaxviews/save_driver_dp.php",
		    data: {name:filename,image: base64img},
		    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		    success: function(data){
		        console.info(data);
		    }
		});
		//------------------
		$.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
               console.info(data);
               if(data['status']=='success'){
            	   $("#actionStatus").removeClass('error').addClass('success').html(data['msg']);
            	   $("#statusAdmin").removeClass('error').addClass('success').html(data['msg']);
            	   if(id != ''){
            		   $("#name_"+id).html(data['name']);
            		   $("#protocol_"+id).html(data['protocol']);
            		   $("#imsi_"+id).html(data['imsi']);
            		   $("#tank_type_"+id).html(data['tank_type']);
            		   $("#owners_"+id).html(data['owners']);
            	   }
            	   $('#modals').modal('hide');
               }else{
            	   $("#actionStatus").removeClass('success').addClass('error').html(data['msg']);
               }
            },
            error: function(data) {
                $("#actionStatus").html("<font color='red'>Something went wrong!</font>");
            }
        });
	
	}
}

//-----------------------
function Close(){
	 $("#listicon").hide("slow");
}
function chooseIcon(path,item){
	$("#id_icon").val(path);
	$("#divicon").html('<img src="'+path +'" />');
	 $("#listicon").hide("slow");
}
function showIcon(){
	  var documentHeight = $(document).height();
	  var documentWidth = $(document).width();
	
	  //--------------------
	  if($("#listicon").css("display")=='none'){
		  $("#listicon").css({"width":700,"height":"auto",
			  "position":"absolute","z-index":999999,"top":"40px",
			  "left": documentWidth/2-250});
		  $("#listicon").show("slow");
		  
	  }else{
		  $("#listicon").hide("slow");
	  }
	  };