<template>
  <modal name="edit-contact-modal" width="100%" :clickToClose="false">
    <div class="tw-p-[30px] md:tw-p-0 tw-h-full tw-overflow-auto">
      <div
        class="modal-header tw-flex tw-justify-start tw-items-center tw-mb-[20px] md:tw-mb-[50px] xl:tw-justify-between">
        <div class="tw-text-[18px] tw-font-bold tw-leading-[26px] md:tw-text-[20px]">
          {{ $t("userProfile.editContactInfo") }}
        </div>
        <button class="close tw-transition tw-btn-md" @click="$modal.hide('edit-contact-modal')"></button>
      </div>

      <div
        class="modal-content tw-grid tw-grid-cols-1 tw-gap-y-[20px] tw-mb-[30px] md:tw-grid-cols-2 md:tw-gap-x-[64px] md:tw-mb-[60px]">
        <div class="element">
          <elementInput :input="{
            id: 'FirstName',
            label: 'userProfile.firstName',
            required: true,
            type: 'text',
          }" :default="formData.first_name" :validation="validation.first_name" @change="formData.first_name = $event">
          </elementInput>
        </div>
        <div class="element">
          <elementInput :input="{
            id: 'LastName',
            label: 'userProfile.lastName',
            required: true,
            type: 'text',
          }" :default="formData.last_name" :validation="validation.last_name" @change="formData.last_name = $event">
          </elementInput>
        </div>
        <div class="element">
          <elementInput :input="{
            id: 'Email',
            label: 'Email',
            required: true,
            type: 'email',
          }" :default="formData.email" :validation="validation.email" @change="formData.email = $event">
          </elementInput>
        </div>
        <div class="element">
          <label class="tw-block tw-mb-[10px]"><span>{{ $t("Phone") }}<span class="required">*</span></span></label>
          <vue-country-code class="d-none" enabledCountryCode :enabledFlags="false" v-model="countryCode"
            @onSelect="showCode"></vue-country-code>
          <vue-phone-number-input color="#EF5A5A" valid-color="#EE9546" :border-radius="5" no-flags no-example
            v-model="formData.phone_number" @update="getPhoneData"></vue-phone-number-input>
        </div>
      </div>
      <div class="modal-footer md:tw-flex md:tw-justify-start md:tw-items-center md:tw-flex-row-reverse">
        <button
          class="tw-transition tw-text-[18px] tw-btn-md tw-bg-primary-1 tw-w-full tw-mb-[10px] md:tw-mb-0 md:tw-w-fit md:tw-px-[30px] tw-py-[9.5px] tw-rounded-2xl hover:tw-bg-primary-2"
          @click="save">
          {{ $t("userProfile.save") }}
        </button>
        <button
          class="tw-transition tw-text-[18px] tw-btn-md tw-text-primary-1 tw-w-full md:tw-w-fit md:tw-px-[30px] tw-py-[9.5px] tw-mr-[10px] tw-rounded-2xl"
          @click="reset">
          {{ $t("Clear") }}
        </button>
      </div>
    </div>
  </modal>
</template>
<script>
import elementInput from "@/components/newComponent/form/ElementInput";
import Swal from "sweetalert2";
import is from "is_js";
export default {
  name: "EditContactModal",
  components: {
    elementInput,
  },
  props: {
    contact: {
      type: Object,
    },
  },
  data() {
    return {
      formData: {},
      isValid: false,
      countryCode: "",
      phoneValid: false,
      validation: {
        first_name: true,
        last_name: true,
        email: true,
        phone_number: true,
      },
      errors: null,
    };
  },
  mounted() { },
  watch: {
    contact: {
      handler: function () {
        this.formData = JSON.parse(JSON.stringify(this.contact));
      },
    },
  },
  methods: {
    reset() {
      this.formData = {
        first_name: "",
        last_name: "",
        email: "",
        country_code: "",
        phone_number: "",
      };
      (this.isValid = false),
        (this.countryCode = ""),
        (this.phoneValid = false);
    },
    getPhoneData(phoneData) {
      this.country_code = phoneData.countryCode;
      this.phoneValid = phoneData.isValid;
    },
    showCode(object) {
      this.formData.country_code = object.dialCode;
    },
    validators() {
      if (is.empty(this.formData.first_name)) {
        this.validation.first_name = false;
      } else {
        this.validation.first_name = true;
      }
      if (is.empty(this.formData.last_name)) {
        this.validation.last_name = false;
      } else {
        this.validation.last_name = true;
      }
      if (is.empty(this.formData.email) || is.not.email(this.formData.email)) {
        this.validation.email = false;
      } else {
        this.validation.email = true;
      }
      if (
        is.empty(this.formData.phone_number) &&
        is.number(this.formData.phone_number)
      ) {
        this.validation.phone_number = false;
      } else {
        this.validation.phone_number = true;
      }
      this.errors = Object.entries(this.validation).filter(
        (e) => e[1] == false
      );
      if (this.errors.length > 0) {
        return false;
      } else {
        return true;
      }
    },
    save() {
      this.validators();
      if (this.validators()) {
        const patchData = JSON.parse(JSON.stringify(this.formData));
        this.$axios
          .patch(
            `/member/contacts/${this.formData.contact_uuid}?jwt=${this.$auth.$storage.getUniversal("jwt").token
            }`,
            patchData
          )
          .then((result) => {
            if ((result.status = 200)) {
              this.$emit("update", true);
              this.$modal.hide("edit-contact-modal");
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.close {
  position: absolute;
  right: 30px;
  top: 32px;
  background-image: url("~/assets/svg/close.svg");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  width: 14px;
  height: 14px;

  @media screen and (min-width: 768px) {
    right: 40px;
    top: 40px;
  }
}

.v--modal-overlay::v-deep {
  .v--modal {
    width: 100% !important;
    height: 100vh !important;

    @media screen and (min-width: 768px) {
      padding: 40px;
      width: max-content;
      height: max-content !important;
      border-radius: 30px;
    }
  }

  .v--modal-box {
    height: 100vh !important;
    overflow: auto;

    @media screen and (min-width: 768px) {
      padding: 40px;
      width: max-content;
      height: max-content !important;
    }
  }
}
</style>
