@extends('layouts.app')

@section('title', 'View Manufacturing BOM')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ $manufacturingBom->name }} - Version {{ $manufacturingBom->version }}</h3>
                    <div class="card-tools">
                        <a href="{{ route('manufacturing.boms.edit', $manufacturingBom->id) }}" class="btn btn-warning btn-sm">
                            <i class="fas fa-edit"></i> Edit
                        </a>
                        <a href="{{ route('manufacturing.boms.index') }}" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left"></i> Back to BOMs
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <!-- BOM Information -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <h5>BOM Information</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td><strong>Name:</strong></td>
                                    <td>{{ $manufacturingBom->name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Version:</strong></td>
                                    <td>{{ $manufacturingBom->version }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Status:</strong></td>
                                    <td>
                                        @if($manufacturingBom->is_active)
                                        <span class="badge badge-success">Active</span>
                                        @else
                                        <span class="badge badge-secondary">Inactive</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Created By:</strong></td>
                                    <td>{{ $manufacturingBom->creator ? $manufacturingBom->creator->name : 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Created At:</strong></td>
                                    <td>{{ $manufacturingBom->created_at->format('d-m-Y h:i A') }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <h5>Finished Goods</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td><strong>Product:</strong></td>
                                    <td>{{ $manufacturingBom->finishedGoods ? $manufacturingBom->finishedGoods->name : 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <td><strong>SKU:</strong></td>
                                    <td>{{ $manufacturingBom->finishedGoods ? $manufacturingBom->finishedGoods->sku : 'N/A' }}</td>
                                </tr>
                                @php
                                $unit = $manufacturingBom->finishedGoods && $manufacturingBom->finishedGoods->uom
                                ? $manufacturingBom->finishedGoods->uom->name
                                : ($manufacturingBom->basic_unit ?? 'N/A');
                                @endphp

                                <tr>
                                    <td><strong>Basic Unit Amount:</strong></td>
                                    <td>
                                        {{ number_format($manufacturingBom->basic_unit_amount, 2) }}
                                        {{ $unit }}
                                    </td>
                                </tr>


                            </table>
                        </div>
                    </div>

                    <div class="row mb-4">
                        <div class="col-12">
                            <h5>Description</h5>
                            <p>{{ $manufacturingBom->description ?: 'No description provided.' }}</p>
                        </div>
                    </div>

                    <!-- Steps Section -->
                    @if($manufacturingBom->steps->count() > 0)
                    <div class="row mb-4">
                        <div class="col-12">
                            <h5>Manufacturing Steps</h5>
                            <div class="accordion" id="stepsAccordion">
                                @foreach($manufacturingBom->steps as $index => $step)
                                <div class="card">
                                    <div class="card-header" id="heading{{ $step->id }}">
                                        <h6 class="mb-0">
                                            <button class="btn btn-link" type="button" data-toggle="collapse"
                                                data-target="#collapse{{ $step->id }}" aria-expanded="{{ $index === 0 ? 'true' : 'false' }}"
                                                aria-controls="collapse{{ $step->id }}">
                                                Step {{ $step->sequence_order }}: {{ $step->step_name }}
                                                <span class="badge badge-info ml-2">{{ $step->ingredients->count() }} ingredients</span>
                                            </button>
                                        </h6>
                                    </div>
                                    <div id="collapse{{ $step->id }}" class="collapse {{ $index === 0 ? 'show' : '' }}"
                                        aria-labelledby="heading{{ $step->id }}" data-parent="#stepsAccordion">
                                        <div class="card-body">
                                            <p><strong>Instruction:</strong> {{ $step->instruction ?: 'No instruction provided.' }}</p>

                                            @if($step->ingredients->count() > 0)
                                            <h6>Step Ingredients:</h6>
                                            <div class="table-responsive">
                                                <table class="table table-sm table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>Ingredient</th>
                                                            <th>SKU</th>
                                                            <th>Quantity Required</th>
                                                            <th>Unit</th>
                                                            <th>Wastage</th>
                                                            <th>Effective Quantity</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        @foreach($step->ingredients as $ing)
                                                        <tr>
                                                            <td>{{ $ing->ingredient->name }}</td>
                                                            <td>{{ $ing->ingredient->sku }}</td>
                                                            <td>{{ $ing->formatted_quantity }}</td>
                                                            <!-- <td>{{ $ing->unit }}</td> -->
                                                             <td>
    {{ optional($ing->ingredient->uom)->name ?? ($ing->unit ?? 'N/A') }}
</td>

                                                            <!-- <td>
                                                                {{ $ing->ingredient && $ing->ingredient->uom
                                                                  ? $ing->ingredient->uom->name
                                                                 : ($ing->unit ?? 'N/A')
                                                                 }}
                                                            </td> -->

                                                            <td>{{ $ing->formatted_wastage }}</td>
                                                            <td>{{ number_format($ing->effective_quantity, 4) }}</td>
                                                        </tr>
                                                        @endforeach
                                                    </tbody>
                                                </table>
                                            </div>
                                            @else
                                            <p class="text-muted">No ingredients assigned to this step.</p>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- Direct Ingredients Section -->
                    @if($manufacturingBom->ingredients()->directIngredients()->count() > 0)
                    <div class="row mb-4">
                        <div class="col-12">
                            <h5>Direct Ingredients (Not assigned to steps)</h5>
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th>Ingredient</th>
                                            <th>SKU</th>
                                            <th>Quantity Required</th>
                                            <th>Unit</th>
                                            <th>Wastage</th>
                                            <th>Effective Quantity</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($manufacturingBom->ingredients()->directIngredients()->with('ingredient')->get() as $ingredient)
                                        <tr>
                                            <td>{{ $ingredient->ingredient->name }}</td>
                                            <td>{{ $ingredient->ingredient->sku }}</td>
                                            <td>{{ $ingredient->formatted_quantity }}</td>
                                            <td>{{ optional($ingredient->ingredient->uom)->name ?? ($ing->unit ?? 'N/A')}}</td>
                                            <td>{{ $ingredient->formatted_wastage }}</td>
                                            <td>{{ number_format($ingredient->effective_quantity, 4) }}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @endif

                    <!-- Summary -->
                    <div class="row">
                        <div class="col-12">
                            <h5>BOM Summary</h5>
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="info-box">
                                        <span class="info-box-icon bg-info"><i class="fas fa-list"></i></span>
                                        <div class="info-box-content">
                                            <span class="info-box-text">Total Steps</span>
                                            <span class="info-box-number">{{ $manufacturingBom->steps->count() }}</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-box">
                                        <span class="info-box-icon bg-success"><i class="fas fa-box"></i></span>
                                        <div class="info-box-content">
                                            <span class="info-box-text">Total Ingredients</span>
                                            <span class="info-box-number">{{ $manufacturingBom->ingredients->count() }}</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-box">
                                        <span class="info-box-icon bg-warning"><i class="fas fa-layer-group"></i></span>
                                        <div class="info-box-content">
                                            <span class="info-box-text">Direct Ingredients</span>
                                            <span class="info-box-number">{{ $manufacturingBom->ingredients()->directIngredients()->count() }}</span>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-box">
                                        <span class="info-box-icon bg-primary"><i class="fas fa-cube"></i></span>
                                        <div class="info-box-content">
                                            <span class="info-box-text">Unique Ingredients</span>
                                            <span class="info-box-number">{{ $manufacturingBom->ingredients()->distinct('ingredient_account_id')->count() }}</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
