<template lang="pug">
  medical-form-wrapper(
    title="Контакты"
    :is-check-change="isCheckChange" 
    :is-loading-data="isLoadingData" 
    :is-edit="isEdit"
    :save="saveChange" 
    :cancel="cancelEdit" 
    :open-edit="openEdit"
    )
    .form-wrap.gap-24.w-full
      .flex.flex-col.gap-4(v-for="(data, key) in contacts")
        .font-semibold.text-sm.whitespace-nowrap {{configData[key].title}}
        .flex.w-full.justify-between.items-center.gap-4(v-for="(value, index) in data")
          .label-field.font-sm.text-sm.whitespace-nowrap {{`${configData[key].fieldName} ${index+1} :`}}
          .input-wrapper.items-center.flex.h-10.gap-10px
            .flex(v-if="key === 'networks'")
              .flex.rounded-full.cursor-pointer.items-center.gap-10px.py-1.pl-1.pr-2(
                v-if="value.kind" 
                @click="() => copyLinkNetwork(value.username)"
                style="background-color: var(--bg-light-grey)"
              )
                q-icon(:name="mapNetworks[value.kind].icon" style="color: var(--btn-blue-color)")
                span.text-sm.font-medium {{ mapNetworks[value.kind].title || value.username }}
              base-input.w-full( 
                v-else
                :disabled="!isEdit" 
                v-model="value.username"
                :mask="configData[key].inputMask"
                @keyup.enter="(e) => defineNetworkByValue(e, index)",
                @update:model-value="checkChangeInput"
                type="text"  
                :standout="!isEdit" 
                :outlined="isEdit" 
                text-color="black"
                )
            base-input.w-full( 
              v-else
              :disabled="!isEdit" 
              v-model="value.username"
              :mask="configData[key].inputMask"
              @update:model-value="checkChangeInput"
              type="text"  
              :standout="!isEdit" 
              :outlined="isEdit" 
              text-color="black"
              )
            .delete-contact.icon-cancel.text-xxs.cursor-pointer(
              v-if="isEdit" 
              @click="() => deleteContact(key, index)"
              )
        .w-full(v-if="isEdit")
          .add-contact.w-fit.flex.gap-10px.items-center.cursor-pointer( 
            @click="() => addNewContact(key)"
            )
            .icon-plus
            span.font-medium.text-base Добавить
</template>

<script>
import MedicalFormWrapper from "@/pages/newMedicalCard/components/MedicalFormWrapper.vue";
import BaseInput from "@/components/base/BaseInput.vue";
import { mapNetworks } from "@/pages/newMedicalCard/utils/medicalConfig";
import { checkChangeData } from "@/shared/utils/changesObjects";
import { contactsDataForm } from "@/pages/newMedicalCard/utils/medicalConfig";
import { mapState, mapGetters } from "vuex";

export default {
  name: "ContactsForm",
  components: { MedicalFormWrapper, BaseInput },
  data() {
    return {
      isEdit: false,
      isLoadingData: false,
      isCheckChange: false,
      isChangeNetwork: false,
      configData: contactsDataForm,
      mapNetworks: mapNetworks,
    };
  },
  computed: {
    ...mapGetters({
      initContactsData: "getContactsData",
    }),
    ...mapState({
      contacts: (state) => state.medical.contactsData,
    }),
  },
  methods: {
    openEdit() {
      this.isEdit = true;
    },
    cancelEdit() {
      this.$store.dispatch("returnInitData");
      this.isEdit = false;
      this.isCheckChange = false;
    },
    saveChange() {
      this.isLoadingData = true;
      this.$store.dispatch("updateContactsData").then(() => {
        this.isEdit = false;
        this.isLoadingData = false;
      });
    },
    checkChangeInput() {
      this.isCheckChange = checkChangeData(
        this.initContactsData,
        this.contacts
      );
    },
    defineNetworkByValue(e, index) {
      const arrNetworkMap = Object.values(this.mapNetworks);
      const value = e.target.value;
      let newKind = arrNetworkMap.find((el) =>
        value.includes(el.markerLink)
      )?.kind;
      this.contacts.networks[index].kind = newKind;
    },
    addNewContact(key) {
      this.contacts[key].push({
        kind: this.configData[key].kind,
        username: null,
      });
      this.checkChangeInput();
    },
    deleteContact(key, index) {
      this.contacts[key].splice(index, 1);
      this.checkChangeInput();
    },
    copyLinkNetwork(value) {
      navigator.clipboard.writeText(value);
    },
  },
};
</script>

<style lang="sass" scoped>
.input-wrapper
  width: 324px
.form-wrap
  display: grid
  grid-template-columns: repeat(3, 1fr)
  grid-template-rows: repeat(1, 1fr)
  @media(max-width: 1900px)
    grid-template-columns: repeat(2, 1fr)
    grid-template-rows: repeat(2, 1fr)
  @media(max-width: 1320px)
    grid-template-columns: repeat(1, 1fr)
    grid-template-rows: repeat(3, 1fr)
.network-field
  background-color: var(--bg-light-grey)
.label-field
  color: var(--font-grey-color)
.delete-contact
  color: var(--font-grey-color)
.add-contact
  color: var(--btn-blue-color)
</style>
