import bannerDetail from '@/components/modals/banner/BannerDetail.vue';
import BannerService from '@/services/resources/BannerService';
import {keyRoleToLabel} from "@/utils/convert"
const service = BannerService.build();
export default {
  components: {
    bannerDetail
  },
  data() {
    return {
      rows: [],
      loading: false
    };
  },
  methods: {
    keyRoleToLabel,
    search() {
      this.loading = true;

      service
        .search({with: 'campaign'})
        .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('banner-detail');
    },
    edit(id) {
      this.$modal.show('banner-detail', { id });
    },
    remove(row) {
      this
        .$fire({
          title: 'Remover banner',
          html: `Deseja realmente remover o banner <strong>${row.title}</strong>?`,
          type: 'question',
          showCancelButton: true,
          confirmButtonText: 'Deletar',
          cancelButtonText: 'Cancelar'
        })
        .then(async ({ value }) => {
          if (value) {
            let text = 'Banner 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 banner';
              type = 'danger';
            } finally {
              this.$fire({
                title: 'Remoção de banner',
                type,
                text
              });
            }
          }
        });
    },
    toStringType(typeBanner){
      if(typeBanner == 'SMALL'){
        return 'Interno';
      }else if(typeBanner == 'LARGER'){
        return "Home"
      }else{
        return "Celular"
      }
    }
  },
  mounted() {
    this.search();
  }
};
