@if (isset($isEdit) && $isEdit)
    <div class="item-set">
        <div class="card-header bg-light py-2">
            <h4 class="card-title mb-0">{{ $grnItem->title ?? '' }}</h4>
        </div>
        <div class="card-body px-0 pb-0">
            <input type="hidden" name="items[{{ $grnItem->id }}][types]"
                value="{{ $grnItem->product_id ? 'product' : 'raw_material_item' }}">

            <div class="row">
                <div class="mb-3 col-md-12">
                    <label>Description</label>
                    <textarea class="form-control" readonly>{{ $grnItem->title . "\n" . ($grnItem->description ?? '') }}</textarea>

                    <input type="hidden"
                        name="items[{{ $grnItem->id }}][{{ $grnItem->product_id ? 'product' : 'raw_material_item' }}]"
                        value="{{ $grnItem->product_id ?? $grnItem->raw_material_item_id }}">
                </div>

                <div class="mb-3 col-md-3">
                    <label>Code</label>
                    <input type="text" class="form-control" name="items[{{ $grnItem->id }}][code]"
                        value="{{ $grnItem->code }}" readonly>
                </div>

                <div class="mb-3 col-md-3">
                    <label>Supplier Bill No <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" name="items[{{ $grnItem->id }}][supplier_bill_no]"
                        value="{{ old('items.' . $grnItem->id . '.supplier_bill_no', $grnItem->supplier_bill_no) }}"
                        required>
                </div>

                <div class="mb-3 col-md-3">
                    <label>Quantity Ordered</label>
                    <input type="number" class="form-control" name="items[{{ $grnItem->id }}][quantity_ordered]"
                        value="{{ $grnItem->quantity_ordered }}" readonly>
                </div>

                <div class="mb-3 col-md-3">
                    <label>Quantity Already Received</label>
                    <input type="number" class="form-control" name="items[{{ $grnItem->id }}][quantity_received]"
                        value="{{ $grnItem->quantity_ordered - ($grnItem->quantity_received + $grnItem->balance_receivable) }}"
                        readonly>
                </div>

                <div class="col-12 grn-rows-container" data-index="{{ $grnItem->id }}">
                    @forelse ($grnItem->grnItemBatches as $batchIndex => $batch)
                        <input type="hidden"
                            name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][batch_id]"
                            value="{{ $batch->id }}">
                        <div class="row grn-row align-items-end">
                            <div class="mb-3 col-md-auto">
                                <button type="button" class="btn btn-danger remove-grn-row">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </div>

                            <div class="mb-3 col-md">
                                <label>Batch#<span class="text-danger">*</span></label>
                                <input type="text" class="form-control"
                                    name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][batch]"
                                    value="{{ $batch->batch }}" required>
                            </div>

                            <div class="mb-3 col-md">
                                <label>MFG Date<span class="text-danger">*</span></label>
                                <input type="date" class="form-control"
                                    name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][mfg_date]"
                                    value="{{ $batch->mfg_date }}" required>
                            </div>

                            <div class="mb-3 col-md">
                                <label>EXP Date<span class="text-danger">*</span></label>
                                <input type="date" class="form-control"
                                    name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][exp_date]"
                                    value="{{ $batch->exp_date }}" required>
                            </div>

                            <div class="mb-3 col-md">
                                <label>Quantity Receiving<span class="text-danger">*</span></label>
                                <input type="number" class="form-control balance_receivable"
                                    name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][receiving_qty]"
                                    value="{{ $batch->receiving_quantity }}" required
                                    oninput="calculateGoodsRecievingAmount()">
                            </div>

                            <div class="mb-3 col-md">batch
                                <label>Unit Price</label>
                                <div class="input-group">
                                    <input type="number" step="0.01" class="form-control w-50 unit_price"
                                        name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][unit_price]"
                                        value="{{ $batch->unit_price }}" required readonly>
                                    <input type="text" class="form-control"
                                        name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][unit]"
                                        value="{{ $batch->unit }}" required readonly>
                                </div>
                            </div>

                            <div class="mb-3 col-md">
                                <label>Total</label>
                                <input type="number" class="form-control product_total"
                                    name="items[{{ $grnItem->id }}][grn_items][{{ $batchIndex }}][total_amount]"
                                    value="{{ $batch->total_amount }}" required readonly>
                            </div>
                        </div>
                    @empty
                        <div class="row grn-row align-items-end">
                            <div class="mb-3 col-md-auto">
                                <button type="button" class="btn btn-danger remove-grn-row">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </div>

                            <div class="mb-3 col-md">
                                <label>Batch#<span class="text-danger">*</span></label>
                                <input type="text" class="form-control"
                                    name="items[{{ $grnItem->id }}][grn_items][0][batch]" placeholder="Enter Batch#">
                            </div>

                            <div class="mb-3 col-md">
                                <label>MFG Date<span class="text-danger">*</span></label>
                                <input type="date" class="form-control"
                                    name="items[{{ $grnItem->id }}][grn_items][0][mfg_date]"
                                    placeholder="Enter MFG Date">
                            </div>

                            <div class="mb-3 col-md">
                                <label>EXP Date<span class="text-danger">*</span></label>
                                <input type="date" class="form-control"
                                    name="items[{{ $grnItem->id }}][grn_items][0][exp_date]"
                                    placeholder="Enter EXP Date">
                            </div>

                            <div class="mb-3 col-md">
                                <label>Quantity Receiving<span class="text-danger">*</span></label>
                                <input type="number" class="form-control balance_receivable"
                                    name="items[{{ $grnItem->id }}][grn_items][0][receiving_qty]"
                                    placeholder="Enter Quantity" oninput="calculateGoodsRecievingAmount()">
                            </div>

                            <div class="mb-3 col-md">
                                <label>Unit Price</label>
                                <div class="input-group">
                                    <input type="number" step="0.01" class="form-control w-50 unit_price"
                                        name="items[{{ $grnItem->id }}][grn_items][0][unit_price]"
                                        placeholder="Enter Unit Price" readonly>
                                    <input type="text" class="form-control"
                                        name="items[{{ $grnItem->id }}][grn_items][0][unit]"
                                        placeholder="Enter Unit" readonly>
                                </div>
                            </div>

                            <div class="mb-3 col-md">
                                <label>Total</label>
                                <input type="number" class="form-control product_total"
                                    name="items[{{ $grnItem->id }}][grn_items][0][total_amount]"
                                    placeholder="Enter Total" readonly>
                            </div>
                        </div>
                    @endforelse
                </div>

                <div class="mb-3 col-md-auto ms-auto d-flex align-items-end">
                    <button type="button" class="btn btn-secondary add-grn-row" data-index="{{ $grnItem->id }}">
                        <i class="fa fa-plus"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
@else
    <div class="item-set">
        <div class="card-header bg-light py-2">
            <h4 class="card-title mb-0">{{ $item->title ?? '' }}</h4>
        </div>
        <div class="card-body px-0 pb-0">
            <input type="hidden" name="items[{{ $item->id }}][types]"
                value="{{ $item->product_id ? 'product' : 'raw_material_item' }}">

            <div class="row">
                <div class="mb-3 col-md-12">
                    <label for="product_description">Description</label>
                    <textarea class="form-control" name="items[{{ $item->id }}][description]" readonly>{{ $item->title . "\n" . ($item->description ?? '') }}
                    </textarea>

                    <input type="hidden"
                        name="items[{{ $item->id }}][{{ $item->product_id ? 'product' : 'raw_material_item' }}]"
                        value="{{ $item->product_id ?? $item->raw_material_item_id }}">
                </div>

                <div class="mb-3 col-md-3">
                    <label for="product_code">Code</label>
                    <input type="text" class="form-control" name="items[{{ $item->id }}][code]"
                        placeholder="Product Code" required value="{{ $item->code }}" readonly>
                </div>
                <div class="mb-3 col-md-3">
                    <label for="supplier_bill_no">Supplier Bill No
                        <span class="text-danger">*</span></label>
                    <input type="text" class="form-control" name="items[{{ $item->id }}][supplier_bill_no]"
                        {{-- name="supplier_bill_no"  --}} id="supplier_bill_no" placeholder="Supplier Bill No" required
                        value="{{ old('supplier_bill_no') }}">
                </div>
                <div class="mb-3 col-md-3">
                    <label>Quantity Ordered</label>
                    <input type="number" class="form-control" name="items[{{ $item->id }}][quantity_ordered]"
                        placeholder="Quantity Ordered" required value="{{ $item->quantity }}" readonly>
                </div>

                <div class="mb-3 col-md-3">
                    <label>Quantity Received</label>
                    <input type="number" class="form-control" name="items[{{ $item->id }}][quantity_received]"
                        placeholder="Quantity Received" required value="{{ $item->grn_qty_received ?? 0 }}" readonly>
                </div>

                <div class="col-12 grn-rows-container" data-index="{{ $item->id }}">
                    <div class="row grn-row align-items-end">
                        <div class="mb-3 col-md-auto d-none">
                            <button type="button" class="btn btn-danger remove-grn-row">
                                <i class="fa fa-trash"></i>
                            </button>
                        </div>

                        <div class="mb-3 col-md">
                            <label for="batch_id">Batch#<span class="text-danger">*</span></label>
                            <input type="text" name="items[{{ $item->id }}][grn_items][0][batch]"
                                placeholder="Enter Batch#" class="form-control">
                        </div>

                        <div class="mb-3 col-md">
                            <label>MFG Date<span class="text-danger">*</span></label>
                            <input type="date" class="form-control"
                                name="items[{{ $item->id }}][grn_items][0][mfg_date]" required
                                placeholder="Select Manufacturing Date">
                        </div>

                        <div class="mb-3 col-md">
                            <label>EXP Date<span class="text-danger">*</span></label>
                            <input type="date" class="form-control"
                                name="items[{{ $item->id }}][grn_items][0][exp_date]" required
                                placeholder="Select Expiry Date">
                        </div>

                        <div class="mb-3 col-md">
                            <label>Quantity Receiving<span class="text-danger">*</span></label>
                            <input type="number" class="form-control balance_receivable"
                                name="items[{{ $item->id }}][grn_items][0][receiving_qty]"
                                placeholder="Balance Receivable" required value=""
                                oninput="calculateGoodsRecievingAmount()">
                        </div>

                        <div class="mb-3 col-md">
                            <label>Unit Price</label>
                            <div class="input-group">
                                <input type="number" step="0.01" class="form-control w-50 unit_price"
                                    name="items[{{ $item->id }}][grn_items][0][unit_price]"
                                    placeholder="Unit Price" required value="{{ $item->unit_price }}" readonly>

                                <input type="text" class="form-control"
                                    name="items[{{ $item->id }}][grn_items][0][unit]" placeholder="Unit" required
                                    value="{{ $item->unit }}" readonly>
                            </div>
                        </div>

                        <div class="mb-3 col-md">
                            <label>Total</label>
                            <input type="number" class="form-control product_total"
                                name="items[{{ $item->id }}][grn_items][0][total_amount]" placeholder="Total"
                                required value="" readonly>
                        </div>
                    </div>
                </div>

                <div class="mb-3 col-md-auto ms-auto d-flex align-items-end">
                    <button type="button" class="btn btn-secondary add-grn-row" data-index="{{ $item->id }}">
                        <i class="fa fa-plus"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
@endif
