<template>
  <div class="overlay">
    <div class="row h-100 d-flex justify-content-center align-items-center">
      <div class="col-12 col-md-6 col-lg-4 p-card">
        <div class="card p-card">
          <div class="container">
            <div class="row mb-4">
              <div class="col-12">
                <h3 class="card-custom-title">
                  {{ $t(i18nConst.lotteryPopupTicketsTitle) }}
                </h3>
              </div>
            </div>
            <div class="row mb-5">
              <div v-for="(t, index) in ticketsNumbers" :key="index" class="col-md-12 d-flex justify-content-around">
                <input type="text" class="form-control form-rounded one-char" @keypress="checkChar($event)" maxlength="1"
                  v-model="t.num2" />
                <input type="text" class="form-control form-rounded one-char" @keypress="checkChar($event)" maxlength="1"
                  v-model="t.num3" />
                <input type="text" class="form-control form-rounded one-char" @keypress="checkChar($event)" maxlength="1"
                  v-model="t.num4" />
                <input type="text" class="form-control form-rounded one-char" @keypress="checkChar($event)" maxlength="1"
                  v-model="t.num5" />
                <input type="text" class="form-control form-rounded one-char" @keypress="checkChar($event)" maxlength="1"
                  v-model="t.num6" />
                <input type="text" class="form-control form-rounded one-char" @keypress="checkChar($event)" maxlength="1"
                  v-model="t.num7" />
              </div>
            </div>
            <div class="row mb-3">
              <div class="col-12 text-center">
                <div class="row">
                  <button class="btn btn-meta-1 mb-2" @click="confirm">
                    {{ $t(i18nConst.lotteryPopupTicketsButtonOk) }}
                  </button>
                </div>
                <div class="row">
                  <button class="btn btn-meta-2" @click="cancel">
                    {{ $t(i18nConst.lotteryPopupTicketsButtonCancel) }}
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import i18nConstantes from "@/constantes/i18n.constantes.js";
import typeActions from "@/store/typeActions.js";

export default {
  data() {
    return {
      i18nConst: i18nConstantes,
    };
  },
  props: {
    ticketsNumbers: { default: {} },
  },
  methods: {
    checkChar(evt) {
      evt = evt ? evt : window.event;
      var charCode = evt.which ? evt.which : evt.keyCode;
      if (charCode < 48 || charCode > 57) {
        evt.preventDefault();
      } else {
        return true;
      }
    },
    cancel() {
      this.$store.commit("lottery/setTicketsNumbers", null);
    },
    confirm() {
      //console.log("confirm - ", this.ticketsNumbers);
      this.$store.dispatch(typeActions.buyTickets, {
        nbTickets: this.ticketsNumbers.length,
        instantly: false,
        tickets: this.ticketsNumbers
      });
    }
  },
};
</script>

<style scoped>
.overlay {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.8);
  z-index: 1031;
  cursor: pointer;
}

.popup {
  margin: 0;
  position: fixed;
  top: 20vh;
  left: 30%;
  width: 40%;
  background-color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
  padding: 1rem;
}

.one-char {
  width: 35px;
  text-align: center;
  border-radius: 1rem;
}
</style>
