@extends('admin.layouts.app')

@section('title', 'View Batch')

@section('content')
    <div class="page-header">
        <div class="row">
            <div class="col-sm-12">
                <h3 class="page-title">Batch Details</h3>
                <ul class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('batches.index') }}">Batches</a></li>
                    <li class="breadcrumb-item active">Batch Details</li>
                </ul>
            </div>
        </div>

        <div class="d-flex align-items-center gap-2">
            @if(auth()->user()->hasPermission('Batch Create'))
                <a href="{{ route('batches.create') }}" class="btn btn-added">
                    <img src="{{ asset('assets/img/icons/plus.svg') }}" alt="img" class="me-1">Create New
                </a>
            @endif
            {{-- Previous Button --}}
            @if ($previous)
                <a href="{{ route('batches.show', $previous->id) }}" class="btn btn-outline-secondary">
                    &laquo; Previous
                </a>
            @else
                <button class="btn btn-outline-secondary" disabled>&laquo; Previous</button>
            @endif

            {{-- Jump to Specific PO Dropdown --}}
            <div class="dropdown">
                <button class="btn btn-primary dropdown-toggle" type="button" id="jumpToPODropdown"
                    data-bs-toggle="dropdown" aria-expanded="false">
                    Jump to Batch
                </button>
                <ul class="dropdown-menu" aria-labelledby="jumpToPODropdown"
                    style="max-height: 300px; overflow-y: auto; min-width: 300px;">
                    <li class="px-3 py-2">
                        <input type="text" class="form-control form-control-sm" id="poSearchInput"
                            placeholder="Search code..." onkeyup="filterPOs()" oninput="filterPOs()">
                    </li>
                    <li>
                        <hr class="dropdown-divider">
                    </li>
                    @foreach ($allBatchess as $po)
                        <li>
                            <a class="dropdown-item po-item {{ $po->id == $batch->id ? 'active fw-bold' : '' }}"
                                href="{{ route('batches.show', $po->id) }}"
                                data-po-date="{{ $po->created_at->format('d/m/Y') }}">
                                <span>{{ $po->batch_no }}</span>
                            </a>
                        </li>
                    @endforeach
                </ul>
            </div>

            {{-- Next Button --}}
            @if ($next)
                <a href="{{ route('batches.show', $next->id) }}" class="btn btn-outline-secondary">
                    Next &raquo;
                </a>
            @else
                <button class="btn btn-outline-secondary" disabled>Next &raquo;</button>
            @endif

            {{-- Edit Button --}}
            <a href="{{ route('batches.edit', $batch->id) }}" class="btn btn-light">
                <img src="{{ asset('assets/img/icons/edit.svg') }}" alt="Edit">
            </a>

            {{-- Print Button --}}
            {{-- <button class="btn btn-light" onclick="printWithTimestamp()" data-approved ="{{ $batch->is_approved }}"
                id="print-button">
                <img src="{{ asset('assets/img/icons/printer.svg') }}" alt="Edit">
            </button> --}}
        </div>
    </div>

    <div class="row">
        <div class="col-sm-12">
            <div class="card">
                <div class="card-body">
                    <div class="form-row row">
                        <div class="mb-3 col-md-12">
                            <label>Company</label>
                            <input type="text" class="form-control" value="{{ $batch->company->name ?? 'N/A' }}"
                                readonly>
                        </div>
                        <div class="mb-3 col-md-6">
                            <label>Chart Of Account</label>
                            <input type="text" class="form-control"
                                value="{{ $batch->chart_of_account->particular ?? 'N/A' }}" readonly>
                        </div>
                        <div class="mb-3 col-md-6">
                            <label>Sub Chart Of Account</label>
                            <input type="text" class="form-control"
                                value="{{ optional($batch->sub_chart_of_account)->name ?? 'N/A' }}" readonly>
                        </div>
                        <div class="mb-3 col-md-4">
                            <label>Product</label>
                            <input type="text" class="form-control" value="{{ $batch->product->brand_name ?? 'N/A' }}"
                                readonly>
                        </div>

                        <div class="mb-3 col-md-4">
                            <label>Batch No</label>
                            <input type="text" class="form-control" value="{{ $batch->batch_no ?? 'N/A' }}" readonly>
                        </div>

                        <div class="mb-3 col-md-4">
                            <label>Batch Size</label>
                            <input type="text" class="form-control" value="{{ $batch->batch_size ?? 'N/A' }}" readonly>
                        </div>

                        <div class="mb-3 col-md-6">
                            <label>MFG date</label>
                            <input type="text" class="form-control" value="{{ $batch->mfg_date ?? 'N/A' }}" readonly>
                        </div>

                        <div class="mb-3 col-md-6">
                            <label>EXP date</label>
                            <input type="text" class="form-control" value="{{ $batch->exp_date ?? 'N/A' }}" readonly>
                        </div>

                        <div class="col-md-12">
                            <div class="card border-0 mb-0" id="mf-item-group">
                                <div class="mf-item-set">
                                    <div
                                        class="card-header bg-white py-2 d-flex justify-content-between align-items-center">
                                        <h4 class="card-title mb-0">Master Formulation</h4>
                                    </div>
                                    <div class="card-body px-0 pb-0">
                                        <div class="row">
                                            <div class="mb-3 col-md-6">
                                                <label>Product</label>
                                                <input type="text" class="form-control"
                                                    value="{{ $batch->masterFormulation->product_name ?? 'N/A' }}"
                                                    readonly>
                                            </div>
                                            <div class="mb-3 col-md-6">
                                                <label>Batch Size</label>
                                                <input type="text" class="form-control"
                                                    value="{{ $batch->masterFormulation->batch_size ?? 'N/A' }}" readonly>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <div class="card border-0 mb-0" id="item-group">
                                @for ($i = 0; $i < count($batch->items); $i++)
                                    <div class="item-set">
                                        <div
                                            class="card-header bg-white py-2 d-flex justify-content-between align-items-center">
                                            <h4 class="card-title mb-0">Item {{ $i + 1 }}</h4>
                                        </div>
                                        <div class="card-body px-0 pb-0">
                                            <div class="row">
                                                <div class="mb-3 col-md-4">
                                                    <label>Product</label>
                                                    <input type="text" class="form-control"
                                                        value="{{ $batch->product->brand_name ?? null }} | {{ $batch->items[$i]->productVariation->strength ?? null }} | {{ $batch->items[$i]->productVariation->dosageForm->name ?? null }} | {{ $batch->items[$i]->productVariation->packingType->name ?? null }} | {{ $batch->items[$i]->productVariation->packSize->name ?? null }}"
                                                        readonly>
                                                </div>
                                                <div class="mb-3 col-md-4">
                                                    <label>Quantity (Pack)</label>
                                                    <input type="number" class="form-control"
                                                        value="{{ $batch->items[$i]->quantity ?? 'N/A' }}" readonly>
                                                </div>
                                                <div class="mb-3 col-md-4">
                                                    <label>Trade Price</label>
                                                    <input type="number" class="form-control"
                                                        value="{{ $batch->items[$i]->trade_price ?? 'N/A' }}" readonly>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                @endfor
                            </div>
                        </div>

                        <div class="mb-3 col-md-6">
                            <label>Start date</label>
                            <input type="text" class="form-control" value="{{ $batch->start_date ?? 'N/A' }}"
                                readonly>
                        </div>

                        <div class="mb-3 col-md-6">
                            <label>End date</label>
                            <input type="text" class="form-control" value="{{ $batch->end_date ?? 'N/A' }}" readonly>
                        </div>

                        <div class="mb-3 col-md-6">
                            <label>Status</label>
                            <input type="text" class="form-control" value="{{ $batch->status ?? 'N/A' }}" readonly>
                        </div>

                    </div>
                    <a class="btn btn-secondary" href="{{ route('batches.index') }}">Cancel</a>
                </div>
            </div>
        </div>
    </div>
@endsection
