<template>
  <section>
    <schema-field v-model="valueCopy.name" :schema="schema.properties.name"></schema-field>
    <schema-field v-model="valueCopy.startDateTime" :schema="schema.properties.startDateTime"></schema-field>
    <schema-field v-model="valueCopy.endDateTime" :schema="schema.properties.endDateTime"></schema-field>
    <schema-component-part v-model="valueCopy.prizeDefinitions" :schema="schema.properties.prizeDefinitions"></schema-component-part>
    <schema-field v-model="valueCopy.isActive" :schema="schema.properties.isActive"></schema-field>

    <div v-for="(propSchema, propName) in schema.properties" :key="propName" v-if="isCustomProperty(propName) && !propSchema.isHidden">
      <schema-component-part v-if="propSchema.type === 'object'" v-model="valueCopy[propName]" :schema="propSchema"></schema-component-part>
      <schema-field v-else v-model="valueCopy[propName]" :schema="propSchema"></schema-field>
    </div>

    <hr />

    <activate-sweepstake :sweepstake-id="valueCopy.idStr" :url-part="valueCopy.urlPart" v-model="activation" v-if="valueCopy.idStr && !valueCopy.isActive"></activate-sweepstake>
    <export-sweepstake-times :sweepstake-id="valueCopy.idStr" :url-part="valueCopy.urlPart" v-if="valueCopy.isActive"></export-sweepstake-times>
  </section>
</template>

<script>
import customEditorExtends from '../shared/custom-editor-extends.vue';
import activateSweepstake from './activate-sweepstake.vue';
import exportSweepstakeTimes from './export-sweepstake-times.vue';

const defaultSweepstakeProps = ['name', 'startDateTime', 'endDateTime', 'activationDateTime', 'prizeDefinitions', 'isActive', 'idStr', 'urlPart'];

export default {
  extends: customEditorExtends,
  computed: {
    activation: {
      get() {
        return { isActive: this.valueCopy.isActive, hashPrizeDefinitions: this.valueCopy.hashPrizeDefinitions };
      },
      set(newValue) {
        this.valueCopy.isActive = newValue.isActive;
        this.valueCopy.hashPrizeDefinitions = newValue.hashPrizeDefinitions;
        this.$emit('input', this.valueCopy);
      }
    }
  },
  methods: {
    isCustomProperty(propName) {
      return !defaultSweepstakeProps.includes(propName);
    } 
  },
  components: {
    activateSweepstake,
    exportSweepstakeTimes
  }
}
</script>
