var save_method;
/** ========================================================================
 * Posts and Categories
 * ========================================================================**/
 function readImage(file) {
  var reader = new FileReader();
  var image = new Image();
  reader.readAsDataURL(file);
  reader.onload = function(_file) {
    image.src = _file.target.result; // url.createObjectURL(file);
    image.onload = function() {
      var w = this.width,
      h = this.height,
      t = file.type, // ext only: // file.type.split('/')[1],
      n = file.name,
      s = file.size;
        //console.log(s);
        if (file.size > 2097152) 
        {
          toastr.error('Image too large: ' + file.size+'. Try less than 2MB!');
        }else {
          $('#postImgPreview').append('<img src="' + this.src + '" class="img-thumbnail thumb" style="height: 120px">');
        };
    };
    image.onerror = function() {
        toastr.error('Invalid file type: ' + file.type);
    };
  };
}
$("#post_image").change(function(e) {
  if(this.disabled){
    toastr.error('Sorry! File upload not supported!');
  }
  let numOfFiles = 0;
  var F = this.files;
  if(F && F[0]) 
  {
    if(numOfFiles + F.length > 4)
    {
//      $("#postImg").attr("disabled", "disabled");
        toastr.error('You can only upload at most 2 Images!');
    }else{
      for(var i = 0; i < F.length; i++) { 
      readImage(F[i]);
      } 
    }  
  }
});
// Post form submission

//Delete Post
 function delete_post(id, name){
  var confirmation = confirm('Are you sure you want to delete '+ name +' post?');
  if(confirmation == true){
      window.location.href = base_url + ('administrator/posts/delete/')+ id;
  }
}
/**===============================================================
 * Delete Post Category
 =================================================================*/
 function delete_postCategory(id, name){
  var confirmation = confirm('Are you sure you want to delete '+ name +' page?');
  if(confirmation == true){
      window.location.href = base_url + ('administrator/posts/delete/')+ id;
  }
}


 /**===============================================================
 * POST COMMENTS
 =================================================================*/

  function add_postcomment(id)
  {
    save_method = 'add';
    var form = $('#comment-form')[0].reset();

    $("#comment_PostModal").modal("show");
    $(".modal-title").text("Add Comment!");
    $(".stbtn").text("Save Comment")
    
    $('[name="post_id"]').val(id);
  }

  function edit_postcomment(id)
  {
    save_method = "update";
    var form = $('#comment-form')[0].reset();

    $.ajax({
      url: base_url + 'administrator/posts/comment/edit/' + id,
      type: "GET",
      dataType: "JSON",
      success: function(data)
      {
        $('[name = "id"]').val(data.id);
        $('[name="post_id"]').val(data.post_id);
        $('[name="description"]').val(data.description);
        $('[name="user_id"]').val(data.user_id);

        $('#comment_PostModal').modal('show');
        $('.modal-title').text('Update Comment');
        $('.stbtn').text('Update'); 
      }
    })
  }

  function save_postcomment()
  {
    var form = $('#comment-form')[0];
    var url;
    if(save_method == 'add'){
      url = base_url + 'administrator/posts/comment/add'
    }else{
     url = base_url + 'administrator/posts/comment/update'
    }

    $.ajax({
      type: "post",
      url: url,
      data: new FormData(form),
      processData: false,
      contentType: false,
      headers: {'X-Requested-With': 'XMLHttpRequest'},
      success: function (data) {
        if (data.success == true) {
          $('#comment_PostModal').modal('hide');
          if(save_method == 'add')
            {
                toastr.success(data.msg);
            }else{
                toastr.info(data.msg);
            }
           $('#comment-form')[0].reset();
        } else {
            toastr.error(data.msg);
        }
        window.location.reload();
      },
      error: function (xhr, status, error) {
        console.error(xhr, status, error);
      }
    });
  }
  function delete_postcomment(id){
    var confirmation = confirm('Are you sure you want to delete this COMMENT');
    if(confirmation == true){
        window.location.href = base_url + ('administrator/posts/comment/delete/')+ id;
    }
  }