$(document).on('turbolinks:load', function(){
  if ($('body.admins-users-index-page').length > 0) {

    var showSuccessNotify = function() {
      $.notify({
        message: '操作成功'
      },{
        type: 'success'
      });
    }

    // lock user
    $('.users-list-container').on('click', '.lock-action', function(){
      var $lockAction = $(this);
      var $unlockAction = $lockAction.siblings('.unlock-action');

      var userId = $lockAction.data('id');

      $.ajax({
        url: '/admins/users/' + userId + '/lock',
        method: 'POST',
        dataType: 'json',
        success: function() {
          showSuccessNotify();
          $lockAction.hide();
          $unlockAction.show();
        }
      });
    });

    // unlock user
    $('.users-list-container').on('click', '.unlock-action', function(){
      var $unlockAction = $(this);
      var $lockAction = $unlockAction.siblings('.lock-action');

      var userId = $unlockAction.data('id');

      $.ajax({
        url: '/admins/users/' + userId + '/unlock',
        method: 'POST',
        dataType: 'json',
        success: function() {
          showSuccessNotify();
          $lockAction.show();
          $unlockAction.hide();
        }
      });
    });

    // active user
    $('.users-list-container').on('click', '.active-action', function(){
      var $activeAction = $(this);
      var $unlockAction = $activeAction.siblings('.unlock-action');
      var $lockAction = $activeAction.siblings('.lock-action');

      var userId = $activeAction.data('id');

      $.ajax({
        url: '/admins/users/' + userId + '/unlock',
        method: 'POST',
        dataType: 'json',
        success: function() {
          showSuccessNotify();
          $activeAction.hide();
          $lockAction.show();
          $unlockAction.hide();
        }
      });
    });

    // ***************** reward grade modal *****************
    var $rewardGradeModal = $('.admin-users-reward-grade-modal');
    var $form = $rewardGradeModal.find('form.admin-users-reward-grade-form');

    $form.validate({
      errorElement: 'span',
      errorClass: 'danger text-danger',
      rules: {
        grade: {
          required: true,
          digits: true
        },
      }
    });

    // modal ready fire
    $rewardGradeModal.on('show.bs.modal', function (event) {
      var $link = $(event.relatedTarget);

      var userId = $link.data('id');
      $rewardGradeModal.find('.modal-body input[name="user_id"]').val(userId);
    });
    // modal visited fire
    $rewardGradeModal.on('shown.bs.modal', function(){
      $rewardGradeModal.find('.modal-body input[name="grade"]').focus();
    });

    $('.admin-users-reward-grade-modal .submit-btn').on('click', function(){
      $form.find('.error').html('');

      if ($form.valid()) {
        var userId = $form.find('input[name="user_id"]').val();

        $.ajax({
          method: 'POST',
          dataType: 'json',
          url: "/admins/users/" + userId + "/reward_grade",
          data: $form.serialize(),
          success: function(data) {
            showSuccessNotify();
            $('.users-list-container .user-item-' + userId + ' td.grade-content').html(data.grade);
            $rewardGradeModal.modal('hide');
          },
          error: function(res) {
            $rewardGradeModal.find('.error').html(res.responseJSON.message);
          }
        });
      }
    });
  }
});