@extends('layouts.admin')

@section('title', 'نظام المبيعات')

@section('content')

<style type="text/css">
	::placeholder {
		color: white;
	}
</style>

			
<script>
document.onkeypress = function(e) {



  var key = e.charCode || e.keyCode || 0;     
  


  if (key == 13) {

new_to_cart(document.getElementById('barcode').value,'1');
    //alert("I told you not to, why did you do it?");
    e.preventDefault();
	document.getElementById('barcode').value = "";
  }



   if(key == "32") {

document.getElementById('submit_btn').click();
    	//alert(document.getElementById('customer_id').value);
    	//alert(document.getElementById('cash2').value);
//confirm_order(document.getElementById('total_amount').value);

get_cart();
  }










}
</script>


<section class="content" style="min-height: 500px;">
	<img src onerror='get_cart();'>

	<div id="cart2" >
		<div class="row">
			<div class="col-md-6 col-lg-4">
				<div class="row mb-2">
					<div class="col">
						<form>
							<input type="text" id="barcode" class="form-control" 
							onmouseout="new_to_cart(this.value,'1');" 
							placeholder=" الباركود ...  " value="">


						</form>
					</div>
					<div class="col">
						<select onclick="" onchange="get_cart();" name="customer_id" id="customer_id" class="form-control">
							<?php 
							$customers = \App\Models\Customer::
							where('type','sale')
							->get();							?>
							@foreach($customers as $r)
							<option value="{{$r->id}}">{{$r->first_name." ".$r->last_name}}</option>
							@endforeach
						</select>
					</div>
				</div>
				<div id="please_wait">
				</div>
				<div class="user-cart2" id="user_cart">
					<div class="card">
						<table class="table table-striped">
							<thead>

								@if(Config::get('app.locale') == "ar")
								<tr>
									<th>المنتج</th>
									<th>الكميـــــــة</th>
									<th class="text-right">السعر</th>
								</tr>
								@else
								<tr>
									<th>Product Name</th>
									<th>Quantity</th>
									<th class="text-right">Price</th>
								</tr>
								@endif
							</thead>
							<tbody>
							
							</tbody>
						</table>
					</div>
				</div>
		

			</div>
			<div class="col-md-6 col-lg-8" >
				<div class="mb-2">
					<div class="row ">


					<div class="col-md-1" style="text-align: center; display: ;">

						<i class="fas fa-home" onclick="show_cats();" style="color: blue; display: ;
font-size: 34px;
cursor: pointer; text-align: left;float: left; "></i> 
							<button class="btn btn-primary form-control"  onclick="show_cats();" style="font-size: 14px; font-weight: bolder; display: none ;">الاقسام الرئيسية</button>
					</div>	

					<div class="col-md-10" style="margin-right: -25px; margin-left: -25px;">



						<input type="text" id="search" onclick='' 
						onkeyup ="search(this.value);" class="form-control" placeholder="{{__('البحث ....')}}" style="text-align: center; border-radius: 100px;

						/* background: #3490dc; color: white !important; */">
					</div>
					<div class="col-md-1" style="text-align: center; display: ;">

						<i class="fas fa-circle" onclick="show_cats();" style="color: blue; display: ;
							font-size: 34px;
cursor: pointer; text-align: left;float: left; visibility: hidden;"></i> 
							<button class="btn btn-primary form-control"  onclick="show_cats();" style="font-size: 14px; font-weight: bolder; display: none ;">Main Categories</button>
					</div>		
					</div>			

				
				</div>
				<div class="order-product">


					<?php 
					$cats = \App\categories::all();
					?>


					<div id="all_categories">
					@foreach($cats as $r)

	<div style="margin: 16px; border-radius: 10px; width: 20%; border: 0; background: none;" class="item"  onclick="get_items('<?php echo $r->id; ?>')">
		<center>
		<img src="<?php /*{{asset('storage')}}/{{$r->img}}*/ ?>{{asset('storage/categories')}}/product.jpg" width="100%" style="width: 156px;
height: 100px;
border: 0px solid gray;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
border-radius: 10px; cursor: pointer;"  alt="">
	</center>
		<h5 style="font-size: 16px;
font-weight: bolder;
text-align: center;
padding: 10px 16px 11px 16px;
border: 0px dashed;
background: #3490dc;
color: white;

border-radius: 100px;
margin-top: 2px;

 ">{{$r->title}}</h5>
	</div>

							@endforeach



					<?php /*
					 <div class="item" onclick="get_items('<?php echo $r->id; ?>')"><img src="{{asset('storage')}}/{{$r->img}}" alt="">
						<h5>{{$r->title}}</h5></div>
						*/ ?>





					</div>

					<div id="all_items"></div>

				
			
				</div>
			</div>
		</div>
	</div>
</section>


<script type="text/javascript">

function get_discount(discount) {

	document.getElementById('total_amount2').innerHTML= parseFloat(document.getElementById('total_amount').value) - parseFloat(discount);

	if(discount == 0) {
		document.getElementById('net_profit').value = parseInt(document.getElementById('net_profit2').value);
	}
	else {
		document.getElementById('net_profit').value =
		 parseInt(document.getElementById('net_profit2').value) - parseInt(discount);
	}


}

function show_cats() {

	document.getElementById("all_items").style.display = "none"; 
	document.getElementById("all_categories").style.display = "block";

}


var ajax = false;
ajax = new XMLHttpRequest();
function get_items(id) 
{

 //alert(document.getElementById('category_id').value);

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {

    if(ajax.readyState == 1 || 2 || 3) {
        document.getElementById("all_items").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	  document.getElementById("all_categories").style.display = "none"; 
		document.getElementById("all_items").style.display = "block"; 

      document.getElementById("all_items").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "{{url('/all_items')}}"+"/"+id, true);
  xhttp.send();

}


















//*************************************************************************

var ajax3 = false;
ajax3 = new XMLHttpRequest();
function change_price(id,price) 
{

 //alert(document.getElementById('category_id').value);

  var xhttp3 = new XMLHttpRequest();
  xhttp3.onreadystatechange = function() {

    if(ajax3.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

    }
  };
  xhttp3.open("GET", "{{url('/change_price')}}"+"/"+id+"/"+price, true);
  xhttp3.send();

}



//*************************************************************


var ajax2 = false;
ajax2 = new XMLHttpRequest();
function new_to_cart(id,quantity) 
{


	if(id != "")
 //alert(document.getElementById('category_id').value);

  var xhttp2 = new XMLHttpRequest();
  xhttp2.onreadystatechange = function() {

    if(ajax2.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

     document.getElementById("submit_btn").style.visibility = "visible"; 

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

      //  document.getElementById("the_quantity").value = "";

document.onkeypress = function(e) {

	
  var key = e.charCode || e.keyCode || 0;     
  

  		if(key == 13) {


  		document.getElementById("search").value = "search";
  		document.getElementById("search").focus();
        document.getElementById("search").select();

     	//   document.getElementById("search").focus();
  		}
  		else {

  		}
  	
	}

	
        document.getElementById("the_quantity").focus();
        document.getElementById("the_quantity").select();




    }
  };
  xhttp2.open("GET", "{{url('/new_to_cart')}}"+"/"+id+"/"+quantity, true);
  xhttp2.send();

}


//*************************************************************************

var ajax3 = false;
ajax3 = new XMLHttpRequest();
function delete_from_cart(id) 
{

 //alert(document.getElementById('category_id').value);

  var xhttp3 = new XMLHttpRequest();
  xhttp3.onreadystatechange = function() {

    if(ajax3.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

    }
  };
  xhttp3.open("GET", "{{url('/delete_from_cart')}}"+"/"+id, true);
  xhttp3.send();

}




//*************************************************************************

var ajax4 = false;
ajax4 = new XMLHttpRequest();
function get_cart() 
{

 //alert(document.getElementById('category_id').value);

  var xhttp4 = new XMLHttpRequest();
  xhttp4.onreadystatechange = function() {

    if(ajax4.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

      document.getElementById('search').focus();

    }
  };
  xhttp4.open("GET", "{{url('/get_cart')}}", true);
  xhttp4.send();

}



//*************************************************************************

var ajax5 = false;
ajax5 = new XMLHttpRequest();
function confirm_order(amount) 
{

 document.getElementById("submit_btn").style.visibility = "hidden"; 


if(amount == 0) {
	alert('الرجاء اخيار  الطلبات أولا ثم الاعتماد ');
}

var bankak2 = document.getElementById('bankak').value;
var cash2 = document.getElementById('cash').value;
var shake2 = document.getElementById('shake').value;
var c_id2 = document.getElementById('customer_id').value;
var special2 = document.getElementById('special').value;


if(((bankak2 + cash2 + shake2) < amount) && c_id2 == 4) {
alert('لا يمكنك اعتماد الفاتورة   , المبلغ المدفوع اقل من المبلغ المطلوب , الرجاء اختيار العميل أولا');
}

/*
if(special == 1 && c_id == 4) {
	alert('لقد اخترت فاتورة خاصة الرجاء اختيار اسم العميل ');
}*/

//if(amount > 0 && special == 0 || (special == 1 && c_id2 != 4))
if(amount > 0)

if((bankak2 + cash2 + shake2) >= amount || c_id2 > 4)

if( confirm('هل أنت متأكد من كل البيانات وترغب باعتماد  الطلب ؟'))

var c_id = document.getElementById('customer_id').value;
var discount = document.getElementById('discount').value;
var net_profit = document.getElementById('net_profit').value;

var bankak = document.getElementById('bankak').value;
var cash = document.getElementById('cash').value;
var shake = document.getElementById('shake').value;
var special = document.getElementById('special').value;

<?php 
/*
$banks = \App\banks::all();
foreach($banks as $r) {
?>

var bank_<?php echo $r->id ?> = document.getElementById('bank_<?php echo $r->id ?>').value;


<?php } */ ?>



  var xhttp5 = new XMLHttpRequest();
  xhttp5.onreadystatechange = function() {

    if(ajax5.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }

    if (this.readyState == 4 && this.status == 200) {

    var win = window.open('<?php echo url('admin/last_ticket'); ?>','','width=400,height=500'); 

    win.focus(); // necessary for IE >= 10*/
    win.print();

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
 		get_cart();

      document.getElementById("please_wait").innerHTML =
      "";


    }
  };
  xhttp5.open("GET", "{{url('/confirm_order')}}" +"/"+c_id+"/"+amount+"/"+discount+"/"+net_profit+"/"+bankak+"/"+cash+"/"+shake+"/"+special, true);
  xhttp5.send();

}




//*************************************************************************

var ajax6 = false;
ajax6 = new XMLHttpRequest();
function delete_all_cart() 
{


if( confirm('هل أنت متأكدد أنك تريد الغاء جميع   الطلبات ؟ '))


 //alert(document.getElementById('category_id').value);

  var xhttp6 = new XMLHttpRequest();
  xhttp6.onreadystatechange = function() {

    if(ajax6.readyState == 1 || 2 || 3) {
        document.getElementById("please_wait").innerHTML =
      "الرجاء الانتظار  ...";
    }
    if (this.readyState == 4 && this.status == 200) {

 	 // document.getElementById("all_categories").style.display = "none"; 
	  // document.getElementById("all_items").style.display = "block"; 
	  // get_carts();

 
      document.getElementById("user_cart").innerHTML =
      this.responseText;

      document.getElementById("please_wait").innerHTML =
      "";

    }
  };
  xhttp6.open("GET", "{{url('/delete_all_cart')}}", true);
  xhttp6.send();

}


//*************************************************************************

var ajax7 = false;
ajax7 = new XMLHttpRequest();
function search(name) 
{

	if(name == "") {
		show_cats();
	}

		if(name != "")
 //alert(document.getElementById('category_id').value);

  var xhttp7 = new XMLHttpRequest();
  xhttp7.onreadystatechange = function() {

    if(ajax.readyState == 1 || 2 || 3) {
        document.getElementById("all_items").innerHTML =
      ""; 
    }
    if (this.readyState == 4 && this.status == 200) {

 	  document.getElementById("all_categories").style.display = "none"; 
		document.getElementById("all_items").style.display = "block"; 

      document.getElementById("all_items").innerHTML =
      this.responseText;
    }

  };
  xhttp7.open("GET", "{{url('/search')}}"+"/"+name, true);
  xhttp7.send();

}

function pay_cash() {


document.getElementById('cash').value = document.getElementById('total_amount').value - document.getElementById('discount').value;

					document.getElementById('bankak').value = 0;
					document.getElementById('shake').value = 0;


}
function pay_bankak() {


document.getElementById('bankak').value = document.getElementById('total_amount').value - document.getElementById('discount').value;

					document.getElementById('cash').value = 0;
					document.getElementById('shake').value = 0;


					
}
function pay_shake() {


document.getElementById('shake').value = document.getElementById('total_amount').value - document.getElementById('discount').value;

					document.getElementById('cash').value = 0;
					document.getElementById('bankak').value = 0;


}

</script>

@endsection
