<?php $__env->startSection('title', 'Data Obat - SI Apotek'); ?>

<?php $__env->startSection('content'); ?>
<div class="flex justify-between items-center mb-6">
    <h2 class="text-2xl font-bold text-gray-800">Manajemen Data Obat</h2>
    <div class="flex gap-2" x-data="{ openAdd: false, openImport: false }">
        <button @click="openImport = true" class="px-4 py-2 text-sm font-semibold text-emerald-700 bg-emerald-50 border border-emerald-200 rounded-lg hover:bg-emerald-100 transition-colors flex items-center gap-2">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
            Import Excel
        </button>
        <button @click="openAdd = true" class="px-4 py-2 text-sm font-semibold text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors flex items-center gap-2 shadow-sm">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
            Tambah Obat
        </button>

        <!-- Modal Import -->
        <div x-show="openImport" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 backdrop-blur-sm" x-cloak>
            <div @click.away="openImport = false" class="bg-white rounded-2xl p-6 w-96 text-left shadow-xl transform transition-all">
                <h3 class="text-lg font-bold text-gray-800 mb-2">Import Data Obat</h3>
                <p class="text-sm text-gray-500 mb-4">Unggah file Excel (.xlsx) untuk menambahkan data obat secara massal.</p>
                
                <div class="mb-4 bg-emerald-50 p-3 rounded-lg border border-emerald-100 flex items-start gap-3">
                    <svg class="w-5 h-5 text-emerald-600 shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    <div class="text-xs text-emerald-800">
                        Pastikan format kolom sesuai dengan template.<br>
                        <a href="<?php echo e(route('medicines.template')); ?>" class="font-bold underline text-emerald-600 hover:text-emerald-700 mt-1 inline-block">Download Template Excel</a>
                    </div>
                </div>

                <form action="<?php echo e(route('medicines.import')); ?>" method="POST" enctype="multipart/form-data">
                    <?php echo csrf_field(); ?>
                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1">File Excel (.xlsx)</label>
                        <input type="file" name="file" accept=".xlsx,.xls" class="w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-emerald-50 file:text-emerald-700 hover:file:bg-emerald-100" required>
                    </div>
                    <div class="flex justify-end gap-2 mt-6">
                        <button type="button" @click="openImport = false" class="px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors">Batal</button>
                        <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors shadow-sm">Import Data</button>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- Modal Tambah -->
        <div x-show="openAdd" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 backdrop-blur-sm" x-cloak>
            <div @click.away="openAdd = false" class="bg-white rounded-2xl p-6 w-[800px] max-w-[90vw] text-left shadow-xl max-h-[90vh] overflow-y-auto">
                <div class="flex justify-between items-center mb-6">
                    <h3 class="text-xl font-bold text-gray-800">Tambah Data Obat Baru</h3>
                    <button @click="openAdd = false" class="text-gray-400 hover:text-gray-600"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg></button>
                </div>
                <form action="<?php echo e(route('medicines.store')); ?>" method="POST">
                    <?php echo csrf_field(); ?>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Kode Obat / Barcode</label>
                            <input type="text" name="code" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" placeholder="Biarkan kosong untuk otomatis">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Nama Obat <span class="text-red-500">*</span></label>
                            <input type="text" name="name" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Kategori <span class="text-red-500">*</span></label>
                            <select name="category_id" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                <option value="">-- Pilih Kategori --</option>
                                <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cat): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($cat->id); ?>"><?php echo e($cat->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Satuan Jual <span class="text-red-500">*</span></label>
                            <select name="unit_id" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                <option value="">-- Pilih Satuan --</option>
                                <?php $__currentLoopData = $units; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($unit->id); ?>"><?php echo e($unit->name); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Harga Beli (Rp) <span class="text-red-500">*</span></label>
                            <input type="number" name="purchase_price" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Harga Jual (Rp) <span class="text-red-500">*</span></label>
                            <input type="number" name="selling_price" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Stok Minimum <span class="text-red-500">*</span></label>
                            <input type="number" name="min_stock" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Lokasi Rak</label>
                            <input type="text" name="rack_location" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" placeholder="Misal: A1">
                        </div>
                    </div>
                    <div class="flex justify-end gap-3 mt-8 border-t pt-5">
                        <button type="button" @click="openAdd = false" class="px-5 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors">Batal</button>
                        <button type="submit" class="px-5 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors shadow-sm">Simpan Obat</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php if(session('success')): ?>
<div class="p-4 mb-4 text-sm text-emerald-700 bg-emerald-100 rounded-lg flex items-center gap-2" role="alert">
    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
    <?php echo e(session('success')); ?>

</div>
<?php endif; ?>

<?php if(session('error')): ?>
<div class="p-4 mb-4 text-sm text-red-700 bg-red-100 rounded-lg flex items-center gap-2" role="alert">
    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
    <?php echo e(session('error')); ?>

</div>
<?php endif; ?>

<?php if($errors->any()): ?>
<div class="p-4 mb-4 text-sm text-red-700 bg-red-100 rounded-lg">
    <ul class="list-disc pl-5">
        <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <li><?php echo e($error); ?></li>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </ul>
</div>
<?php endif; ?>

<!-- Form Pencarian & Filter -->
<div class="bg-white rounded-lg shadow-sm border border-gray-100 p-4 mb-6">
    <form action="<?php echo e(route('medicines.index')); ?>" method="GET" class="flex flex-col md:flex-row gap-4 items-end">
        <div class="flex-1 w-full">
            <label class="block text-xs font-semibold text-gray-500 uppercase mb-1">Pencarian</label>
            <input type="text" name="search" value="<?php echo e(request('search')); ?>" placeholder="Cari nama atau kode obat..." class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500 text-sm">
        </div>
        <div class="w-full md:w-48">
            <label class="block text-xs font-semibold text-gray-500 uppercase mb-1">Kategori</label>
            <select name="category_id" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500 text-sm">
                <option value="">Semua Kategori</option>
                <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cat): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($cat->id); ?>" <?php echo e(request('category_id') == $cat->id ? 'selected' : ''); ?>><?php echo e($cat->name); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
        <div class="w-full md:w-48">
            <label class="block text-xs font-semibold text-gray-500 uppercase mb-1">Supplier (Riwayat)</label>
            <select name="supplier_id" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500 text-sm">
                <option value="">Semua Supplier</option>
                <?php $__currentLoopData = $suppliers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $sup): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($sup->id); ?>" <?php echo e(request('supplier_id') == $sup->id ? 'selected' : ''); ?>><?php echo e($sup->name); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
        <div class="flex gap-2">
            <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors shadow-sm">
                Filter
            </button>
            <?php if(request('search') || request('category_id') || request('supplier_id')): ?>
                <a href="<?php echo e(route('medicines.index')); ?>" class="px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors">
                    Reset
                </a>
            <?php endif; ?>
        </div>
    </form>
</div>

<div class="bg-white rounded-lg shadow-sm border border-gray-100 overflow-hidden">
    <div class="overflow-x-auto">
        <table class="w-full whitespace-no-wrap">
            <thead>
                <tr class="text-xs font-semibold tracking-wide text-left text-gray-500 uppercase border-b bg-gray-50">
                    <th class="px-4 py-3">Kode/Barcode</th>
                    <th class="px-4 py-3">Nama Obat</th>
                    <th class="px-4 py-3">Kategori</th>
                    <th class="px-4 py-3">Harga Jual</th>
                    <th class="px-4 py-3">Rak</th>
                    <th class="px-4 py-3 text-center">Stok</th>
                    <th class="px-4 py-3 text-right">Aksi</th>
                </tr>
            </thead>
            <tbody class="divide-y">
                <?php $__empty_1 = true; $__currentLoopData = $medicines; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $med): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                <tr class="text-gray-700 hover:bg-gray-50 transition-colors">
                    <td class="px-4 py-3 text-sm font-mono text-gray-500"><?php echo e($med->code); ?></td>
                    <td class="px-4 py-3 text-sm font-semibold text-gray-800"><?php echo e($med->name); ?></td>
                    <td class="px-4 py-3 text-xs">
                        <span class="px-2 py-1 leading-tight text-blue-700 bg-blue-100 rounded-full"><?php echo e($med->category->name ?? '-'); ?></span>
                    </td>
                    <td class="px-4 py-3 text-sm font-medium">Rp <?php echo e(number_format($med->selling_price, 0, ',', '.')); ?></td>
                    <td class="px-4 py-3 text-sm"><?php echo e($med->rack_location ?? '-'); ?></td>
                    <td class="px-4 py-3 text-sm text-center">
                        <span class="px-2 py-1 font-bold leading-tight rounded-full <?php echo e($med->current_stock <= $med->min_stock ? 'text-red-700 bg-red-100' : 'text-emerald-700 bg-emerald-100'); ?>">
                            <?php echo e($med->current_stock); ?> <?php echo e($med->unit->name ?? ''); ?>

                        </span>
                    </td>
                    <td class="px-4 py-3 text-sm text-right">
                        <div class="flex justify-end gap-2">
                            <!-- Edit -->
                            <div x-data="{ openEdit: false }">
                                <button @click="openEdit = true" class="px-3 py-1 text-xs font-medium leading-5 text-emerald-600 bg-emerald-100 rounded-lg hover:bg-emerald-200 transition-colors">Edit</button>
                                
                                <!-- Modal Edit -->
                                <div x-show="openEdit" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50 backdrop-blur-sm" x-cloak>
                                    <div @click.away="openEdit = false" class="bg-white rounded-2xl p-6 w-[800px] max-w-[90vw] text-left shadow-xl max-h-[90vh] overflow-y-auto">
                                        <div class="flex justify-between items-center mb-6">
                                            <h3 class="text-xl font-bold text-gray-800">Edit Data Obat</h3>
                                            <button @click="openEdit = false" class="text-gray-400 hover:text-gray-600"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg></button>
                                        </div>
                                        <form action="<?php echo e(route('medicines.update', $med->id)); ?>" method="POST">
                                            <?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>
                                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Kode Obat</label>
                                                    <input type="text" name="code" value="<?php echo e($med->code); ?>" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500">
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Nama Obat</label>
                                                    <input type="text" name="name" value="<?php echo e($med->name); ?>" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Kategori</label>
                                                    <select name="category_id" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                        <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cat): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                        <option value="<?php echo e($cat->id); ?>" <?php echo e($med->category_id == $cat->id ? 'selected' : ''); ?>><?php echo e($cat->name); ?></option>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </select>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Satuan Jual</label>
                                                    <select name="unit_id" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                        <?php $__currentLoopData = $units; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unit): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                        <option value="<?php echo e($unit->id); ?>" <?php echo e($med->unit_id == $unit->id ? 'selected' : ''); ?>><?php echo e($unit->name); ?></option>
                                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                    </select>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Harga Beli (Rp)</label>
                                                    <input type="number" name="purchase_price" value="<?php echo e((int)$med->purchase_price); ?>" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Harga Jual (Rp)</label>
                                                    <input type="number" name="selling_price" value="<?php echo e((int)$med->selling_price); ?>" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Stok Minimum</label>
                                                    <input type="number" name="min_stock" value="<?php echo e($med->min_stock); ?>" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500" required>
                                                </div>
                                                <div>
                                                    <label class="block text-sm font-medium text-gray-700 mb-1">Lokasi Rak</label>
                                                    <input type="text" name="rack_location" value="<?php echo e($med->rack_location); ?>" class="w-full px-4 py-2 border rounded-lg focus:ring-emerald-500 focus:border-emerald-500">
                                                </div>
                                            </div>
                                            <div class="flex justify-end gap-3 mt-8 border-t pt-5">
                                                <button type="button" @click="openEdit = false" class="px-5 py-2 text-sm font-medium text-gray-600 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors">Batal</button>
                                                <button type="submit" class="px-5 py-2 text-sm font-medium text-white bg-emerald-600 rounded-lg hover:bg-emerald-700 transition-colors shadow-sm">Simpan Perubahan</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <!-- Delete -->
                            <form action="<?php echo e(route('medicines.destroy', $med->id)); ?>" method="POST" onsubmit="return confirm('Yakin ingin menghapus obat ini? Menghapus obat dapat mempengaruhi riwayat transaksi.');" class="inline">
                                <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                                <button type="submit" class="px-3 py-1 text-xs font-medium leading-5 text-red-600 bg-red-100 rounded-lg hover:bg-red-200 transition-colors">Hapus</button>
                            </form>
                        </div>
                    </td>
                </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                <tr>
                    <td colspan="7" class="px-4 py-8 text-center text-gray-500">
                        <div class="flex flex-col items-center justify-center">
                            <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"></path></svg>
                            <p>Belum ada data obat.</p>
                        </div>
                    </td>
                </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\PROJECT\xampp\htdocs\apoteks\resources\views/master/medicines.blade.php ENDPATH**/ ?>