<template>
  <div class="box is-primary">
    <span class="icon is-large">
      <i class="fas fa-2x fa-piggy-bank"></i>
    </span>
    <nav class="columns is-mobile is-multiline">
      <div class="column">
          <p class="heading">PC</p>
          <p class="is-size-4">{{ wealth[0] }}</p>
      </div>
      <div class="column">
          <p class="heading">PA</p>
          <p class="is-size-4">{{ wealth[1] }}</p>
      </div>
      <div class="column">
          <p class="heading">PO</p>
          <p class="is-size-4">{{ wealth[2] }}</p>
      </div>
      <div class="column">
          <p class="heading">PP</p>
          <p class="is-size-4">{{ wealth[3] }}</p>
      </div>
    </nav>
    <div v-if="edit"> <!-- or v-show ? -->
    <nav class="columns is-1  is-variable is-mobile">
      <template v-for="i in 4">
      <div :key="`input-col-${i}`" class="column">
        <NumberInput v-model="edit_values[i - 1]"></NumberInput>
      </div>
      </template>
    </nav>
    <nav class="columns is-mobile">
      <div class="column is-4 is-offset-2">
        <button class="button is-outlined is-fullwidth is-danger"
            @click="updateWealth('minus')">
          <span class="icon"><i class="fas fa-2x fa-minus"></i></span>
        </button>
      </div>
      <div class="column is-4">
        <button class="button is-outlined is-primary is-fullwidth"
            @click="updateWealth('plus')">
          <span class="icon"><i class="fas fa-2x fa-plus"></i></span>
        </button>
      </div>
    </nav>
    </div>
  </div>
</template>

<script>
    import NumberInput from './NumberInput.vue'
    export default {
        components: { NumberInput },
        props: ["wealth", "edit"],
        data () { 
            return {
                edit_values: [0,0,0,0], 
            }; 
        },
        methods: {
            updateWealth (op) {
                const values = this.edit_values;
                // Is it optimal, considering NumberInput already validates numbers ?
                // Check that all fields are valid numbers
                const success = values
                    .map(v => !isNaN(v))
                    .reduce(
                        (t,v) => t && v, 
                        true);
                if (success) {
                    console.log('updated', op, values);
                    this.$emit('updated');
                    this.resetValues();
                } else {
                    console.log('correct errors');
                }
            },
            resetValues () {
                this.edit_values.fill(0);
            }
        }
    }
</script>
