/** ========================================================================
 * 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);
        }
      });
    });
   }
  /**===============================================================
   * Edit/Update/Delete Menu Item
   =================================================================*/
   function edit_menu(id){
    var form =  $('#form-menu')[0].reset();
     $.ajax({
         url:  base_url + '/administrator/menu/item/custom/edit/'+ id,
         type: "GET",
         dataType: "JSON",
         success: function(data)
         {
             $('[name="id"]').val(data.id);
             $('[name = "title"]').val(data.title);
             $('[name = "menutype_id"]').val(data.menutype_id);
             //$('[name = "parent_id"]').val(data.parent_id);
             $('[name = "ordering"]').val(data.ordering);
             $('[name = "description"]').val(data.description);
             $('[name = "path"]').val(data.path);
             $('[name = "url"]').val(data.url);
             $('[name = "level"]').val(data.level);
             if(data.photo !== null){
                $("#preview-image").html("<img src="+data.photo+" width='80px'>");
              }

            //Show selected location
            $( "#parent_id option:selected").text(data.parent_id);
            $( "#parent_id option:selected").val(data.parent_name);
            
             //load modal
             start_load()
             $("#EditMenuModal").modal('show');
             $(".modal-title").text("Edit Menu Item!");
             $(".stbtn").text("Update");
         }
     });
 }
 function save_menu() {
  var form = $('#form-menu')[0];
  var formData = new FormData(form);

  //console.log(formData);

  $.ajax({
      type: "post",
      url: base_url + '/administrator/menu/item/custom/update',
      dataType: "JSON",
      data: formData,
      processData: false,
      contentType: false,
      headers: {'X-Requested-With': 'XMLHttpRequest'},
      success: function(data) {
          if (data.success == true) {
              $('#EditMenuModal').modal('hide');
              toastr.success(data.msg);
              $('#EditMenuModal').reset;
          } else {
              toastr.error(data.msg);
          }
      }
  });
}
/**Delete Menu*/
function delete_menuItem(id, name) {
  var confirmation = confirm('Are you sure you want to delete ' + name + '?');
  if (confirmation) {
      window.location.href = base_url + '/administrator/menu/item/delete/' + id;
  }
}
  
/**Delete Menu type*/
function delete_menuType(id, name)
{
  var confirmation = confirm('Are you sure you want to delete ' + name + '?');
  if (confirmation) {
      window.location.href = base_url + '/administrator/menu/group/delete/' + id;
  }
}
   