<template>
  <modal name="edit-contact-modal" width="100%" :clickToClose="false">
    <div class="tw-p-[40px]">
      <div
        class="modal-header tw-flex tw-justify-center tw-items-center tw-mb-[40px] md:tw-mb-[40px] xl:tw-justify-between">
        <div class="tw-text-[20px] tw-font-bold tw-leading-[26px]">
          {{ $t("Edit Contact Info") }}
        </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-[10px] md:tw-grid-cols-2 md:tw-gap-x-[60px]">
        <div class="element">
          <elementInput :input="{
            id: 'FirstName',
            label: 'First name',
            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: 'Last Name',
            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"></vue-country-code>
          <vue-phone-number-input color="#EF5A5A" valid-color="#EE9546" :border-radius="5" no-flags no-example
            v-model="formData.phone_number"></vue-phone-number-input>
        </div>
      </div>
      <div class="modal-footer tw-flex tw-justify-end tw-items-center tw-mt-[60px]">
        <button class="tw-transition tw-btn-md tw-text-primary-1 tw-px-[30px] tw-py-[9.5px] tw-mr-[10px] tw-rounded-2xl"
          @click="reset">
          {{ $t("Clear") }}
        </button>
        <button
          class="tw-transition tw-btn-md tw-bg-primary-1 tw-px-[30px] tw-py-[9.5px] tw-rounded-2xl hover:tw-bg-primary-2"
          @click="save">
          {{ $t("Save") }}
        </button>
      </div>
    </div>
  </modal>
</template>
<script>
import elementInput from "@/components/newComponent/form/ElementInput";
import is from "is_js";
export default {
  name: "EditContactModal",
  components: {
    elementInput,
    is,
  },
  props: {
    contact: {
      type: Object,
    },
  },
  data() {
    return {
      formData: {},
      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: "",
      };
    },
    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) => {
            console.log(result);
            if ((result.status = 200)) {
              this.$emit("update", true);
              this.$modal.hide("edit-contact-modal");
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
    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;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.close {
  position: absolute;
  right: 25px;
  top: 25px;
  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: 1366px) {
    position: relative;
    right: initial;
    top: initial;
  }
}

:deep() {
  .v--modal-box {
    height: 100vh !important;

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

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

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