


    function get_languages(){

      // var lang = $("#language_id").val();


      $.ajax({
                      url : 'models/crul_languages.php',
                      type : 'POST',
                      data : {type:'get'},
                      dataType : 'json',
                      success : function(response) {
                        console.log(response)
                          var attach ='<option value="" selected="selected">'+Translation('اللغة')+'</option>';
                          
                            for(var i=response.length-1 ; i >= 0;i--){
                              
                              attach = attach + '<option value="'+response[i][0]+'">'+response[i][1]+'</option>';
                             

                            }
                            $("#language_id").html(attach);
                            set_Lang(); // this function found in profile.php page
                            // if(lang > 0){
                            //   $("#language_id").val(lang);
                            // }
                            
                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

$(document).ready(function() {
  
  get_languages();

  $("#cover_image").on("change", function() {

        $("#imageUploadForm").submit();
  });

  $("#profile_image").on("change", function() {

        $("#imageUploadFormPic").submit();
  });

});

    $('#imageUploadFormPic').on('submit',(function(e) {
        e.preventDefault();
        var formData = new FormData(this);

        var acc_tok = $("#access_token").val();

        $.ajax({
            url : 'models/crul_profile.php',
            type : 'POST',
            dataType: 'JSON',
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success:function(response){
              if(response.success == true) {

                    noty({
                          width: 200,
                          text: Translation(response.data),
                          type: 'success',
                          dismissQueue: true,
                          timeout: 3000,
                          layout: 'top',
                    });

                    $.ajax({
                       url : 'models/crul_profile.php',
                       data:{type:'load'},
                       success : function(response) {

                       }
                    });
                    location.reload();
              } else {

                 noty({
                       width: 200,
                       text: Translation(response.data),
                       type: 'error',
                       dismissQueue: true,
                       timeout: 3000,
                       layout: 'top',
                 });
              }
            },
            error: function(data){
                console.log("error");
                console.log(data);
            }
        });
    }));


   $('#imageUploadForm').on('submit',(function(e) {
        e.preventDefault();
        var formData = new FormData(this);
        formData.append("type", 'update_profile_cover_image');
        var acc_tok = $("#access_token").val();

        $.ajax({
            url : 'models/crul_profile.php',
            type : 'POST',
            dataType: 'JSON',
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success:function(response){
              if(response.success == true) {

                    noty({
                          width: 200,
                          text: Translation(response.data),
                          type: 'success',
                          dismissQueue: true,
                          timeout: 3000,
                          layout: 'top',
                    });

                    $.ajax({
                       url : 'models/crul_profile.php',
                       data:{type:'load'},
                       success : function(response) {

                       }
                    });
                    location.reload();
              } else {

                 noty({
                       width: 200,
                       text: Translation(response.data),
                       type: 'error',
                       dismissQueue: true,
                       timeout: 3000,
                       layout: 'top',
                 });
              }
            },
            error: function(data){
                console.log("error");
                console.log(data);
            }
        });
    }));





   // Update General Info

$('#profileform').on('submit', function(e){
    
      e.preventDefault();
      
      var name      = $("#username").val();
      var fname     = $("#fname").val();
      var phone     = $("#phone").val();
      var email     = $("#email").val();
      var lang_id   = $("#language_id").val();

      var type    = 'update_profile';
      var acc_tok = $("#access_token").val();

      var DataBody ='';
      var Check1 = true;
      var Check2 = true;
      var Check3 = true;
      var Check4 = true;

      if(name.length >3) {
         DataBody = DataBody + 'username:'+name+',';
         $("#username").closest('.form-group').removeClass('has-error');
         $("#username").closest('.form-group').addClass('has-success');
      }else{
         Check1 = false;
         noty({
                  width: 200,
                  text: Translation("اسم المستخدم يجب ان يكون على الاقل 4 احرف"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
         $("#username").closest('.form-group').removeClass('has-success');
         $("#username").closest('.form-group').addClass('has-error');
      }



      if(fname.length >3) {
         $("#fname").closest('.form-group').removeClass('has-error');
         $("#fname").closest('.form-group').addClass('has-success');
         DataBody = DataBody + 'full_name:'+fname+',';
      }else{
         Check2 = false;
         noty({
                  width: 200,
                  text: Translation("الاسم الكامل يجب ان يكون على الاقل 4 احرف"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
         $("#fname").closest('.form-group').removeClass('has-success');
         $("#fname").closest('.form-group').addClass('has-error');
      }

      if(phone !="") {
         $("#phone").closest('.form-group').removeClass('has-error');
         $("#phone").closest('.form-group').addClass('has-success');
         DataBody = DataBody + 'phone_number:'+phone+',';
      }else{
         Check3 = false;
         noty({
                  width: 200,
                  text: Translation("الرجاء قم بادخال رقم الهاتف"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
         $("#phone").closest('.form-group').removeClass('has-success');
         $("#phone").closest('.form-group').addClass('has-error');
      }

      if(email !="") {
         $("#email").closest('.form-group').removeClass('has-error');
         $("#email").closest('.form-group').addClass('has-success');
         DataBody = DataBody + 'email:'+email+',';
      }else{
         Check4 = false;
         noty({
                  width: 200,
                  text: Translation("الرجاء قم بادخال البريد الالكتروني"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
         $("#email").closest('.form-group').removeClass('has-success');
         $("#email").closest('.form-group').addClass('has-error');
      }

      if(Check1 && Check2 && Check3 && Check4 && DataBody !=""){
            DataBody ="{"+ DataBody.slice(0, -1)+"}";
            console.log(DataBody);
            //'X_CSRF_TOKEN':'xxxxxxxxxxxxxxxxxxxx',

            $.ajax({
                  url : 'models/crul_profile.php',
                  type : 'POST',
                  data : {username:name,full_name:fname,phone_number:phone,email:email,lang_id:lang_id,type:type},
                  dataType : 'json',
                  success : function(response) {
                        // console.log(response['message '])
                        if(response.success == true) {

                              var msgg = response.data;

                              $.ajax({
                                 url : 'models/crul_profile.php',
                                 data:{type:'load'},
                                 success : function(response) {
                                  noty({
                                    width: 200,
                                    text: Translation(msgg),
                                    type: 'success',
                                    dismissQueue: true,
                                    timeout: 2000,
                                    layout: 'top',
                                    callback: {
                                              onShow: function() {},
                                              afterShow: function() {},
                                              onClose: function() {location.reload();},
                                              afterClose: function() {},
                                              onCloseClick: function() {},
                                            },
                                  });
                                 },
                                 complete: function(da) {
                                  // location.reload();
                                }
                              });
                              
                        } else {

                           noty({
                                 width: 200,
                                 text: Translation(response.data),
                                 type: 'error',
                                 dismissQueue: true,
                                 timeout: 3000,
                                 layout: 'top',
                           });
                        }
                  },
                  complete: function(da) {
                    //console.log(da)
                        //   me.data('requestRunning', false);
                  }

            }); // submit ajax
      }else{
            noty({
                                    width: 200,
                                    text: Translation("You didn't change any things or you entered fewer characters"),
                                    type: 'error',
                                    dismissQueue: true,
                                    timeout: 3000,
                                    layout: 'top',
                              });
      }

});



   // Update Password

$('#profileformpass').on('submit', function(e){
      e.preventDefault();

      var pass    = $("#password").val();
      var repass   = $("#repassword").val();
      //var uid     = $("#userid").val();
      var acc_tok = $("#access_token").val();
      var Check1 = true;
      var Check2 = true;
      var Check3 = true;

      if(repass != pass) {
         $("#password").closest('.form-group').removeClass('has-success');
         $("#repassword").closest('.form-group').removeClass('has-success');
         $("#password").closest('.form-group').addClass('has-error');
         $("#repassword").closest('.form-group').addClass('has-error');
         Check3 = false;
         noty({
                  width: 200,
                  text: Translation("Passwords Should Matches"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
      }else{
         $("#password").closest('.form-group').removeClass('has-error');
         $("#repassword").closest('.form-group').removeClass('has-error');
         $("#password").closest('.form-group').addClass('has-success');
         $("#repassword").closest('.form-group').addClass('has-success');
      }

      if(pass.length <6) {
         $("#password").closest('.form-group').removeClass('has-success');
         $("#password").closest('.form-group').addClass('has-error');
         Check1 = false;
         noty({
                  width: 200,
                  text: Translation("Password Should be at least 6 characters"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
      }else{
         $("#password").closest('.form-group').removeClass('has-error');
         $("#password").closest('.form-group').addClass('has-success');
      }



      if(repass.length <6) {
         $("#repassword").closest('.form-group').removeClass('has-success');
         $("#repassword").closest('.form-group').addClass('has-error');
         Check2 = false;
         noty({
                  width: 200,
                  text: Translation("Password Should be at least 6 characters"),
                  type: 'error',
                  dismissQueue: true,
                  timeout: 3000,
                  layout: 'top',
            });
      }else{
         $("#repassword").closest('.form-group').removeClass('has-error');
         $("#repassword").closest('.form-group').addClass('has-success');
      }





      if(Check1 && Check2 && Check3 ){

            //'X_CSRF_TOKEN':'xxxxxxxxxxxxxxxxxxxx',

            $.ajax({
                  url : 'models/crul_profile.php',
                  type : 'POST',
                  data : {password:pass,repassword:repass,type:'update_password'},
                  dataType : 'json',
                  success : function(response) {
                        console.log(response)
                        if(response.success == true) {

                              noty({
                                    width: 200,
                                    text: Translation(response.data),
                                    type: 'success',
                                    dismissQueue: true,
                                    timeout: 3000,
                                    layout: 'top',
                              });

                        } else {

                           noty({
                                 width: 200,
                                 text: Translation(response.data),
                                 type: 'error',
                                 dismissQueue: true,
                                 timeout: 3000,
                                 layout: 'top',
                           });
                        }
                  },
                  complete: function() {
                          // me.data('requestRunning', false);
                  }

            }); // submit ajax
      }else{
            noty({
                                    width: 200,
                                    text: Translation('You entered wrong password! or passwords does not Match'),
                                    type: 'error',
                                    dismissQueue: true,
                                    timeout: 3000,
                                    layout: 'top',
                              });
      }

});


$(function() {


    // Form components
    // ------------------------------

    // Select2 selects
    $('.select').select2({
        minimumResultsForSearch: Infinity
    });


    // Styled file input
    $(".file-styled").uniform({
        fileButtonClass: 'action btn bg-warning'
    });


    // Styled checkboxes, radios
    $(".styled").uniform({
        radioClass: 'choice'
    });


    // Render in hidden elements
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        $('.schedule').fullCalendar('render');
    });

});
