<?php
  require_once("../../session_check.php");
  require_once("../../../classes/main_classes.php");

  //OBJECTS
  $retrieveObj=new RETRIEVE();
  $rowcountObj=new ROWCOUNT();

  $get_blog_categories=$retrieveObj->retriever("blog_categories ORDER BY b_category_name","*","fetchAll");

  $null_b_id=0;

  //check if last inserted blog has no image
  $get_blog_c=$retrieveObj->retriever("blog","*","blog.b_image='' and blog.user_id",$user_id,"blog.b_id","DESC","fetch");

  if($get_blog_c!="1020"){
    $null_b_id=$get_blog_c->b_id;
  }else{
     $null_b_id=0;
  }
  

?>


<h4 class="page-title">
  <span class="fas fa-blog"></span>
  Blog
</h4>
<p><a href="#" class="bold" onclick="blog_view()">Blog</a> <span class="fas fa-angle-right"></span> Add blog post</p>
<div class="content-wrapper ">
    <div class="row">
        <div class="col-md-12">
          <div class="mx-auto card p-2">
            <p class="text-center lead">Add blog post : <i>Information</i></p>
            <form id="add_blog_form" class="row">
              <div class="col-md-6">
                <div class="my-2">
                  <input class="form-control" type="text" name="title" placeholder="Blog post title">
                </div>
              </div>
              <div class="col-md-6">
                <div class="my-2">
                  <select class="form-control" name="blog_category">
                    <option selected disabled>Blog category</option>
                    <?php
                      if($get_blog_categories!="1020"){
                        foreach ($get_blog_categories as $blog_category) {
                
                      ?>
                          <option value="<?php echo $blog_category->b_category_id; ?>"><?php echo $blog_category->b_category_name; ?></option>
                      <?php
                        }
                      }
                    ?>
                  </select>
                </div>
              </div>
              <div class="col-md-12 pt-3">
                <label for="">Blog post body :</label>
                <textarea class="form-control" name="blog_post_body" id="blog_post_body"></textarea>
              </div>
              <div class="col-md-12">
                <div class="my-2" id="add_blog_response">
                  
                </div>

                <div class="my-2">
                  <input type="hidden" name="add_blog_note">
                  <button type="submit" class="btn btn-primary btn-block enter-btn" onclick="add_blog_basic_info()">Next</button>
                </div>
              </div>
            </form>
          </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var editor = new Simditor({
      textarea: $('#blog_post_body')
      //optional options
    });

    var null_b_id=<?php echo $null_b_id ?>

    $(document).ready(function() {
      if(null_b_id==0){
        
      }else{
        $("#main_section").load("includes_/blogs/add_blog_post_image.php",{
          b_id:null_b_id
        });
      }
      
    })
</script>