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

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

$record_type = "Token";
$script_name = "manage-token.php";
$redirect_script_name = "manage-brand-edit.php";
$table_name = "brand_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 (isset($_POST['update_brand']) && isset($_POST['brandid']) && isset($_POST['catid'])) {

    $name = $_POST['brandimage'];
    $target_file = '../uploaded-files/brand-logo/'.$name;
    if(!empty($_FILES['upFile']['name'])){
        if(move_uploaded_file($_FILES["upFile"]["tmp_name"], $target_file)){
            $name = basename($_FILES["upFile"]["name"]);
        }
    }



    updateTable($table_name, " brandName='" . $brandname . "',brandTitle='" . $brandname . "',brandImage='" . $name . "' WHERE slno=".$brandid);

################ Update Category Table ################
    updateTable('category_tbl', " catName='" . $catDname . "',displayName='" . $catDname . "',catDesc='" . $shortdesc . "',catDesc1='" . $fulldesc . "', pageTitle='" . $metatitle . "',pageKeywords='" . $metakeyword . "',pageDescription='" . $metadesc . "' WHERE catID='$catid'");
    $_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();
$brand = getResult($table_name, " where slno='$recordID'");
$cat = getResult('category_tbl', " where catID=".$brand['catID']);
?>
<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-brand.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 action="#" method="post" enctype="form-data/multipart">
                    <div class="modal-body p-2">
                        <div class="form-group row">
                            <div class="col">
                                <label>Brand Name</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="brandname" placeholder="Brand Name" required value="<?= $brand['brandName']; ?>">
                                </div>
                            </div>
                            <div class="col">
                                <label>Brand Title</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="brandtitle" placeholder="Brand Title" required value="<?= $brand['brandTitle']; ?>">
                                </div>
                            </div>
                            <div class="col">
                                <label>Brand (Category) Display Name</label>
                                <div id="bloodhound">
                                    <input class="typeahead" type="text" name="catDname" placeholder="Brand Display Name" required value="<?= $cat['catName']; ?>">
                                </div>
                            </div>
                        
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Meta Title</label>
                            <input class="typeahead" type="text" name="metatitle" placeholder="Meta Title" value="<?= $cat['pageTitle']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Meta Keyword</label>
                            <input class="typeahead" type="text" name="metakeyword" placeholder="Meta Keyword" value="<?= $cat['pageKeywords']; ?>">
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Meta Description</label>
                            <textarea class="form-control" name="metadesc" rows="3"><?= $cat['pageDescription']; ?></textarea>
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Short Description</label>
                            <textarea class="form-control editor" name="shortdesc" rows="6"><?= $cat['catDesc']; ?></textarea>
                        </div>
                        <div class="form-group">
                            <label for="exampleTextarea1">Description</label>
                            <textarea class="form-control editor" name="fulldesc" rows="4"><?= $cat['catDesc1']; ?></textarea>
                        </div>

                        <div class="form-group">
                            <label for="exampleTextarea1">brand Image</label>
                            <input type="file" class="dropify" name="upFile">
                            <input type="hidden" name="brandimage" value="<?= $brand['brandImage']; ?>">
                            <input type="hidden" name="brandid" value="<?= $brand['slno']; ?>">
                            <input type="hidden" name="catid" value="<?= $brand['catID']; ?>">
                        </div>
                        
                    </div>
                    <div class="modal-footer">
                        <input type="hidden" name="update_brand" value="update_brand">
                        <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>
    </div>
</div>
<!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>