@include('admin::layouts.header')
<?php
use App\Models\Supliers;
use App\Models\Products;
use App\Models\AdminDetails;
?>
<main id="main-container">
        <!-- Hero -->
        <div class="bg-body-light">
          <div class="content content-full">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center">
              <h1 class="flex-grow-1 fs-3 fw-semibold my-2 my-sm-3">Add GRN</h1>
              <nav class="flex-shrink-0 my-2 my-sm-0 ms-sm-3" aria-label="breadcrumb">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item">Dashboard</li>
                  <li class="breadcrumb-item">GRN</li>
                  <li class="breadcrumb-item active" aria-current="page">Add GRN</li>
                </ol>
              </nav>
            </div>
          </div>
        </div>
        <!-- END Hero -->

        <!-- Page Content -->
        <div class="content">
          <!-- Your Block -->
          <div class="block block-rounded">
            <div class="block-header block-header-default">
              <h3 class="block-title">Add GRN</h3>
            </div>
            <div class="block-content">
            @if(Session::has('success')) <div class="alert alert-success mt-2 mb-2">{{ Session::get('success') }}</div>@endif
            @if(Session::has('fail')) <div class="alert alert-danger mt-2 mb-2">{{ Session::get('fail') }}</div>@endif
            <form class="row" action="" method="POST" >
            @csrf
                    <div class="col-md-3 mb-3">
                      <label class="form-label">GRN Number</label>
                      <input type="text" class="form-control" name="grn_number" value="{{$grnNumber}}" readonly>
                      @if($errors->has("grn_number")) <div class="alert alert-danger mt-2">{{ $errors->first('grn_number') }}</div>@endif
                    </div>
                    <div class="col-md-3 mb-3">
                      <label class="form-label">Invoice No</label>
                      <input type="text" class="form-control" name="invoice_no" value="" >
                      @if($errors->has("invoice_no")) <div class="alert alert-danger mt-2">{{ $errors->first('invoice_no') }}</div>@endif
                    </div>
                    <div class="col-md-3 mb-3">
                      <label class="form-label">Invoice Value</label>
                      <input type="number" class="form-control" name="invoice_no" value="" >
                      @if($errors->has("invoice_no")) <div class="alert alert-danger mt-2">{{ $errors->first('invoice_no') }}</div>@endif
                    </div>
                    <div class="col-md-3 mb-3">
                      <label class="form-label">PO No</label>
                      <input type="text" class="form-control" name="po_no" value="" >
                      @if($errors->has("po_no")) <div class="alert alert-danger mt-2">{{ $errors->first('po_no') }}</div>@endif
                    </div>
                    <div class="col-md-3 mb-3">
                    <label class="form-label">Suplier</label>
                    <select class="js-select2 form-select" id="suplier" name="suplier" style="width: 100%;" data-placeholder="Select a suplier..">
                    <?php if(old('suplier')) { ?> 
                        <option value="{{old('suplier')}}">{{Supliers::where('id', old('suplier'))->value('suplier_name')}}</option>
                    <?php } ?>
                
                    <?php foreach($supliers as $suplier){ ?> 
                        <option value="{{$suplier->id}}">{{$suplier->suplier_name}}</option>
                    <?php } ?>
                    </select>
                      @if($errors->has("suplier")) <div class="alert alert-danger mt-2">{{ $errors->first('suplier') }}</div>@endif
                    </div>
                    <!-- <div class="col-md-3 mb-3">
                      <label class="form-label">Purchase Order Number</label>
                      <input type="text" class="form-control" name="purchase_order_number" value="{{old('purchase_order_number')}}">
                      @if($errors->has("purchase_order_number")) <div class="alert alert-danger mt-2">{{ $errors->first('purchase_order_number') }}</div>@endif
                    </div> -->

                    <div class="col-md-3 mb-3">
                      <label class="form-label">Received Date</label>
                      @php
                          $defaultDate = old('received_date', \Carbon\Carbon::now()->toDateString());
                      @endphp
                      <input type="date" class="form-control" name="received_date" value="{{ $defaultDate }}">

                      @if($errors->has("received_date")) <div class="alert alert-danger mt-2">{{ $errors->first('received_date') }}</div>@endif
                    </div>
                    <!-- <div class="col-md-3 mb-3">
                    <label class="form-label">Warehouse Location</label>
                      <select class="js-select2 form-select" id="warehouse_location" name="warehouse_location" style="width: 100%;" data-placeholder="Select a warehouse..">
                    <?php if(old('warehouse_location')) { ?> 
                        <option value="{{old('warehouse_location')}}">{{old('warehouse_location')}}</option>
                    <?php } ?>
                      <option value="Default">Default</option>
                    </select>
                      @if($errors->has("warehouse_location")) <div class="alert alert-danger mt-2">{{ $errors->first('warehouse_location') }}</div>@endif
                    </div>
                    <div class="col-md-3 mb-3">
                      <label class="form-label">Inspector Name</label>
                      @php
                          $defaultName = old('inspector_name', AdminDetails::where('user_id', Auth::user()->id)->value('name'));
                      @endphp
                      <input type="text" class="form-control" name="inspector_name" value="{{$defaultName}}">
                      @if($errors->has("inspector_name")) <div class="alert alert-danger mt-2">{{ $errors->first('inspector_name') }}</div>@endif
                    </div>
                    <div class="col-md-12 mb-3">
                      <label class="form-label">Notes</label>
                     <textarea  class="form-control" name="notes" rows="3">{{old('notes')}}</textarea>
                      @if($errors->has("notes")) <div class="alert alert-danger mt-2">{{ $errors->first('notes') }}</div>@endif
                    </div> -->
                     <div>
                     <button type="button" class="btn btn-success me-1 mt-1" onclick="AddMoreProducts()">
                            <i class="fa fa-fw fa-plus me-1"></i> Add Product
                    </button>
                    <div class="table-responsive">
                            <table class="table table-bordered table-striped table-vcenter mt-4">
                                <thead>
                                    <tr>
                                        <th>Product</th>
                                        <th>Quantity</th>
                                        <th>Wholesale Purchase Price</th>
                                        <!-- <th>Wholesale Selling Price</th> -->
                                        <th>Retail Selling Price</th>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody id="products-tbody">
                                    <tr>
                                        <td>
              
                                            <select class="js-select2 form-select product-select" name="product[]" style="width: 100%;" data-placeholder="Select a product.." required>
                                                <option></option>
                                                <?php foreach($products as $product){ ?> 
                                                    <option value="{{$product->id}}">{{$product->product_name}}</option>
                                                <?php } ?>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="number" class="form-control" name="quantity[]" required>
                                        </td>
                                        <td>
                                           <input type="number" class="form-control" name="wholesale_purchase_price[]" required>
                                        </td>
                                        <!-- <td>
                                             <input type="number" class="form-control" name="wholesale_selling_price[]" required>
                                        </td> -->
                                       <td>
                                             <input type="number" class="form-control" name="retail_selling_price[]" required>
                                        </td>
     
                                        <td >   <button type="button" class="btn btn-danger btn-xs btn-remove">
                                               X
                                            </button></td>
                                    </tr>
                                </tbody>
                              
                            </table>
                        </div>
                    </div>
                    <div>

                      <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                  </form>
            </div>
          </div>
          <!-- END Your Block -->
        </div>
        <!-- END Page Content -->
</main>
<!-- END Main Container -->
@include('admin::layouts.footer')
<script>
    function AddMoreProducts() {
       /* var newRow = `<tr>
                                        <td>
              
                                            <select class="js-select2 form-select product-select" name="product[]" style="width: 100%;" data-placeholder="Select a product.." required>
                                                <option></option>
                                                <?php foreach($products as $product){ ?> 
                                                    <option value="{{$product->id}}">{{$product->product_name}}</option>
                                                <?php } ?>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="number" class="form-control" name="quantity[]" required>
                                        </td>
                                        <td>
                                           <input type="number" class="form-control" name="wholesale_purchase_price[]" required>
                                        </td>
                                        <td>
                                             <input type="number" class="form-control" name="wholesale_selling_price[]" required>
                                        </td>
                                       <td>
                                             <input type="number" class="form-control" name="retail_selling_price[]" required>
                                        </td>
     
                                        <td >   <button type="button" class="btn btn-danger btn-xs btn-remove">
                                               X
                                            </button></td>
                                    </tr>`;*/
            var newRow = `<tr>
                                        <td>
              
                                            <select class="js-select2 form-select product-select" name="product[]" style="width: 100%;" data-placeholder="Select a product.." required>
                                                <option></option>
                                                <?php foreach($products as $product){ ?> 
                                                    <option value="{{$product->id}}">{{$product->product_name}}</option>
                                                <?php } ?>
                                            </select>
                                        </td>
                                        <td>
                                            <input type="number" class="form-control" name="quantity[]" required>
                                        </td>
                                        <td>
                                           <input type="number" class="form-control" name="wholesale_purchase_price[]" required>
                                        </td>
                                       <td>
                                             <input type="number" class="form-control" name="retail_selling_price[]" required>
                                        </td>
     
                                        <td >   <button type="button" class="btn btn-danger btn-xs btn-remove">
                                               X
                                            </button></td>
                                    </tr>`;
        $('#products-tbody').append(newRow);

        // Initialize select2 for new elements
        $('.js-select2').select2();


    }
    
        document.getElementById('products-tbody').addEventListener('click', function(event) {
        if (event.target.matches('.btn-remove, .btn-remove *')) {
            removeRow(event);
        }
    });
    function removeRow(event) {
        const button = event.target;
        const row = button.closest('tr');
        row.remove();
    }
</script>