var type_play = 1;
var count_s = 0;
var count_mony = 0;
var url_img = "";
var id_memeber = 0;
function open_cus()
{
	id_type_page = 0;
	$('.form-control').val('');
	get_select_member("select * from `member` where active_member = '0'","id_member_cus");
    $('#model_cus').modal('show');
}
function cus()
{
	 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="open_cus()" >
				 +  
				</button>
				</div>
				  <table id="Table1" class="table " cellspacing="0" width="100%">
				   <thead style="">
						  <tr>
							  
							  <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.php",
						type: "post",  
						dataType:"JSON",
						data:{c_2:emp_acc[1],c_3:emp_acc[2]},
						error: function(){
							
						}
					}
				} );
				$('#Table1').DataTable();	
				get_page();
				select_count_cus()
}
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+"',`count_s` = '"+count_s+"',`mony_cus` = '0' ,`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 check_scanner()
// {

// 	var scanner = $('#number_cus').val();
// 	if(id_type_page == 0)
// 	{
// 		var txt = "select * from `cus` where `id_scanner` = '"+scanner+"' and `cus_active` = '0'";
// 	}else{
// 		var txt = "select * from `cus` where `id_scanner` = '"+scanner+"' and `id_cus` != '"+id_type_page+"' and `cus_active` = '0'";
// 	}
// 	$.ajax({
// 		url:"model/class/class.php",
// 		type:"POST",
// 		dataType:"JSON",
// 		data:{txt:txt,type:2},
// 		success:function(data)
// 		{
// 			if(data.num == 0)
// 			{
// 				sweetAlert("الرقم متاح","","success");
// 			}else{
// 				sweetAlert("تم استخدام الرقم من قبل","","error");
// 				$('#number_cus').val('');
// 			}
// 		}
// 	})
// }
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_cus()
{
	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)
		{
			$('.count_cus_all').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)
{
	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()
							{
								success();
							}
						})
					}
				})
			}
		}
	})
}
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("لم يتم حذف العميل","")
	}

}