<?php if (basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME'] ?? '')) { http_response_code(403); exit('Forbidden'); } ?>
<div class="page-head"><div><h1><?= $product ? (!empty($viewOnly) ? 'View Product' : 'Edit Product') : 'Add Product' ?></h1><p>Maintain catalog details and prices.</p></div></div>
<form class="card app-card p-4" method="post" action="<?= e($action) ?>">
    <?= csrf_field() ?>
    <div class="row g-3">
        <div class="col-md-4"><label class="form-label">Product Category</label><select class="form-select" name="product_category" required <?= !empty($viewOnly)?'disabled':'' ?>><option value="">Select category</option><?php foreach (PRODUCT_CATEGORIES as $cat): ?><option value="<?= e($cat) ?>" <?= ($product['product_category'] ?? '')===$cat?'selected':'' ?>><?= e($cat) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-8"><label class="form-label">Product Name</label><input class="form-control" name="product_name" value="<?= e($product['product_name'] ?? '') ?>" required <?= !empty($viewOnly)?'readonly':'' ?>></div>
        <div class="col-12"><label class="form-label">Description</label><textarea class="form-control" name="description" rows="3" <?= !empty($viewOnly)?'readonly':'' ?>><?= e($product['description'] ?? '') ?></textarea></div>
        <div class="col-md-3"><label class="form-label">Brand</label><input class="form-control" name="brand" value="<?= e($product['brand'] ?? '') ?>" <?= !empty($viewOnly)?'readonly':'' ?>></div>
        <div class="col-md-3"><label class="form-label">Model</label><input class="form-control" name="model" value="<?= e($product['model'] ?? '') ?>" <?= !empty($viewOnly)?'readonly':'' ?>></div>
        <div class="col-md-3"><label class="form-label">Unit</label><select class="form-select" name="unit" required <?= !empty($viewOnly)?'disabled':'' ?>><?php foreach (PRODUCT_UNITS as $unit): ?><option value="<?= e($unit) ?>" <?= ($product['unit'] ?? 'pcs')===$unit?'selected':'' ?>><?= e($unit) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-3"><label class="form-label">Unit Price</label><input class="form-control" type="number" min="0" step="0.01" name="unit_price" value="<?= e((string)($product['unit_price'] ?? '0')) ?>" required <?= !empty($viewOnly)?'readonly':'' ?>></div>
        <div class="col-md-3"><label class="form-label">Status</label><select class="form-select" name="status" <?= !empty($viewOnly)?'disabled':'' ?>><?php foreach (PRODUCT_STATUSES as $status): ?><option value="<?= e($status) ?>" <?= ($product['status'] ?? 'active')===$status?'selected':'' ?>><?= e(ucfirst($status)) ?></option><?php endforeach; ?></select></div>
    </div>
    <div class="mt-4"><?php if (empty($viewOnly)): ?><button class="btn btn-primary"><?= $product ? 'Save Product' : 'Add Product' ?></button><?php endif; ?> <a class="btn btn-outline-secondary" href="<?= APP_BASE ?>/products/index.php">Back</a></div>
</form>
