<?php
include("header.php");
require("connect.php");

?>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>

<script>

$(document).ready(function(){
    $('#prod_tbl').DataTable({'paging':false});
});

$(document).on('click','#prod_add_btn',function(){
        total = 0;
        $('.products_qty').each(function(i){
            if($(this).val()>0){
                model = $(this).attr('pmod');
                qty = $(this).val();
                id = $(this).attr('pid');
                application = $(this).attr('papp');
                price = $(this).attr('pprice');
                pretotal = price*qty
                total+=pretotal;

                toDB(1,id,model,qty,application,price,pretotal);      
                // alert(next);      
            }
        });
        toDB(0,0,0,0,0,total);
        alert('Η ενέργεια ολοκληρώθηκε επιτυχώς!');
    });

function toDB(type=0,id=0, model=0, qty=0, application=0, price=0, total=0){
    $.ajax({
            url: 'toDB.php',
            data: {type: type, id: id, model: model, qty: qty, application: application, price: price, total: total, user: <?php echo $_GET['cID']; ?>},
            method: 'POST',
            async: false,
            success: function(e){
                if(e!=''){
                   alert(e);
                }
            }
        });
}

</script>

    <div class="row">
        <div class="box col-md-12">
            <div class="box-inner">
                <div class="box-header well" data-original-title="">
                    <h2>Προϊόντα</h2>

                    
                </div>
                <div class="box-content">
                    <table class="table table-striped table-bordered" id="prod_tbl">
                        <thead>
                        <tr>
                            <!-- <th>img</th> -->
                            <th>ID</th>
                            <th>Όνομα Προϊόντος</th>
                            <th>Ποσότητα</th>                                                      
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                            $q = "SELECT products_id, products_model, products_image, products_price, application, products_quantity FROM products";
                            $res = mysqli_query($dbC,$q);
                            while($row = mysqli_fetch_array($res)){
                                //<td><img width=\"50px\" src=\"http://www.libertab2b.gr/cus/images/".$row['products_image']."\" /></td>
                                echo "<tr>
                                        <td>".$row['products_model']."</td>
                                        <td>".$row['application']."</td>
                                        <td><input class='products_qty' pid='".$row['products_id']."' pprice='".$row['products_price']."' papp='".$row['application']."' pmod='".$row['products_model']."' type='number' value='0' id='qty-".$row['products_model']."' /></td>
                                    </tr>";
                            }
                        ?>
                        
                        </tbody>
                    </table>
                   <input type="button" name="prod_add_btn" id="prod_add_btn" value="Προσθήκη" />
                </div>
            </div>
        </div>
    </div><!--/span-->
