@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('stores.index') }}" class="btn btn-warning btn-sm button-cek-permission" data-permission="stores.index">
                                <i class="fas fa-list"></i> Daftar
                            </a>
                            <a href="{{ route('stores.edit', $data->id) }}" class="btn btn-warning btn-sm button-cek-permission" data-permission="stores.edit">
                                <i class="fas fa-edit"></i> Ubah
                            </a>
                            <a href="{{ route('stores.destroy', $data->id) }}" data='delete' onclick="notificationBeforeDelete(event, this)" class="btn btn-warning btn-sm button-cek-permission" data-permission="stores.destroy">
                                <i class="fas fa-trash"></i> {{ $data->delete_flag ? 'Aktifkan' : 'Nonaktifkan' }}
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <!-- Store Header Info -->
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="40%"><strong>Kode</strong></td>
                                        <td>: <span class="badge badge-info">{{ $data->code }}</span></td>
                                    </tr>
                                    <tr>
                                        <td><strong>Nama</strong></td>
                                        <td>: {{ $data->name }}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Penanggung Jawab</strong></td>
                                        <td>: {{ $data->pic ?: '-' }}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Telepon PJ</strong></td>
                                        <td>: 
                                            @if($data->pic_phone)
                                            <a href="tel:{{ $data->pic_phone }}" class="text-decoration-none">
                                                {{ $data->pic_phone }}
                                            </a>
                                            @else
                                            -
                                            @endif
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><strong>Email PJ</strong></td>
                                        <td>: 
                                            @if($data->pic_email)
                                            <a href="mailto:{{ $data->pic_email }}" class="text-decoration-none">
                                                {{ $data->pic_email }}
                                            </a>
                                            @else
                                            -
                                            @endif
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="40%"><strong>Status</strong></td>
                                        <td>: 
                                            @if($data->status == 'Active')
                                            <span class="badge badge-success">{{ $data->status }}</span>
                                            @else
                                            <span class="badge badge-secondary">{{ $data->status }}</span>
                                            @endif
                                        </td>
                                    </tr>
                                    @if($data->inactive_date)
                                    <tr>
                                        <td><strong>Tanggal Nonaktif</strong></td>
                                        <td>: {{ $data->inactive_date }}</td>
                                    </tr>
                                    @endif
                                    <tr>
                                        <td><strong>Dibuat Oleh</strong></td>
                                        <td>: {{ $data->creator->fullname ?? 'System' }} ({{ $data->created_at->format('d M Y, H:i') }})</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Diubah Oleh</strong></td>
                                        <td>: {{ $data->modificator->fullname ?? '-' }} ({{ $data->updated_at ? $data->updated_at->format('d M Y, H:i') : '-' }})</td>
                                    </tr>
                                    <tr>
                                        <td><strong>Total Item</strong></td>
                                        <td>: {{ $data->storeRawMaterials->where('delete_flag', 0)->count() }} item</td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-12">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="20%"><strong>Alamat</strong></td>
                                        <td>: {{ $data->address ?: '-' }}</td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        @if($data->description)
                        <div class="row">
                            <div class="col-12">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="20%"><strong>Deskripsi</strong></td>
                                        <td>: {{ $data->description }}</td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        @endif

                        <!-- Items Mapping Section -->
                        <div class="row mt-3">
                            <div class="col-12">
                                <div class="card card-outline card-warning">
                                    <div class="card-header">
                                        <h3 class="card-title">
                                            <i class="fas fa-boxes"></i> Pemetaan Item ({{ $data->storeRawMaterials->where('delete_flag', 0)->count() }} item)
                                        </h3>
                                        <div class="card-tools">
                                            <a href="{{ route('store_raw_materials.index') }}?store={{ $data->id }}" class="btn btn-sm btn-warning">
                                                <i class="fas fa-edit"></i> Kelola Pemetaan
                                            </a>
                                        </div>
                                    </div>
                                    <div class="card-body">
                                        @if($data->storeRawMaterials->where('delete_flag', 0)->count() > 0)
                                        <div class="table-responsive">
                                            <table class="table table-sm table-hover">
                                                <thead class="bg-warning">
                                                    <tr>
                                                        <th width="5%">No</th>
                                                        <th>Kode Item</th>
                                                        <th>Nama Item</th>
                                                        <th>UOM</th>
                                                        <th>Tipe Dokumen</th>
                                                        <th>Tipe Barang</th>
                                                        <th>Status</th>
                                                        <th>Stok Min</th>
                                                        <th>Stok Maks</th>
                                                        <th>Reorder Point</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    @foreach($data->storeRawMaterials->where('delete_flag', 0)->sortBy('rawMaterial.code') as $index => $mapping)
                                                    <tr>
                                                        <td>{{ $index + 1 }}</td>
                                                        <td>
                                                            <a href="{{ route('raw_materials.show', $mapping->rawMaterial->id) }}" target="_blank">
                                                                {{ $mapping->rawMaterial->code }}
                                                            </a>
                                                        </td>
                                                        <td>{{ $mapping->rawMaterial->name }}</td>
                                                        <td>{{ $mapping->rawMaterial->uom }}</td>
                                                        <td>
                                                            @if($mapping->tipe_dokumen === 'DO')
                                                            <span class="badge badge-primary">DO</span>
                                                            @elseif($mapping->tipe_dokumen === 'PO')
                                                            <span class="badge badge-secondary">PO</span>
                                                            @else
                                                            <span class="badge badge-info">PR</span>
                                                            @endif
                                                        </td>
                                                        <td>
                                                            @if($mapping->rawMaterial->tipe_barang === 'Basah')
                                                            <span class="badge badge-info">Basah</span>
                                                            @else
                                                            <span class="badge badge-secondary">Kering</span>
                                                            @endif
                                                        </td>
                                                        <td>
                                                            @if($mapping->is_active)
                                                            <span class="badge badge-success">Aktif</span>
                                                            @else
                                                            <span class="badge badge-secondary">Nonaktif</span>
                                                            @endif
                                                        </td>
                                                        <td class="text-right">{{ number_format($mapping->min_stock, 2) }}</td>
                                                        <td class="text-right">{{ number_format($mapping->max_stock, 2) }}</td>
                                                        <td class="text-right">{{ number_format($mapping->reorder_point, 2) }}</td>
                                                    </tr>
                                                    @endforeach
                                                </tbody>
                                            </table>
                                        </div>

                                        <!-- Summary by Tipe Dokumen -->
                                        <div class="row mt-3">
                                            <div class="col-md-12">
                                                <div class="alert alert-info">
                                                    <strong><i class="fas fa-info-circle"></i> Ringkasan Pengelompokan SPB:</strong><br>
                                                    Item di toko ini akan dikelompokkan menjadi SPB berdasarkan kombinasi Tipe Dokumen × Tipe Barang:
                                                    <ul class="mb-0 mt-2">
                                                        @php
                                                        $grouping = $data->storeRawMaterials
                                                        ->where('delete_flag', 0)
                                                        ->where('is_active', true)
                                                        ->groupBy(function($item) {
                                                        return $item->tipe_dokumen . '-' . $item->rawMaterial->tipe_barang;
                                                        });
                                                        @endphp
                                                        @foreach($grouping as $key => $items)
                                                        @php
                                                        list($tipeDoc, $tipeBarang) = explode('-', $key);
                                                        $docBadge = $tipeDoc === 'DO' ? 'primary' : ($tipeDoc === 'PO' ? 'warning' : 'info');
                                                        $barangBadge = $tipeBarang === 'Basah' ? 'info' : 'secondary';
                                                        @endphp
                                                        <li>
                                                            <span class="badge badge-{{ $docBadge }}">{{ $tipeDoc }}</span>
                                                            <span class="badge badge-{{ $barangBadge }}">{{ $tipeBarang }}</span>
                                                            : {{ $items->count() }} item
                                                        </li>
                                                        @endforeach
                                                    </ul>
                                                    <small class="text-muted">
                                                        <i class="fas fa-arrow-right"></i>
                                                        Dari 1 Stock Opname, toko ini bisa menghasilkan maksimal {{ $grouping->count() }} SPB
                                                    </small>
                                                </div>
                                            </div>
                                        </div>
                                        @else
                                        <div class="alert alert-info">
                                            <i class="fas fa-exclamation-triangle"></i>
                                            Belum ada item yang dipetakan ke toko ini.
                                            <a href="{{ route('store_raw_materials.create') }}" class="alert-link">Tambahkan pemetaan</a>
                                        </div>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

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