<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    echo "<script>window.location.assign('login.php');</script>";
}
    include("header.php");
    
    $data = mysqli_fetch_array($obj->Edit_Sub_Category($_GET["id"]));
    
?>

<style>
    .page-header .left {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between !important;
    }
    a, .btn-link {
        color: #fff;
    }
    a:hover {
        color: #fff;
    }
    
    th,td{
        text-align:left !important;
    }
    
    @media screen and (max-width: 767px) {
        .page-title {
            margin-right: 0;
            font-size: 14px;
        }
    }
</style>


    <div class="page">
        <div id="page_top" class="section-body top_dark">
            <div class="container-fluid">
                <div class="page-header">
                    <div class="left">
                        <!--<a href="javascript:void(0)" class="icon menu_toggle mr-3"><i class="fa fa-align-left"></i></a>-->
                        <h1 class="page-title" style="color: #fff !important;">Admin Portal</h1>
                        <img src="myassets/images/logo1.png" width="30%" class="d-sm-none"/>
                        <a href="logout.php" class="icon menu_toggle mr-3"><i class="fa fa-sign-out"></i> Logout</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="section-body mt-3">
            <div class="container-">
                <div class="card p-5">
                    <div class="mb-4">
                        <h3>Edit Sub Category</h3>
                    </div>
                
                    <form method="post" enctype="multipart/form-data">
                    
                    <div class="row">
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="title" class="form-label">Category Name</label>
                                <input type="text" class="form-control" id="title" name="cat_name" placeholder="Category Name" value="<?php echo $data["title"];?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="image" class="form-label">Image</label>
                                <input class="form-control" type="file" id="image" name="image">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="title" class="form-label">Slug</label>
                                <input type="text" class="form-control" id="title" name="slug" placeholder="Slug" value="<?php echo $data["slug"];?>">
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="title" class="form-label">Parent Category</label>
                                <select name="cat_id" class="form-control">
                                    <option selected disabled>Default</option>
                                    <?php
                                        $cat = $obj->View_Category();
                                        foreach($cat as $key => $value){
                                    ?>
                                    
                                    <option <?php if($value["id"] == $data["cat_id"]){echo "selected";}?> value="<?php echo $value["id"];?>"><?php echo $value["title"];?></option>
                                    
                                    <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        
                        
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="title" class="form-label">Meta Title</label>
                                <input type="text" class="form-control" id="title" name="meta_title" placeholder="Meta title" value="<?php echo $data["meta-title"];?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="title" class="form-label">Meta Keywords</label>
                                <input type="text" class="form-control" id="title" name="meta_key" placeholder="Meta Keywords" value="<?php echo $data["meta-keyword"];?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="title" class="form-label">Meta Description</label>
                                <textarea class="form-control" id="summary" name="meta_desc" rows="2" placeholder="Meta Description"><?php echo $data["meta-description"];?></textarea>
                            </div>
                        </div>
                       
                       
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="summary" class="form-label">Description</label>
                                <textarea class="form-control" id="summary" name="desciption" rows="3" placeholder="Description"><?php echo $data["description"];?></textarea>
                            </div>
                        </div>
                        
                        <div class="col-md-12">
                            <button type="submit" name="_btn" class="btn btn-primary">Submit</button>
                        </div>
                    </div>
              
                    
                </form>
                </div> 
                
            </div>
        </div>
        
        
        
        
        
        
<?php
    include("footer.php");
    
   if(isset($_POST["_btn"])){
        $title = $_POST["cat_name"];
        $description = $_POST["desciption"];
        $slug = $_POST["slug"];
        $cat_id = $_POST["cat_id"];
        $metatitle = $_POST["meta_title"];
        $metakeyword = $_POST["meta_key"];
        $metadescription = $_POST["meta_desc"];
        
        $old_img = $data["image"];
        $image = $old_img;
        
        if(!empty($_FILES["image"]["name"])){
        	$image = time().$_FILES["image"]["name"];
        	move_uploaded_file($_FILES["image"]["tmp_name"], "upload/".$image);
        	unlink("upload/$old_img");
        }
        
        $obj->Update_Sub_Category($cat_id,$image,$title,$description,$slug,$metatitle,$metakeyword,$metadescription,$_GET["id"]);
        echo "<script>alert('Sub Category Updated Successfully'); window.location.assign('sub_category.php')</script>";
    }
?>

    