<template>
  <div>
    <app-Not-Found v-if="isNotFound" />
    <div v-if="!isNotFound">
      <app-full-loader v-if="fullLoader" />
      <div v-if="!fullLoader">
        <app-Login-Template v-if="!isLogged" />
        <app-Change-Password-Template v-if="isLogged && isFirstLogin" />
        <app-template v-if="isLogged && !isFirstLogin" />
      </div>
    </div>
  </div>
</template>


<style>


  .text-bold{
    font-weight: 600;
  }

  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.5s;
  }
  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }

  .fadeInOnly-enter-active {
    transition: opacity 0.5s;
  }
  .fadeInOnly-enter, .fadeInOnly-leave-to /* .fadeInOnly-leave-active below version 2.1.8 */ {
    opacity: 0;
  }

  .fadeOutOnly-enter-active {
    transition: opacity 0.5s;
  }
  .fadeOutOnly-enter, .fadeOutOnly-leave-to /* .fadeOutOnly-leave-active below version 2.1.8 */ {
    opacity: 0;
  }

  .margin-side40 {
    margin-left: 40px;
    margin-right: 40px;
  }

  .margin-topbottom20 {
    margin-top: 20px;
    margin-bottom: 20px;
  }

  .uniq-card .form-horizontal .label-on-left {
    padding: 28px 5px 0 0;
    text-align: right;
  }

  .modal-80 {
    width: 80%;
  }

  .modal-70 {
    width: 70%;
  }

  .modal-60 {
    width: 60%;
  }

  .modal-50 {
    width: 50%;
  }

  .modal-90 {
    width: 90%;
  }

  .modal-120 {
    width: 120%;
  }

  .jv-error {
    color: red !important;
  }

  .file-error {
    color: red !important;
  }

  .form-control-static {
    padding-top: 19px !important;
  }

  .form-group-sm .form-control {
    font-size: 14px !important;
  }

  .align-right {
    text-align: right;
  }

  .font18 {
    font-size: 16px;
  }

  .font16px {
    font-size: 16px;
  }

  .font14 {
    font-size: 13px;
  }

  .font18px {
    font-size: 18px;
  }

  .font20pt {
    font-size: 20pt;
  }

  .margintop15 {
    margin-top: 15px;
  }

  .margintop28 {
    margin-top: 28px;
  }
  .cell-padding{
    width:100%;
  }
  .colorgray {
    color: #716767 !important;
  }

  .margintop10 {
    margin-top: 10px !important;
  }

  .paddingbottom20 {
    padding-bottom: 20px !important;
  }

  .text-uppercase {
    text-transform: uppercase;
  }

  .table, tr, .text-center {
    text-align:center;
    font-size:9pt;
  }

  .text-center{
    text-align:center;
  }

  .scrollTable {
    display: block;
    overflow-y: auto;
    white-space: nowrap;
  }

  .tableScrollable {
    display: block;
    overflow-y: auto;
    white-space: nowrap;
  }

  .tableCellWidth {
    width: 100% !important;
    border-spacing: 0px;
    border-collapse: separate;
  }

  .width-5 {
    width: 5%;
  }

  .width-10 {
    width: 10% !important;
  }

  .width-40 {
    width: 40% !important;
  }

  p{
    font-size: 16px;
  }

  h1{
    font-size: 4.3em;
  }

  h2{
    font-size: 3.1em;
  }

  h3{
    font-size: 2.325em;
  }

  h4{
    font-size: 1.8em;
  }

  h5{
    font-size: 1.75em;
  }

  h6{
    font-size: 1.5em;
  }

  .adjusth3{
    font-size: 1.825em;
  }

  .adjusth6{
    font-size: 1em;
  }
  .btn-modified{
    margin: 0px;
    padding: 6px 12px;
  }






  @keyframes placeHolderShimmer{
    0%{
        background-position: -468px 0
    }
    100%{
        background-position: 468px 0
    }
}
.linear-background {
    animation-duration: 1s;
    animation-fill-mode: forwards;
    animation-iteration-count: infinite;
    animation-name: placeHolderShimmer;
    animation-timing-function: linear;
    background: #f6f7f8;
    background: linear-gradient(to right, #eeeeee 8%, #dddddd 18%, #eeeeee 33%);
    background-size: 1000px 104px;
    position: relative;
    overflow: hidden;
}

















</style>


<script>
import Template from "./master/Template";
import LoginTemplate from "./master/LoginTemplate.vue";
import ChangePasswordTemplate from "./master/ChangePasswordTemplate.vue";
import NotFound from "./master/NotFound.vue";
import FullLoader from "./master/FullLoader";
// import Login from "./master/Login.vue";

import { mapGetters } from "vuex";

export default {
  components: {
    appFullLoader: FullLoader,
    appTemplate: Template,
    appNotFound: NotFound,
    appLoginTemplate: LoginTemplate,
    appChangePasswordTemplate: ChangePasswordTemplate
  },

  computed: {
    ...mapGetters({
      isNotFound: "getNotFound",
      fullLoader: "getFullLoader",
      allowedModule: "getAllowedModule",
      isLogged: "getIsLogged",
      isFirstLogin: "getIsFirstLogin",
      
    })
  },

  mounted() {
    function isNumeric(obj){
      var realStringObj = obj && obj.toString();
        return !jQuery.isArray( obj ) && ( realStringObj - parseFloat( realStringObj ) + 1 ) >= 0;
    }

    $(document).on("keypress", ".input-money", function(e) {
      let selectedval = this.value;

      let lastcharacter = selectedval.slice(-1);
      let isAllowDecimal = 1;
      let inputValue = event.which;

      if(lastcharacter == 0 && selectedval.length == 1){
        if(inputValue == 46){
          if (inputValue == 46) {
            if (isNumeric(lastcharacter) && !(selectedval.indexOf(".") > -1)) {
              isAllowDecimal = 1;
            } else {
              isAllowDecimal = 0;
            }
          } else {
            isAllowDecimal = 0;
          }

          if (isAllowDecimal == 1) {
            if (!(inputValue >= 48 && inputValue <= 57) && !(inputValue == 46)) {
              event.preventDefault();
            }
            isAllowDecimal = 0;
          } else {
            if (!(inputValue >= 48 && inputValue <= 57)) {
              event.preventDefault();
            }
          }
        } else if (!(inputValue >= 48 && inputValue <= 57)) {
          event.preventDefault();
        } else {
          this.value = "";
        }
      } else {
        if (event.which === 32 && !this.value.length) {
          event.preventDefault();
        }

        if (inputValue == 46) {
          if (isNumeric(lastcharacter) && !(selectedval.indexOf(".") > -1)) {
            isAllowDecimal = 1;
          } else {
            isAllowDecimal = 0;
          }
        } else {
          isAllowDecimal = 0;
        }

        if (isAllowDecimal == 1) {
          if (!(inputValue >= 48 && inputValue <= 57) && !(inputValue == 46)) {
            event.preventDefault();
          }
          isAllowDecimal = 0;
        } else {
          if (!(inputValue >= 48 && inputValue <= 57)) {
            event.preventDefault();
          }
        }
      }


      
    });

    $(document).on("keypress", ".input-mobile", function(e) {
      let inputValue = event.which;
      if (event.which === 32 && !this.value.length) {
        event.preventDefault();
      }
      if (!(inputValue >= 48 && inputValue <= 57)) {
        event.preventDefault();
      }
    });

    $(document).on("keypress", ".block-specialChar", function(e) {

      var regex = new RegExp("^[a-zA-Z0-9 `-]+$");
      var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
      if (!regex.test(key)) {
        event.preventDefault();
      }
    });

    $(document).on("keypress", ".notyping", function(e) {
      if (e.which === 8) {
        return false;
      }
      event.preventDefault();
    });

    $(document).on("keypress", ".input-integer-can-start-zero", function(e) {
      let selectedval = this.value;
      let lastcharacter = selectedval.slice(-1);
      let inputValue = event.which;
      
      if(lastcharacter == 0 && selectedval.length == 1){
        if (!(inputValue >= 48 && inputValue <= 57)) {
          event.preventDefault();
        } else {
          this.value = "";
        }
      } else {
        if (!(inputValue >= 48 && inputValue <= 57)) {
          event.preventDefault();
        }
      }
    });
  },

  async created() {
    this.$store.commit("setFullLoader", true);
    // if (localStorage.getItem("token") != null) {
    //   let res = await this.$store.dispatch("auth/reconnect");
    //   if (res.status >= 200 && res.status <= 299) {
    //     if (this.$route.name == "Login"){
    //       this.$router.push({ name: "Dashboard" }).catch(err => {});
    //     }
    //   }

     if (localStorage.getItem("token") != null) {
        
        let sessionCred = JSON.parse(localStorage.getItem("session"))
        let roleModules = []
        sessionCred.roles.roleModules.map((data, i) => {
             roleModules.push(data.module)
        })

        this.$store.commit("setIsLogged", true);
        this.$store.commit("setSession", sessionCred);
        this.$store.commit("setAllowedModule", roleModules);
 }
 
    //   if (typeof this.$route.meta.moduleId != "undefined") {
    //     if (!this.allowedModule.includes(this.$route.meta.moduleId)){
    //       this.$router.push({ name: "Page Not Found" }).catch(err => {});
    //     }   
    //   }
    // } else if (this.$route.name != "Page Not Found") {
    //   this.$router.push({ name: "Login" }).catch(err => {});
    // }

    

    this.$store.commit("setFullLoader", false);
  }
};
</script>