@extends('layouts/base_view')

@section('title')
{!! $variables['header_routes'] !!}
@endsection

@section('content')
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">{!! $variables['judul'] !!}</h3>
                        <div class="float-right">
                            <a href="{{ route('fstrs.index') }}" class="btn btn-warning btn-sm button-cek-permission" data-permission="fstrs.index">
                                <i class="fas fa-list"></i> Daftar
                            </a>
                            <a href="{{ route('fstrs.edit', $data->id) }}" class="btn btn-warning btn-sm button-cek-permission" data-permission="fstrs.edit">
                                <i class="fas fa-edit"></i> Ubah
                            </a>
                            <a href="{{ route('fstrs.destroy', $data->id) }}" data='delete' onclick="notificationBeforeDelete(event, this)" class="btn btn-warning btn-sm button-cek-permission" data-permission="fstrs.destroy">
                                <i class="fas fa-trash"></i> {{ $data->delete_flag ? 'Aktifkan' : 'Nonaktifkan' }}
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <!-- Single Card with All Information -->
                            <div class="col-12">
                                <div class="card card-outline card-warning">
                                    <div class="card-body">
                                        <div class="row">
                                            <!-- Code -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Kode</label>
                                                    <div class="mt-1">
                                                        <span class="badge badge-info">{{ $data->code }}</span>
                                                    </div>
                                                </div>
                                            </div>

                                            <!-- Name -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Nama</label>
                                                    <div class="mt-1">{{ $data->name }}</div>
                                                </div>
                                            </div>

                                            <!-- Total Cost -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Total Biaya</label>
                                                    <div class="mt-1">
                                                        <span class="badge badge-info">{{ $data->formatted_total_cost ?? 'Rp ' . number_format($data->total_cost, 0, ',', '.') }}</span>
                                                    </div>
                                                </div>
                                            </div>

                                            <!-- Ingredients Count -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Bahan</label>
                                                    <div class="mt-1">
                                                        <span class="badge badge-info">{{ $data->fstrDetails->count() }} item</span>
                                                    </div>
                                                </div>
                                            </div>

                                            <!-- Status -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Status</label>
                                                    <div class="mt-1">
                                                        @if($data->is_active)
                                                        <span class="badge badge-success">Aktif</span>
                                                        @else
                                                        <span class="badge badge-secondary">Nonaktif</span>
                                                        @endif
                                                    </div>
                                                </div>
                                            </div>

                                            <!-- Average Cost -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Rata-rata Biaya</label>
                                                    <div class="mt-1">
                                                        <span class="badge badge-info">
                                                            Rp {{ $data->fstrDetails->count() > 0 ? number_format($data->total_cost / $data->fstrDetails->count(), 0, ',', '.') : 0 }}
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>

                                            <!-- Created By -->
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Dibuat Oleh</label>
                                                    <div class="mt-1">
                                                        <small class="text-muted">
                                                            {{ $data->creator->fullname ?? 'System' }}<br>
                                                            {{ $data->created_at->format('d M Y, H:i') }}
                                                        </small>
                                                    </div>
                                                </div>
                                            </div>

                                            <!-- Updated By -->
                                            @if($data->modificator)
                                            <div class="col-lg-3 col-md-4 col-sm-6 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Diubah Oleh</label>
                                                    <div class="mt-1">
                                                        <small class="text-muted">
                                                            {{ $data->modificator->fullname }}<br>
                                                            {{ $data->updated_at->format('d M Y, H:i') }}
                                                        </small>
                                                    </div>
                                                </div>
                                            </div>
                                            @endif

                                            <!-- Description (Full Width) -->
                                            @if($data->description)
                                            <div class="col-12 mb-3">
                                                <div class="form-group">
                                                    <label class="font-weight-bold">Deskripsi</label>
                                                    <div class="mt-1">{{ $data->description }}</div>
                                                </div>
                                            </div>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Recipe Details -->
                        @if($data->fstrDetails->count() > 0)
                        <div class="row">
                            <div class="col-12">
                                <div class="card card-outline card-warning">
                                    <div class="card-header">
                                        <h5 class="mb-0">Detail Resep ({{ $data->fstrDetails->count() }} bahan)</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="table-responsive">
                                            <table class="table table-hover table-sm">
                                                <thead class="bg-light">
                                                    <tr>
                                                        <th width="5%">No</th>
                                                        <th width="15%">Kode Item</th>
                                                        <th width="25%">Nama Item</th>
                                                        <th width="10%">Jumlah</th>
                                                        <th width="8%">UOM</th>
                                                        <th width="12%">Biaya UOM</th>
                                                        <th width="12%">Total Biaya</th>
                                                        <th width="13%">Catatan</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @foreach($data->fstrDetails as $index => $detail)
                                                    <tr>
                                                        <td>{{ $index + 1 }}</td>
                                                        <td>{{ $detail->rawMaterial ? $detail->rawMaterial->code : '-' }}</td>
                                                        <td>{{ $detail->rawMaterial ? $detail->rawMaterial->name : '-' }}</td>
                                                        <td class="text-right">{{ number_format($detail->quantity, 4) }}</td>
                                                        <td>{{ $detail->uom }}</td>
                                                        <td class="text-right">Rp {{ number_format($detail->unit_cost, 0, ',', '.') }}</td>
                                                        <td class="text-right"><strong>Rp {{ number_format($detail->total_cost, 0, ',', '.') }}</strong></td>
                                                        <td>{{ $detail->notes ?? '-' }}</td>
                                                    </tr>
                                                    @endforeach
                                                </tbody>
                                                <tfoot class="bg-light">
                                                    <tr>
                                                        <th colspan="6" class="text-right">Total Biaya Resep:</th>
                                                        <th class="text-right">{{ $data->formatted_total_cost ?? 'Rp ' . number_format($data->total_cost, 0, ',', '.') }}</th>
                                                        <th></th>
                                                    </tr>
                                                </tfoot>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<form action="" id="delete-form" method="post">
    @method('delete')
    @csrf
</form>
@endsection