<?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 Price/Variants";
$record_type = "Price";
$script_name = "manage_price.php";
$redirect_script_name = "manage_price.php";
$table_name = "attribute_tbl";
####################################################

if ($id == "add_record") {
    $sql = db_query("insert into $table_name set price='$price',validity='$validity', brand_id='$brand_id', cat_id='$category_id', user_type='$user_type', required_doc='$req_docs', updatetime=now()");
    $succee_msg = 'New ' . $record_type . ' has been added successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&category_id=" . $category_id . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}



if (isset($recordID) && !empty($recordID)) {

    updateTable($table_name, "price='$eprice',validity='$evalidity', brand_id='$ebrand_id', user_type='$euser_type', required_doc='$ereq_docs', updatetime=now() where slno='$recordID'");

    $_SESSION['session_message'] = $record_type . ' has been updated successfully';

    header("Location:" . $referurl);

    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&category_id=" . $category_id . "&catID=" . $catID . "&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 slno = '$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 . "&category_id=" . $category_id . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;

}



admin_header("");

$qry = db_query("SELECT * FROM attribute_tbl WHERE cat_id='$category_id'");

$br_qyery = db_query("select * from brand_tbl");
$brndList = [];
while ($brand = mysqli_fetch_array($br_qyery)) {
    $brndList[$brand['slno']] = $brand['brandName'];
}
$catname = get_category_name($category_id);

?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title"><?= $heading; ?>  <a href="manage-category.php?parentID=1"><small>(<?= $catname ?>)</small></a></h3>
        <!-- <h3 class="page-title"><a href="<?= SITE_ADMIN_URL; ?>/<?= $script_name; ?>?category_id=<?= $category_id; ?>">Add New Price</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="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>Category Name</th>
                                        <th>User type</th>
                                        <th>Brand</th>
                                        <th>Price</th>
                                        <th>Validity</th>
                                        <th>Documents</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['slno'] ?>"></td>
                                        <td><?= $sl ?></td>
                                        <?php if ($category_id != 0) { ?>
                                        <td><?= $catname ?></td>
                                        <?php } ?>
                                        <td><?= $arr['user_type'] ?></td>
                                        <td><?= $brndList[$arr['brand_id']] ?></td>
                                        <td><?= $arr['price'] ?></td>
                                        <td><?= $arr['validity'] ?> Year</td>
                                        <td><?= $arr['required_doc'] ?></td>
                                        <td>
                                            <a href="#" class="edtRcd" data-toggle="modal" data-target="#updateBox" data-id="<?= $arr['slno'] ?>" data-brand="<?= $arr['brand_id'] ?>" data-user="<?= $arr['user_type'] ?>" data-price="<?= $arr['price'] ?>" data-val="<?= $arr['validity'] ?>" data-doc="<?= $arr['required_doc'] ?>"><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 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/Variants</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 ?>"
                            placeholder="Category Name" readonly>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>User type</label>
                                <select name="user_type" class="form-control">
                                    <option value="Individuals">Individuals</option>
                                    <option value="Organization">Organization</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Brand</label>
                                <select name="brand_id" class="form-control">
                                    <?php foreach($brndList as $bk =>$bv) { ?>
                                    <option value="<?= $bk ?>"><?= $bv ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Validity</label>
                                <select name="validity" class="form-control">
                                    <option value="1">1 Year</option>
                                    <option value="2">2 Year</option>
                                    <option value="3">3 Year</option>
                                    <option value="4">4 Year</option>
                                    <option value="5">5 Year</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Price</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-12">
                            <div class="form-group">
                                <label>Required Documents <small class="text-danger">(Must be seperate by 'comma')</small></label>
                                <input class="form-control form-control-sm" type="text" name="req_docs" placeholder="Required Documents" required>
                            </div>
                        </div>
                    </div>
                
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                    <input type="hidden" name="id" value="add_record">
                    <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/Variants</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>User type</label>
                                <select name="euser_type" class="form-control">
                                    <option value="Individuals">Individuals</option>
                                    <option value="Organization">Organization</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Brand</label>
                                <select name="ebrand_id" class="form-control">
                                    <?php foreach($brndList as $bk =>$bv) { ?>
                                    <option value="<?= $bk ?>"><?= $bv ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Validity</label>
                                <select name="evalidity" class="form-control">
                                    <option value="1">1 Year</option>
                                    <option value="2">2 Year</option>
                                    <option value="3">3 Year</option>
                                    <option value="4">4 Year</option>
                                    <option value="5">5 Year</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Price</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-12">
                            <div class="form-group">
                                <label>Required Documents <small class="text-danger">(Must be seperate by 'comma')</small></label>
                                <input class="form-control form-control-sm" type="text" name="ereq_docs" placeholder="Required Documents" required>
                            </div>
                        </div>
                    </div>
                
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                    <input type="hidden" name="recordID" value="">
                    <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'));
            $('select[name=euser_type]').val($(this).data('user'));
            $('select[name=ebrand_id]').val($(this).data('brand'));
            $('select[name=evalidity]').val($(this).data('val'));
            $('input[name=eprice]').val($(this).data('price'));
            $('input[name=ereq_docs]').val($(this).data('doc'));
        });
    });
</script>