$(document).ready(function() {
var xin_table = $('#xin_table').dataTable({
	"bDestroy": true,
	"bFilter": true,
	"ajax": {
		url : base_url+"/users_list/",
		type : 'GET'
	},
	"fnDrawCallback": function(settings){
	//$('[data-toggle="tooltip"]').tooltip();          
	}
});


$('.collapseFour a').removeClass("collapsed");
$('#collapseFour').addClass("show");
$('.users').addClass("active");
    
    
$('[data-plugin="select_hrm"]').select2($(this).attr('data-options'));
$('[data-plugin="select_hrm"]').select2({ width:'100%' }); 


/* Delete data */
$("#delete_record").submit(function(e){
/*Form Submit*/
e.preventDefault();
	var obj = $(this), action = obj.attr('name');
	$.ajax({
		type: "POST",
		url: e.target.action,
		data: obj.serialize()+"&is_ajax=2&form="+action,
		cache: false,
		success: function (JSON) {
			if (JSON.error != '') {
				toastr.error(JSON.error);
			} else {
				$('.delete-modal').modal('toggle');
				xin_table.api().ajax.reload(function(){ 
					toastr.success(JSON.result);
				}, true);							
			}
		}
	});
});

// edit
$('.edit-modal-data').on('show.bs.modal', function (event) {
	var button = $(event.relatedTarget);
	var employee_id = button.data('employee_id');
	var modal = $(this);
$.ajax({
	url : base_url+"/read/",
	type: "GET",
	data: 'jd=1&is_ajax=1&mode=modal&data=employee&employee_id='+employee_id,
	success: function (response) {
		if(response) {
			$("#ajax_modal").html(response);
		}
	}
	});
});


/* Add data */ /*Form Submit*/
$("#xin-form").submit(function(e){
e.preventDefault();
	var obj = $(this), action = obj.attr('name');
	$('.save').prop('disabled', true);
	$.ajax({
		type: "POST",
		url: e.target.action,
		data: obj.serialize()+"&is_ajax=1&add_type=user&form="+action,
		cache: false,
		success: function (JSON) {
			if (JSON.error != '') {
				toastr.error(JSON.error);
				$('.save').prop('disabled', false);
			} else {
				xin_table.api().ajax.reload(function(){ 
					toastr.success(JSON.result);
				}, true);
				$('.add-form').fadeOut('slow');
				$('#xin-form')[0].reset(); // To reset form fields
				$('.save').prop('disabled', false);
			}
		}
	});
});
});
$( document ).on( "click", ".delete", function() {
$('input[name=_token]').val($(this).data('record-id'));
$('#delete_record').attr('action',base_url+'/delete/'+$(this).data('record-id'));
});



function add_role_to_table(){
    
    var load_country_div='<select class="form-control load_country_div" name="company[]" data-plugin="select_search">'+$("#load_country_div").html()+'</select>';
    var load_role_div='<select class="form-control load_role_div" name="role[]" data-plugin="select_search">'+$("#load_role_div").html()+'</select>';
    
    $('.estimate-cargo-table tbody').append('<tr class="table_tr_items"><td></td><td>'+load_role_div+'</td><td>'+load_country_div+'</td><td><button type="button" class="btn pull-right btn-danger remove_role_item"><i class="fa fa-trash"></i></button></td></tr>');
    
    $('[data-plugin="select_search"]').select2($(this).attr('data-options'));
    $('[data-plugin="select_search"]').select2({ width:'100%' });
}

$(document).on('click', '.remove_role_item', function() {
    $(this).parent().parent().remove();
});


function add_role_to_table2(){
    
    var load_country_div='<select class="form-control load_country_div" name="company[]" data-plugin="select_search">'+$("#load_country_div").html()+'</select>';
    var load_role_div='<select class="form-control load_role_div" name="role[]" data-plugin="select_search">'+$("#load_role_div").html()+'</select>';
    
    $('.estimate-role-table tbody').append('<tr class="table_tr_items"><td></td><td>'+load_role_div+'</td><td>'+load_country_div+'</td><td><button type="button" class="btn pull-right btn-danger remove_role_item2"><i class="fa fa-trash"></i></button></td></tr>');
    
    $('[data-plugin="select_search"]').select2($(this).attr('data-options'));
    $('[data-plugin="select_search"]').select2({ width:'100%' });
}

$(document).on('click', '.remove_role_item2', function() {
    $(this).parent().parent().remove();
});
