<div class="row justify-content-center">
<div class="col-12">
<div class="card-box">

    <div class="card-box-header">
        <h5>
            <i class="fa-solid fa-box" style="color:#7c3aed;"></i>
            <?php if(!empty($productdata)) echo 'Edit Product'; else echo 'Add New Product'; ?>
        </h5>
        <a href="<?php echo site_url('product'); ?>" class="btn btn-sm" style="background:#f1f5f9;color:#374151;">
            <i class="fa-solid fa-arrow-left me-1"></i> Back to List
        </a>
    </div>

    <form method="post" id="Form">

        <input type="hidden" name="id" id="id" value="<?php if(!empty($productdata)) echo $productdata[0]->id; ?>">

        <div class="form-row row g-3">

            <div class="col-md-2">
                <label class="form-label">Product Code</label>
                <input type="text" name="product_code" id="product_code" class="form-control"
                       value="<?php if(!empty($productdata)) echo $productdata[0]->product_code; ?>"
                       placeholder="e.g. PROD-001">
            </div>

            <div class="col-md-6">
                <label class="form-label">Product Name <span class="required-star">*</span></label>
                <div class="input-group">
                    <input type="text" name="product_name" id="product_name" class="form-control"
                           value="<?php if(!empty($productdata)) echo $productdata[0]->product_name; ?>"
                            autofocus>
                </div>
            </div>

            <div class="col-md-4">
                <label class="form-label">Category <span class="required-star">*</span></label>
                <select name="category_id" id="category_id" class="form-select select2">
                    <option value="">— Select Category —</option>
                    <?php foreach($categories as $c): ?>
                    <option value="<?php echo $c->id; ?>"
                        <?php if(!empty($productdata) && $productdata[0]->category_id == $c->id) echo 'selected'; ?>>
                        <?php echo htmlspecialchars($c->category_name); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-4">
                <label class="form-label">Unit <span class="required-star">*</span></label>
                <select name="unit_id" id="unit_id" class="form-select select2">
                    <option value="">— Select Unit —</option>
                    <?php foreach($units as $u): ?>
                    <option value="<?php echo $u->id; ?>"
                        <?php if(!empty($productdata) && $productdata[0]->unit_id == $u->id) echo 'selected'; ?>>
                        <?php echo htmlspecialchars($u->unit_name); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-2">
                <label class="form-label">Purchase Rate (₹) <span class="required-star">*</span></label>
                <div class="input-group">
                    <span class="input-group-text">₹</span>
                    <input type="number" step="0.01" min="0" name="purchase_rate" id="purchase_rate" class="form-control"
                           value="<?php if(!empty($productdata)) echo $productdata[0]->purchase_rate; ?>"
                           placeholder="0.00">
                </div>
            </div>

            <div class="col-md-2">
                <label class="form-label">Sale Rate (₹) <span class="required-star">*</span></label>
                <div class="input-group">
                    <span class="input-group-text">₹</span>
                    <input type="number" step="0.01" min="0" name="sale_rate" id="sale_rate" class="form-control"
                           value="<?php if(!empty($productdata)) echo $productdata[0]->sale_rate; ?>"
                           placeholder="0.00">
                </div>
            </div>

            <div class="col-md-2">
                <label class="form-label">GST %</label>
                <div class="input-group">
                    <input type="number" step="0.01" min="0" max="100" name="gst_percent" id="gst_pct" class="form-control"
                           value="<?php if(!empty($productdata)) echo $productdata[0]->gst_percent; else echo '0'; ?>"
                           placeholder="0">
                    <span class="input-group-text">%</span>
                </div>
            </div>

            <div class="col-md-2">
                <label class="form-label text-muted" style="font-size:12px;">Sale Rate incl. GST (preview)</label>
                <div class="form-control bg-light text-success fw-bold" id="sale_incl_gst" style="cursor:default;">₹ 0.00</div>
            </div>

            <div class="col-md-2">
                <label class="form-label">Opening Stock</label>
                <input type="number" step="0.01" min="0" name="opening_stock" id="opening_stock" class="form-control"
                       value="<?php if(!empty($productdata)) echo $productdata[0]->opening_stock; else echo '0'; ?>"
                       placeholder="0">
            </div>

            <?php if(!empty($productdata)): ?>
            <div class="col-md-2">
                <label class="form-label">Current Stock</label>
                <div class="form-control bg-light fw-bold <?php echo $productdata[0]->current_stock < 5 ? 'text-danger' : 'text-success'; ?>" style="cursor:default;">
                    <?php echo (int)($productdata[0]->current_stock); ?>
                </div>
            </div>
            <?php endif; ?>

            <div class="col-md-2">
                <label class="form-label">Status</label>
    <select name="status" id="status" class="form-select select2 select2-no-search">
        <!--<option value="">-- Select Status --</option>-->
        <option value="1" <?php if(!empty($productdata) && $productdata[0]->status == 1) echo 'selected'; ?>>Active</option>
        <option value="0" <?php if(!empty($productdata) && $productdata[0]->status == 0) echo 'selected'; ?>>Inactive</option>
    </select>
            </div>

        </div>

        <div class="d-flex gap-2 mt-4">
            <button type="button" id="btn_savee" class="btn btn-primary px-4">
                <i class="fa-solid fa-floppy-disk me-1"></i> Save
            </button>
            <a href="<?php echo site_url('product'); ?>" class="btn" style="background:#f1f5f9;color:#374151;">Cancel</a>
        </div>

    </form>
</div>
</div>
</div>