@extends('layouts.admin')

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

@section('content')

<style type="text/css">
	::placeholder {
		color: white;
	}
</style>
<section class="content" style="min-height: 500px;">
	<img src onerror='get_cart_purchase();'>

	<div id="cart2" >
		<div class="row">
			<div class="col-md-6 col-lg-5">
				<div class="row mb-2">
					<div class="col">
						<form>
							<input type="text" class="form-control" id="barcode"
							onclick="new_to_cart_purchase(this.value,'1','0000-01-01','product');" placeholder="Scan Barcode.7.." value="">
						</form>
					</div>
					<div class="col">



						<select onclick="" name="customer_id" id="customer_id" class="form-control">
							<?php 
							/*
							$customers = \App\Models\Customer::
							where('id','4')
							->orwhere('type','purchases')
							->get(); */


							$customers = \App\Models\Customer::where('type','purchases')
							->get();

							?>

							<option value="0"> اختر المورد أولا </option>

							@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-7" >
				<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" 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_purchase')}}"+"/"+id, true);
  xhttp.send();

}


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


var ajax2 = false;
ajax2 = new XMLHttpRequest();
function new_to_cart_purchase(id,quantity,expire_date,type) 
{

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

if(typeof expire_date.value == "undefined") {
	var expire = expire_date;
	//alert('that '.expire)
	//alert('this '.expire);
}
else
{
	var expire = expire_date.value;
	//alert('this '.expire);
}
/*
if (typeof expire_date === 'undefined') {	{
		alert('hi');
	}
	else
	{
		alert(expire_date.value);
	} */

	if(id != "")
  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 =
      "";

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

}


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

var ajax3 = false;
ajax3 = new XMLHttpRequest();
function delete_from_cart_purchase(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_purchase')}}"+"/"+id, true);
  xhttp3.send();

}




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

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

 //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 =
      "";

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

}



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

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

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


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

var c_id2 = document.getElementById('customer_id').value;

if(c_id2 == 0)
{
	alert('الرجاء اختيار المورد أولا ');
}

if(amount > 0 && c_id2 > 0)
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;

<?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_purchase();

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


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

}




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

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


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_purchase')}}", 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();

}




</script>

@endsection
