<template>
  <div class="container">
    <div class="supplier-container">
      <div class="caption-column">Supplier</div>
      <div class="input-field input-field-name"><span class="user-icon" v-if="isUserSupplier"><ph-user
          weight="fill"></ph-user></span> {{ supplierName }}
      </div>
      <div class="caption-column">Address</div>
      <div class="input-field select-node-input-field-address">
        <div class="supplier-flag">
          <flag :iso="isoCode"/>
        </div>
        <div class="supplier-address">{{ supplierAddress }}</div>
      </div>
      <div class="caption-column">Coordinates</div>
      <div class="input-field">{{ coordinatesDMS }}</div>
    </div>
    <div class="supplier-map">
      <azure-maps-component :draggable="true" :coordinates="supplierCoordinates"></azure-maps-component>
    </div>
    <div class="footer">
      <modal :state="selectSupplierModalState" @close="closeEditModal">
        <select-node @update-supplier="modalDialogClose"></select-node>
      </modal>
      <icon-button icon="plus" @click="openModal"></icon-button>
      <icon-button icon="pencil-simple" @click="openModal"></icon-button>
    </div>
  </div>
</template>
<script>


import IconButton from "@/components/UI/IconButton.vue";
import InputField from "@/components/UI/InputField.vue";
import Flag from "@/components/UI/Flag.vue";
import {PhUser} from "@phosphor-icons/vue";
import ModalDialog from "@/components/UI/ModalDialog.vue";
import SelectNode from "@/components/layout/node/SelectNode.vue";
import Modal from "@/components/UI/Modal.vue";
import AzureMapsComponent from "@/components/UI/AzureMapsComponent.vue";

export default {
  name: "SupplierView",
  components: {AzureMapsComponent, Modal, SelectNode, ModalDialog, PhUser, Flag, InputField, IconButton},
  emits: ['updateSupplier'],
  props: {
    supplierName: {
      type: String,
      required: true
    },
    supplierAddress: {
      type: String,
      required: true
    },
    isoCode: {
      type: String,
      required: true
    },
    supplierCoordinates: {
      type: Object,
      required: false,
      default: null
    },
    isUserSupplier: {
      type: Boolean,
      required: false,
      default: true
    },
    openSelectDirect: {
      type: Boolean,
      default: false,
    }
  },
  computed: {

    // Kombinierte Ausgabe beider Koordinaten
    coordinatesDMS() {
      return `${this.convertToDMS(this.supplierCoordinates?.latitude, 'lat')}, ${this.convertToDMS(this.supplierCoordinates?.longitude, 'lng')}`;
    }
  },
  data() {
    return {
      selectSupplierModalState: false
    }
  },
  created() {
    this.selectSupplierModalState = this.openSelectDirect;
  },
  methods: {
    closeEditModal() {
      this.selectSupplierModalState = false;
    },
    modalDialogClose(data) {
      this.selectSupplierModalState = false;
      if (data.action === 'accept') {
        this.$emit('updateSupplier', data);
      }
    },
    openModal() {
      this.selectSupplierModalState = true;
    },
    convertToDMS(coordinate, type) {

      if (!coordinate)
        return '';

      let direction;
      if (type === 'lat') {
        direction = coordinate >= 0 ? 'N' : 'S';
      } else {
        direction = coordinate >= 0 ? 'E' : 'W';
      }

      // Arbeite mit Absolutwert
      const abs = Math.abs(coordinate);

      // Grad (ganzzahliger Teil)
      const degrees = Math.floor(abs);

      // Minuten
      const minutesFloat = (abs - degrees) * 60;
      const minutes = minutesFloat.toFixed(4);


      return `${degrees}° ${minutes}' ${direction}`;
    }
  }
}
</script>


<style scoped>

.supplier-address {
  font-size: 1.4rem;
  color: #6b7280;
  max-width: 30rem;
}

.supplier-map {
  display: flex;
  justify-content: center;
}

.user-icon {
  display: inline-flex;
  align-items: center;
}

.input-field-name {
  display: flex;
  gap: 0.8rem;
  align-items: center;
  flex: 0 1 10rem
}

.select-node-input-field-address {
  display: flex;
  gap: 0.8rem;
  align-items: center;
}

.caption-column {
  font-size: 1.4rem;
  font-weight: 500;
  align-self: center;
  justify-self: end;
  color: #001D33
}

.input-field {
  align-self: center;
  font-size: 1.4rem;
  color: #6b7280;
}

.container {
  display: flex;
  flex-direction: column;
  gap: 1.6rem;
  flex: 1 1 auto;
}

.supplier-container {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: repeat(3, fit-content(0));
  align-content: center;
  gap: 1.6rem;
}

.footer {
  display: flex;
  gap: 1.6rem;
  justify-self: flex-end;
  align-self: flex-end;
}

</style>