<?php
session_start();
if (!isset($_SESSION['admin_id'])) {
    echo "<script>window.location.assign('login.php');</script>";
}
    include("header.php");
    
?>

<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>Add New Blogs</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">Blog Title</label>
                                <input type="text" class="form-control" id="title" name="title" placeholder="Enter title">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="image" class="form-label">Blog 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">Parent Category</label>
                                <select name="cat_id" class="form-control">
                                    <option selected disabled>Default</option>
                                    <?php
                                        $data = $obj->View_Category();
                                        foreach($data as $key => $value){
                                    ?>
                                    
                                    <option 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">Slug</label>
                                <input type="text" class="form-control" id="slug" name="slug" placeholder="Slug">
                            </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">
                            </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">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="mb-3">
                                <label for="summary" class="form-label">Meta Description</label>
                                <textarea class="form-control" id="summary" name="meta-desc" rows="3" placeholder="Meta Description"></textarea>
                            </div>
                        </div>
                        
                    
                        <div class="col-md-12">
                            <div class="mb-3">
                                <label for="summary" class="form-label">Summary</label>
                                <textarea class="form-control" id="summary" name="summary" rows="3" placeholder="Enter a short summary"></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["title"];
        $metatitle = $_POST["meta-title"];
        $metakey = $_POST["meta-key"];
        $metadesc = $_POST["meta-desc"];
        $summary = $_POST["summary"];
        $slug = $_POST["slug"];
        $category = $_POST["cat_id"];
        $subcategory = $_POST["sub_cat_id"];
        $image = "";
        
        if(!empty($_FILES["image"]["name"])){
        	$image = time().$_FILES["image"]["name"];
        	move_uploaded_file($_FILES["image"]["tmp_name"], "upload/".$image);
        }
        
        $obj->Add_Blog($title,$metatitle,$metakey,$metadesc,$summary,$image,$category,$subcategory,$slug);
        echo "<script>alert('Blog Add Successfully'); window.location.assign('blogs.php')</script>";
    }
?>


// <script>
//   $("select[name=cat_id]").change(function () {
//     var id = $(this).val();
//     $.post("ajax.php", { cat_id: id }, function (res) {
//      // res = JSON.parse(res);
//       if (res) {
//         $("select[name=sub_cat_id]").html(res);
//       }
//     })
//   })
// </script>
    