<template>
   <add-address
      v-bind:key="key"
      v-bind:context="context"
      v-bind:options="options"
      v-bind:addressChangedCallback="submitAddress"
      ref="addAddress">
   </add-address>
</template>

<script>
import AddAddress from './components/AddAddress.vue';
import { postAddressToHousehold, postAddressToPerson } from "ChillPersonAssets/vuejs/_api/AddAddress";

export default {
   name: "App",
   components: {
      AddAddress
   },
   props: ['addAddress'],
   computed: {
      context() {
         return this.addAddress.context;
      },
      options() {
         return this.addAddress.options;
      },
      key() {
         return (this.context.edit) ? 'address_' + this.context.addressId
            : this.context.target.name + '_' + this.context.target.id ;
      }
   },
   mounted() {
      //console.log('AddAddress: data context', this.context);
      //console.log('AddAddress: data options', this.options);
   },
   methods: {
      displayErrors() {
         return this.$refs.addAddress.errorMsg;
      },
      submitAddress(payload) {
         console.log('@@@ click on Submit Address Button', payload);

         // Existing address
         if (this.context.edit) {

            // address is already linked, just finish !
            this.$refs.addAddress.afterLastPaneAction({});

         // New created address
         } else {
            this.postAddressTo(payload);
         }
      },

      /*
      * Patch date
      * TO MOVE in DatePane
      addDateToAddressAndPostAddressTo(payload) {
         payload.body = {
            validFrom: {
               datetime: `${this.context.valid.from.toISOString().split('T')[0]}T00:00:00+0100`
            }
         };
         console.log('addDateToAddress', payload);

         this.$refs.addAddress.flag.loading = true;
         return patchAddress(payload.addressId, payload.body)
            .then(address => new Promise((resolve, reject) => {
                  this.context.valid.from = address.validFrom;
                  resolve();
               })
                  .then(this.postAddressTo(payload))
            )
            .catch((error) => {
               this.$refs.addAddress.errorMsg.push(error);
               this.$refs.addAddress.flag.loading = false;
            });
      },
      */

      /*
      *  Post new created address to targetEntity
      */
      postAddressTo(payload) {
         console.log('postAddress', payload.addressId, 'To', payload.target, payload.targetId);
         switch (payload.target) {
            case 'household':
               postAddressToHousehold(payload.targetId, payload.addressId)
                  .then(address => new Promise((resolve, reject) => {
                     console.log('..household address', address);
                     this.$refs.addAddress.flag.loading = false;
                     this.$refs.addAddress.flag.success = true;

                     // finish
                     this.$refs.addAddress.afterLastPaneAction({ addressId: address.address_id });

                     resolve();
                  }))
                  .catch((error) => {
                     this.$refs.addAddress.errorMsg.push(error);
                     this.$refs.addAddress.flag.loading = false;
                  })
               ;
               break;
            case 'person':
               postAddressToPerson(payload.targetId, payload.addressId)
                  .then(address => new Promise((resolve, reject) => {
                     console.log('..person address', address);
                     this.$refs.addAddress.flag.loading = false;
                     this.$refs.addAddress.flag.success = true;

                     // finish
                     this.$refs.addAddress.afterLastPaneAction({ addressId: address.address_id });

                     resolve();
                  }))
                  .catch((error) => {
                     this.$refs.addAddress.errorMsg.push(error);
                     this.$refs.addAddress.flag.loading = false;
                  })
               ;
               break;
            default:
               this.$refs.addAddress.errorMsg.push('That entity is not managed by address !');
         }
      }
   }
}
</script>
