<template>
  <modal name="addCompany" :clickToClose="false">
    <div class="tw-text-base-primary">
      <div class="modal-header tw-w-full tw-mb-[20px] md:tw-mb-[50px]">
        <div class="tw-text-[18px] tw-font-bold md:tw-text-[20px]">
          {{ $t("userProfile.addCompanyInfo") }}
        </div>
        <button class="close tw-transition tw-btn-md" @click="$modal.hide('addCompany')"></button>
      </div>
      <div
        class="modal-content tw-grid tw-grid-cols-1 tw-gap-y-[20px] tw-mb-[30px] md:tw-grid-cols-[260px_260px] md:tw-gap-x-[64px] md:tw-mb-[60px]">
        <div class="element">
          <elementInput :input="{
            id: 'CompanyName',
            label: 'userProfile.companyName',
            required: true,
            type: 'text',
          }" :default="userCompanyInfo.company_name" :validation="validation.company_name"
            @change="userCompanyInfo.company_name = $event"></elementInput>
        </div>
        <div class="element">
          <elementInput :input="{
            id: 'TaxNumber',
            label: 'userProfile.taxNumber',
            required: true,
            type: 'tel',
          }" :default="userCompanyInfo.company_tax_no" :validation="validation.company_tax_no"
            @change="userCompanyInfo.company_tax_no = $event"></elementInput>
        </div>
        <div class="element md:tw-col-span-2">
          <elementAddress :input="{
            id: 'StreetAddress',
            label: 'userProfile.companyAddress',
            required: true,
            type: 'street',
          }" :default1="userCompanyInfo.company_address1" :default2="userCompanyInfo.company_address2"
            :validation1="validation.company_address1" :validation2="validation.company_address2"
            @change1="userCompanyInfo.company_address1 = $event" @change2="userCompanyInfo.company_address2 = $event">
          </elementAddress>
        </div>
        <div class="element">
          <elementInput :input="{
            id: 'City',
            label: 'userProfile.companyCity',
            required: true,
            type: 'text',
          }" :default="userCompanyInfo.company_city" :validation="validation.company_city"
            @change="userCompanyInfo.company_city = $event"></elementInput>
        </div>
        <div class="element">
          <elementInput :input="{
            id: 'State',
            label: 'userProfile.stateAndProvince',
            required: false,
            type: 'text',
          }" :default="userCompanyInfo.company_state" :validation="validation.company_state"
            @change="userCompanyInfo.company_state = $event"></elementInput>
        </div>

        <div class="element">
          <elementSelect :select="{
            id: 'Country',
            label: 'userProfile.companyCountry',
            required: true,
          }" :selectList="countryOptions" :default="userCompanyInfo.company_country"
            :validation="validation.company_country" @change="userCompanyInfo.company_country = $event"></elementSelect>
        </div>
        <div class="element">
          <elementInput :input="{
            id: 'ZIP',
            label: 'userProfile.zipAndPostalCode',
            required: true,
            type: 'zip',
          }" :default="userCompanyInfo.company_zipcode" :validation="validation.company_zipcode"
            @change="userCompanyInfo.company_zipcode = $event"></elementInput>
        </div>
      </div>
      <div class="md:tw-flex md:tw-flex-row-reverse">
        <button
          class="tw-transition tw-btn-md tw-bg-primary-1 tw-text-white tw-w-full tw-py-[13px] tw-rounded-[16px] tw-mb-[10px] md:hover:tw-bg-primary-2 md:tw-w-fit md:tw-px-[24px] md:tw-mb-0"
          @click="save()">
          {{ $t("userProfile.add") }}
        </button>
        <button
          class="tw-transition tw-btn-md tw-bg-white tw-text-primary-1 tw-w-full tw-py-[13px] tw-rounded-[16px] md:tw-w-fit md:tw-px-[24px] md:tw-mr-[10px]"
          @click="reset()">
          {{ $t('userProfile.cancel')}}
        </button>
      </div>
    </div>
  </modal>
</template>
<script>
import elementInput from "@/components/newComponent/form/ElementInput.vue";
import elementAddress from "@/components/newComponent/form/ElementAddress.vue";
import elementSelect from "@/components/newComponent/form/ElementSelect.vue";
import is from "is_js";
export default {
  name: "addCompany",
  props: {
    countryOptions: {
      type: Array,
    },
    selectList: {
      type: Array,
    },
  },
  data() {
    return {
      userCompanyInfo: {
        company_name: "",
        company_tax_no: "",
        company_address1: "",
        company_address2: "",
        company_city: "",
        company_state: "",
        company_country: 0,
        company_zipcode: "",
      },
      validation: {
        company_name: true,
        company_tax_no: true,
        company_address1: true,
        company_address2: true,
        company_city: true,
        company_country: true,
        company_state: true,
        company_zipcode: true,
      },
      // valid: false,
      errors: null,
    };
  },
  components: {
    elementInput,
    elementAddress,
    elementSelect,
    is,
  },
  methods: {
    reset() {
      this.userCompanyInfo = {
        company_name: "",
        company_tax_no: "",
        company_address1: "",
        company_address2: "",
        company_city: "",
        company_state: "",
        company_country: 0,
        company_zipcode: "",
      };
      this.$modal.hide("addCompany");
    },
    patchUserData() {
      this.$axios
        .post(
          `/member/company?jwt=${this.$auth.$storage.getUniversal("jwt").token || ""
          }`,
          this.userCompanyInfo
        )
        .then((result) => {
          this.$emit("refetch-user");
          this.$modal.hide("addCompany");
        })
        .catch((err) => {
          console.log(err);
        });
    },

    validators() {
      if (is.empty(this.userCompanyInfo.company_name)) {
        this.validation.company_name = false;
      } else {
        this.validation.company_name = true;
      }
      if (is.empty(this.userCompanyInfo.company_tax_no)) {
        this.validation.company_tax_no = false;
      } else {
        this.validation.company_tax_no = true;
      }
      if (is.empty(this.userCompanyInfo.company_address1)) {
        this.validation.company_address1 = false;
      } else {
        this.validation.company_address1 = true;
      }
      if (is.empty(this.userCompanyInfo.company_city)) {
        this.validation.company_city = false;
      } else {
        this.validation.company_city = true;
      }
      if (this.userCompanyInfo.company_country == 0) {
        this.validation.company_country = false;
      } else {
        this.validation.company_country = true;
      }
      if (is.empty(this.userCompanyInfo.company_zipcode)) {
        this.validation.company_zipcode = false;
      } else {
        this.validation.company_zipcode = 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.userCompanyInfo));
        this.$axios
          .post(
            `/member/company?jwt=${this.$auth.$storage.getUniversal("jwt").token
            }`,
            patchData
          )
          .then((result) => {
            if (result.status == 200) {
              this.$emit("refetch-user");
              this.reset();
              this.$modal.hide("addCompany");
            }
          })
          .catch((err) => {
            console.log(err);
          });
      }
    },
  },
};
</script>
<style scoped lang="scss">
.close {
  position: absolute;
  right: 30px;
  top: 30px;
  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 {
    padding: 30px;
    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>
