var type_play = 1;
var count_s = 0;
var count_mony = 0;
var url_img = "";
var id_memeber = 0;
var stay_day_fraze = 0;

var id_member_cus = 0;
function open_cus()
{
	id_type_page = 0;
	if(emp_acc[8] == 0){
		sweetAlert('لا يوجد صلاحيات','','error')
	}else{
		$('.form-control').val('');
		get_select_member("select * from `member` where active_member = '0'","id_member_cus");
		$('#model_cus').modal('show');
	}

}
function push_cus_member()
{
	$('#select_member_cus').html('');
	$('#select_member_cus').append('<option value="0">كل الاشتراكات</option>');
	var txt = "select * from `member` where `active_member` = '0'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response){
			var l = Number(response.length);
			var ll = parseFloat(l - 1)
			for(var i = 0;i<= l;i++)
			{
				var html_code = `
				<option value = `+response[i].id_member+`>`+response[i].name_member+`</option>
				`;
				$('#select_member_cus').append(html_code);
				if(i == ll)
				{
					$('#select_member_cus').val(id_member_cus);
				}
			}
		}
	})
}
function select_member_cus()
{
	
	var x = $('#select_member_cus').val();
	id_member_cus = x;
	cus(x);
}
function cus(s)
{
	
	random_number()


	var txt = `<div class="card-body">
	<h1 class="count_cus_all"></h1>

	<a  onclick="open_cus()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user-plus" aria-hidden="true"></i>
	
	</a>

	<a  onclick="open_cus_del()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user-times" aria-hidden="true"></i></a>

	<a  onclick="cus(0)" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user" aria-hidden="true"></i></a>

	<a  onclick="open_cus_active()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
	</a>
	<select id="select_member_cus" class="form-control" onchange="select_member_cus()">

	</select>




  <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>
  <th></th>
</tr>
  </thead>
  <tbody>


  </tbody>

</table>
	 </div>`;
	 $('#pg_body').html(txt);
	 $(function () {

	   var table = $("#example1").DataTable({
			   dom: 'Bfrtip',
			   //'paging':false,
			   "responsive": true, "lengthChange": true, "autoWidth": false,
			   buttons: ["colvis","copy", "excel", "pdf", "print"],
			   
					   "processing": true,
					   "serverSide": true,
					   "ajax":{
						url :"model/class/cus.php",
						type: "post",  
						dataType:"JSON",
						data:{c_9:emp_acc[9],c_10:emp_acc[10],c_11:emp_acc[11],c_12:emp_acc[12],c_13:emp_acc[13],id_emp:id_emp,s:s},
					   }
					   
		   }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
	   
	 });

				get_page();
				if(s == 0)
				{
					var txt = "select * from `cus` where `cus_active` = '0'";
				}else{
					var txt = "select * from `cus` where `cus_active` = '0' and `id_member` = '"+s+"'";
				}
				select_count(txt,".count_cus_all")
				push_cus_member()
				$("img").click(function () {
					//var img = $(this).attr("src")
					var img = $(this).attr("src");
						$('#img_modal_cus').prop('src','');
				$('#img_modal_cus').prop('src',img);
				$('#img').modal('show');
				});
}
function radio_btn_cus(s)
{
	if(s == 2)
	{
		$('#scanner_div').hide(1000);
		type_play = 0;
	}
	if(s == 3)
	{
		$('#scanner_div').show(1000);
		type_play = 1;
	}
}
function save_cus()
{
	var id_member = $('#id_member_cus').val();
	var txt = "select * from `member` where `id_member` = '"+id_member+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			count_s = data[0].count_game;
			count_mony = data[0].price_game;
			var name = $('#name_cus').val();
			var phone_cus = $('#phone_cus').val();
			var id_member = $('#id_member_cus').val();
			var cus_stop = $('#cus_stop').val();
			get_info_memeber();
			var scanner = $('#number_cus').val();
			if(id_type_page == 0)
			{
				var txt = "insert into `cus` (`name_cus`,`phone_cus`,`type_play`,`id_scanner`,`date_cus`,`id_member`,`count_s`,`member_date`,`mony_cus`,`active_play`,`type_stop`) values('"+name+"','"+phone_cus+"','"+1+"','"+scanner+"','"+date_today+"','"+id_member+"','"+count_s+"','"+date_today+"','"+count_mony+"','1','"+cus_stop+"')";

			}else{
				var txt = "update `cus` set `name_cus` = '"+name+"',`phone_cus` = '"+phone_cus+"',`type_play` = '1',`id_scanner` = '"+scanner+"',`id_member` = '"+id_member+"' ,`type_stop` = '"+cus_stop+"' where `id_cus` = '"+id_type_page+"'";
/* 				$.ajax({
					url:"model/member_change.php",
					type:"POST",
					dataType:"JSON",
					data:{old:id_memeber,new:id_member,id:id_type_page},
					success:function()
					{
						
					}
				}) */
			}
			$.ajax({
				url:"model/class/class.php",
				type:"POST",
				dataType:"JSON",
				data:{txt:txt,type:0},
				success:function()
				{
					success();
				}
			})

		}
	})
}
function get_info_memeber()
{
	var id_member = $('#id_member_cus').val();
	var txt = "select * from `member` where `id_member` = '"+id_member+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			count_s = data[0].count_game;
			count_mony = data[0].price_game;
		}
	})	
}
function check_scanner()
{

	var scanner = $('#number_cus').val();
	vaild_card_check(scanner)

}
function massage_cus(id)
{
	id_type_page = id;
	var txt = "select `massage_cus` from `cus` where `id_cus` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			$('#massage_cus').val(data[0].massage_cus);
			$('#modal_massage_cus').modal('show')
		}
	});

}
function get_massage_rev(id)
{
	var txt = "select `massage_cus` from `cus` where `id_cus` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			if(data[0].massage_cus.length == 0)
			{
				sweetAlert("لا يوجد رساله","","success")

			}else{
				sweetAlert("رساله",data[0].massage_cus,"info")
			}
		}
	});	
}
function save_massage_cus()
{
	var massage = $('#massage_cus').val()

	var txt = "update `cus` set `massage_cus` = '"+massage+"' where `id_cus` = '"+id_type_page+"'"
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:'JSON',
		data:{txt:txt,type:0},
		success:function(data)
		{
			success()
		}
	})
}
function get_cus(id)
{
	id_type_page = id;
	var txt = "select * from `cus` where `id_cus` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
		    $('#name_cus').val(data[0].name_cus);
			$('#phone_cus').val(data[0].phone_cus);
			$('#cus_stop').val(data[0].type_stop);
			//$('#number_cus').val(data[0].id_scanner);
			if(data[0].type_play == 0)
			{
				$('#m1').prop("checked",true);
				radio_btn_cus(2);
			}
			if(data[0].type_play == 1)
			{
				$('#m2').prop("checked",true);
				radio_btn_cus(3);
				$('#number_cus').val(data[0].id_scanner);
			}
			id_memeber = data[0].id_member;
			get_select_member("select * from `member` where `active_member` = '0'","id_member_cus");
			push_option(data[0].id_member,"id_member_cus","model_cus");	
			
		}
	})
}
function photo(id)
{
	id_type_page = id;
	$('#old_img').html('');
	$('#results').html('');
	$('#button_save').hide();
	var txt = "select img from `cus` where `id_cus` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			if(data[0].img == "")
			{

			}else{
				$('#old_img').html('<img src="'+data[0].img+'"/>');
			}
			if(data[0].img.length < 40)
			{
				$('#old_img').html('<img src=assets/img/'+data[0].img+'>');

			}
			Webcam.set({
				width: 320,
				height: 240,
				image_format: 'jpeg',
				jpeg_quality: 90,        
			});
			Webcam.attach( '#my_camera' );
			$('#modal_img_cus').modal('show');
		}

	})
}
function take_snapshot() {
	Webcam.snap( function(data_uri) {
		url_img = data_uri;
		$('#results').html('<img src="'+data_uri+'"/>')
		$('#button_save').show();
	} );
}
function save_snapshot()
{
	var txt = "update `cus` set `img` = '"+url_img+"' where `id_cus` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(data)
		{
			success();
			Webcam.class();
		}
	})
}
function change_input_mony()
{
	var x = $('#mony_input').val();
	var z = $('#all_mony').val();
	var t = z - x;
	if(t < 0)
	{
		sweetAlert("Bad job!", "لا يجب الزيادة عن الحد الادنى", "error");
		$('#mony_input').val(z);
		$('#stay_mony').val('0');
	}else{

		$('#stay_mony').val(t);
	}
}
function open_mony(id)
{
	id_type_page = id;
	var txt = "select * from `cus` where `id_cus` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			id_memeber = data[0].id_member;
			if(data[0].mony_cus == 0)
			{
				sweetAlert("Good job!", "لا يوجد مستحقات", "success");
			}else{
				$('#stay_mony').val(0);
				$('#all_mony').val(0);
				$('#mony_input').val('');
				setTimeout(function() {
					$('#mony_input').focus()
					}, 1000);
				$('#mony_cus').modal('show');
				$('#mony_input').focus();
				$('#stay_mony').val(data[0].mony_cus);
				$('#all_mony').val(data[0].mony_cus);
			}

		}
	});
}
function select_count(txt,id_selector)
{
	//var txt = "select * from `cus` where `cus_active` = '0'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:2},
		success:function(data)
		{
			$(id_selector).html(data.num);
		}
	})
}

function save_mony_cus()
{
	$('#mony_cus').modal('hide');
	var x = $('#mony_input').val();
	var t = $('#all_mony').val();
	var s = $('#stay_mony').val();
	anti_d(id_emp,id_type_page,t,x,s,id_memeber,date_today)

}
function anti_d(id_emp,id_type_page,t,x,s,id_memeber,date_today)
{
	var txt = "INSERT INTO `stock_mony`( `id_user`, `id_cus`, `id_safe`, `total`, `paid`, `stay`, `id_member`,`date`)VALUES ('"+id_emp+"','"+id_type_page+"','1','"+t+"','"+x+"','"+s+"','"+id_memeber+"','"+date_today+"');";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			$('#mony_input').val('');
			$('#all_mony').val('');
			$('#stay_mony').val('');
			var txt = "update `cus` set `mony_cus` = '"+s+"' where `id_cus` = '"+id_type_page+"';";
			$.ajax({
				url:"model/class/class.php",
				type:"POST",
				dataType:"JSON",
				data:{txt:txt,type:0},
				success:function()
				{
					// success();
					success()
					var txt = "select * from `stock_mony` order by desc limit 2";
					$.ajax({
						url:"model/class/class.php",
						type:"POST",
						dataType:"JSON",
						data:{txt:txt,type:1},
						success:function(response)
						{
							if(response[0].time == response[1].time)
							{
								del_cus("delete `stock_mony` where `id_stock_mony` = '"+response[1].id_stock_mony+"'")
								console.log("dubler mony id >=" + response[0].id_stock_mony)
							}
							
						}
					})
				}
			})			
		}
	});
}
function del_cus(txt)
{
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function()
		{

		}
	})
}
function ser_cus(id)
{
	id_type_page = id;
	$('#service_cus').modal('show');
	$('.form-control').val('');
}
function save_ser()
{
	var x = $('#mony_ser').val();
	var yy = $('#text_ser').val();
	var txt = "insert into`mony` (`idCus`,`x`,`y`,`date`,`id_user`)values('"+id_type_page+"','"+x+"','"+yy+"','"+date_today+"','"+id_emp+"')";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success();
		}
	});
}
function return_memeber(id)
{
	var c = confirm("هل تريد تجديد الاشتراك");
	if(c)
	{
		id_type_page = id;
	var txt = "select * from `cus` where `id_cus` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			if(data[0].mony_cus > 0)
			{
				sweetAlert("يجب تسديد باقى الاموال","","error");
			}else{
				var txt = id_type_page;
				$.ajax({
					url:"model/class/class.php",
					type:"POST",
					dataType:"JSON",
					data:{txt:data[0].id_cus,type:3,date:date_today},
					success:function(data)
					{
						if(data.type == 0)
						{
							var new_count = parseFloat(Number(data.count_game) + Number(data.count_s))
							var txt = "update `cus` set `count_s` = '"+new_count+"',`mony_cus` = '"+data.price_game+"' where `id_cus` = '"+id_type_page+"'";
						}
						if(data.type == 1)
						{
							var txt = "update `cus` set `member_date` = '"+date_today+"',`mony_cus` = '"+data.price_game+"' where `id_cus` = '"+id_type_page+"'";
						}
						if(data.type == 2)
						{
							//var new_count = parseFloat(Number(data.count_game) + Number(data.count_s))
							var new_count = data.count_game;
							var txt = "update `cus` set `member_date` = '"+date_today+"',`count_s` = '"+new_count+"' ,`mony_cus` = '"+data.price_game+"' where `id_cus` = '"+id_type_page+"'";
						}
						$.ajax({
							url:"model/class/class.php",
							type:"POST",
							dataType:"JSON",
							data:{txt:txt,type:0},
							success:function()
							{
								return_cus_save(id_type_page)
								success();
							}
						})
					}
				})
			}
		}
	})	
	}else{
		sweetAlert("لم يتم تجديد الاشتراك","","")
	}

}
function return_cus_save(id)
{
	var txt = "INSERT INTO `return_cus`( `id_cus`,`date`,  `id_emp`) VALUES ('"+id+"','"+date_today+"','"+id_emp+"')";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(){

		}
	})
}
function change_cus(id)
{
	id_type_page = id;
	var txt = "select * from `cus` where `id_cus` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			$('#change_cus').modal('show');
			$('#c_chaneg').val(data[0].count_s);
			$('#m_change').val(data[0].mony_cus);
			$('#d_change').val(data[0].member_date);
		}
	})
}
function save_change()
{
	
	var c = $('#c_chaneg').val();
	var m = $('#m_change').val();
	var d = $('#d_change').val();
var txt = "update `cus` set `count_s` = '"+c+"',`mony_cus` = '"+m+"' , `member_date` = '"+d+"' where `id_cus` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success();
		}
	})
}
function del_cus(id)
{
	id_type_page = id;
	
	var end_cus = confirm("Are You Sure");
	if(end_cus)
	{
		var txt = "update cus set `cus_active` = '1' , `id_scanner` = '' where `id_cus` = '"+id+"' ";
		$.ajax({
			url:"model/class/class.php",
			type:"POST",
			dataType:"JSON",
			data:{txt:txt,type:0},
			success:function()
			{
				success();
			}
		})		
	}else{
		sweetAlert("لم يتم حذف العميل","")
	}

}
function save_inv_cus(){
	var name_cus = $('#name_cus_inv').val()
	var name_inv = $('#name_inv_inv').val()
	var phone_cus = $('#phone_cus_inv').val()
	var comment_inv = $('#comment_cus_inv').val()

	var txt = "insert into `inv` (`name_cus`,`phone_cus`,`name_inv`,`date_inv`,`comment_inv`) values ('"+name_cus+"','"+phone_cus+"','"+name_inv+"','"+date_today+"','"+comment_inv+"')";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(data){
			success()
		}
	});
}
function add_cus_inv(){
	$('#modal_cus_inv').modal('show');
	$('.form-control').val('')
}
function phone_inv_check(){
	var inv_phone = $('#phone_cus_inv').val();

	var txt = "select * from `inv` where `phone_cus` = '"+inv_phone+"'"
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:2},
		success:function(data){
			if(data.num != 0){
				$('#phone_cus_inv').val('')
				sweetAlert("تم استخدام الرقم من قبل","","")
			}
		}
	})
}
function open_cus_inv()
{
		var txt = `<div class="row">
	<div class="c_2">
   <h1 class="count_cus_all"></h1>
	   <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="add_cus_inv()" >
		+  
	   </button>
	   <button style="" type="button" class="btn btn-success " onclick="open_cus_del()" >
	   العملاء المحذفون
	  </button>
	  <button style="" type="button" class="btn btn-success " onclick="cus(0)" >
	  العملاء 
	 </button>
	  <button style="" type="button" class="btn btn-success " onclick="open_cus_active()" >
	  العملاء الموقوفون 
	 </button>
	 <button style="" type="button" class="btn btn-success " onclick="open_cus_inv()" >
الدعوات
</button>
<button style="" type="button" class="btn btn-success " onclick="open_cus_fraze()" >
تجميد الاشتراكات
</button>
	   </div>
		 <table id="Table1" class="table " cellspacing="0" width="100%">
		  <thead style="">
				 <tr>
					 <th>#</th>
					 <th>اسم العميل</th>
					 <th>رقم الهاتف</th>
					 <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,
			"order": [[ 0, "desc" ]],
		   "ajax":{
			   url :"model/class/cus_inv.php",
			   type: "post",  
			   dataType:"JSON",
			   data:{},
			   error: function(){
				   
			   }
		   }
	   } );
	   $('#Table1').DataTable();	
	   get_page();
	   var txt = "select * from `inv` where `active_inv` = '0'";
	   select_count(txt,".count_cus_all")	
}
function del_cus_inv(id){
	var txt = "update `inv` set `active_inv` = '1' where `id_inv` = '"+id+"'"
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(){
			//success()
			open_cus_inv()
		}
	})
}
function open_cus_active()
{
	var txt = `<div class="card-body">
	<h1 class="count_cus_all"></h1>

	<a  onclick="open_cus()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user-plus" aria-hidden="true"></i>
	
	</a>

	<a  onclick="open_cus_del()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user-times" aria-hidden="true"></i></a>

	<a  onclick="cus(0)" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user" aria-hidden="true"></i></a>

	<a  onclick="open_cus_active()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
	</a>
	<select id="select_member_cus" class="form-control" onchange="select_member_cus()">

	</select>




  <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>
  <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/cus_del_1.php",
						 type: "post",  
						 dataType:"JSON",
						}
						
			}).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
		
	  });
	
	   get_page();
	   var txt = "select * from `cus` where `cus_active` = '5'";
	   select_count(txt,".count_cus_all")	
}
function open_cus_del()
{
	var txt = `<div class="card-body">
	<h1 class="count_cus_all"></h1>

	<a  onclick="open_cus()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user-plus" aria-hidden="true"></i>
	
	</a>

	<a  onclick="open_cus_del()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user-times" aria-hidden="true"></i></a>

	<a  onclick="cus(0)" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-user" aria-hidden="true"></i></a>

	<a  onclick="open_cus_active()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
	</a>
	<select id="select_member_cus" class="form-control" onchange="select_member_cus()">

	</select>




  <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>
  <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/cus_del.php",

						 type: "post",  
						 dataType:"JSON",
						}
						
			}).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
		
	  });


	   get_page();
	   var txt = "select * from `cus` where `cus_active` = '1'";
	   select_count(txt,".count_cus_all")	
}
function return_active(id)
{
	var txt = "update `cus` set `cus_active` = '0' where `id_cus` = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(data)
		{
			success()
			select_count("select * from `cus` where `cus_active` = '1'",".count_cus_all")	
		}
	})
}

function change_mode(id,s)
{
	var txt = "update `cus` set `cus_active` = '"+s+"' where `id_cus`  = '"+id+"' ";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success()
		}
	})	
}
function end_cus_rev()
{
	//var txt = "select * from `cus` where `cus_active` = '0' order by `count_s` asc";
	var txt = `<div class="row">
	<div class="c_2">
</div>
		 <table id="Table1" class="table " 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,
			"order": [[ 3, "asc" ]],
		   "ajax":{
			   url :"model/class/cus_end.php",
			   type: "post",  
			   dataType:"JSON",
			   data:{c_2:emp_acc[1],c_3:emp_acc[2]},
			   error: function(){
				   
			   }
		   }
	   } );
	   $('#Table1').DataTable();	
	   get_page();
	   $("img").click(function () {
		//var img = $(this).attr("src")
		var img = $(this).attr("src");
			$('#img_modal_cus').prop('src','');
	$('#img_modal_cus').prop('src',img);
	$('#img').modal('show');
	});

}


function c_card(id,card)
{
	id_type_page = id;
	$('#card_cud_id').val(card);
	$('#change_card').modal('show')
}
function save_change_cus()
{
	var card = $('#card_cud_id').val();
	var txt = "update `cus` set `id_scanner` = '"+card+"' where `id_cus` = '"+id_type_page+"'";
	send_data(txt);
}


function cus_end()
{

	//var txt = "select * from `cus` where `cus_active` = '0' order by `count_s` asc";
	var txt = `<div class="row">
	<div class="c_2">
</div>
		<div class="row">
			<div class="col-12">
			<button style="" type="button" class="btn btn-success " onclick="cus_end()" >
			التاريخ
		   </button>
		   <button style="" type="button" class="btn btn-success " onclick="cus_end_count()" >
		   العدد 
		  </button>
			</div>

		</div>
		 <table id="Table1" class="table " cellspacing="0" width="100%">
		  <thead style="">
				 <tr>
					 <th>اسم العميل</th>
					 <th>رقم الهاتف</th>
					 <th>الرقم المسلسل</th>
					 <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,
			"order": [[ 3, "desc" ]],
			"pageLength": 500,
		   "ajax":{
			   url :"model/class/cus_end.php",
			   type: "post",  
			   dataType:"JSON",
			   error: function(){
				   
			   },
			   rowCallback: function( row, data, index ) {
				if (data['column'] <= 0) {
					$(row).hide();
				}
			},
		   }
	   } );
	   $('#Table1').DataTable();	
	   get_page();


}
function cus_end_count()
{
	var txt = `<div class="row">
	<div class="c_2">
</div>
		<div class="row">
			<div class="col-12">
			<button style="" type="button" class="btn btn-success " onclick="cus_end()" >
			التاريخ
		   </button>
		   <button style="" type="button" class="btn btn-success " onclick="cus_end_count()" >
		   العدد 
		  </button>
			</div>

		</div>
		 <table id="Table1" class="table " cellspacing="0" width="100%">
		  <thead style="">
				 <tr>
					 <th>اسم العميل</th>
					 <th>رقم الهاتف</th>
					 <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,
			"order": [[ 3, "asc" ]],
		   "ajax":{
			   url :"model/class/cus_end_count.php",
			   type: "post",  
			   dataType:"JSON",
			   error: function(){
				   
			   },
			   rowCallback: function( row, data, index ) {
				if (data['column'] <= 0) {
					$(row).hide();
				}
			},
		   }
	   } );
	   $('#Table1').DataTable();	
	   get_page();	
	   $("img").click(function () {
		//var img = $(this).attr("src")
		var img = $(this).attr("src");
			$('#img_modal_cus').prop('src','');
	$('#img_modal_cus').prop('src',img);
	$('#img').modal('show');
	});
}

function open_cus_row()
{
	var txt = "select * from `cus` where `cus_active` = '0'";
    $('#pg_body').html(`<div class="row body_cus_row" > </div>`);
	$.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 x = response[i].img;
				if(x.length == 0)
				{
					var img = "images/a1.png";
				}else{
					var img = x;
				}
				if(response[i].mony_cus > 0){
					var class_mony = "danger";
				}else{
					var class_mony = "";
				}
				var html_code = `
				<div class="col-3 ">
				<div class="card btn-`+class_mony+`" style="width: 18rem;padding:5px;margin:5px">
				<img src="`+img+`" class="card-img-top" alt="...">
				<div class="card-body">
					<h6><`+response[i].name_cus+`</h6>
					<a href="#" class="btn btn-primary" onclick="get_cus_info(`+response[i].id_cus+`)">معلومات </a>
				</div>
				</div>
				</div>
				`;

				$('.body_cus_row').append(html_code);
			}
		}
	})
}

function change_discount_cus(){
	var x = Number($('#stay_cus_discount_cus').val())
	var y = Number($('#discount_cus').val())


	var z = parseFloat(x - y)

	if(z < 0){
		sweetAlert("لا يمكن التقليل عن الحد الادني","","")
		$('#discount_cus').val(x)
		$('#after_discount_cus').val(0)
	}else{
		$('#after_discount_cus').val(z)
	}

}

function discount_cus_go(){
	var discount = $('#discount_cus').val();
	var stay = $('#after_discount_cus').val(); 

	var comment = $('#comment_discount_cus').val();

	var txt = "insert into `dscount_cus` (`id_cus`,`id_user`,`value_discount`,`comment_discount`,`date_discount`) values ('"+id_type_page+"','"+id_emp+"','"+discount+"','"+comment+"','"+date_today+"')"
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(data){
			var txt = "update `cus` set `mony_cus` = '"+stay+"' where `id_cus` = '"+id_type_page+"'"
			$.ajax({
				url:"model/class/class.php",
				type:"POST",
				dataType:"JSON",
				data:{txt:txt,type:0},
				success:function(){
					success()
				}
			})
		}
	})
}
function discount_cus(id){
	$('.form-control').html('')
	id_type_page = id;
	var txt = "SELECT * FROM `CUS` where id_cus = '"+id+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data){
			$('#stay_cus_discount_cus').val(data[0].mony_cus)
			$('#after_discount_cus').val(data[0].mony_cus)
			if(data[0].mony_cus != 0){
				$('#body_discount_table').html('')
				var txt = "select * from `dscount_cus` where `id_cus` = '"+id+"' order by `id_discount_cus` desc limit 5";
				$.ajax({
					url:"model/class/class.php",
					type:"POST",
					dataType:"JSON",
					data:{txt:txt,type:1},
					success:function(response){
						var l = response.length;
						if(l == 0){
							var html_code = `
							<center>
								<h1>لا يوجد خصومات</h1>
							</center>
							`;
							$('#body_discount_table').html(html_code)							
						}
						for(var i =0;i <= l;i++){
							var ii = i;
							ii = ii + 1;
							var html_code = `
								<tr>
									<td>`+ii+`</td>
									<td>`+response[i].value_discount+`</td>
									<td>`+response[i].date_discount+`</td>
								</tr>
							`;
							$('#body_discount_table').append(html_code)
						}
					}
				})
				$('#discount_pro').modal('show')
			}else{
				sweetAlert("لا يوجد مستحقات","","");
			}
		},
		error:function(){
			var html_code = `
			<center>
				<h1>لا يوجد خصومات</h1>
			</center>
			`;
			$('#body_discount_table').html(html_code)
		}
	});
}


function open_cus_fraze()
{
	var txt = `<div class="row">
	<div class="c_2">
   <h1 class="count_cus_all"></h1>

	   </div>
		 <table id="Table1" class="table " cellspacing="0" width="100%">
		  <thead style="">
				 <tr>
					 <th>#</th>
					 <th>اسم العميل</th>
					 <th>رقم الهاتف</th>
					 <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,
			"order": [[ 0, "desc" ]],
		   "ajax":{
			   url :"model/class/cus_fraze.php",
			   type: "post",  
			   dataType:"JSON",
			   data:{},
			   error: function(){
				   
			   }
		   }
	   } );
	   $('#Table1').DataTable();	
	   get_page();
	   var txt = "select * from `cus` where `cus_active` = '5'";
	   select_count(txt,".count_cus_all")		
}

function cus_fraze(id)
{
	stay_day_fraze = 0;
	id_type_page = id;
	$.ajax({
		url:"model/class/stay_day.php",
		type:"POST",
		dataType:"JSON",
		data:{id:id,type:0},
		success:function(data)
		{
			if(data.day_end <= 0){
				sweetAlert("","تم انتهاء الاشتراك","info")
			}else{
				$('#stay_fraze').val(data.day_end)
				$('#modal_fraze_cus').modal('show')
				stay_day_fraze = data.day_end
			}

		}
	})
}

function save_fraze_cus()
{
	stay_day_fraze = $('#stay_fraze').val()
	var txt = "update `cus` set `cus_active` = '5' , `date_fraze` = '"+date_today+"' , `day_stay_fraze` = '"+stay_day_fraze+"' , `id_emp_fraze` = '"+id_emp+"' where `id_cus` = '"+id_type_page+"' "
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function(data)
		{
			var txt = "insert into `fr_cus` (`id_cus`, `data_fr`, `stay_day`, `id_emp_s`) values ('"+id_type_page+"','"+date_today+"','"+stay_day_fraze+"','"+id_emp+"')"
			$.ajax({
				url:"model/class/class.php",
				type:"POST",
				dataType:"JSON",
				data:{txt:txt,type:0},
				success:function(data)
				{
					success()
				}
			})
		}
	})
}

function return_active_fraze(id)
{
	id_type_page = id;
	var txt = "select * from `cus` where `id_cus` = '"+id+"'";

	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			var stay = data[0].day_stay_fraze
			var date = data[0].date_fraze

			$('#stay_fraze_cus').val(stay)
			$('#stay_fraze_cus_ro').val(stay)

			$('#date_fraze_cus').val(date)


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

function cahneh_fraze_stay()
{
	var stay_y = Number($('#stay_fraze_cus_ro').val());
	var stay_x = Number($("#stay_fraze_cus").val())

	if(stay_x > stay_y)
	{
		var stay_x = $("#stay_fraze_cus").val(stay_y)
		sweetAlert("","لا يمكن الزياده عن الحد","error")
	}
}

function save_return_fraze_cus()
{
	// $('#second_date').val(output_s[0]);

	var stay = Number($("#stay_fraze_cus").val())
	var date = 
	$.ajax({
		url:"model/class/add_day.php",
		type:"POST",
		dataType:"JSON",
		data:{day:stay},
		success:function(data)
		{
			//alert(data.end_day)
			var txt = "update `cus` set `cus_active` = '0' where `id_cus` = '"+id_type_page+"'";
			$.ajax({
				url:"model/class/class.php",
				type:"POST",
				dataType:"JSON",
				data:{txt:txt,type:0},
				success:function(data)
				{
					var txt = "update `fr_cus` set `id_emp_e` = '"+id_emp+"',`data_fr_end` = '"+date_today+"', `type` = '1'"
					$.ajax({
						url:"model/class/class.php",
						type:"POST",
						dataType:"JSON",
						data:{txt:txt,type:0},
						success:function(data)
						{
							success()
						}
					})
				}
			})
		}
	})

}