@extends('layouts.master')

@section('content')
    <!-- Breadcrumb and Page Title -->
    <div class="az-content-breadcrumb">
        <span>Goods Receipts</span>
        <span>Edit Goods Receipts</span>
    </div>
    <h2 class="az-content-title" style="display: inline">Edit Goods Receipts
        <span>
            <a href="{{ route('good_receipts.list') }}" class="btn border" style="float: right">
                <i class="fa-solid fa-bars-staggered"></i> Back to List
            </a>
        </span>
    </h2>
    <div class="row">
        <div class="col-md-12 col-lg-12 col-xl-12">
            <div class="card card-body pd-40">
                <h5 class="card-title mg-b-20">Edit Goods Receipts</h5>
                <form method="post" action="{{ route('good_receipts.update', $goodReceipt->id_good_rec) }}">
                    @csrf
                    @method('PUT')
                    <!-- Error Display -->
                    @if (count($errors) > 0)
                        <div class="p-1">
                            @foreach ($errors->all() as $error)
                                <div class="alert alert-warning alert-danger fade show" role="alert">{{ $error }}
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                            @endforeach
                        </div>
                    @endif

                    <!-- Basic Details -->
                    <div class="row row-sm mg-b-20">
                        <!-- Date, PNo, Truck Number -->
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="az-content-label" for="goods_date">Date</label>
                                <input type="date" name="goods_date" class="form-control"
                                    value="{{ $goodReceipt->date }}" required />
                            </div>
                            @error('goods_date')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="az-content-label">PNo</label>
                                <input type="text" name="p_no" class="form-control"
                                    value="{{ $goodReceipt->p_no }}" />
                            </div>
                            @error('p_no')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="az-content-label">Truck Number</label>
                                <input type="text" name="truck_number" class="form-control"
                                    value="{{ $goodReceipt->goodReceiptTruck->truck_no }}" required />
                            </div>
                            @error('truck_number')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>

                    <!-- Suppliers, Builty Number, Rent Received -->
                    <div class="row row-sm mg-b-20">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="az-content-label">Suppliers</label>
                                <select class="form-control select2" name="suppliers" id="suppliers" required>
                                    <option value="">- Select -</option>
                                    @foreach ($suppliers as $key => $supplier)
                                        <option value="{{ $supplier->id_supplier }}"
                                            {{ $goodReceipt->supplier_id == $supplier->id_supplier ? 'selected' : '' }}>
                                            {{ $supplier->supplier_name }} - {{ $supplier->supplier_name_urdu }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            @error('suppliers')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="az-content-label">Builty Number</label>
                                <input type="text" name="builty_number" class="form-control"
                                    value="{{ $goodReceipt->builty_no }}" />
                            </div>
                            @error('builty_number')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label class="az-content-label" for="rent_received">Rent Received</label>
                                <input type="text" name="rent_received" id="rent_received" class="form-control"
                                    value="{{ $goodReceipt->rent_recieve }}" />
                            </div>
                            @error('rent_received')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>

                    <!-- Details -->
                    <div class="row row-sm mg-b-20">
                        <div class="col-md-4">
                            <label class="az-content-label tx-11 tx-medium tx-gray-600" for="goods_status">Status</label>
                            <select id="goods_status" name="goods_status" class="form-control">
                                <option value="">Select</option>
                                <option value="1" {{ $goodReceipt->status == '1' ? 'selected' : '' }}>Active</option>
                                <option value="0" {{ $goodReceipt->status == '0' ? 'selected' : '' }}>In-Active
                                </option>
                            </select>
                            @error('goods_status')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                        <div class="col-md-8">
                            <div class="form-group">
                                <label class="az-content-label">Details</label>
                                <textarea class="form-control" name="details" id="details" cols="30" rows="10">{{ $goodReceipt->details }}</textarea>
                            </div>
                            @error('details')
                                <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                            @enderror
                        </div>
                    </div>

                    <!-- Items Section -->
                    <hr class="my-5">
                    <div id="items-wrapper">
                        @foreach ($goodReceipt->goodReceiptDetails as $index => $detail)
                            <div class="row row-sm mg-b-20 shadow p-3 bg-light rounded" id="item-row-{{ $index }}">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="az-content-label">Items</label>
                                        <input type="text" name="items[]" class="form-control"
                                            value="{{ $detail->goods_name }}" required />
                                    </div>
                                    @error('items')
                                        <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                                    @enderror
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="az-content-label">Marka</label>
                                        <input type="text" name="marka[]" class="form-control"
                                            value="{{ $detail->marka }}" />
                                    </div>
                                    @error('marka')
                                        <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                                    @enderror
                                </div>
                                <div class="col-md-1">
                                    <div class="form-group">
                                        <label class="az-content-label">Bori</label>
                                        <input type="number" name="bori[]" class="form-control"
                                            value="{{ $detail->bori }}" />
                                    </div>
                                    @error('bori')
                                        <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                                    @enderror
                                </div>
                                <div class="col-md-1">
                                    <div class="form-group">
                                        <label class="az-content-label">Weight</label>
                                        <input type="number" name="weight[]" class="form-control"
                                            value="{{ $detail->weight }}" />
                                    </div>
                                    @error('weight')
                                        <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                                    @enderror
                                </div>
                                <div class="col-md-1">
                                    <div class="form-group">
                                        <label class="az-content-label">Rate</label>
                                        <input type="number" name="rate[]" class="form-control"
                                            value="{{ $detail->rate }}" />
                                    </div>
                                    @error('rate')
                                        <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                                    @enderror
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="az-content-label">Remarks</label>
                                        <textarea class="form-control" name="remarks[]" cols="30" rows="10">{{ $detail->remarks }}</textarea>
                                    </div>
                                    @error('remarks')
                                        <span class="form-text text-danger"><strong>{{ $message }}</strong></span>
                                    @enderror
                                </div>
                            </div>
                        @endforeach
                    </div>

                    <!-- Add More Items Button -->
                    <div class="row row-sm mg-b-20">
                        <div class="col-md-12">
                            <a href="javascript:void(0);" class="btn btn-info" id="add-item"><i
                                    class="fa-solid fa-plus"></i> Add More</a>
                        </div>
                    </div>

                    <!-- Submit and Cancel Buttons -->
                    <button type="submit" class="btn btn-az-primary pd-x-20">Save Changes</button>
                    <a href="{{ route('good_receipts.list') }}" class="btn btn-az-secondary pd-x-20">Cancel</a>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        $(document).ready(function() {
            var itemIndex =
            {{ count($goodReceipt->goodReceiptDetails) }}; // Start from the number of existing items

            $('#add-item').click(function() {
                var newRow = `
                <div class="row row-sm mg-b-20 shadow p-3 bg-light rounded" id="item-row-${itemIndex}">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label class="az-content-label">Items</label>
                            <input type="text" name="items[]" class="form-control" required />
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label class="az-content-label">Marka</label>
                            <input type="text" name="marka[]" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-1">
                        <div class="form-group">
                            <label class="az-content-label">Bori</label>
                            <input type="number" name="bori[]" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-1">
                        <div class="form-group">
                            <label class="az-content-label">Weight</label>
                            <input type="number" name="weight[]" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-1">
                        <div class="form-group">
                            <label class="az-content-label">Rate</label>
                            <input type="number" name="rate[]" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label class="az-content-label">Remarks</label>
                            <textarea class="form-control" name="remarks[]" cols="30" rows="10"></textarea>
                        </div>
                    </div>
                </div>`;
                $('#items-wrapper').append(newRow);
                itemIndex++;
            });
        });
    </script>
@endpush
