<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/Item.php';
require_once '../Model/ItemCategory.php';
require_once '../Model/ItemLevel.php';
require_once '../Model/Supplier.php';
require_once '../Model/ItemType.php';

$item = new Item();
$item_category = new ItemCategory();
$item_type = new ItemType();
$item_level = new ItemLevel();
$supplier = new supplier();

$items = $item->read_all_items();
$item_types = $item_type->read_all_item_types();

$all_low_inventory_items = $item->read_all_low_inventory_items();

if(isset($_GET['all_items'])){
    echo json_encode(
        array("data"=>$items)
    ); 
}

if(isset($_GET['all_low_inventory_items'])){
    echo json_encode(
        array("data"=>$all_low_inventory_items)
    ); 
}

if(isset($_POST['add_item'])){
    $item_name = $_POST['item_name'];
    $item_barcode = $_POST['item_barcode'];
    $item_category_name = $_POST['item_category_name'];
    $item_category_details = $item_category->get_item_category_by_name($item_category_name);
    $item_category_id = $item_category_details[0]['item_category_id'];
    $item_type_name = $_POST['item_type_name'];
    $item_type_details = $item_type->get_item_type_by_name($item_type_name);
    $item_type_id = $item_type_details[0]['item_type_id'];
    $item_level_name = $_POST['item_level_name'];
    $item_level_details = $item_level->get_item_level_by_name($item_level_name);
    $item_level_id = $item_level_details[0]['item_level_id'];
    $supplier_company = $_POST['supplier_company'];
    $item_supplier_details = $supplier->get_supplier_by_name($supplier_company);
    $item_supplier_id = $item_supplier_details[0]['supplier_id'];
    $item_wholesale_price = $_POST['item_wholesale_price'];
    $item_retail_price = $_POST['item_retail_price'];
    $item_quantity = $_POST['item_quantity'];
    $item_reorder_level = $_POST['item_reorder_level'];
    $image = '';
    if($item_type_name=="Textbook"){
        $image = 'items.jpg';
    }else{
        $image = 'images2.jpg';
    }
    $data = array(
        "item_name"=>$item_name,
        "item_barcode"=>$item_barcode,
        "item_category_id"=>$item_category_id,
        "item_type_id"=>$item_type_id,
        "item_level_id"=>$item_level_id,
        "item_supplier_id"=>$item_supplier_id,
        "item_wholesale_price"=>$item_wholesale_price,
        "item_retail_price"=>$item_retail_price,
        "item_quantity"=>$item_quantity,
        "item_reorder_level"=>$item_reorder_level,
        "item_image"=>$image
    );
    $success = $item->add_item($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_item'])){
    $item_id = $_POST['item_id'];
    $item_name = $_POST['item_name'];
    $item_barcode = $_POST['item_barcode'];
    $item_category_name = $_POST['item_category_name'];
    $item_category_details = $item_category->get_item_category_by_name($item_category_name);
    $item_category_id = $item_category_details[0]['item_category_id'];
    $item_type_name = $_POST['item_type_name'];
    $item_type_details = $item_type->get_item_type_by_name($item_type_name);
    $item_type_id = $item_type_details[0]['item_type_id'];
    $item_level_name = $_POST['item_level_name'];
    $item_level_details = $item_level->get_item_level_by_name($item_level_name);
    $item_level_id = $item_level_details[0]['item_level_id'];
    $supplier_company = $_POST['supplier_company'];
    $item_supplier_details = $supplier->get_supplier_by_name($supplier_company);
    $item_supplier_id = $item_supplier_details[0]['supplier_id'];
    $item_wholesale_price = $_POST['item_wholesale_price'];
    $item_retail_price = $_POST['item_retail_price'];
    $item_quantity = $_POST['item_quantity'];
    $item_reorder_level = $_POST['item_reorder_level'];
    $image = '';
    if($item_type_name=="Textbook"){
        $image = 'items.jpg';
    }else{
        $image = 'images2.jpg';
    }
    $data = array(
        "item_name"=>$item_name,
        "item_barcode"=>$item_barcode,
        "item_category_id"=>$item_category_id,
        "item_type_id"=>$item_type_id,
        "item_level_id"=>$item_level_id,
        "item_supplier_id"=>$item_supplier_id,
        "item_wholesale_price"=>$item_wholesale_price,
        "item_retail_price"=>$item_retail_price,
        "item_quantity"=>$item_quantity,
        "item_reorder_level"=>$item_reorder_level,
        "item_image"=>$image
    );
    $key = array(
        "item_id"=>$item_id
    );
    $success = $item->update_item($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_item'])){
    $item_id = $_POST['item_id'];
    $data = $item->remove_item($item_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['items_to_return'])){
    $invoice_no = $_POST['invoice_no'];
    $data = $item->read_all_items_from_invoice($invoice_no);
?>
<div class="col-6">
    <table class="table table-stripped">
        <thead>
            <tr>
                <td style="font-weight:bold;">Items
                </td>
            </tr>
        </thead>
        <tbody>
        <?php $i=0;?>
        <?php if($data):?>
    <?php foreach($data as $data_key):?>
    <tr>
        <td>
        <div class="custom-control custom-checkbox box-t">
            <input type="checkbox" class="selected_request_items custom-control-input item-type" id="item-checkbox-<?php echo $data_key['item_request_order_id']?>" value="<?php  echo $data_key['item_request_order_id']?>">
            <label class="custom-control-label" for="item-checkbox-<?php echo $data_key['item_request_order_id']?>"></label>
            <?php echo $data_key['item_name'] ?> | <?php echo $data_key['item_type_name'] ?> | <?php echo $data_key['item_level_name'] ?> |
            Qty: <input class="selected_qty" id="qty-<?php echo $data_key['item_request_order_id']?>" type="number" style="width:80px;border:2px solid #EEE;" placeholder="Qty" value="<?php echo $data_key['qty']  ?>"> 
            | Px: <input class="selected_px" id="px-<?php echo $data_key['item_request_order_id']?>" type="number" style="width:80px;border:2px solid #EEE;" placeholder="PX" value="<?php echo $data_key['px']  ?>"> 
            | Total: <span>₵</span><span id="total-<?php echo $data_key['item_request_order_id']?>" style="font-weight:bolder;"><?php echo intval($data_key['px'])*intval($data_key['qty'])  ?></span>
            <input type="hidden" id="subtotal-<?php echo $data_key['item_request_order_id']?>" class="subtotal" value="<?php echo intval($data_key['px'])*intval($data_key['qty'])  ?>"> 
        </div>
            
        </td>
    </tr>
    <script>
        $(document).ready(function(){
            
            // Init
            // 
            // Computation
            var qty = $('#qty-<?php echo $data_key['item_request_order_id']?>').val();
            var px = $('#px-<?php echo $data_key['item_request_order_id']?>').val();
            var total = 0;
            total = parseInt(qty)*parseFloat(px);
            
            $('#total-<?php echo $data_key['item_request_order_id']?>').text(total);
            // Computation End
            var selected_qty = 0;
            var selected_px = 0;
            $(document).on('change','#qty-<?php echo $data_key['item_request_order_id']?>',function(){
                qty = $('#qty-<?php echo $data_key['item_request_order_id']?>').val();
                px = $('#px-<?php echo $data_key['item_request_order_id']?>').val();
                total = 0;
                total = parseInt($(this).val())*parseFloat(px);
                amount_due = 0;
                current_checkbox = $(this).prev().prev();
                current_checkbox.attr('title',qty);
                current_checkbox.attr('name',px);
                $(".selected_request_items:checked").each(function () {
                    var selected_el = $(this).siblings();
                    var selected_qty_el = selected_el[1].value;
                    var selected_px_el = selected_el[2].value;
                    selected_qty = parseInt(selected_qty_el);
                    selected_px = parseFloat(selected_px_el);
                    if(selected_qty&&selected_px){
                        amount_due += selected_qty*selected_px;
                        // console.log(amount_due)
                        var discounted_value = 0;
                        var commission_value = 0;
                        if(discount&&commission){
                            discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                            commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                            actual_amount = amount_due-(discounted_value+commission_value);
                        }else if(discount){
                            discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                            actual_amount = amount_due-discounted_value;
                        }else if(commission){
                            commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                            actual_amount = amount_due-commission_value;
                        }else{
                            actual_amount = amount_due;
                        }
                        // $('#actual_amount').text(actual_amount);
                        // $('#amount_due').text(amount_due);
                    }
                    
                });
                
                
                $('#total-<?php echo $data_key['item_request_order_id']?>').text(total);
                $('#subtotal-<?php echo $data_key['item_request_order_id']?>').text(total);
            });

            $(document).on('change','#px-<?php echo $data_key['item_request_order_id']?>',function(){
                qty = $('#qty-<?php echo $data_key['item_request_order_id']?>').val();
                px = $('#px-<?php echo $data_key['item_request_order_id']?>').val();
                total = 0;
                total = parseInt(qty)*parseFloat($(this).val());
                $('#total-<?php echo $data_key['item_request_order_id']?>').text(total);
                $('#subtotal-<?php echo $data_key['item_request_order_id']?>').text(total);
                amount_due = 0;
                current_checkbox = $(this).prev().prev().prev();
                current_checkbox.attr('title',qty);
                current_checkbox.attr('name',px);
                // console.log(current_checkbox)
                $(".selected_request_items:checked").each(function () {
                    var selected_el = $(this).siblings();
                    var selected_qty_el = selected_el[1].value;
                    var selected_px_el = selected_el[2].value;
                    selected_qty = parseInt(selected_qty_el);
                    selected_px = parseFloat(selected_px_el);
                    if(selected_qty&&selected_px){
                        amount_due += selected_qty*selected_px;
                        // console.log(amount_due);
                        var discounted_value = 0;
                        var commission_value = 0;
                        if(discount&&commission){
                            discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                            commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                            actual_amount = amount_due-(discounted_value+commission_value);
                        }else if(discount){
                            discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                            actual_amount = amount_due-discounted_value;
                        }else if(commission){
                            commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                            actual_amount = amount_due-commission_value;
                        }else{
                            actual_amount = amount_due;
                        }
                        // $('#actual_amount').text(actual_amount);
                        // $('#amount_due').text(amount_due);
                    }
                    
                });
                
            });
            
            var item_request_order_id = "item-checkbox-<?php echo $data_key['item_request_order_id']?>";
            var item = $('#item-checkbox-<?php echo $data_key['item_request_order_id']?>');
            
            // amount_due = 0;
            var selected_el = '';
            var arr = [];
            $(document).on('click','#item-checkbox-<?php echo $data_key['item_request_order_id']?>',function(){
                // var check_false = $('#'+item_request_order_id).not(this).prop('checked', this.checked);
                selected_el = $(".selected_request_items:checked").next().next();
                // console.log($(this).next().next())
                if($(this).prop('checked')){
                    
                    var selected_qty_el = $(this).next().next()[0].value;
                    var selected_px_el = $(this).next().next().next()[0].value;

                    var itemValue = '<?php echo $data_key['item_request_order_id'] ?>';
                    itemValue = parseInt(itemValue);
                    // if (selected_item_request_order_ids.includes(itemValue) === false){
                    //     selected_item_request_order_ids.push(itemValue);
                    //     selected_item_px.push(selected_px_el);
                    //     selected_item_qtx.push(selected_qty_el);

                    // }else{
                    //     $key = selected_item_request_order_ids.indexOf(itemValue)
                    //     // console.log($key);
                    //     selected_item_px[$key] = obj.name;
                    //     selected_item_qtx[$key] = obj.title;
                    // }
                    
                    $(this).attr('title',selected_qty_el);
                    $(this).attr('name',selected_px_el);
                    arr.push(selected_qty*selected_px_el)
                        if(selected_qty_el&&selected_px_el){
                            amount_due += selected_qty_el*selected_px_el;
                            actual_amount = amount_due;
                            var discounted_value = 0;
                            var commission_value = 0;
                            if(discount&&commission){
                                discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                actual_amount = amount_due-(discounted_value+commission_value);
                            }else if(discount){
                                discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                actual_amount = amount_due-discounted_value;
                            }else if(commission){
                                commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                actual_amount = amount_due-commission_value;
                            }else{
                                actual_amount = amount_due;
                            }
                            if(amount_due<0){
                                amount_due = 0;
                                actual_amount = amount_due;
                            }
                            // $('#amount_due').text(amount_due)
                            // $('#actual_amount').text(actual_amount);
                            
                        }
                }else{
                    var selected_qty_el = $(this).next().next()[0].value;
                    var selected_px_el = $(this).next().next().next()[0].value;
                    selected_qty = parseInt(selected_qty_el);
                    selected_px = parseFloat(selected_px_el);

                    var itemValue = '<?php echo $data_key['item_request_order_id'] ?>';
                    itemValue = parseInt(itemValue);
                    if (selected_item_request_order_ids.includes(itemValue) === false){
                        $key = selected_item_request_order_ids.indexOf(itemValue)
                        selected_item_request_order_ids.splice($key, 1);
                        selected_item_px.splice($key, 1);
                        selected_item_qtx.splice($key, 1);
                        // console.log($key);
                        // console.log(selected_item_px)

                        }else{
                        }
                    
                        selected_items = {
                            selected_item_request_order_ids:selected_item_request_order_ids,
                            selected_item_px:selected_item_px,
                            selected_item_qtx:selected_item_qtx
                        }

                    $(this).attr('title',selected_qty_el);
                    $(this).attr('name',selected_px_el);
                    amount_due = parseFloat(amount_due)-parseFloat(selected_qty*selected_px);
                    actual_amount = amount_due;
                    var discounted_value = 0;
                    var commission_value = 0;
                    if(discount&&commission){
                        discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                        commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                        actual_amount = amount_due-(discounted_value+commission_value);
                    }else if(discount){
                        discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                        actual_amount = amount_due-discounted_value;
                    }else if(commission){
                        commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                        actual_amount = amount_due-commission_value;
                    }else{
                        actual_amount = amount_due;
                    }
                    if(amount_due<0){
                        amount_due = 0;
                        actual_amount = amount_due;
                    }
                    // $('#amount_due').text(amount_due);
                    // $('#actual_amount').text(actual_amount);
                }
                
            });
        });
    </script>
    <?php $i++; ?>
<?php endforeach ?>
<?php endif ?>
    </tbody>
    </table>
    
</div>
<?php
}

if(isset($_POST['items_for_request'])){
    $item_category_id = $_POST['category_id'];
    $level_id = $_POST['level_id'];
    
?>

<?php if($item_types):?>
    <?php foreach($item_types as $type_key):?>
        
        <div class="col-6">
            <table class="table table-stripped">
                <thead>
                    <tr>
                        <td style="font-weight:bold;">
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="item-type-<?php echo $type_key['item_type_id']?>">
                            <label class="custom-control-label" for="item-type-<?php echo $type_key['item_type_id']?>">All</label>
                            <?php echo $type_key['item_type_name']?>
                        </div>
                            </td>
                    </tr>
                </thead>
                <tbody>
                    
                    <?php 
                    $i=0;
                    $data = $item->read_all_items_for_request($item_category_id,$level_id,$type_key['item_type_id']);
                    ?>
                    <?php if($data):?>
                        <?php foreach($data as $data_key):?>
                        <tr>
                            <td>
                            <?php if($data_key['item_quantity']<=0): ?>
                                <?php echo $data_key['item_name'] ?> | 
                                <span class="badge badge-danger">Out of Stock</span>
                            <?php else:?>
                            <div class="custom-control custom-checkbox box-t">
                                <input type="checkbox" class="selected_request_items custom-control-input item-type-<?php echo $type_key['item_type_id']?>" id="item-checkbox-<?php echo $data_key['item_id']?>" value="<?php  echo $data_key['item_id']?>">
                                <label class="custom-control-label" for="item-checkbox-<?php echo $data_key['item_id']?>"></label>
                                <?php echo $data_key['item_name'] ?> | 
                                Qty: <input class="selected_qty" id="qty-<?php echo $data_key['item_id']?>" type="number" style="width:80px;border:2px solid #EEE;" placeholder="Qty" value="1"> 
                                | Px: <input class="selected_px" id="px-<?php echo $data_key['item_id']?>" type="number" style="width:80px;border:2px solid #EEE;" placeholder="PX" value="<?php echo $data_key['item_retail_price']  ?>"> 
                                | Total: <span>₵</span><span id="total-<?php echo $data_key['item_id']?>" style="font-weight:bolder;"><?php echo intval($data_key['item_retail_price'])*intval($data_key['item_quantity'])  ?></span>
                                <input type="hidden" id="subtotal-<?php echo $data_key['item_id']?>" class="subtotal" value="<?php echo intval($data_key['item_retail_price'])*intval($data_key['item_quantity'])  ?>"> 
                                
                                <?php if($data_key['item_reorder_level']>=$data_key['item_quantity']): ?>
                                    <span class="badge badge-warning">You need to reorder</span>
                                <?php endif ?>
                                
                            </div>
                            <?php endif ?>    
                            </td>
                        </tr>
                        <script>
                            $(document).ready(function(){
                                
                                // Init
                                // 
                                // Computation
                                var qty = $('#qty-<?php echo $data_key['item_id']?>').val();
                                var px = $('#px-<?php echo $data_key['item_id']?>').val();
                                var total = 0;
                                total = parseInt(qty)*parseFloat(px);
                                
                                $('#total-<?php echo $data_key['item_id']?>').text(total);
                                // Computation End
                                var selected_qty = 0;
                                var selected_px = 0;
                                $(document).on('change','#qty-<?php echo $data_key['item_id']?>',function(){
                                    qty = $('#qty-<?php echo $data_key['item_id']?>').val();
                                    px = $('#px-<?php echo $data_key['item_id']?>').val();
                                    total = 0;
                                    total = parseInt($(this).val())*parseFloat(px);
                                    amount_due = 0;
                                    current_checkbox = $(this).prev().prev();
                                    current_checkbox.attr('title',qty);
                                    current_checkbox.attr('name',px);
                                    $(".selected_request_items:checked").each(function () {
                                        var selected_el = $(this).siblings();
                                        var selected_qty_el = selected_el[1].value;
                                        var selected_px_el = selected_el[2].value;
                                        selected_qty = parseInt(selected_qty_el);
                                        selected_px = parseFloat(selected_px_el);
                                        if(selected_qty&&selected_px){
                                            amount_due += selected_qty*selected_px;
                                            // console.log(amount_due)
                                            var discounted_value = 0;
                                            var commission_value = 0;
                                            if(discount&&commission){
                                                discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                actual_amount = amount_due-(discounted_value+commission_value);
                                            }else if(discount){
                                                discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                actual_amount = amount_due-discounted_value;
                                            }else if(commission){
                                                commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                actual_amount = amount_due-commission_value;
                                            }else{
                                                actual_amount = amount_due;
                                            }
                                            // $('#actual_amount').text(actual_amount);
                                            // $('#amount_due').text(amount_due);
                                        }
                                        
                                    });
                                    
                                    
                                    $('#total-<?php echo $data_key['item_id']?>').text(total);
                                    $('#subtotal-<?php echo $data_key['item_id']?>').text(total);
                                });

                                $(document).on('change','#px-<?php echo $data_key['item_id']?>',function(){
                                    qty = $('#qty-<?php echo $data_key['item_id']?>').val();
                                    px = $('#px-<?php echo $data_key['item_id']?>').val();
                                    total = 0;
                                    total = parseInt(qty)*parseFloat($(this).val());
                                    $('#total-<?php echo $data_key['item_id']?>').text(total);
                                    $('#subtotal-<?php echo $data_key['item_id']?>').text(total);
                                    amount_due = 0;
                                    current_checkbox = $(this).prev().prev().prev();
                                    current_checkbox.attr('title',qty);
                                    current_checkbox.attr('name',px);
                                    // console.log(current_checkbox)
                                    $(".selected_request_items:checked").each(function () {
                                        var selected_el = $(this).siblings();
                                        var selected_qty_el = selected_el[1].value;
                                        var selected_px_el = selected_el[2].value;
                                        selected_qty = parseInt(selected_qty_el);
                                        selected_px = parseFloat(selected_px_el);
                                        if(selected_qty&&selected_px){
                                            amount_due += selected_qty*selected_px;
                                            // console.log(amount_due);
                                            var discounted_value = 0;
                                            var commission_value = 0;
                                            if(discount&&commission){
                                                discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                actual_amount = amount_due-(discounted_value+commission_value);
                                            }else if(discount){
                                                discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                actual_amount = amount_due-discounted_value;
                                            }else if(commission){
                                                commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                actual_amount = amount_due-commission_value;
                                            }else{
                                                actual_amount = amount_due;
                                            }
                                            // $('#actual_amount').text(actual_amount);
                                            // $('#amount_due').text(amount_due);
                                        }
                                        
                                    });
                                    
                                });
                                
                                var item_id = "item-checkbox-<?php echo $data_key['item_id']?>";
                                var item = $('#item-checkbox-<?php echo $data_key['item_id']?>');
                                var id = "item-type-<?php echo $type_key['item_type_id']?>";
                                var checkbox_trigger = $('#item-type-<?php echo $type_key['item_type_id']?>');
                                $(document).on('click','#item-type-<?php echo $type_key['item_type_id']?>',function(){
                                    if($(this).prop('checked')){
                                    amount_due = 0;
                                    $('.'+id).not(this).prop('checked', this.checked);
                                    
                                    $(".selected_request_items:checked").each(function (i,obj) {

                                        if (selected_item_ids.includes(obj.value) === false){
                                            selected_item_ids.push(obj.value);
                                            selected_item_px.push(obj.name);
                                            selected_item_qtx.push(obj.title);

                                        }else{
                                            $key = selected_item_ids.indexOf(obj.value)
                                            // console.log($key);
                                            selected_item_px[$key] = obj.name;
                                            selected_item_qtx[$key] = obj.title;
                                        }
                                        
                                            var selected_el = $(this).siblings();
                                            var selected_qty_el = selected_el[1].value;
                                            var selected_px_el = selected_el[2].value;
                                            selected_qty = parseInt(selected_qty_el);
                                            selected_px = parseFloat(selected_px_el);
                                            $(this).attr('title',selected_qty_el);
                                            $(this).attr('name',selected_px_el);
                                            // console.log(amount_due,selected_qty*selected_px)
                                            if(selected_qty&&selected_px){
                                                amount_due += selected_qty*selected_px;
                                                actual_amount = amount_due;
                                                var discounted_value = 0;
                                                var commission_value = 0;
                                                if(discount&&commission){
                                                    discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                    commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                    actual_amount = amount_due-(discounted_value+commission_value);
                                                }else if(discount){
                                                    discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                    actual_amount = amount_due-discounted_value;
                                                }else if(commission){
                                                    commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                    actual_amount = amount_due-commission_value;
                                                }else{
                                                    actual_amount = amount_due;
                                                }
                                                // console.log(amount_due)
                                                if(amount_due<0){
                                                    amount_due = 0;
                                                    actual_amount = amount_due;
                                                }
                                                // $('#amount_due').text(amount_due)
                                                // $('#actual_amount').text(actual_amount);
                                            }
                                        
                                        
                                    
                                    });
                                    
                                    }else{
                                        // console.log('Mills')
                                        $('.selected_request_items:checked').each(function(i,obj){
                                            if (selected_item_ids.includes(obj.value) === false){
                                            // selected_item_ids.pop(obj.value);
                                            // selected_item_px.pop(obj.name);
                                            // selected_item_qtx.pop(obj.title);

                                            }else{
                                                $key = selected_item_ids.indexOf(obj.value)
                                                selected_item_ids.splice($key, 1);
                                                selected_item_px.splice($key, 1);
                                                selected_item_qtx.splice($key, 1);
                                                // console.log($key);
                                                // console.log(selected_item_px)
                                            }
                                        });
                                        
                                            selected_items = {
                                                selected_item_ids:selected_item_ids,
                                                selected_item_px:selected_item_px,
                                                selected_item_qtx:selected_item_qtx
                                            }
                                        $('.'+id).prop('checked', '');
                                        // console.log($(this))
                                        $(".item-type-<?php echo $type_key['item_type_id']?>:not(:checked)").each(function () {
                                            
                                            var selected_el = $(this).siblings();
                                            // console.log(selected_el.siblings())
                                            var selected_qty_el = selected_el[1].value;
                                            var selected_px_el = selected_el[2].value;
                                            selected_qty = parseInt(selected_qty_el);
                                            selected_px = parseFloat(selected_px_el);
                                            $(this).attr('title',selected_qty_el);
                                            $(this).attr('name',selected_px_el);
                                            // console.log(amount_due,selected_qty*selected_px)
                                            if(selected_qty&&selected_px){
                                                amount_due -= selected_qty*selected_px;
                                                actual_amount = amount_due;
                                                var discounted_value = 0;
                                                var commission_value = 0;
                                                if(discount&&commission){
                                                    discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                    commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                    actual_amount = amount_due-(discounted_value+commission_value);
                                                }else if(discount){
                                                    discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                    actual_amount = amount_due-discounted_value;
                                                }else if(commission){
                                                    commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                    actual_amount = amount_due-commission_value;
                                                }else{
                                                    actual_amount = amount_due;
                                                }
                                                // console.log(amount_due)
                                                if(amount_due<0){
                                                    amount_due = 0;
                                                    actual_amount = amount_due;
                                                }
                                                // $('#amount_due').text(amount_due)
                                                // $('#actual_amount').text(actual_amount);
                                            }
                                            return false;
                                        });
                                    }
                                });
                                // amount_due = 0;
                                var selected_el = '';
                                var arr = [];
                                $(document).on('click','#item-checkbox-<?php echo $data_key['item_id']?>',function(){
                                    // var check_false = $('#'+item_id).not(this).prop('checked', this.checked);
                                    selected_el = $(".selected_request_items:checked").next().next();
                                    // console.log($(this).next().next())
                                    if($(this).prop('checked')){
                                        
                                        var selected_qty_el = $(this).next().next()[0].value;
                                        var selected_px_el = $(this).next().next().next()[0].value;

                                        var itemValue = '<?php echo $data_key['item_id'] ?>';
                                        itemValue = parseInt(itemValue);
                                        // if (selected_item_ids.includes(itemValue) === false){
                                        //     selected_item_ids.push(itemValue);
                                        //     selected_item_px.push(selected_px_el);
                                        //     selected_item_qtx.push(selected_qty_el);

                                        // }else{
                                        //     $key = selected_item_ids.indexOf(itemValue)
                                        //     // console.log($key);
                                        //     selected_item_px[$key] = obj.name;
                                        //     selected_item_qtx[$key] = obj.title;
                                        // }
                                        
                                        $(this).attr('title',selected_qty_el);
                                        $(this).attr('name',selected_px_el);
                                        arr.push(selected_qty*selected_px_el)
                                            if(selected_qty_el&&selected_px_el){
                                                amount_due += selected_qty_el*selected_px_el;
                                                actual_amount = amount_due;
                                                var discounted_value = 0;
                                                var commission_value = 0;
                                                if(discount&&commission){
                                                    discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                    commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                    actual_amount = amount_due-(discounted_value+commission_value);
                                                }else if(discount){
                                                    discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                                    actual_amount = amount_due-discounted_value;
                                                }else if(commission){
                                                    commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                                    actual_amount = amount_due-commission_value;
                                                }else{
                                                    actual_amount = amount_due;
                                                }
                                                if(amount_due<0){
                                                    amount_due = 0;
                                                    actual_amount = amount_due;
                                                }
                                                // $('#amount_due').text(amount_due)
                                                // $('#actual_amount').text(actual_amount);
                                                
                                            }
                                    }else{
                                        var selected_qty_el = $(this).next().next()[0].value;
                                        var selected_px_el = $(this).next().next().next()[0].value;
                                        selected_qty = parseInt(selected_qty_el);
                                        selected_px = parseFloat(selected_px_el);

                                        var itemValue = '<?php echo $data_key['item_id'] ?>';
                                        itemValue = parseInt(itemValue);
                                        if (selected_item_ids.includes(itemValue) === false){
                                            $key = selected_item_ids.indexOf(itemValue)
                                            selected_item_ids.splice($key, 1);
                                            selected_item_px.splice($key, 1);
                                            selected_item_qtx.splice($key, 1);
                                            // console.log($key);
                                            // console.log(selected_item_px)

                                            }else{
                                            }
                                        
                                            selected_items = {
                                                selected_item_ids:selected_item_ids,
                                                selected_item_px:selected_item_px,
                                                selected_item_qtx:selected_item_qtx
                                            }

                                        $(this).attr('title',selected_qty_el);
                                        $(this).attr('name',selected_px_el);
                                        amount_due = parseFloat(amount_due)-parseFloat(selected_qty*selected_px);
                                        actual_amount = amount_due;
                                        var discounted_value = 0;
                                        var commission_value = 0;
                                        if(discount&&commission){
                                            discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                            commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                            actual_amount = amount_due-(discounted_value+commission_value);
                                        }else if(discount){
                                            discounted_value = parseFloat(amount_due)*parseFloat(discount)/100;
                                            actual_amount = amount_due-discounted_value;
                                        }else if(commission){
                                            commission_value = parseFloat(amount_due)*parseFloat(commission)/100;
                                            actual_amount = amount_due-commission_value;
                                        }else{
                                            actual_amount = amount_due;
                                        }
                                        if(amount_due<0){
                                            amount_due = 0;
                                            actual_amount = amount_due;
                                        }
                                        // $('#amount_due').text(amount_due);
                                        // $('#actual_amount').text(actual_amount);
                                    }
                                    
                                });
                            });
                        </script>
                        <?php $i++; ?>
                    <?php endforeach ?>
                    <?php endif ?>
                    
                </tbody>
            </table>
            
        </div>
            <?php endforeach ?>
            
        <?php endif ?>



<?php
}