var parcode = 0;
var code_pos = "";
// function get_last_rev()
// {
//     $('#body_rev').html('');
//     var txt = " select * from `table_day`left join `cus` on `cus`.`id_cus` = `table_day`.`id_cus` left join `member` on `member`.`id_member` = `cus`.`id_member` where `table_day`.`date_table` = '"+date_today+"' order by `table_day`.`id_table_day`";
//     $.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;
// 				if(response[i].mony_cus == 0)
//                 {
//                     var color = "success"
//                 }else{
// 					var color = "danger"
// 				}
               
//                 var img = response[i].img;
//                 if(img.length > 50)
//                 {
//                     img = img;
//                 }else{
//                     img = "/gym_progegypt/assets/img/"+img;
//                 }
//                 var code_html = `    
//                 <div class="row col-12 class_body_rev" >
// 				<div class="col-1">
				
// 				<button type="button" class="btn btn-`+color+`">`+ii+`</button>
// 				</div>
//                 <div class="col-2" onclick="open_img('`+img+`')">
//                     <img style="height:50px;" src="`+img+`" />
//                 </div>
//                 <div class="col-3">
//                     <h5 style='font-size:15px'>`+response[i].name_cus+` | `+response[i].id_scanner+`</h5>
//                 </div>  
//                 <div class="col-3">
//                     <h5 style='font-size:15px'>الحصص المتبقية = `+response[i].count_s+`</h5>
//                 </div>   				
//                 <div class="col-2">
//                     <h5 style='font-size:15px'>الاموال المتبقية = `+response[i].mony_cus+`</h5>
//                 </div>   
// 				                <div class="col-2">
//                    <button class="btn btn-primary" onclick="change_mode(`+response[i].id_cus+`,5)">ايقاف الاشتراك </button> 
//                 </div>   
// 				<div class="col-2">
//                     <button class="btn btn-primary" onclick="del_r(`+response[i].id_table_day+`)">حذف التحضير</button>       
   
//             </div>
// 			<div class="col-2">
//                     <button class="btn btn-primary" onclick="open_mony(`+response[i].id_cus+`)">استلام نقدية </button>       
   
//             </div>
//                 <div class="col-2">
//                     <button class="btn btn-primary" onclick="get_cus_info(`+response[i].id_cus+`)">معلومات العميل</button>       
   
//             </div>
// 			<div class="col-2">
// 				<button class="btn btn-primary" onclick="ser_cus(`+response[i].id_cus+`)">اضافة خدمة</button>
// 			</div>
// 			<div class="col-2">
//                     <button class="btn btn-primary" onclick="">`+response[i].name_member+`</button>       
   
//             </div>
// 			<div class="col-2">
//                     <button class="btn btn-primary end_cus_revz_`+response[i].id_cus+`" onclick=""></button>       
   
//             </div>
			
// 			`;
//                 $('#body_rev').prepend(code_html);
//             }
//             if(emp_acc[2] == 0)
//             {
//                 $('.c_3').css("display","none");
//             }else{
//                 $('.c_3').css("display","");

//             }
// 			get_page_t();
// 			get_end_cus(response[i].id_cus)
//             $("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 get_end_cus(id)
{
		$.ajax({
		url:"model/class/cus_s.php",
		type:"POST",
		dataType:"JSON",
		data:{id:id},
		success:function(data)
		{
			$('.end_cus_revz_'+id).text(data.end);
		}
		});
}
function pos()
{
    var txt = `
    <div class="row class_input_pos  card-body">
    <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"  class="form-control" onchange="parcode_scan()" 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_rev();
}

// function pos_emp()
// {
//     var txt = `
//     <div class="row class_input_pos " 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"  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_rev_emp();
// }
// function get_last_rev_emp(){
//     var txt = "select * from `rev_emp` left join `emp` on `emp`.`id_emp` = `rev_emp`.`id_emp` where `emp`.`type_rev` = 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 html_code = `
//                 <div class="row col-12 class_body_rev" >
//                 <div class="col-3">
//                     <h5 style='font-size:15px'>`+response[i].name_emp+`</h5>
//                 </div>  
//                     </div>
//                 `;
//                 $('#body_rev').prepend(code_html);
//             }
//         }
//     })
// }

// function parcode_scan_emp(){
//     parcode = $('#parcode').val();
//     var txt = "select * from `emp` where `"
// }

function get_last_rev()
{

    $('#body_rev').html('');
    var txt = " select * from `table_day`left join `cus` on `cus`.`id_cus` = `table_day`.`id_cus` left join `member` on `member`.`id_member` = `cus`.`id_member` where `table_day`.`date_table` = '"+date_today+"' order by `table_day`.`id_table_day`";
    $.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;
				if(response[i].mony_cus == 0)
                {
                    var color = "success"
                }else{
					var color = "danger"
				}
                if(response[i].massage_cus.length == 0)
                {
                    var color1 = "success"
                }else{
					var color1 = "danger"
				}
// start test end rev


                $.ajax({
                    url:'model/class/class.php',
                    type:'POST',
                    dataType:'JSON',
                    data:{txt:response[i].id_cus,type:3},
                    success:function(data)
                    {
                        var a = data.end_d;
                    }
                })


                var img = response[i].img;
                if(img.length > 50)
                {
                    img = img;
                }else{
                    img = "/gym_progegypt/assets/img/"+img;
                }
                var code_html = `    
                <center>
                <div class="row col-12 class_body_rev" >
				<div class="col-3">
				
				<button type="button" class="btn btn-`+color+`">`+ii+`</button>
				<button type="button" class="btn btn-`+color1+`" onclick="push_msg(`+response[i].id_cus+`,'`+response[i].massage_cus+`')"><i class="fa fa-bell" aria-hidden="true"></i>
                </button>

                                				<button type="button" class="btn " onclick="open_mony(`+response[i].id_cus+`)"><i class="fa fa-credit-card" aria-hidden="true"></i>
                </button>
                                                				<button type="button" class="btn " onclick="get_cus_info(`+response[i].id_cus+`)"><i class="fa fa-address-book" aria-hidden="true"></i>
                </button>
                
                
                
				</div>
                <div class="col-1" onclick="open_img('`+img+`')">
                    <img style="height:50px;" src="`+img+`" />
                </div>
                <div class="col-2">
                    <h5 style='font-size:15px'>`+response[i].name_cus+` | `+response[i].id_scanner+`</h5>
                </div>  
                <div class="col-1">
                    <h5 style='font-size:15px'>`+response[i].count_s+`</h5>
                </div>   				
                <div class="col-1">
                    <h5 style='font-size:15px'> `+response[i].mony_cus+` $</h5>
                </div>   
                <div class="col-2">
                <h5 style='font-size:15px'>`+response[i].name_member+`</h5>     
                </div>
                </center>


  
                    


			
			`;
			
                $('#body_rev').prepend(code_html);

            }
            if(emp_acc[2] == 0)
            {
                $('.c_3').css("display","none");
            }else{
                $('.c_3').css("display","");

            }
			get_page_t();
            $("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 alert_massage(m){
    sweetAlert("يوجد رساله متروكه للاعب",m,"info")
}
function parcode_scan()
{

    parcode = $('#parcode').val();
    var mm;
    var m;
    var txt = "select * from `cus` where `id_scanner` = '"+parcode+"'";
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:2},
        success:function(data)
        {

            if(data.num != 0)
            {
                $.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)
                        {
                             mm = 1
                            m = data[0].massage_cus
                        }else{
                             mm = 0
            
                        }
                        if(data[0].cus_active == 0)
                        {
                            if(data[0].active_play == 1)
                            {
                               // alert(parcode);
                                $.ajax({
                                    url:"model/class/class.php",
                                    type:"POST",
                                    dataType:"JSON",
                                    data:{txt:data[0].id_cus,type:3},
                                    success:function(data)
                                    {
                                        //alert(data.type);
                                        if(data.type == 0)
                                        {
                                            if(data.end_c != 0)
                                            {
                                                push_cus(data.id_cus,data.end_c,data.type_rev_cus,data.count_rev_today,mm,m);
                                                // if(mm == 1){
                                                //     alert_massage(m)
                                                // }
                                            }else{
                                                sweetAlert("يجب تجديد الاشتراك","","error")
												run_audio();
										  }
                                        }
                                        if(data.type == 1)
                                        {
                                            if(data.end_c != 0)
                                            {
                                                if(data.end_date == 1)
                                                {
                                                    push_cus(data.id_cus,data.end_c,data.type_rev_cus,data.count_rev_today,mm,m);
                                                    // if(mm == 1){
                                                    //     alert_massage(m)
                                                    // }

                                                }else{
                                                   
												   sweetAlert("يجب تجديد الاشتراك","","error");
													run_audio();
                                                }
                                            }else{
                                                sweetAlert("يجب تجديد الاشتراك","","error");
												run_audio();
											}
                                        }
                                        if(data.type == 2)
                                        {
                                            if(data.end_c != 0)
                                            {
                                                if(data.end_date == 1)
                                                {
                                                    push_cus(data.id_cus,data.end_c,data.type_rev_cus,data.count_rev_today,mm,m);
                                                    // if(mm == 1){
                                                    //     alert_massage(m)
                                                    // }

                                                }else{
                                                    sweetAlert("يجب تجديد الاشتراك","","error");
													run_audio();
												}
                                            }else{
                                                sweetAlert("يجب تجديد الاشتراك","","error");
												run_audio();
											}
                                        }
                                    }
                                })
                            }else{
                                sweetAlert("لم يتم وضع اشتراك","","error")
                            }
                         }
                         if(data[0].cus_active == 1){
                            sweetAlert("تم حذف العميل","","error");
                         }
                         if(data[0].cus_active == 5){
                            sweetAlert("تم تجميد الأشتراك","","error");
                         }
                        //else{
                        //     sweetAlert("تم حذف العميل","","error");
                        // }
                    }
                })
            }else{
                sweetAlert("تحقق من صحة الرقم","","error");
            }
            $('#parcode').val('');
        }
    })
}
function push_cus(id_cus,count,type_rev_cus,count_rev_today,mm,m)
{
    var c = parseFloat(Number(count) - 1);
	get_day_stay(0,id_cus,"")
	
    var txt = "insert into `table_day` (`id_user`,`id_cus`,`date_table`) values ('"+id_emp+"','"+id_cus+"','"+date_today+"')";
    if(count_rev_today >= 1)
    {
        if(type_rev_cus == 1)
        {
            $.ajax({
                url:"model/class/class.php",
                type:"POST",
                dataType:"JSON",
                data:{txt:txt,type:0},
                success:function()
                {
                    var txt = "update `cus` set `count_s` = '"+c+"' where `id_cus` = '"+id_cus+"'";
                    $.ajax({
                        url:"model/class/class.php",
                        type:"POST",
                        dataType:"JSON",
                        data:{txt:txt,type:0},
                        success:function()
                        {

                            get_last_rev();       
                            if(mm == 1){
                                alert_massage(m)
                            }            
                        }
                    })
                }
            })
        }else{
            sweetAlert("تم تحضير العميل اليوم");
        }
    }else{
        $.ajax({
            url:"model/class/class.php",
            type:"POST",
            dataType:"JSON",
            data:{txt:txt,type:0},
            success:function()
            {
                var txt = "update `cus` set `count_s` = '"+c+"' where `id_cus` = '"+id_cus+"'";
                $.ajax({
                    url:"model/class/class.php",
                    type:"POST",
                    dataType:"JSON",
                    data:{txt:txt,type:0},
                    success:function()
                    {
                        get_last_rev();    
                        if(mm == 1){
                            alert_massage(m)
                        }               
                    }
                })
            }
        })
    }
 
    
}
function get_rev()
{
    $('#last_rev').html('');
    var txt = "select * from `table_day` where `id_cus` = '"+id_type_page+"'  ORDER BY `table_day`.`id_table_day` DESC ";
    $.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 html_code = "<tr><td>"+response[i].date_table+"</td></tr>";
                $('#last_rev').append(html_code);
            }
            $('.tt1').css("color","white");
        }
    })
}
function get_paid()
{
    $('#last_paid').html('');
    var txt = "select * from `stock_mony` where `id_cus` = '"+id_type_page+"'  ORDER BY `stock_mony`.`id_stock_mony` DESC ";
    $.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 html_code = "<tr><td>"+response[i].date+"</td><td>"+response[i].paid+"</td></tr>";
                $('#last_paid').append(html_code);
            }
            $('.tt1').css("color","white");
        }
    })
}
function global_cus_info()
{
    var scanner = $('#get_info_cus_1').val();
    var txt = "select * from `cus` where `id_scanner` = '"+scanner+"'";
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{type:2,txt:txt},
        success:function(data)
        {
            $('#get_info_cus_1').val('');
            if(data.num == 0)
            {
                sweetAlert("رقم الكرت خطى تاكد من صحته","","error");
            }else{
                $.ajax({
                    url:"model/class/class.php",
                    type:"POST",
                    dataType:"JSON",
                    data:{txt:txt,type:1},
                    success:function(data)
                    {
                        get_cus_info(data[0].id_cus);
                    }
                })
            }
        }
    })

}

function open_img(img)
{

    // img = img.slice(1);
    
    // alert(img)
    // $('#img_modal_cus').prop('src','');
    // $('#img_modal_cus').prop('src',img);
    // $('#img').modal('show');
}
function get_today()
{
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:"0",type:7},
        success:function(data)
        {
            //alert(data.today);
            date_today = data.today;
            $('#ul_date').text(data.today);
        }
    })
}
function get_day_stay(s,id,class_elem)
{
	$.ajax({
		url:"model/class/stay_day.php",
		type:"POST",
		dataType:"JSON",
		data:{id:id,type:0},
		success:function(data)
		{
			var day = data.day_end;
			if(s == 0)
			{
				var day = data.day_end;
				if(day <= 3)
				{
					sweetAlert("لايام المتبقة",""+day+"","error")
				}
			}
			if(s == 1)
			{
				$('.'+class_elem).text(day);
				$('.end_cus_revz_1'+id).text("نهاية  الأشتراك "+data.end);
			}
		}
	});
}
function run_audio()
{
 //	var audio = new Audio('a1.m4a');
 //	audio.play();
}
function c_img(url)
{
	$('.all').prop('style', 'background-image: url("'+url+'");background-repeat: repeat;');
}
$("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');
});