var acc_emp = [];
function open_emp()
{
	id_type_page = 0;
	$('.form-control').val('');
	radio_check(2);
	get_select_job("select * from `jobs` where `active_job` = '0'","id_job_emp");
	get_select_branch("select * from `branch` where `active_branch` = '0'","id_branch_emp");
    $('#model_emp').modal('show');
}
function emp()
{
 	var txt = `<div class="row">
				<button style="font-size:25px;background-color:#fff;border-radius:50%" type="button" class="btn btn-outline-success btn-floating waves-effect waves-light btn-large" onclick="open_emp()" >
				 +  
				</button>
				  <table id="Table1" class="mdl-data-table dataTable" cellspacing="0" width="100%">
				   <thead style="">
						  <tr>
							  <th>اسم الموظف</th>
							  <th>الفرع</th>
							  <th>الوظيفة</th>
							  <th></th>
						  </tr>
					  </thead>
						<tbody>

						</tbody>
				  </table>
			   </div>`;
			   $('#pg_body').html(txt);
			   
				var dataTable = $('#Table1').DataTable( {
							columnDefs: [
					{
						targets: [ 0, 1, 2 ],
						className: 'mdl-data-table__cell--non-numeric'
				}],
					"processing": true,
					"serverSide": true,
					"ajax":{
						url :"model/class/emp.php",
						type: "post",  
						dataType:"JSON",
						error: function(){
							
						}
					}
				} );
				$('#Table1').DataTable();
				get_page();
}
function get_select_job(txt,id_select)
{
	$('#'+id_select).html('');
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response)
		{
			var l = response.length;
			for(var i = 0;i < l;i++)
			{
				var code_html = "<option value="+response[i].id_job+">"+response[i].name_job+"</option>";
				$('#'+id_select).append(code_html);
			}
		}
	})
}
function get_select_branch(txt,id_select)
{
	$('#'+id_select).html('');
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response)
		{
			var l = response.length;
			for(var i = 0;i < l;i++)
			{
				var code_html = "<option value="+response[i].id_branch+">"+response[i].name_branch+"</option>";
				$('#'+id_select).append(code_html);
			}
		}
	})
}
function save_emp()
{
	var name = $('#name_emp').val();
	var id_job = $('#id_job_emp').val();
	var id_branch = $('#id_branch_emp').val();
	if($('#radio_web_emp').is(':checked'))
	{
		var web_emp  = 1;
	}else{
		var web_emp  = 0;
	}
	var username = $('#username_emp').val();
	var password = $('#password_emp').val();
	if(id_type_page == 0)
	{
		var txt = "select * from `emp` where `username` = '"+username+"' and `active_emp` = '0' and`login_emp` = '1'";
		$.ajax({
			url:"model/class/class.php",
			type:"POST",
			dataType:"JSON",
			data:{txt:txt,type:2},
			success:function(data)
			{
				if(data.num == 0)
				{
					var txt = "insert into `emp` (`name_emp`,`id_branch`,`id_job`,`login_emp`,`username`,`password`) values('"+name+"','"+id_branch+"','"+id_job+"','"+web_emp+"','"+username+"','"+password+"')";
					$.ajax({
						url:"model/class/class.php",
						type:"POST",
						dataType:"JSON",
						data:{txt:txt,type:0},
						success:function(data)
						{
							success();
						}
					})
				}else{
					sweetAlert("تم استخدام اسم المستخدم من قبل","","error");
				}
			}
		})
	}else{
		var txt = "select * from `emp` where `username` = '"+username+"' and `active_emp` = '0' and`login_emp` = '1' and `id_emp` != '"+id_type_page+"'";
		$.ajax({
			url:"model/class/class.php",
			type:"POST",
			dataType:"JSON",
			data:{txt:txt,type:2},
			success:function(data)
			{
				if(data.num == 0)
				{
					var txt = "update `emp` set `name_emp` = '"+name+"',`id_branch` = '"+id_branch+"',`id_job` = '"+id_job+"',`login_emp` = '"+web_emp+"',`username` = '"+username+"',`password` = '"+password+"' where `id_emp` = '"+id_type_page+"'";
					$.ajax({
						url:"model/class/class.php",
						type:"POST",
						dataType:"JSON",
						data:{txt:txt,type:0},
						success:function(data)
						{
							success();
						}
					})
				}else{
					sweetAlert("تم استخدام اسم المستخدم من قبل","","error");
				}
			}
		})		
	}

}

function radio_check(id)
{
	if(id == 0)
	{
		$('#addemp').modal('show');
		$('#radio_Nweb_emp').prop("checked",true);
		$('#radio_web_emp').prop("checked",false);
		$('#web_emp').hide(1000);
	}
	
	if(id == 1)
	{
		$('#radio_Nweb_emp').prop("checked",false);
		$('#radio_web_emp').prop("checked",true);
		$('#web_emp').show(1000);
	}
	if(id == 2)
	{
		web = 0;
		$('#model_emp').modal('show');
		$('#radio_Nweb_emp').prop("checked",true);
		$('#radio_web_emp').prop("checked",false);
		$('#web_emp').hide(1000);		
	}
	if(id == 3)
	{
		web = 1;
		$('#model_emp').modal('show');
		$('#radio_Nweb_emp').prop("checked",false);
		$('#radio_web_emp').prop("checked",true);
		$('#web_emp').show(1000);		
	}
}
function get_emp(id)
{
	id_type_page = id;
	var txt = "select * from `emp` where `id_emp` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			open_emp();
			$('#name_emp').val(data[0].name_emp);
			if(data[0].login_emp == 1)
			{
				radio_check(3);
				$('#username_emp').val(data[0].username);
				$('#password_emp').val(data[0].password);
			}
			if(data[0].login_emp == 0)
			{
				radio_check(2);
			}
			push_option(data[0].id_branch,"id_branch_emp","model_emp");			
			push_option(data[0].id_job,"id_job_emp","model_emp");			
		}
	})

}
function acc_login(id)
{
	id_type_page = id;
	acc_emp = [];
	var txt = "select * from `emp` where `id_emp` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			acc_emp = [data[0].c_1,data[0].c_2,data[0].c_3,data[0].c_4,data[0].c_5,data[0].c_6,data[0].c_7,data[0].c_8,data[0].c_9,data[0].c_10,data[0].c_11,data[0].c_12,data[0].c_13,data[0].c_14];
			var length_emp = acc_emp.length;
			for(var a = 0;length_emp >= a;a++)
			{
				var b = a + 1;
				if(acc_emp[a] == 1)
				{
					$('#c_'+b).prop("checked",true);
				}else{
					$('#c_'+b).prop("checked",false);
				}
			}
			$('#acc_emp').modal('show');
		}
	})
}
function save_emp_acc()
{
	acc_emp = [];
	var length_emp = 13;
	for(var a = 0;length_emp >= a;a++)
	{
		push_acc();
		var b = a + 1;
		if($('#c_'+b).is(':checked'))
		{
			acc_emp[a] = 1;
		}else{
			acc_emp[a] = 0;
		}

	}	
	var txt = "update `emp` set `c_1` = '"+acc_emp[0]+"',`c_2` = '"+acc_emp[1]+"',`c_3` = '"+acc_emp[2]+"',`c_4` = '"+acc_emp[3]+"',`c_5` = '"+acc_emp[4]+"',`c_6` = '"+acc_emp[5]+"',`c_7` = '"+acc_emp[6]+"',`c_8` = '"+acc_emp[7]+"',`c_9` = '"+acc_emp[8]+"',`c_10` = '"+acc_emp[9]+"',`c_11` = '"+acc_emp[10]+"',`c_12` = '"+acc_emp[11]+"',`c_13` = '"+acc_emp[12]+"',`c_14` = '"+acc_emp[13]+"' where `id_emp` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success();
		}
	})
}