var acc_emp = [];
var type_emp_mode = 0;
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()
{
	$('#name_index_page').text('الموظفين')
	var txt = `<div class="card-body">
	<a  onclick="acc_custem(1,2,0)" 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>اسم الموظف</th>
  <th>الفرع</th>
  <th>الوظيفة</th>
  <th></th>
  </tr>
  </thead>
  <tbody>


  </tbody>

</table>
	 </div>`;
	 $('#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/emp.php",
						   type: "post",  
						   dataType:"JSON",
					   }
					   
		   }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
	   
	 });
				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();

	// var start_rev = $('#start_rev_emp').val();
	// var end_rev = $('#end_rev_emp').val();

    var code = $('#code_emp').val()
    // var price = $('#price_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`,`code_emp`) values('"+name+"','"+id_branch+"','"+id_job+"','"+web_emp+"','"+username+"','"+password+"','"+code+"')";
					$.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`code_emp`= '"+code+"',`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);
            $('#code_emp').val(data[0].code_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");	
            id_type_page = id;		
		}
	})

}
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 = [0,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,data[0].c_15,data[0].c_16,data[0].c_17,data[0].c_18,data[0].c_19,data[0].c_20,data[0].c_21,data[0].c_22];
			var length_emp = acc_emp.length;

			//alert(acc_emp)

			//alert(length_emp)
			for(var a = 0;length_emp >= a;a++)
			{
				var b = a ;
				if(acc_emp[a] == 1)
				{
					$('#c_'+b).prop("checked",true);
				}else{
					$('#c_'+b).prop("checked",false);
				}
			}
			$('#c_emp').modal('show');
		}
	})
}
function save_emp_acc()
{
	acc_emp = [];
	var length_emp = 24;
	for(var a = 0;length_emp >= a;a++)
	{
		push_acc();
		var b = a;
		if($('#c_'+b).is(':checked'))
		{
			acc_emp[a] = 1;
		}else{
			acc_emp[a] = 0;
		}

	}	
	var txt = "update `emp` set `c_1` = '"+acc_emp[1]+"',`c_2` = '"+acc_emp[2]+"',`c_3` = '"+acc_emp[3]+"',`c_4` = '"+acc_emp[4]+"',`c_5` = '"+acc_emp[5]+"',`c_6` = '"+acc_emp[6]+"',`c_7` = '"+acc_emp[7]+"',`c_8` = '"+acc_emp[8]+"',`c_9` = '"+acc_emp[9]+"',`c_10` = '"+acc_emp[10]+"',`c_11` = '"+acc_emp[11]+"',`c_12` = '"+acc_emp[12]+"',`c_13` = '"+acc_emp[13]+"',`c_14` = '"+acc_emp[14]+"' ,`c_15` = '"+acc_emp[15]+"',`c_16` = '"+acc_emp[16]+"',`c_17` = '"+acc_emp[17]+"',`c_18` = '"+acc_emp[18]+"',`c_19` = '"+acc_emp[19]+"',`c_20` = '"+acc_emp[20]+"',`c_21` = '"+acc_emp[21]+"',`c_22` = '"+acc_emp[22]+"' where `id_emp` = '"+id_type_page+"'";
	
	//alert(txt)
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success();
		}
	})
}
function pos_emp()
{
	$('#name_index_page').text('تحضير الموظفين ')
    var txt = `
    <div class="row class_input_pos card" style="background-color:#212529;height:100px;:5px">
    <center class="col-12"> 
	<br/>
			<div class="input-group input-group-lg">
  <div class="input-group-prepend">
    <span class="input-group-text" id="inputGroup-sizing-lg">ادخل الكرت</span>
  </div>
  <input type="password" id="parcode_emp"  class="form-control" onchange="parcode_scan_emp()" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-lg">
</div>
    </center>
    </div>
    <br/>
    <div id="body_rev">

    </div>
    `;
    $('#pg_body').html(txt);
	get_last_emp()
}
function get_last_emp()
{
	$('#body_rev').html('');
    var txt = " select * from `emp` where `type_emp` = '1'";
    $.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 ii = i + 1;

                var code_html = `    
                <div class="row col-12 class_body_rev" >
				<div class="col-1">
				
				<button type="button" class="btn">`+ii+`</button>
				</div>

                <div class="col-3">
                    <h5 style='font-size:15px'>`+response[i].name_emp+` </h5>
                </div>  
                <div class="col-3">
                    <h5 style='font-size:15px'>`+response[i].code_emp+`</h5>
                </div>   				
                <div class="col-2">
                    <h5 style='font-size:15px'>`+response[i].start_emp+`</h5>
                </div> 
			
			`;
                $('#body_rev').prepend(code_html);
            }

        }
    })
}
function parcode_scan_emp()
{
	var x = $('#parcode_emp').val();

	var txt = "select * from `emp` where `code_emp` = '"+x+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			var id_emp = data[0].id_emp;
			var type_emp = data[0].type_emp;
			var start_date = data[0].start_emp
			$('#parcode_emp').val('')
			change_type_emp(id_emp,type_emp,start_date,end_date)


			
		},
		error:function(){
			sweetAlert("تاكد من الكود","","error")
			$('#parcode_emp').val('');
		}
	})

}
function change_type_emp(id_emp_1,s,start,end)
{

	var today = new Date();
	var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
	var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
	var dateTime = date+' '+time;



	if(s == 0)
	{
		var txt = "update `emp` set `type_emp` = '1' , `start_emp` ='"+dateTime+"' where `id_emp` = '"+id_emp_1+"'";

	}
	if(s == 1)
	{
		var txt = "update `emp` set `type_emp` = '0' , `end_emp` ='"+dateTime+"' where `id_emp` = '"+id_emp_1+"'"	
		
		var start_rev = start;
		var end_rev = dateTime;




		var startTime = new Date(start_rev); 
		var endTime = new Date(end_rev);

		var txt_emp = "insert into `rev_emp` (`id_emp`,`start`,`end`,`date`) values ('"+id_emp+"','"+start_rev+"','"+dateTime+"','"+date_today+"')"
		$.ajax({
			url:"model/class/class.php",
			type:"POST",
			dataType:"JSON",
			data:{txt:txt_emp,type:0},
			success:function()
			{
			}

		})
	}

	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
		}


		
	})
	$(document).ready(function () {
		setTimeout( function(){
			get_last_emp()
		},100);
	});
	
}

function add_m_emp(id,s)
{
	id_type_page = id;
	type_emp_mode = s;
	$('#modal_mony_emp').modal('show')
}
function save_mony_emp()
{

	var mony = $('#value_mony_emp').val();
	var commant = $('#commant_mony_emp').val();
	var txt = "INSERT INTO `rev_emp`(`id_emp`, `price`, `date`, `id_emp_e`, `msg_emp`, `type_mony`) VALUES ('"+id_type_page+"','"+mony+"','"+date_today+"','"+id_emp+"','"+commant+"','"+type_emp_mode+"')";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success()
		}


		
	})	
}
function mony_emp_show(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)
		{
			var name = data[0].name_emp;

			var start_text = "الحضور =>"+data[0].start_emp + " الحضور الطبيعي => " + data[0].start_rev_emp ;

			var type = data[0].type_emp;
			if(type == 0)
			{
				var v_type = "تم الانصراف";
				
			}else{
				var v_type = "موجود بالعمل";
			}

			var today = new Date();
			var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
			var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
			var dateTime = date+' '+time;


			var start_rev = data[0].start_emp;
			var end_rev = dateTime;
			
			
			var startTime = new Date(start_rev); 
			var endTime = new Date(end_rev);
			var difference = endTime.getTime() - startTime.getTime(); // This will give difference in milliseconds
			var resultInMinutes = Math.round(difference / 60000);




			var price = data[0].price_emp;

			var diffMs = ( endTime - startTime); // milliseconds between now & Christmas
			var diffDays = Math.floor(diffMs / 86400000); // days
			var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
			var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
			var msg_emp = diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes"

			var price_min = Number(price / 60) * diffMins;


			$('#name_emp_show').val(name)
			$('#start_emp_show').val(start_text)
			$('#type_emp_show').val(v_type)
			$('#m_emp_show').val(price_min)


			$('#emp_show_mony').modal('show')
		}
	})
}




function rev_emp()
{
	
}