alert("ssssssssssssssssssssssssssssssssss");
$(document).ready(function () {
  //button for click INPUT file
  //button id=selpic
  //INPUT file id=upload
  $("#span").click(function () {
    alert("sssss");
    $("#upload").trigger('click');
  });
  //after select file and change INPUT file data
  $('#upload').change(function (e) {
       var self=this;
  //check filereader and send image to crroper canvas
       if (typeof (FileReader) != "undefined") {
        if (self.files && self.files[0]) {
          var reader = new FileReader();
           reader.onload = function (e) {
  //check filereader and send image to crroper canvas
              //$("#croper_picpro").children().eq(1).attr("src", e.target.result );
              alert(e.target.result);
              if (cropper!=undefined){
//build crroper canvas
              //  cropme();
              alert("kkkk");
              rescrop(e.target.result);
              }
              //cropme();
//send image to crroper canvas
            alert("1111");
            $("#upax").css("visibility","visible");
            $("#upax").css("cursor","pointer");
              alert("uu333uu");
           };
           reader.readAsDataURL(self.files[0]);
         }else{
           alert("عکس را انتخاب کنید");
           $("#upax").css("visibility","hidden");
           $("#upax").css("cursor","default");
           cropper.destroy();
         }
       } else {
         alert("مرورگر شما از امکانات HTML5 پشتیبانی نمی نماید.");
       }
       alert("oooooooookkkkkkkkkkkk");
  });
  $("#upax").click(async function(){
    alert("ppppppppppp");
      if(myurl_ins=="upd_project"){$("#chk").prev().attr("disabled",false);}
      alert("oooooooooooo");
       formdata=new FormData(frm);
       alert("555---"+$("#image").attr("src"));
       if($("#image").attr("src")){
         alert("url------>"+cropper.getCroppedCanvas({width: width_out,height: height_out}).toDataURL('image/jpeg'));
//var imageBlob=cropper.getCroppedCanvas({width: width_out,height: height_out});
         //var imageBlob = await new Promise(resolve => cropper.getCroppedCanvas({width: width_out,height: height_out}).toDataURL(resolve, 'image/jpeg'));
          var imageBlob = await new Promise(resolve => cropper.getCroppedCanvas({width: width_out,height: height_out}).toBlob(resolve, 'image/jpeg'));
        // var myfile=new File(cropper.getCroppedCanvas().toDataURL('image/jpeg'), "hhhhh.jpg", { type: 'image/jpeg' });

         alert(imageBlob);
         //myfile=new File([blob], document.getElementById("upload").files[0]["name"], { type: 'image/jpeg' });

       // alert("1----"+formdata.has("ax"));
       alert("x1");
       if(formdata.has("ax")){
         alert("x2");
          formdata.delete("ax");
          alert("x3");
        }
        alert("x4");

       formdata.append("ax", imageBlob, "ax.jpg");
       alert("x5");
     }
     alert("x6");
     alert(myurl_ins);
     //var result=await jqajax(myurl_ins,"x="+imageBlob,"get","loader");
     var result=await jqajax("/inspicnewcase/",formdata,"post","loader");
     //var result=await jqajax("insPic_newcase","province=1","get","loader");
     // jqajax("insPic_newcase",imageBlob,"POST","loader");
     alert("result--->"+result);
      /////////////// show_res(result);
       alert("ppp");
 });
    alert("test4");

    cropme(uprate,downrate);
    alert("test5");

});
function cropme(uprate,downrate){
  alert(uprate);
  alert(downrate);
 const image = document.getElementById('image');
  cropper = new Cropper(image, {
       dragMode: 'move',
       aspectRatio: uprate/downrate,
       // viewMode: 0,
       // autoCropArea: 1,
       // restore: false,
       // guides: false,
       // center: false,
       // highlight: false,
       // cropBoxMovable: false,
       // cropBoxResizable: false,
       // toggleDragModeOnDblclick: false,
       viewMode: 3,
dragMode: 'move',
autoCropArea: 1,
restore: false,
modal: false,
guides: false,
highlight: false,
cropBoxMovable: false,
cropBoxResizable: false,
toggleDragModeOnDblclick: false,

 });
alert("cropper");
}
