<?php require_once __DIR__ . '/../../layouts/header.php'; ?>

<div style="max-width: 1000px;">
    <h2 style="margin-bottom: 1.5rem;">Edit Stock & Medicine</h2>

    <form action="/inventory/update/<?php echo $batch['id']; ?>" method="POST">
        <div style="display: flex; flex-direction: column; gap: 2rem;">
            
            <!-- Top Card: Medicine Settings -->
            <div class="card" style="padding: 1.25rem;">
                <h4 style="margin-bottom: 1rem; color: var(--text-muted); border-bottom: 1px solid var(--glass-border); padding-bottom: 0.5rem;">Medicine Settings</h4>
                <p style="font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1.25rem;">Changes here apply to ALL batches of this medicine.</p>
                
                <div style="display: flex; gap: 1rem; margin-bottom: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label>Medicine Name</label>
                        <input type="text" value="<?php echo htmlspecialchars($batch['medicine_name']); ?>" disabled style="padding: 0.5rem 0.75rem; background: rgba(15,23,42,0.8); cursor: not-allowed; color: var(--text-muted);">
                    </div>
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label>Generic Name</label>
                        <input type="text" value="<?php echo htmlspecialchars($batch['generic_name']); ?>" disabled style="padding: 0.5rem 0.75rem; background: rgba(15,23,42,0.8); cursor: not-allowed; color: var(--text-muted);">
                    </div>
                </div>

                <div style="display: flex; gap: 1rem; margin-bottom: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="quantity">Available Quantity *</label>
                        <input type="number" id="quantity" name="quantity" required min="0" value="<?php echo htmlspecialchars($batch['quantity']); ?>" style="padding: 0.5rem 0.75rem;">
                    </div>
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="reorder_level">Reorder Level</label>
                        <input type="number" id="reorder_level" name="reorder_level" value="<?php echo htmlspecialchars($batch['reorder_level']); ?>" min="0" style="padding: 0.5rem 0.75rem;">
                    </div>
                </div>

                <div style="display: flex; gap: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="status">Medicine Status *</label>
                        <select id="status" name="status" required style="width: 100%; padding: 0.5rem 0.75rem; background: rgba(15, 23, 42, 0.6); border: 1px solid var(--glass-border); border-radius: 8px; color: var(--text-light); outline: none;">
                            <option value="active" <?php echo $batch['medicine_status'] === 'active' ? 'selected' : ''; ?> style="background: var(--bg-dark);">Active</option>
                            <option value="inactive" <?php echo $batch['medicine_status'] === 'inactive' ? 'selected' : ''; ?> style="background: var(--bg-dark);">Inactive</option>
                        </select>
                    </div>
                    <div style="flex: 1;"></div>
                </div>
            </div>

            <!-- Bottom Card: Batch & Stock Details -->
            <div class="card" style="padding: 1.25rem;">
                <h4 style="margin-bottom: 1rem; color: var(--text-muted); border-bottom: 1px solid var(--glass-border); padding-bottom: 0.5rem;">Batch & Stock Details</h4>
                <p style="font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1.25rem;">Changes here apply ONLY to Batch <strong style="color: #c084fc;"><?php echo htmlspecialchars($batch['batch_number']); ?></strong>.</p>
                
                <div style="display: flex; gap: 1rem; margin-bottom: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label>Batch Number</label>
                        <input type="text" value="<?php echo htmlspecialchars($batch['batch_number']); ?>" disabled style="padding: 0.5rem 0.75rem; background: rgba(15,23,42,0.8); cursor: not-allowed; color: #c084fc;">
                    </div>
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="expiry_date">Expiry Date *</label>
                        <input type="date" id="expiry_date" name="expiry_date" value="<?php echo htmlspecialchars($batch['expiry_date']); ?>" required style="padding: 0.5rem 0.75rem; border-color: #f59e0b;">
                    </div>
                </div>

                <div style="display: flex; gap: 1rem; margin-bottom: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="supplier">Supplier</label>
                        <input type="text" id="supplier" name="supplier" value="<?php echo htmlspecialchars($batch['supplier']); ?>" style="padding: 0.5rem 0.75rem;">
                    </div>
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="expiry_alert_days">Notify Days Before Expiry</label>
                        <input type="number" id="expiry_alert_days" name="expiry_alert_days" value="<?php echo htmlspecialchars($batch['expiry_alert_days']); ?>" min="1" required style="padding: 0.5rem 0.75rem;">
                    </div>
                </div>

                <div style="display: flex; gap: 1rem; margin-bottom: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="unit_price">Unit Cost (<?php echo htmlspecialchars($_SESSION['currency_code'] ?? '$'); ?>)</label>
                        <input type="number" id="unit_price" name="unit_price" step="0.01" min="0" value="<?php echo htmlspecialchars($batch['unit_price']); ?>" style="padding: 0.5rem 0.75rem;">
                    </div>
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="selling_price">Selling Price (<?php echo htmlspecialchars($_SESSION['currency_code'] ?? '$'); ?>)</label>
                        <input type="number" id="selling_price" name="selling_price" step="0.01" min="0" value="<?php echo htmlspecialchars($batch['selling_price']); ?>" style="padding: 0.5rem 0.75rem;">
                    </div>
                </div>

                <div style="margin-bottom: 1rem;">
                    <div class="input-group" style="margin-bottom: 0;">
                        <label for="notes">Notes</label>
                        <textarea id="notes" name="notes" rows="3" placeholder="Optional notes about this batch..." style="width: 100%; padding: 0.5rem 0.75rem; background: rgba(15, 23, 42, 0.6); border: 1px solid var(--glass-border); border-radius: 8px; color: var(--text-light); outline: none; resize: vertical; font-family: inherit; font-size: inherit;"><?php echo htmlspecialchars($batch['notes'] ?? ''); ?></textarea>
                    </div>
                </div>

                <div style="display: flex; gap: 1rem;" class="form-row">
                    <div class="input-group" style="flex: 1; margin-bottom: 0;">
                        <label for="batch_status">Batch Status *</label>
                        <select id="batch_status" name="batch_status" required style="width: 100%; padding: 0.5rem 0.75rem; background: rgba(15, 23, 42, 0.6); border: 1px solid var(--glass-border); border-radius: 8px; color: var(--text-light); outline: none;">
                            <option value="active" style="background: var(--bg-dark);" <?php echo ($batch['status'] ?? 'active') === 'active' ? 'selected' : ''; ?>>Active</option>
                            <option value="inactive" style="background: var(--bg-dark);" <?php echo ($batch['status'] ?? 'active') === 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                        </select>
                    </div>
                    <div style="flex: 1;"></div>
                </div>
            </div>

        </div>

        <div style="display: flex; gap: 1rem; margin-top: 1.5rem; max-width: 300px;" class="form-row">
            <button type="submit" class="btn-primary" style="flex: 1; margin-top: 0; padding: 0.6rem;">Update Stock</button>
            <a href="/inventory" class="btn-primary" style="flex: 1; margin-top: 0; padding: 0.6rem; background: transparent; border: 1px solid var(--glass-border); color: var(--text-light); text-align: center; text-decoration: none;">Cancel</a>
        </div>
    </form>
</div>

<?php require_once __DIR__ . '/../../layouts/footer.php'; ?>
