@extends('layouts.app')
@section('title', 'Cost of Production Result')

@section('content')
<div class="page-content">
    <div class="row">
        <div class="col-md-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h6 class="card-title">Cost of Production Result</h6>
                        <a href="{{ route('manufacturing.cost-of-production.index') }}" class="btn btn-outline-primary">
                            <i class="fas fa-arrow-left me-2"></i>Back to Calculator
                        </a>
                    </div>

                    <!-- Summary Cards -->
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <div class="card bg-primary text-white">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="flex-grow-1">
                                            <h6 class="card-title text-white">Finished Goods</h6>
                                            <h4 class="mb-0">{{ $finishedGoods->name }}</h4>
                                            <small>{{ $finishedGoods->sku }}</small>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <i class="fas fa-box fa-2x opacity-75"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="card bg-info text-white">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="flex-grow-1">
                                            <h6 class="card-title text-white">Period</h6>
                                            <h4 class="mb-0">{{ $forMonth->format('M Y') }}</h4>
                                            <small>{{ $forMonth->format('F Y') }}</small>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <i class="fas fa-calendar fa-2x opacity-75"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="card bg-success text-white">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="flex-grow-1">
                                            <h6 class="card-title text-white">Total Cost</h6>
                                            <h4 class="mb-0">₹{{ number_format($totalCost, 2) }}</h4>
                                            <small>Production Cost</small>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <i class="fas fa-rupee-sign fa-2x opacity-75"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="card bg-warning text-white">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="flex-grow-1">
                                            <h6 class="card-title text-white">Avg Cost/Unit</h6>
                                            <h4 class="mb-0">₹{{ number_format($averageCostPerUnit, 2) }}</h4>
                                            <small>Per Unit Cost</small>
                                        </div>
                                        <div class="flex-shrink-0">
                                            <i class="fas fa-calculator fa-2x opacity-75"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Manufacturing Runs Table -->
                    <div class="row">
                        <div class="col-12">
                            <h6 class="mb-3">Manufacturing Runs for {{ $forMonth->format('F Y') }}</h6>
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>M-Number</th>
                                            <th>Planned Output Qty</th>
                                            <th>Cost of Production</th>
                                            <th>Cost per Unit</th>
                                            <th>Created Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @forelse($manufacturingRuns as $run)
                                        <tr>
                                            <td>
                                                <a href="{{ route('manufacturing.runs.show', $run->id) }}" class="text-decoration-none">
                                                    {{ $run->m_number }}
                                                </a>
                                            </td>
                                            <td>{{ number_format($run->planned_output_qty, 4) }}</td>
                                            <td>₹{{ number_format($run->cost_of_production, 2) }}</td>
                                            <td>₹{{ number_format($run->cost_of_production / $run->planned_output_qty, 2) }}</td>
                                            <td>{{ $run->created_at->format('M d, Y H:i') }}</td>
                                        </tr>
                                        @empty
                                        <tr>
                                            <td colspan="5" class="text-center text-muted">No manufacturing runs found for this period</td>
                                        </tr>
                                        @endforelse
                                    </tbody>
                                    @if($manufacturingRuns->count() > 0)
                                    <tfoot>
                                        <tr class="table-dark">
                                            <th>Total</th>
                                            <th>{{ number_format($totalQuantity, 4) }}</th>
                                            <th>₹{{ number_format($totalCost, 2) }}</th>
                                            <th>₹{{ number_format($averageCostPerUnit, 2) }}</th>
                                            <th>{{ $manufacturingRuns->count() }} runs</th>
                                        </tr>
                                    </tfoot>
                                    @endif
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- Calculation Details -->
                    <div class="row mt-4">
                        <div class="col-12">
                            <h6 class="mb-3">Calculation Details</h6>
                            <div class="card">
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <ul class="list-unstyled">
                                                <li><strong>Chart of Account:</strong> {{ $chartOfAccount->name }} ({{ $chartOfAccount->code }})</li>
                                                <li><strong>Total Manufacturing Runs:</strong> {{ $manufacturingRuns->count() }}</li>
                                                <li><strong>Total Quantity Produced:</strong> {{ number_format($totalQuantity, 4) }} units</li>
                                            </ul>
                                        </div>
                                        <div class="col-md-6">
                                            <ul class="list-unstyled">
                                                <li><strong>Total Cost of Production:</strong> ₹{{ number_format($totalCost, 2) }}</li>
                                                <li><strong>Average Cost per Unit:</strong> ₹{{ number_format($averageCostPerUnit, 2) }}</li>
                                                <li><strong>Record Status:</strong>
                                                    @if($costRecord->wasRecentlyCreated)
                                                        <span class="badge bg-success">Newly Created</span>
                                                    @else
                                                        <span class="badge bg-info">Updated</span>
                                                    @endif
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
