$(function(){

	

	$("#addCompanyForm").validate({
		rules: {
			name : {
				required : true
			},
			email: {
				required: true,
				email: true
			},
			url : {
				required : true,
				url : true
			}
		},
		messages: {	
			name : {
				required : "Please enter your company name"
			},
			url : {
				required : "Please enter your company website",
				url : "Please enter a valid url"
			},
			email: { 				
				required: "Enter your Company email address",
                email: "Please enter a valid email address",
			}
		}
	});


    	

	$('#addCompanyDialog').on('hide.bs.modal', function (e) {
		refresh();
	});
	
	$('#addCompanyDialog').on('shown.bs.modal', function (e) {
	  	$('#name').focus();
		$('#id').val('');

	  	var id = $(e.relatedTarget).attr('id');
	  	var isEdit = $(e.relatedTarget).hasClass('fa-edit');
	  	
	  	console.log(isEdit);  	

  		if(isEdit){
  			$('#id').val(id);	  		

	  		$.getJSON('/account/companies/' + id, function(data){
    			
    			if(data.result){
		  			$('#name').val(data.result.name);
		  			$('#email').val(data.result.email);
		  			$('#url').val(data.result.url);		  			
				}
    		});
		}

		var validator = $( "#addCompanyForm" ).validate();
		validator.resetForm();
	});




	$('#confirm-delete').on('show.bs.modal', function(e) {
		var id = $(e.relatedTarget).attr('id');
	    $(this).find('.btn-ok').on('click', function(){
			$.ajax({
				url: '/account/companies/' + id,
				type: 'delete',
				dataType: 'json',
				success: function(data) {					
					$('#message ').html('<div class="error" style="text-align:center;padding:5px;">' + data.message + '</div>')
					
					$('#confirm-delete').modal('hide');

					if(data.result)
						getCompanies();
				}
			});
	    });
	});


	 
    $("#addCompanyForm").submit(function(e) {
      
        e.preventDefault();

        if($( "#addCompanyForm" ).valid()){
        	var actionurl = '/account/companies';
        	var type = 'post';
        	console.log($('#id').val() != '');
        	if($('#id').val() != ''){
	        	type = 'put';	
	        	actionurl = '/account/companies/' + $('#id').val();        	
	        }
	        //var actionurl = e.currentTarget.action;

			$.ajax({
				url: actionurl,
				type: type,
				dataType: 'json',
				data: $("#addCompanyForm").serialize(),
				success: function(data) {					
					$('#message ').html('<div class="error" style="text-align:center;padding:5px;">' + data.message + '</div>')
					
					$('#addCompanyDialog').modal('hide');

					if(data.result)
						getCompanies();
				}
			});
		}
    });


    var getCompanies= function(){
    	$('#companylist').html('<div class="loader"><i class="fa fa-spinner fa-pulse"></i></div>');
    	$.get('/account/companies/list', function(data){
            $('#companylist').html(data);
            $('#message ').html('');
    	});
    };

    $('#refresh').on('click', function () {
    	$('#message ').html('');
	  	 getCompanies();

	});


	var refresh = function () {
	    $('#id').val('');
	    $('#name').val('');
	    $('#url').val('');
	    $('#email').val('');    
	  
	};


    getCompanies();
});