<?php
    ob_start();
    include_once("../include/main.inc.php");
    check_session('MyCpanel', 'index.php');
?>

<?php

################ Paging Starts From Here ##################

@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$referurl = $_SERVER['HTTP_REFERER'];
$heading = "Manage Token price";
$record_type = "Price";
$script_name = "manage-token-price.php";
$redirect_script_name = "manage-token.php";
$table_name = "token_price_tbl";
####################################################

if (isset($_POST["add_record"])) {
    $sql = db_query("insert into $table_name set pack_name='$name', price='$price', min_quantity='$min_qty',max_quantity='$max_qty', token_id='$add_record'");
    $succee_msg = 'New ' . $record_type . ' has been added successfully';
    header("Location: " . SITE_ADMIN_URL."/".$script_name."?start=".$start."&token_id=".$add_record."&pagesize=" . $pagesize);
    exit;
}



if (isset($recordID) && !empty($recordID)) {
    updateTable($table_name, "pack_name='$ename', price='$eprice',min_quantity='$emin_qty' ,max_quantity='$emax_qty' where price_id='$recordID'");
    $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location:" . $referurl);
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&token_id=" . $token_id . "&pagesize=" . $pagesize);
    exit;
}



if ($id == "delete_record") {
    if (!empty($_POST['arr_id'])) {
        $catid = $_POST['arr_id'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "delete from $table_name WHERE price_id = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&token_id=" . $token_id . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;

}



admin_header("");
$qry = db_query("SELECT * FROM token_price_tbl WHERE token_id='$token_id'");
$catname = get_category_name($token_id);

?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title"><?= $heading; ?>  <a href="manage-token.php"><small>(<?= $catname ?>)</small></a></h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="nav-item d-none d-lg-flex">
                    <a class="nav-link" href="#" data-toggle="modal" data-target="#addBox">
                        <span class="btn btn-primary">+ Add New Price/Variants</span>
                    </a>
                </li>
            </ol>
        </nav>
        
    </div>

    <div class="card">
        <div class="card-body">
            <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="table-responsive">
                        <form method="post" id="detailsID">
                            <table id="order-listing" class="table">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>S.No.</th>
                                        <th>Pack Name</th>
                                        <th>Token Name</th>
                                        <th>Min Qty-Max Qty</th>
                                        <th>Price (Single)</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sl = $start + 1;
                                    while ($arr = mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td><input type="checkbox" name="arr_id[]" value="<?= $arr['price_id'] ?>"></td>
                                        <td><?= $sl ?></td>
                                        <td><?= $arr['pack_name'] ?></td>
                                        <td><?= $catname ?></td>
                                        <td><?= $arr['min_quantity'] ?>-<?= $arr['max_quantity'] ?></td>
                                        <td><?= $arr['price'] ?></td>
                                        <td>
                                            <a href="#" class="edtRcd" data-toggle="modal" data-target="#updateBox" data-id="<?= $arr['price_id'] ?>" data-name="<?= $arr['pack_name'] ?>" data-mqty="<?= $arr['min_quantity'] ?>" data-qty="<?= $arr['max_quantity'] ?>" data-price="<?= $arr['price'] ?>"><i class="icon-sm fa fa-edit mr-2"></i></a>
                                        </td>
                                    </tr>
                                    <?php $sl++; } ?>
                                </tbody>
                            </table>
                            <div class="btn-grp">
                                <button type="submit" class="btn btn-danger btn-icon-text btn-sm" onclick="select_chk();">
                                    <i class="fas fa-trash-alt mr-1"></i>Delete
                                </button>
                                <input type="hidden" name="id" value="delete_record">
                            </div>
                        </form>
                    </div>
                </div>
            </div>

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

<div class="modal fade" id="addBox" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-2">
                <h5 class="modal-title">Add New Price</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body p-2">
                <form class="forms-sample" method="POST" action="">
                    <div class="form-group">
                        <label>Category</label>
                        <input class="typeahead" type="text" value="<?= $catname ?>" readonly>
                    </div>
                    <div class="row">
                    <div class="col-md-6">
                            <div class="form-group">
                                <label>Pack Name <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="name" placeholder="Name" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Price <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="price" placeholder="Price" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            
                            <div class="form-group">
                                <label>Min. Quantity <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="min_qty" placeholder="Min. Quantity" required onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Max. Quantity <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="max_qty" placeholder="Max. Quantity" required onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')">
                            </div>
                        </div>
                                                
                    </div>
                
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                    <input type="hidden" name="add_record" value="<?= $token_id; ?>">
                    <button class="btn btn-light" data-dismiss="modal" type="button">Cancel</button>
                </form>
            </div>                
        </div>
    </div>
</div>

<div class="modal fade" id="updateBox" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-2">
                <h5 class="modal-title">Update Price</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body p-2">
                <form class="forms-sample" method="POST" action="">
                    <div class="form-group">
                        <label>Category</label>
                        <input class="typeahead" type="text" value="<?= $catname ?>" readonly>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Pack name <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="ename" placeholder="Name" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Price <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="eprice" placeholder="Price" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Min. Quanity <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="emin_qty" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" placeholder="Min. Quantity" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Max. Quanity <sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="emax_qty" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" placeholder="Max. Quantity" required>
                            </div>
                        </div>                  
                        
                        
                    </div>
                
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                    <input type="hidden" name="recordID" value="">
                    <input type="hidden" name="token_id" value="<?= $token_id; ?>">
                    <button class="btn btn-light" data-dismiss="modal" type="button">Cancel</button>
                </form>
            </div>                
        </div>
    </div>
</div>



<script language="javascript">
    function select_chk() {
        var chks = document.getElementsByName('arr_id[]');
        var hasChecked = false;
        for (var i = 0; i < chks.length; i++) {
            if (chks[i].checked) {
                hasChecked = true;
                break;
            }
        }
        if (hasChecked == false) {
            alert("Please Select At Least One.");
            return false;
        }
    }
</script>

<?php admin_footer(); ?>

<script>
    $(document).ready(function(){
        $('.edtRcd').click(function(){
            $('input[name=recordID]').val($(this).data('id'));
            $('input[name=ename]').val($(this).data('name'));
            $('input[name=eprice]').val($(this).data('price'));
            $('input[name=emax_qty]').val($(this).data('qty'));
            $('input[name=emin_qty]').val($(this).data('mqty'));
        });
    });
</script>