<template>
  <div v-if="visible" class="dialog-overlay">
    <div class="dialog">
      <h1 class="text-lg text-center">
        {{ message }} {{ giver?.name || "somebody not specified" }}
      </h1>
      <input
        type="text"
        class="block w-full rounded border border-slate-400 mb-4 px-3 py-2"
        placeholder="What was received"
        v-model="description"
      />
      <div class="flex flex-row">
        <span class="py-4">Hours</span>
        <input
          type="text"
          class="block w-8 rounded border border-slate-400 ml-4 text-center"
          v-model="hours"
        />
        <div class="flex flex-col px-1">
          <div>
            <fa icon="square-caret-up" size="2xl" @click="increment()" />
          </div>
          <div>
            <fa icon="square-caret-down" size="2xl" @click="decrement()" />
          </div>
        </div>
      </div>
      <p class="text-right">Sign & Send to publish to the world</p>
      <div class="text-right">
        <button class="rounded border border-slate-400" @click="confirm">
          <span class="m-2">Sign & Send</span>
        </button>
        &nbsp;
        <button class="rounded border border-slate-400" @click="cancel">
          <span class="m-2">Cancel</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Prop, Emit } from "vue-facing-decorator";

@Component
export default class GiftedDialog extends Vue {
  @Prop message = "";

  giver = null;
  description = "";
  hours = "0";
  visible = false;

  open(giver) {
    // giver: GiverInputInfo
    this.giver = giver;
    this.visible = true;
  }

  close() {
    this.visible = false;
  }

  increment() {
    this.hours = `${(parseFloat(this.hours) || 0) + 1}`;
  }

  decrement() {
    this.hours = `${Math.max(0, (parseFloat(this.hours) || 1) - 1)}`;
  }

  @Emit("dialog-result")
  confirm() {
    const result = {
      action: "confirm",
      giver: this.giver,
      hours: parseFloat(this.hours),
      description: this.description,
    };
    this.close();
    this.description = "";
    this.giver = null;
    this.hours = "0";

    return result;
  }

  @Emit("dialog-result")
  cancel() {
    const result = { action: "cancel" };
    this.close();
    return result;
  }
}
</script>

<style>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.dialog {
  background-color: white;
  padding: 1rem;
  border-radius: 0.5rem;
  width: 50%;
}
</style>
