/** ========================================================================
 * Pages 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]);
        } 
      }  
    }
  });
  //Delete Page
  function delete_post(id, name)
   {
      $("#delPageModal").modal('show');
    $("#pname").html(name);
    $("#postbtn").on('click', function(){
      $.ajax({
        url : base_url+"/pages/delete/"+id,
        type: "POST",
        dataType: "JSON",
        headers: {'X-Requested-With': 'XMLHttpRequest'},
        success: function(data)
        {
          $("#delPageModal").modal('hide');
          html = '<div class="alert alert-danger" role="alert">'+data.msg+'</div>';
          document.getElementById('showMsg').innerHTML = html;
          //toastr.error(data.msg);
          setTimeout(function(){
           location.href = base_url+"/pages";
          },800);
        }
      });
    });
   }
  /**===============================================================
   * Delete Page Category
   =================================================================*/
   function delete_pageCategory(id, name)
   {
      $("#delCatModal").modal('show');
    $("#catname").html(name);
    $("#del-Catbtn").on('click', function(){
      $.ajax({
        url : base_url+"/pages/categories/delete/"+id,
        type: "POST",
        dataType: "JSON",
        headers: {'X-Requested-With': 'XMLHttpRequest'},
        success: function(data)
        {
          $("#delCatModal").modal('hide');
          html = '<div class="alert alert-danger" role="alert">'+data.msg+'</div>';
          document.getElementById('showMsg').innerHTML = html;
          //toastr.error(data.msg);
          setTimeout(function(){
           location.href = base_url+"/pages/categories";
           //location.reload();
          },800);
        }
      });
    });
   }
   