$(document).ready(function() {
   var xin_table = $('#xin_table').dataTable({
        "bDestroy": true,
		"ajax": {
            url : base_url+"/role_list/",
            type : 'GET'
        },
		"fnDrawCallback": function(settings){
		//$('[data-toggle="tooltip"]').tooltip();          
		}
    });
	
	$('[data-plugin="select_hrm"]').select2($(this).attr('data-options'));
	$('[data-plugin="select_hrm"]').select2({ width:'100%' }); 
		
	$('.collapseFour a').removeClass("collapsed");
    $('#collapseFour').addClass("show");
    $('.roles').addClass("active");

	/* 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 role_id = button.data('role_id');
		var modal = $(this);
	$.ajax({
		url : base_url+"/read/",
		type: "GET",
		data: 'jd=1&is_ajax=1&mode=modal&data=role&role_id='+role_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=role&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'));
});
jQuery("#treeview_r1").kendoTreeView({
checkboxes: {
checkChildren: true,
template: "<label class='custom-control custom-checkbox'><input type='checkbox' #= item.check# class='#= item.class #' name='role_resources[]' value='#= item.value #'  /><span class='custom-control-indicator'></span><span class='custom-control-description'>#= item.text #</span><span class='custom-control-info'>#= item.add_info #</span></label>"
},
//<label class='custom-control custom-checkbox'><input type='checkbox' class='#= item.class #' name='role_resources[]' value='#= item.value #'  /><span class='custom-control-indicator'></span><span class='custom-control-description'>#= item.text #</span><span class='custom-control-info'>#= item.add_info #</span></label>

//template: "<div class='checkbox'><label><input type='checkbox' #= item.check# class='#= item.class #' name='role_resources[]' value='#= item.value #'>#= item.text #</label></div>"
check: onCheck,
dataSource: [
    
{ id: "", class: "role-checkbox custom-control-input", text: "Dashboard", add_info: "", value: "111", items: [
// sub 1

{ id: "", class: "role-checkbox custom-control-input", text: "All count",  add_info: "All count", value: "112",},

{ id: "", class: "role-checkbox custom-control-input", text: "Branch total count",  add_info: "Branch total count", value: "113",},

{ id: "", class: "role-checkbox custom-control-input", text: "Employee Add Count",  add_info: "Employee Add Count", value: "114",},

]},


{ id: "", class: "role-checkbox custom-control-input", text: "Shipments", add_info: "", value: "1", items: [
// sub 1

{ id: "", class: "role-checkbox custom-control-input", text: "Show All",  add_info: "Show All", value: "212",},

{ id: "", class: "role-checkbox custom-control-input", text: "Edit",  add_info: "Edit", value: "2",},

{ id: "", class: "role-checkbox custom-control-input", text: "View",  add_info: "View", value: "211",},

{ id: "", class: "role-checkbox custom-control-input", text: "Add New",  add_info: "Add", value: "3",},

{ id: "", class: "role-checkbox custom-control-input", text: "Delete",  add_info: "Delete", value: "4",},
]}, // sub 1 end


{ id: "", class: "role-checkbox custom-control-input", text: "Customer", add_info: "", value: "5", items: [
// sub 1
{ id: "", class: "role-checkbox custom-control-input", text: "Add",  add_info: "Add", value: "6",},

{ id: "", class: "role-checkbox custom-control-input", text: "Edit",  add_info: "Edit", value: "7",},

{ id: "", class: "role-checkbox custom-control-input", text: "View",  add_info: "View", value: "8",},

{ id: "", class: "role-checkbox custom-control-input", text: "Delete",  add_info: "Delete", value: "9",},

]}, // sub 1 end


{ id: "", class: "role-checkbox custom-control-input", text: "Quotations", add_info: "", value: "10", items: [
    
{ id: "", class: "role-checkbox custom-control-input", text: "Show All",  add_info: "Show All", value: "411",},

{ id: "", class: "role-checkbox custom-control-input", text: "Add",  add_info: "Add", value: "11",},

{ id: "", class: "role-checkbox custom-control-input", text: "Edit",  add_info: "Edit", value: "12",},

{ id: "", class: "role-checkbox custom-control-input", text: "View",  add_info: "View", value: "13",},

{ id: "", class: "role-checkbox custom-control-input", text: "Download",  add_info: "View", value: "14",},

{ id: "", class: "role-checkbox custom-control-input", text: "Delete",  add_info: "Delete", value: "15",},

]}, // sub 1 end

//
]
});

jQuery("#treeview_r2").kendoTreeView({
checkboxes: {
checkChildren: true,
template: "<label class='custom-control custom-checkbox'><input type='checkbox' #= item.check# class='#= item.class #' name='role_resources[]' value='#= item.value #'  /><span class='custom-control-indicator'></span><span class='custom-control-description'>#= item.text #</span><span class='custom-control-info'>#= item.add_info #</span></label>"
},
check: onCheck,
dataSource: [

{ id: "", class: "role-checkbox custom-control-input", text: "Rate Request", add_info: "", value: "16", items: [
// sub 1
{ id: "", class: "role-checkbox custom-control-input", text: "Add",  add_info: "Add", value: "17",},

{ id: "", class: "role-checkbox custom-control-input", text: "Edit",  add_info: "Edit", value: "18",},

{ id: "", class: "role-checkbox custom-control-input", text: "View",  add_info: "View", value: "19",},

{ id: "", class: "role-checkbox custom-control-input", text: "Delete",  add_info: "Delete", value: "20",},

]}, // sub 1 end


{ id: "", class: "role-checkbox custom-control-input", text: "Users", add_info: "", value: "21", items: [
// sub 1
{ id: "", class: "role-checkbox custom-control-input", text: "Add",  add_info: "Add", value: "22",},

{ id: "", class: "role-checkbox custom-control-input", text: "Edit",  add_info: "Edit", value: "23",},

{ id: "", class: "role-checkbox custom-control-input", text: "View",  add_info: "View", value: "24",},

{ id: "", class: "role-checkbox custom-control-input", text: "Delete",  add_info: "Delete", value: "25",},

]}, // sub 1 end


{ id: "", class: "role-checkbox custom-control-input", text: "Roles",  add_info: "View", value: "26",},

{ id: "", class: "role-checkbox custom-control-input", text: "Company",  add_info: "View", value: "27",},


{ id: "", class: "role-checkbox custom-control-input", text: "Settings",  add_info: "", value: "28",  items: [
{ id: "", class: "role-checkbox custom-control-input", text: "Currency",  add_info: "Add", value: "29"},
]},


//
]
});
		
// show checked node IDs on datasource change
function onCheck() {
var checkedNodes = [],
		treeView = jQuery("#treeview2").data("kendoTreeView"),
		message;
		jQuery("#result").html(message);
}
$(document).ready(function(){
	$("#role_access").change(function(){
		var sel_val = $(this).val();
		if(sel_val=='1') {
			$('.role-checkbox').prop('checked', true);
		} else {
			$('.role-checkbox').attr("checked", false);
		}
	});
});