<template>
  <b-modal id="add-attachments-modal" :no-close-on-backdrop="true" size="md">
    <template v-slot:modal-header="{ }">
      <b-container>
        <b-row class="d-flex justify-content-between mb-2 mt-1">
          <b-col class="d-flex modal-text-color ml-2" cols="8">
            <div class="d-flex align-self-center justify-content-center rounded-div mr-2 mt-1">
              <font-awesome-icon icon="exclamation-circle" class="align-self-center" />
            </div>
            <div class="pt-1">
              <div class="akkurat-bold max-header-text">Adicionar anexos</div>
            </div>
          </b-col>
          <b-col class="d-flex justify-content-end mr-2 mt-1">
            <span class="close-icon" @click="closeModal">
              <font-awesome-icon icon="times" class="align-self-center" />
            </span>
          </b-col>
        </b-row>
      </b-container>
    </template>
      <b-container>
        <b-row v-for="(item, index) in attachments" v-if="attachments_count < sales_consultant_attachments.length">
          <b-col>
            <b-form-checkbox
              :id="`checkbox-${index}`"
              v-model="selected"
              :name="`checkbox-${index}`"
              :value="item"
            >
            {{ item.description }}
            </b-form-checkbox>
          </b-col>
        </b-row>
        <b-row v-else class="justify-content-center">
         Todos os anexos já foram adicionados.
        </b-row>
      </b-container>

    <template v-slot:modal-footer="{ }">
      <b-container>
        <b-row>
          <b-col cols="12" class="d-flex justify-content-center my-3 px-3">
            <b-button
                class="mr-2 btn shadow-none border-0 btn-default btn-red"
                @click="closeModal"
              >
                Cancelar
            </b-button>
            
            <b-button
              v-if="attachments_count < sales_consultant_attachments.length"
              class="mr-2 btn shadow-none border-0 btn-default"
              @click="done"
            >
              Adicionar
            </b-button>
          </b-col>
        </b-row>
      </b-container>
    </template>
  </b-modal>
</template>

<script>
import SalesConsultantStatusService from "@/services/resources/SalesConsultantStatusService";
import SalesConsultantCrmService from "@/services/resources/SalesConsultantCrmService";
import AttachmentsService from "@/services/resources/AttachmentsService";
import moment from "moment";

const service = SalesConsultantCrmService.build();
const attachmentsService = AttachmentsService.build();

export default {
  name: 'accept-credit-card',
  props:{
    attachments_count:{
      type: Number,
      default: 0
    }
  },
  data: () => ({
    sales_consultant_attachments: [],
    selected: []
  }),
  methods: {
    closeModal() {
      this.attachments = [];
      this.$bvModal.hide('add-attachments-modal');
    },
    load(id){
      attachmentsService
      .search()
      .then(response => {

        this.sales_consultant_attachments = response;

      }).catch(err => {
          console.log(err);
      });
    },
    done(){

      if(this.selected.length > 0){
        this.$emit('added', this.selected);
        this.$bvModal.hide('add-attachments-modal');
      }
    }    
  },
  computed:{
    base(){
      return process.env.VUE_APP_API_HOST;
    },
    attachments(){
        var attachments = [];
        this.sales_consultant_attachments.forEach(element => {
            if(!element.required)
              attachments.push(element);
        });

        return attachments;
    }
  },
  filters:{
    date(date){
      return moment(date, "YYYY-MM-DD HH:mm:ss").format('DD/MM/YYYY HH:mm')
    }
  },
  mounted(){
    this.load();
  }
};
</script>

<style scoped lang="scss">
@import '../../../../../assets/css/main.scss';


.item-attachment{
  height: 150px;
  overflow: hidden;
  cursor: pointer;
}

.modal-text-color {
  color: #3E3E3E;
}

.min-header-text {
  letter-spacing: 0.2px;
  font-size: 9px;
}

.max-header-text {
  letter-spacing: 0.4px;
  font-size: 16px;
}


.close-icon {
  color: #979797;
  font-size: 20px;
}

.btn-default {
  border-radius: 20px;
  width: 100%;
  max-width: 192px;
  height: 35px;
  background: #278937;
}

.btn-red {
  background: #CB2D2D;
}

</style>
