"use strict";

(function ($) {
  $(document).ready(function () {
    new Vue({
      el: '#stm-lms-reviews',
      data: function data() {
        return {
          loading: true,
          reviews: [],
          offset: 0,
          total: false
        };
      },
      created: function created() {
        this.getReviews();
      },
      methods: {
        getReviews: function getReviews() {
          var vm = this;
          var url = stm_lms_ajaxurl + '?action=stm_lms_get_reviews&nonce=' + stm_lms_nonces['stm_lms_get_reviews'] + '&offset=' + vm.offset + '&post_id=' + stm_lms_post_id;
          vm.loading = true;
          this.$http.get(url).then(function (response) {
            response.body['posts'].forEach(function (review) {
              vm.reviews.push(review);
            });
            vm.total = response.body['total'];
            vm.loading = false;
            vm.offset++;
          });
        }
      }
    });
    new Vue({
      el: '#stm_lms_add_review',
      data: function data() {
        return {
          is_rtl: false,
          loading: false,
          review_text: '',
          openReview: false,
          rating: 0,
          ratingWidth: 75,
          singleRating: 75 / 5,
          message: '',
          status: ''
        };
      },
      mounted: function mounted() {
        var vm = this;
        Vue.nextTick(function () {
          if ($('body').hasClass('rtl-demo')) {
            vm.is_rtl = true;
          }
        });
      },
      methods: {
        ratingW: function ratingW(rating) {
          var rWidth = rating * 20; //if(this.is_rtl) rWidth = 100 - rWidth;

          return rWidth + '%';
        },
        addReview: function addReview() {
          var vm = this;

          if (this.openReview) {
            vm.message = '';
            vm.loading = true;
            var url = stm_lms_ajaxurl + '?action=stm_lms_add_review&nonce=' + stm_lms_nonces['stm_lms_add_review'] + '&post_id=' + stm_lms_post_id + '&mark=' + this.rating + '&review=' + this.review_text;
            vm.loading = true;
            vm.$http.get(url).then(function (response) {
              vm.message = response.body['message'];
              vm.status = response.body['status'];
              vm.loading = false;
              if (response.body['status'] === 'success') vm.openReview = false;
            });
          }

          this.openReview = true;
        },
        ratingHover: function ratingHover($event) {
          // if(this.is_rtl) {
          //     this.rating = parseInt((this.ratingWidth - $event.offsetX) / this.singleRating) + 1;
          //     console.log(this.rating, 'rating', $event.offsetX);
          // } else {
          this.rating = parseInt($event.offsetX / this.singleRating) + 1;
          console.log(this.rating, 'rating'); // }
        }
      }
    });
  });
})(jQuery);