@include('dashboard.include.header')

  
  <div class="content-wrapper">
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">Wholesale Orders Packing</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/home">Home</a></li>
              <li class="breadcrumb-item active">Wholesale Orders Packing</li>
            </ol>
          </div>
        </div>
      </div>
    </div>

    <section class="content">
      <div class="container-fluid">
      <div class="card">
        <div class="card-body">
          <div class="row">
          <div class="col-md-12">
          @foreach($item as $data)
                    <table class="table borderless"  border="0">
                        <tbody>
                            <tr>
                            <td class="text-left"><b>Invoice ID - </b>  {{ $data->id}}</td>
                            <td class="text-center"><b>Customer Name - </b>  {{$data->getseholsellername->name}}</td>
                            <td class="text-right"><b>Address - </b> {{$data->getseholsellername->address}}</td>
                            <td class="text-right"><b>Phone - </b> {{$data->getseholsellername->phone}}</td>
                            </tr>
                        </tbody>
                    </table>  
                    <form method="post" id="formFilter">
                    {{csrf_field()}}
                        <input type="hidden" value="{{ $data->id}}" name="id">
                    </form>
            @endforeach
                </div>
          </div>
                    <hr>
        <div class="row">
          <div class="col-md-4">
                    <form method="post" id="barcodeaddpacking">
                        {{csrf_field()}}
                        <input type="hidden" class="form-control" name="id" id="id" value="{{ $cus_id }}" required>
                        <input type="text" class="form-control barcodetxt" name="barcode" id="barcode" placeholder="Barcode" required autofocus>
                    </form>
                </div>
          </div><br>
          <div class="row">
            <div class="col-md-12">
                <table id="example12" data-page-length='1000' class="table table-bordered" style="width:100%;">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Product Name</th>
                            <th>Barcode</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                </table>
            </div>
          </div>



         

          </div>
        </div>

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




  @include('dashboard.include.footer');




<script>

$(document).ready(function() {
  var table = $('#example12').DataTable({
        "ajax": {   "url": '/getwholebarcode',
                    "type": 'POST',
                    "data": function ( d ) {
                    return $('#formFilter').serialize();
                    }
        },
        columns: [
            { data: 'id', name: 'id' },
            {  data: 'productname', name: 'productname' },
            { data: 'barcode', name: 'barcode'},
            { data: 'action', name: 'action', orderable: false, searchable: false}
        ]
    });
} );





// $('input[name=barcode]').change(function() { 
//     $("#barcodeaddpacking").submit();
//  });

$('.barcodetxt').bind("enterKey",function(e){
    $("#barcodeaddpacking").submit();
});

    $('#barcodeaddpacking').on('submit', function(event){
    let ele = this;
        event.preventDefault();
        var form_data = $(this).serialize();
        $.ajax({
            url:"/packingaddwholebarcode",
            method:"POST",
            data:form_data,
            dataType:"json",      
            success: function (data) {
                jQuery.each(data, function(key,value){
                    if(value=="1"){
                        $('#barcode').val("");
                        $("#barcode").focus();
                        swal({
                            title: "Error!",
                            text: "This Barcode is not in the database",
                            icon: "error",
                            button: "OK",
                        });
  
                    }
                    else if(value=="2"){
                        $('#barcode').val("");
                        $("#barcode").focus();
                        swal({
                            title: "Error!",
                            text: "Product Not Matching",
                            icon: "error",
                            button: "OK",
                        });

                    }
                    else if(value=="3"){
                        $('#barcode').val("");
                        $("#barcode").focus();
                        swal({
                            title: "Error!",
                            text: "All Barcode Added successfully",
                            icon: "error",
                            button: "OK",
                        });
                    }
                    else if(value=="4"){
                        window.location.href = '/packwholesale';

                    }
                    else if(value=="5"){
                        $('#barcode').val("");
                        $("#barcode").focus();
                        swal({
                            title: "Error!",
                            text: "All Barcode Add not Complete",
                            icon: "error",
                            button: "OK",
                        });

                    }
                    else{
                        toastr.success('Barcode add successfully!')
                        // location.reload();
                        $('#example12').DataTable().ajax.reload();
                        $('#barcode').val("");
                        $("#barcode").focus();
                    }
                
        });
                
            },
            error: function (jqXHR, textStatus, errorThrown) {
                swal("Warning", "Unable to Process your request", "error");
            },
        })
    });



</script>

<script>
@if (session('success'))
        // swal("Success!","{{session('success')}}","success");
        toastr.success('Your work has been successfully!')

@endif
@if (session('error'))
    swal({
            title: "Error!",
            text: "{{session('error')}}",
            icon: "error",
            button: "OK",
        });
@endif

</script>
