var name_table = "";
var name_fild = "";
var id_table = "";
function type_1(type)
{
    type_page = type;
	if(type == 0)
	{
		var name = "اسم الوظيفة";
		var name_class = "jobs";
	    name_table = "jobs";
		name_fild = "name_job";
		id_table = "id_job";
	}
	if(type == 1)
	{
		var name = "اسم الفرع";
		var name_class = "branch";
	    name_table = "branch";
		name_fild = "name_branch";
		id_table = "id_branch";
	}
	if(type == 2)
	{
		var name = "اسم المجموعة";
		var name_class = "cata";
		 name_table = "cata";
		 name_fild = "name_cata";
		 id_table = "id_cata";
	}
	if(type == 3)
	{
		var name = "اسم القسم";
		var name_class = "cat";
	    name_table = "cat";
		name_fild = "name_cat";
		id_table = "id_cat";
	}
	if(type == 4)
	{
		var name = "اسم المصروف";
		var name_class = "exp";
	    name_table = "expen";
		name_fild = "name_expense";
		id_table = "id_expense";
	}
	if(type == 5)
	{
		var name = "اسم الايراد";
		var name_class = "inp";
	    name_table = "inp";
		name_fild = "name_inp";
		id_table = "id_inp";
	}
	if(type == 6)
	{
		var name = "اسم المورد";
		var name_class = "sup";
	    name_table = "sup";
		name_fild = "name_sup";
		id_table = "id_sup";
	}
	if(type == 7)
	{
		var name = "اسم الكابتن";
		var name_class = "cap";
	    name_table = "cap";
		name_fild = "name_cap";
		id_table = "id_cap";
	}
	if(type == 8)
	{
		var name = "اسم المنطقه";
		var name_class = "area";
	    name_table = "area";
		name_fild = "name_area";
		id_table = "id_area";
	}
	// if(type == 7)
	// {
	// 	var name = "اسم السيلز";
	// 	var name_class = "cap";
	//     name_table = "cap";
	// 	name_fild = "name_cap";
	// 	id_table = "id_cap";
	// }

	var txt = `<div class="card-body">
	<a  onclick="open_`+name_class+`()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fas fa-user-plus"></i></a>
  <table id="example1" class="table_image table table-bordered table-striped">
  <thead style="color:white;background-color:#007bff;font-family:bold;font-size:19px">
  <tr>
  						  <th class="hidden-xs">م</th>
  						  <th>`+name+`</th>
  						  <th></th>
  </tr>
  </thead>
  <tbody>


  </tbody>

</table>
	 </div>`;
	 if(emp_acc[4] == 0){
		sweetAlert("لا يوجد صلاحيه","","error")
	 }else{
		$('#pg_body').html(txt);
		$(function () {
   
		  var table = $("#example1").DataTable({
				  dom: 'Bfrtip',
				  //'paging':false,
				  "responsive": true, "lengthChange": false, "autoWidth": false,
				  buttons: ["colvis","copy", "excel", "pdf", "print"],
				  
						  "processing": true,
						  "serverSide": true,
						  "ajax":{
						   url :"model/class/"+name_class+".php",
						   type: "post",  
						   dataType:"JSON",
						   data:{type:type},
						  }
						  
			  }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
		  
		});
				   get_page();
	 }

}
function open_jobs()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('الوظيفة');
	id_type_page = 0;
}
function open_branch()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('الفرع');
	id_type_page = 0;
}
function open_cata()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('المجموعة');
	id_type_page = 0;
}
function open_cat()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('القسم');
	id_type_page = 0;
}
function open_exp()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('المصروف');
	id_type_page = 0;
}
function open_inp()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('الايراد');
	id_type_page = 0;
}
function open_sup()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('المورد');
	id_type_page = 0;
}
function open_area()
{
	$('.form-control').val('');
	$('#modal_type_1').modal('show');
	$('#label_type_1').text('المنطقه');
	id_type_page = 0;
}
function save_type_1()
{
	var name = $('#name_type_1').val();
	if(id_type_page == 0)
	{
		var txt = "insert into `"+name_table+"` (`"+name_fild+"`) values ('"+name+"')"
	}else{
		var txt = "update `"+name_table+"` set `"+name_fild+"` = '"+name+"' where `"+id_table+"` = '"+id_type_page+"'";
	}
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:0,txt:txt},
		success:function()
		{
			success();
		}
	})

}
function success()
{
	$('.modal').modal('hide');
	sweetAlert("تمت الاضافة بنجاح","","success");
	$('#example1').DataTable().ajax.reload();
	id_type_page = 0;

}
function get_job(id)
{
	id_type_page = id;
	var txt = "select * from `jobs` where `id_job` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_job);
			$('#modal_type_1').modal('show');
		}
	})
}
function get_cat(id)
{
	id_type_page = id;
	var txt = "select * from `cat` where `id_cat` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_cat);
			$('#modal_type_1').modal('show');
		}
	})
}
function get_cata(id)
{
	id_type_page = id;
	var txt = "select * from `cata` where `id_cata` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_cata);
			$('#modal_type_1').modal('show');
		}
	})
}
function get_exp(id)
{
	id_type_page = id;
	var txt = "select * from `expen` where `id_expense` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_expense);
			$('#modal_type_1').modal('show');
		}
	})
}
function get_inp(id)
{
	id_type_page = id;
	var txt = "select * from `inp` where `id_inp` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_inp);
			$('#modal_type_1').modal('show');
		}
	})
}
function get_sup(id)
{
	id_type_page = id;
	var txt = "select * from `sup` where `id_sup` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_sup);
			$('#modal_type_1').modal('show');
		}
	})
}
function get_branch(id)
{
	id_type_page = id;
	var txt = "select * from `branch` where `id_branch` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_branch);
			$('#modal_type_1').modal('show');
		}
	})
}


function get_area(id)
{
	id_type_page = id;
	var txt = "select * from `area` where `id_area` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{type:1,txt:txt},
		success:function(data)
		{
			$('#name_type_1').val(data[0].name_area);
			$('#modal_type_1').modal('show');
		}
	})
}
