<div class="content-wrapper">
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-4">
                    <div class="card card-primary card-outline">
                        <div class="card-header">
                            <h5 class="m-0">
                                <?php echo isset($item) ? 'Edit' : 'Add'; ?>
                                <?php myLang('Inventory Item'); ?>
                                <!-- <?php //echo $pageTitle; 
                                        ?> -->
                                <?php //showListButton($this->uri->segment(1) . "/" . $this->uri->segment(2) . "/list", "List " . $pageTitle); 
                                ?>
                            </h5>
                        </div>
                        <div class="card-body">
                            <form method="POST" action="<?php echo isset($item) ? site_url($this->uri->segment(1) . "/" . $this->uri->segment(2) . "/edit/" . $item->item_id) : site_url($this->uri->segment(1) . "/" . $this->uri->segment(2) . "/add"); ?>" enctype="multipart/form-data" name="tbl_items">
                                <div class="row">
                                    <!--COL START-->
                                    <div class="col">
                                        <div class="form-group">
                                            <label for="title"><?php myLang('Name'); ?><span class="text-danger">*</span></label>
                                            <input type="text" class="form-control" id="title" value="<?php echo isset($item) ? $item->title : ''; ?>" name="title" required>
                                        </div>
                                    </div>
                                    <!--COL END-->
                                </div>
                                <div class="row">
                                    <!--COL START-->
                                    <div class="col-6">
                                        <div class="form-group">
                                            <label for="item_code"><?php myLang('Code'); ?></label>
                                            <input type="text" class="form-control" id="item_code" value="<?php echo isset($item) ? $item->item_code : ''; ?>" name="item_code">
                                        </div>
                                    </div>
                                    <!--COL END-->
                                    <!--COL START-->
                                    <div class="col-6">
                                        <div class="form-group">
                                            <?php fillComboWithValue("units_id", "Units <span class='text-danger'>*</span>", "units_id", "tbl_units", "title", "unit_id", isset($item) ? $item->units_id : '', "", false, "", true); ?>
                                        </div>
                                    </div>
                                    <!--COL END-->
                                </div>
                                <div class="row">
                                    <!--COL START-->
                                    <div class="col">
                                        <div class="form-group">
                                            <?php fillComboWithValue("itemcategories_id", "Group <span class='text-danger'>*</span>", "itemcategories_id", "tbl_itemcategories", "title", "itemcategory_id", isset($item) ? $item->itemcategories_id : '', "", false, "", true); ?>
                                        </div>
                                    </div>
                                    <!--COL END-->
                                </div>
                                <div class="row">
                                    <!--COL START-->
                                    <div class="col-4">
                                        <div class="form-group">
                                            <label for="opening_stock"><?php myLang('Opening Stock'); ?></label><input type="text" class="form-control" id="opening_stock" value="<?php echo isset($item) ? $item->Stock->opening_stock : ''; ?>" name="opening_stock">
                                        </div>
                                    </div>
                                    <!--COL END-->
                                    <!--COL START-->
                                    <div class="col-4">
                                        <div class="form-group">
                                            <label for="opening_stock_rate"><?php myLang('Rate'); ?></label><input type="text" class="form-control" id="opening_stock_rate" value="<?php echo isset($item) ? $item->Stock->opening_stock_rate : ''; ?>" name="opening_stock_rate">
                                        </div>
                                    </div>
                                    <!--COL END-->
                                    <!--COL START-->
                                    <div class="col-4">
                                        <div class="form-group">
                                            <label for="opening_stock_amount"><?php myLang('Amount'); ?></label><input type="text" class="form-control" id="opening_stock_amount" value="<?php echo isset($item) ? $item->Stock->opening_stock_amount : ''; ?>" name="opening_stock_amount">
                                        </div>
                                    </div>
                                    <!--COL END-->
                                </div>
                                <div class="row">
                                    <!--COL START-->
                                    <div class="col">
                                        <div class="form-group">
                                            <label for="description"><?php myLang('Remarks'); ?></label>
                                            <textarea class="form-control" id="description" name="description"><?php echo isset($item) ? $item->description : ''; ?></textarea>
                                        </div>
                                    </div>
                                    <!--COL END-->
                                </div>
                                <div class="row">
                                    <!--COL START-->
                                    <!--COL END-->
                                </div>
                                <!-- <div class="row">
                                    <div class="col">
                                        <div class="form-group">
                                            <label for="remarks"><?php //myLang('Remarks'); 
                                                                    ?></label>
                                            <textarea class="form-control" id="remarks" name="remarks"><?php //echo isset($item) ? $item->remarks : ''; 
                                                                                                        ?></textarea>
                                        </div>
                                    </div>
                                </div> -->
                                <!-- <button type="reset" class="btn btn-default btn-sm">Reset</button>  -->
                                <button class="btn btn-primary btn-sm float-right" type="submit" name="submit"><?php echo isset($item) ? 'Update' : 'Save'; ?></button>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="col-8">
                    <div class="card card-primary card-outline">
                        <div class="card-header">
                            <h5 class="m-0">List <?php echo $pageTitle; ?>
                                <?php //showCreateButton($this->uri->segment(1) . "/" . $this->uri->segment(2) . "/add", "Create New " . $pageTitle); 
                                ?>
                            </h5>
                        </div>
                        <div class="card-body">
                            <table class="table table-bordered table-striped longdataTable">
                                <thead>
                                    <tr>
                                        <th width="5%"><?php myLang('S.N'); ?></th>
                                        <th width="25%"><?php myLang('Name'); ?></th>
                                        <th width="10%"><?php myLang('Code'); ?></th>
                                        <th width="10%"><?php myLang('Units'); ?></th>
                                        <th width="10%"><?php myLang('Group'); ?></th>
                                        <th width="10%" class="text-right"><?php myLang('Qty'); ?></th>
                                        <th width="10%" class="text-right"><?php myLang('Rate'); ?></th>
                                        <th width="10%" class="text-right"><?php myLang('Total'); ?></th>
                                        <td width="7%" class="col-1 text-bold text-center"><?php myLang("Action"); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $a = 0;
                                    foreach ($TableData as $TableRow) : $a++; ?>
                                        <?php foreach ($TableRow as $cols) : $id = $cols;
                                            break;
                                        endforeach; ?><tr>
                                            <td><?php echo $a; ?></td>
                                            <td><?php echo $TableRow->title; ?></td>
                                            <td><?php echo $TableRow->item_code; ?></td>
                                            <td><?php echo getFieldfromValue("tbl_units", "title", "unit_id", $TableRow->units_id); ?></td>
                                            <td><?php echo getFieldfromValue("tbl_itemcategories", "title", "itemcategory_id", $TableRow->itemcategories_id); ?></td>
                                            <td class="text-right"><?php echo $TableRow->Stock->qty; ?></td>
                                            <td class="text-right"><?php echo $TableRow->Stock->rate; ?></td>
                                            <td class="text-right"><?php echo $TableRow->Stock->amount; ?></td>
                                            <td class="text-center">
                                                <?php showEditButton($this->uri->segment(1) . "/" . $this->uri->segment(2) . "/edit/$id"); ?>
                                                <?php showDeleteButton($id); ?>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <tbody>
                            </table>
                            <script>
                                function doDelete(id) {
                                    if (confirm('Are you sure to delete?')) {
                                        window.location = '<?php echo site_url($this->uri->segment(1) . "/" . $this->uri->segment(2) . "/delete/"); ?>' + id;
                                    }
                                }
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    // Function to calculate the amount
    function calculateAmount() {
        var opening_stock = parseFloat(document.getElementById('opening_stock').value);
        var opening_stock_rate = parseFloat(document.getElementById('opening_stock_rate').value);
        var opening_stock_amount = opening_stock * opening_stock_rate;
        // Set the calculated amount in the amount field
        document.getElementById('opening_stock_amount').value = opening_stock_amount.toFixed(2); // Adjust decimal places as needed
    }
    // Event listeners to trigger calculation on input change
    document.getElementById('opening_stock').addEventListener('input', calculateAmount);
    document.getElementById('opening_stock_rate').addEventListener('input', calculateAmount);
    // Initial calculation on page load
    calculateAmount();
</script>