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

@extract($_REQUEST);
$heading = "Edit Token";
$res = getResult("admin_details", " where user_type='admin'");

$record_type = "Token";
$script_name = "manage-token.php";
$redirect_script_name = "manage-token-edit.php";
$table_name = "category_tbl";
#################### Add Record ##################
if ($id == 'remove_image') {
    mysqli_query($dbconn, "UPDATE $table_name SET $field='' where catID = '$recordID'");
    $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
}

################ Update Record ##################

if ($_REQUEST['action'] == "update_record" && $catname != "") {
    $numCnt = getCount($table_name, " where catName='$catname' and parentID='$parentID' and catID!='$recordID' and catgType='$catgType'");
    if ($numCnt > 0) {
        $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
        header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
        exit;
    }

    if ($_FILES['image']['name'] != "") {
        $file_extension = strtolower(getExtension($_FILES['image']['name']));
        if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png") {
            $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
            header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
            exit;
        }
        if ($_FILES['image']['name'] != "") {
            $categoryRes = getResult($table_name, " where catID='$recordID'", "catImage");
            $filename = $categoryRes['catImage'];
            if ($filename != "") {
                $file = site_url . "/uploaded-files/category/images/" . $filename;
                if (file_exists($file)) {
                    unlink($file);
                }

                $file_thumb = site_url . "/uploaded-files/category/images/thumbs/" . $filename;
                if (file_exists($file_thumb)) {
                    unlink($file_thumb);
                }
            }
            $cat = str_replace(" ", "-", $catname);
            $img_name = $cat;
            $img = upload_file($img_name, "image", "uploaded-files/category/images");
            updateTable($table_name, " catImage='$img' where catID='$recordID'");
        }
    }

    



    updateTable($table_name, " catName='" . $catname . "',displayName='" . $catDname . "',catDesc='" . $shortdesc . "',catDesc1='" . $fulldesc . "', pageTitle='" . $metatitle . "',pageKeywords='" . $metakeyword . "',pageDescription='" . $metadesc . "' where catID='$recordID'");
    $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';

    //header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name);

    header("Location: " . SITE_ADMIN_URL . "/" . $redirect_script_name . "?start=" . $start . "&recordID=" . $recordID . "&pagesize=" . $pagesize);
    exit;
}
########################################################
admin_header();
$res = getResult($table_name, " where catID='$recordID'");
?>
<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="<?= admin_url ?>manage-token.php">
                        <span class="btn btn-primary">Go Back</span>
                    </a>
                </li>
            </ol>
        </nav>
    </div>
    <div class="row">

        <div class="col-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <form class="forms-sample" method="post" action="" enctype="multipart/form-data"
                        onsubmit="return validate(this);">
                        <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"
                                        value="<?= $res['catName'] ?>" required>
                                </div>
                            </div>
                            <div class="col">
                                <label>Token Display Name</label>
                                <div id="bloodhound">
                                    <input class="typeahead" type="text" name="catDname"
                                        value="<?= $res['displayName'] ?>" placeholder="Token Display Name" required>
                                </div>
                            </div>
                            <!-- <div class="col">
                                <label>Price</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="number" name="catprice"
                                        value="<?= $res['catprice'] ?>" placeholder="Price">
                                </div>
                            </div> -->
                        </div>
                        <div class="form-group ">
                            <label for="exampleTextarea1">Meta Title</label>
                            <input class="typeahead" type="text" name="metatitle" value="<?= $res['pageTitle'] ?>"
                                placeholder="Meta Title">
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Meta Keyword</label>
                            <input class="typeahead" type="text" name="metakeyword" value="<?= $res['pageKeywords'] ?>"
                                placeholder="Meta Keyword">
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Meta Description</label>
                            <textarea class="form-control" name="metadesc"
                                rows="3"><?= $res['pageDescription'] ?></textarea>
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Token Short Description</label>
                            <textarea class="form-control editor" name="shortdesc"
                                rows="6"><?= $res['catDesc'] ?></textarea>
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Token Description</label>
                            <textarea class="form-control editor" name="fulldesc"
                                rows="4"><?= $res['catDesc1'] ?></textarea>
                        </div>

                     
                        <div class="form-group">
                            <label for="exampleTextarea1">Token Image </label>
                            <?php
                            if ($res['catImage'] != "") {
                                $file1 = SITE_DIR_PATH . "/uploaded-files/category/images/" . $res['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/<?= $res['catImage']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')" class="pay-status-dcs float-right">View Image <i class="far fa-eye ml-1"></i></a>
                            <input type="file" class="dropify mt-1" name="image" value="">
                        </div>
                        


                        <button type="submit" class="btn btn-primary mr-2" name="update_record">Update</button>
                        <input type="hidden" name="action" value="update_record">

                        <button class="btn btn-light"
                            onclick="javascript: window.location.href = '<?= SITE_ADMIN_URL; ?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $recordID; ?>&start=<?= $start; ?>&pagesize=<?= $pagesize; ?>'">Cancel</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>