<?php

include_once("../include/main.inc.php");

check_session('MyCpanel', 'index.php');
################ Paging Starts From Here ##################
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$heading = "Manage Token";
$record_type = "Token";
$script_name = "manage-token.php";
$table_name = "category_tbl";



if (isset($_POST['active'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'Y' WHERE catID = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
    exit;
}



if (isset($_POST['inactive'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'N'  WHERE catID = '$id'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
    exit;
}
########################################################

admin_header("");
?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">
            <?= $heading; ?>
        </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 Token</span>
                    </a>
                </li>
            </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
                    <form action="" method="post">
                        <div class="table-responsive">
                            <table id="order-listing" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th class="_reab">
                                            <div class="form-check mt-0">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input">
                                                    <i class="input-helper"></i>
                                                </label>
                                            </div>
                                        </th>
                                        <th>S.No.</th>
                                        <th>Token Name</th>
                                        <th>Image</th>
                                        <th>Price</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sl = $start + 1;
                                    $qry = mysqli_query($dbconn, "SELECT * FROM category_tbl WHERE parentID=11");
                                    while ($arr = mysqli_fetch_array($qry)) { ?>
                                        <tr>
                                            <td>
                                                <div class="form-check mt-0">
                                                    <label class="form-check-label">
                                                        <input type="checkbox" class="form-check-input" name="cat_arr[]"
                                                            value="<?= $arr['catID'] ?>"><i class="input-helper"></i>
                                                    </label>
                                                </div>
                                            </td>
                                            <td>
                                                <?= $sl ?>
                                            </td>
                                            <td>
                                                <p class="ordern">
                                                    <?= $arr['catName']; ?>
                                                </p>
                                                <p class="user-dcs">
                                            <a href="<?=SITE_ADMIN_URL;?>/manage_location_content.php?category_id=<?=$arr['catID'];?>">Sub
                                           Domain Content </a>
                                        </p>
                                            </td>
                                            <td>
                                                <?php
                                                if ($arr['catImage'] != "") {
                                                    $file1 = SITE_DIR_PATH . "/uploaded-files/category/images/" . $arr['catImage'];
                                                    $dimens = GetImageSize($file1);
                                                    if ($dimens[0] > 130) {
                                                        $dimens[0] = $dimens[0] + 30;
                                                        $dimens[1] = $dimens[1] + 80;
                                                    } else {
                                                        $dimens[0] = 200;
                                                        $dimens[1] = 200;
                                                    }
                                                }
                                                ?>
                                                <a href="javascript:void(0);"
                                                    onClick="openwin('<?= SITE_ADMIN_URL; ?>/popup_new.php?img=category/images/<?= $arr['catImage']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')"
                                                    class="pay-status-dcs">View Image <i class="far fa-eye ml-1"></i></a>
                                            </td>
                                            <td>
                                                <a href="<?= SITE_ADMIN_URL ?>/manage-token-price.php?token_id=<?= $arr['catID'] ?>" class="rec-date-dcs">Manage Price</a>                                                
                                            </td>
                                            <td>
                                                <p class="status-dcs">
                                                    <?= ($arr['status'] == 'Y') ? '<label class="badge badge-success badge-pill">Active</label>' : '<label class="badge badge-danger badge-pill">In-Active</label>'; ?>
                                                </p>
                                            </td>
                                            <td>
                                                <p class="download-dcs">
                                                    <a href="<?= SITE_ADMIN_URL ?>/manage-token-edit.php?recordID=<?= $arr['catID']; ?>">
                                                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                            viewBox="0 0 24 24" fill="none" stroke="currentColor"
                                                            stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                                                            class="feather feather-edit-3">
                                                            <path d="M12 20h9"></path>
                                                            <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
                                                        </svg>
                                                    </a>
                                                </p>
                                              
                                            </td>
                                        </tr>
                                        <?php $sl++;
                                    } ?>
                                </tbody>
                            </table>
                            <div class="btn-grp">
                                <div class="btn-grp">
                                    <button type="submit" name="active" onclick="return select_chk()"
                                        class="btn btn-success btn-icon-text btn-sm">
                                        <i class="fas fa-toggle-on mr-1"></i>Activate
                                    </button>
                                    <button type="submit" name="inactive" onclick="return select_chk()"
                                        class="btn btn-warning btn-icon-text btn-sm">
                                        <i class="far fa-times-circle mr-1"></i>Deactivate
                                    </button>
                                </div>
                            </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal" id="addBox" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header p-2">
                <h5 class="modal-title">Add token</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="form-data/multipart">
                <div class="modal-body p-2">
                    <div class="form-group row">
                        <div class="col">
                            <label>Token Name</label>
                            <div id="the-basics">
                                <input class="typeahead" type="text" name="catname" placeholder="Token Name" required>
                            </div>
                        </div>
                        <div class="col">
                            <label>Token Display Name</label>
                            <div id="bloodhound">
                                <input class="typeahead" type="text" name="catDname" placeholder="Token Display Name" required>
                            </div>
                        </div>
                        <!-- <div class="col">
                            <label>Price</label>
                            <div id="the-basics">
                                <input class="typeahead" type="number" name="catprice" placeholder="Price">
                            </div>
                        </div> -->
                    </div>
                    <div class="form-group ">
                        <label for="exampleTextarea1">Meta Title</label>
                        <input class="typeahead" type="text" name="metatitle" placeholder="Meta Title">
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Meta Keyword</label>
                        <input class="typeahead" type="text" name="metakeyword" placeholder="Meta Keyword">
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Meta Description</label>
                        <textarea class="form-control" name="metadesc" rows="6"> </textarea>
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Short Description</label>
                        <textarea class="form-control editor" name="shortdesc" rows="6"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Description</label>
                        <textarea class="form-control editor" name="fulldesc" rows="4"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Image</label>
                        <input type="file" class="dropify" name="upFile" value="">
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="add_token_rcd" value="add_token_rcd">
                    <button type="submit" class="btn btn-primary">Save changes</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script language="javascript">
    function select_chk() {
        var chks = document.getElementsByName('cat_arr[]');
        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(){
        $('.form_submit').submit(function(e){
            e.preventDefault();
            let frm = $(this);
            let lnk = frm.attr('action');
            frm.find('input, textarea, select, button').prop('readonly', true);
            $('.frm-error').remove();
            $.ajax({
            url: lnk,
            data: new FormData(this),
            type:'post',
            dataType: 'JSON',
            processData: false,
            cache: false,
            contentType: false,
            success: function(r){
                if(r.success == true){
                if(r.alert){alert(alert());}
                if(r.msg){frm.before(r.msg);}
                if(r.rlink){window.location.href = r.rlink;}
                if(r.reload){location.reload();}
                }else{
                frm.find('input, textarea, select, button').prop('readonly', false);
                $.each(r.message, function(k,v){
                    $('input[name='+k+'],select[name='+k+'],textarea[name='+k+']').parent().before(v);
                });
                }
            }
            });
        });
    });
</script>