<template>
  <b-modal id="general-pending" :no-close-on-backdrop="true" size="lg">
    <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">Pendência</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>
          <b-col>
            <b-table hover :items="pendingsComputed" :fields="fields" :tbody-tr-class="rowClass" head-variant="dark" bordered="bordered" striped="striped" small="small">
              <template v-slot:cell(created_at)="data">
                {{ data.value | date }}
              </template>
              <template v-slot:cell(approved)="data">
                {{ data.value ? 'Sim' : 'Não' }}
              </template>
              <template v-slot:cell(updated_at)="data">
                {{ data.value | date }} 
              </template>
            </b-table>
          </b-col>
        </b-row>
        <b-row v-if="!isDisable">
          <b-col cols="12">
            <label>Pendência:</label>
            <b-form-textarea no-resize v-model="description">
            </b-form-textarea>
          </b-col>
          <b-col cols="12" class="d-flex justify-content-end">
            <b-button
              class="mt-2 btn shadow-none border-0 btn-default"
              @click="add"
            >
              Adicionar
            </b-button>
          </b-col>
        </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="!isDisable"
              class="mr-2 btn shadow-none border-0 btn-default"
              @click="done"
            >
              Salvar
            </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 moment from "moment";

const service = SalesConsultantCrmService.build();

export default {
  name: 'ageneral-pending-modal',
  props:{
    pendings:{
      type: Array
    },
    isDisable:{
      type: Boolean
    }
  },
  data: () => ({
    description: '',
    newPendings: [],
    pendingsComponent: [],
    fields:[
      {key: 'description', label: 'Pendência'},
      {key: 'created_at', label: 'Criada'},
      {key: 'updated_at', label: 'Última alteração'},
      {key: 'approved', label: 'Finalizada'}
    ]
  }),
  methods: {
    rowClass(item, type) {
      if (!item.approved) return 'table-danger'
    },
    closeModal() {
      this.newPendings = [];
      this.pendingsComponent = [];
      this.$bvModal.hide('general-pending');
    },
    add(){
      this.newPendings.push(this.description);
      var data = {
        description: this.description,
        approved: false,
        created_at: moment(),
        updated_at: moment()
      };

      this.pendingsComputed.push(data);
      this.description = '';

    },
    done(){

      if(this.newPendings.length > 0){
        this.$emit('generalPending', this.newPendings);
        this.$bvModal.hide('general-pending');
      }
    }    
  },
  computed:{
    pendingsComputed:{
      get(){
        if(this.pendingsComponent.length == 0)
          this.pendingsComponent = this.pendings;

        return this.pendingsComponent;
      },
      set(newValue){
        this.pendingsComponent.push(newValue);
      }
    }
  },
  filters:{
    date(date){
      return moment(date, "YYYY-MM-DD HH:mm:ss").format('DD/MM/YYYY HH:mm')
    }
  }
};
</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>
