$(document).ready(function() {
  $("#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_setting.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_setting.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

  $('#company_info_form').on('submit', function(e){

        e.preventDefault();

        var company_name     = $("#company_name").val();
        var manger_name_en   = $("#manger_name_en").val();
        var manger_name_ar   = $("#manger_name_ar").val();
        var manger_name_kr   = $("#manger_name_kr").val();
        var company_address  = $("#company_address").val();
        var company_phone    = $("#company_phone").val();
        var company_email    = $("#company_email").val();
        var notify_email     = $("#notify_email").val();

        var Check1 = true;
        var Check2 = true;
        var Check3 = true;
        var Check4 = true;
        var Check5 = true;
        var Check6 = true;

         Check1 = Checks('company_name');
         Check2 = Checks('manger_name_en');
         Check3 = Checks('company_address');
         Check4 = Checks('company_phone');
         Check5 = Checks('company_email');
         Check6 = Checks('notify_email');


 
        if(Check1 && Check2 && Check3 && Check4 && Check5){
              $.ajax({
                    url : 'models/crul_setting.php',
                    type : 'POST',
                    data : {company_name:company_name,
                            manger_name_en:manger_name_en,
                            manger_name_ar:manger_name_ar,
                            manger_name_kr:manger_name_kr,
                            company_address:company_address,
                            company_phone:company_phone,
                            company_email:company_email, 
                            notify_email:notify_email, 
                            type:'update_company_info'},
                    dataType : 'json',
                    success : function(response) {
                          console.log(response['message '])
                          if(response.success == true) {

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

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

                                   }
                                });

                          } else {

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

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

  });


  $('#system_info_form').on('submit', function(e){

        e.preventDefault();

        var time_zone         = $("#time_zone").val();
        var usd_rate          = $("#usd_rate").val();
        

        var Check1 = true;
        var Check2 = true;
        var Check3 = true;
        var Check4 = true;
        var Check5 = true;

         Check1 = Checks('time_zone');
         Check2 = Checks('usd_rate');
         Check3 = Checks('company_address');
         Check4 = Checks('company_phone');
         Check5 = Checks('company_email');


 
        if(Check1 && Check2 && Check3 && Check4 && Check5){
              $.ajax({
                    url : 'models/crul_setting.php',
                    type : 'POST',
                    data : {time_zone:time_zone,usd_rate:usd_rate, type:'update_time_zone'},
                    dataType : 'json',
                    success : function(response) {
                          console.log(response['message '])
                          if(response.success == true) {

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

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

                                   }
                                });

                          } else {

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

              }); // submit ajax
        }else{
              noty({
                                      width: 200,
                                      text: Translation('You didnt 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 <5) {
         $("#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 <5) {
         $("#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 currency_format(cash,cur='$'){
    // console.log(cash);
    if(isNaN(cash) || cash == null || cash==0){
      return "$ "+ 0;
    }else{
      return cur +" "+ cash;
    }

  }

    function Checks(id){
      var name = $("#"+id).val();
      if(name =='') {
            $("#"+id).closest('.form-group').removeClass('has-success');
            $("#"+id).closest('.form-group').addClass('has-error');
            
            noty({
                     width: 200,
                     text: Translation("Please Enter the Required Fields"),
                     type: 'error',
                     dismissQueue: true,
                     timeout: 3000,
                     layout: 'top',
               });
            return false;
         }else{
            $("#"+id).closest('.form-group').removeClass('has-error');
            $("#"+id).closest('.form-group').addClass('has-success');
            return true;
         }

    }

$(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');
    });

});
