@extends('layouts.admin')

@section('content')
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mt-4 mb-3">
        <h4 class="mb-0"><i class="fas fa-boxes text-warning mr-2"></i>Manajemen Bahan Baku</h4>
        @if(auth()->user()->role !== 'admin')
        <button class="btn btn-primary shadow-sm" data-toggle="modal" data-target="#addRawMaterialModal">
            <i class="fas fa-plus mr-1"></i> Tambah Bahan Baku
        </button>
        @endif
    </div>

    @if(session('success'))
        <div class="alert alert-success border-left-success shadow-sm alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle mr-2"></i>{{ session('success') }}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif
    
    @if($errors->any())
        <div class="alert alert-danger border-left-danger shadow-sm alert-dismissible fade show" role="alert">
            <i class="fas fa-exclamation-circle mr-2"></i>{{ $errors->first() }}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-header bg-white py-3 border-0">
            <h6 class="m-0 font-weight-bold text-dark">Daftar Bahan Baku & Stok Saat Ini</h6>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th width="70" class="text-center">#</th>
                            <th>Nama Bahan Baku</th>
                            <th width="150" class="text-center">Satuan</th>
                            <th width="150" class="text-right">Total Stok</th>
                            <th width="150" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($rawMaterials as $rm)
                        <tr>
                            <td class="text-center text-muted font-weight-bold">{{ $loop->iteration }}</td>
                            <td class="font-weight-bold">{{ $rm->name }}</td>
                            <td class="text-center"><span class="badge badge-secondary px-3 py-2">{{ $rm->unit }}</span></td>
                            <td class="text-right font-weight-bold" style="color: #ff5e00;">
                                {{ number_format($rm->stock_quantity, 0, ',', '.') }} {{ $rm->unit }}
                            </td>
                            <td class="text-center">
                                @if(auth()->user()->role !== 'admin')
                                <button class="btn btn-sm btn-link text-warning" title="Edit" data-toggle="modal" data-target="#editModal{{ $rm->id }}">
                                    <i class="fas fa-edit fa-lg"></i>
                                </button>
                                <button class="btn btn-sm btn-link text-danger" title="Hapus" onclick="if(confirm('Yakin ingin menghapus bahan baku ini?')) document.getElementById('delete-form-{{ $rm->id }}').submit();">
                                    <i class="fas fa-trash fa-lg"></i>
                                </button>
                                <form id="delete-form-{{ $rm->id }}" action="/bahan-baku/{{ $rm->id }}" method="POST" style="display: none;">
                                    @csrf
                                    @method('DELETE')
                                </form>
                                @else
                                <span class="text-muted small">View Only</span>
                                @endif
                            </td>
                        </tr>

                        @if(auth()->user()->role !== 'admin')
                        <!-- Edit Modal -->
                        <div class="modal fade" id="editModal{{ $rm->id }}" tabindex="-1" role="dialog" aria-hidden="true">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content border-0 shadow">
                                    <div class="modal-header bg-light border-0">
                                        <h5 class="modal-title font-weight-bold text-dark"><i class="fas fa-edit text-warning mr-2"></i>Edit Bahan Baku</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <form action="/bahan-baku/{{ $rm->id }}" method="POST">
                                        @csrf
                                        @method('PUT')
                                        <div class="modal-body">
                                            <div class="form-group">
                                                <label class="font-weight-bold">Nama Bahan Baku</label>
                                                <input type="text" name="name" class="form-control bg-light" value="{{ $rm->name }}" required>
                                            </div>
                                            <div class="form-group">
                                                <label class="font-weight-bold">Satuan Dasar</label>
                                                <select name="unit" class="form-control bg-light" required>
                                                    <option value="">-- Pilih Satuan --</option>
                                                    <option value="Gram" {{ $rm->unit == 'Gram' ? 'selected' : '' }}>Gram</option>
                                                    <option value="Kilogram" {{ $rm->unit == 'Kilogram' ? 'selected' : '' }}>Kilogram</option>
                                                    <option value="Mililiter" {{ $rm->unit == 'Mililiter' ? 'selected' : '' }}>Mililiter</option>
                                                    <option value="Liter" {{ $rm->unit == 'Liter' ? 'selected' : '' }}>Liter</option>
                                                    <option value="Pcs" {{ $rm->unit == 'Pcs' ? 'selected' : '' }}>Pcs</option>
                                                    <option value="Botol" {{ $rm->unit == 'Botol' ? 'selected' : '' }}>Botol</option>
                                                    <option value="Lembar" {{ $rm->unit == 'Lembar' ? 'selected' : '' }}>Lembar</option>
                                                    <option value="Sendok Makan" {{ $rm->unit == 'Sendok Makan' ? 'selected' : '' }}>Sendok Makan</option>
                                                    <option value="Sendok Teh" {{ $rm->unit == 'Sendok Teh' ? 'selected' : '' }}>Sendok Teh</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="modal-footer border-0 bg-light">
                                            <button type="button" class="btn btn-secondary font-weight-bold" data-dismiss="modal">Batal</button>
                                            <button type="submit" class="btn btn-primary font-weight-bold"><i class="fas fa-save mr-1"></i> Simpan Perubahan</button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                        @endif
                        @endforeach
                        @if($rawMaterials->isEmpty())
                        <tr>
                            <td colspan="5" class="text-center py-4 text-muted">Belum ada bahan baku yang terdaftar.</td>
                        </tr>
                        @endif
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@if(auth()->user()->role !== 'admin')
<!-- Add Modal -->
<div class="modal fade" id="addRawMaterialModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-light border-0">
                <h5 class="modal-title font-weight-bold text-dark"><i class="fas fa-plus-circle text-primary mr-2"></i>Tambah Bahan Baku</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="/bahan-baku" method="POST">
                @csrf
                <div class="modal-body">
                    <div class="form-group">
                        <label class="font-weight-bold">Nama Bahan Baku</label>
                        <input type="text" name="name" class="form-control bg-light" placeholder="Misal: Kopi Bubuk" required>
                    </div>
                    <div class="form-group">
                        <label class="font-weight-bold">Satuan Dasar</label>
                        <select name="unit" class="form-control bg-light" required>
                            <option value="">-- Pilih Satuan --</option>
                            <option value="Gram">Gram</option>
                            <option value="Kilogram">Kilogram</option>
                            <option value="Mililiter">Mililiter</option>
                            <option value="Liter">Liter</option>
                            <option value="Pcs">Pcs</option>
                            <option value="Botol">Botol</option>
                            <option value="Lembar">Lembar</option>
                            <option value="Sendok Makan">Sendok Makan</option>
                            <option value="Sendok Teh">Sendok Teh</option>
                        </select>
                        <small class="form-text text-warning mt-2"><i class="fas fa-info-circle mr-1"></i>Gunakan satuan terkecil agar mudah dibuat resepnya. Untuk air mineral atau mie instan, pilih <strong>Pcs</strong> atau <strong>Botol</strong> agar tidak menggunakan <strong>Dus</strong>.</small>
                    </div>
                </div>
                <div class="modal-footer border-0 bg-light">
                    <button type="button" class="btn btn-secondary font-weight-bold" data-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-primary font-weight-bold"><i class="fas fa-save mr-1"></i> Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endif
@endsection
