import promotionProgressiveDiscountDetail from '@/components/modals/promotion-progressive-discount/PromotionProgressiveDiscountDetail.vue';
import PromotionProgressiveDiscountService from '@/services/resources/PromotionProgressiveDiscountService';


const service = PromotionProgressiveDiscountService.build();


export default {
  components: {
    promotionProgressiveDiscountDetail
  },
  data() {
    return {
      rows: [],
      loading: false
    };
  },
  methods: {
    search() {
      this.loading = true;

      service
        .search({with: "products_per_campaigns.product,products_per_campaigns.campaign,range"})
        .then((resp) => {
          this.rows = resp;
this.loading = false
        })
        .catch((err) => {
          const resp = JSON.parse(err.response.data);
          this.$fire({
            title: 'Falha na solicitação',
            text: resp.msg,
            type: 'error'
          });
          this.loading = false;
        });
    },
    showModal() {
      this.$modal.show('promotion-progressive-discount-detail');
    },
    edit(id) {
      this.$modal.show('promotion-progressive-discount-detail', { id });
    },
    remove(row) {
      this
        .$fire({
          title: 'Remover oferta',
          html: `Deseja realmente remover o oferta <strong>${row.name}</strong>?`,
          type: 'question',
          showCancelButton: true,
          confirmButtonText: 'Deletar',
          cancelButtonText: 'Cancelar'
        })
        .then(async ({ value }) => {
          if (value) {
            let text = 'Oferta removido com sucesso.';
            let type = 'success';
            try {
              await service.destroy(row.id);
              this.search();
            } catch (error) {
              console.error(error);
              text = 'Ocorreu um erro ao tentar remover o oferta';
              type = 'danger';
            } finally {
              this.$fire({
                title: 'Remoção de oferta',
                type,
                text
              });
            }
          }
        });
    }
  },
  mounted() {
    this.search();
  }
};
