<template>
  <!-- QUICK NAV -->
  <nav id="QuickNav" class="fixed bottom-0 left-0 right-0 bg-slate-200">
    <ul class="flex text-2xl p-2 gap-2">
      <!-- Home Feed -->
      <li class="basis-1/5 rounded-md text-slate-500">
        <a href="" class="block text-center py-3 px-1"
          ><fa icon="house-chimney" class="fa-fw"></fa
        ></a>
      </li>
      <!-- Search -->
      <li class="basis-1/5 rounded-md text-slate-500">
        <a href="search.html" class="block text-center py-3 px-1"
          ><fa icon="magnifying-glass" class="fa-fw"></fa
        ></a>
      </li>
      <!-- Projects -->
      <li class="basis-1/5 rounded-md bg-slate-400 text-white">
        <a href="" class="block text-center py-3 px-1"
          ><fa icon="folder-open" class="fa-fw"></fa
        ></a>
      </li>
      <!-- Commitments -->
      <li class="basis-1/5 rounded-md text-slate-500">
        <a href="" class="block text-center py-3 px-1"
          ><fa icon="hand" class="fa-fw"></fa
        ></a>
      </li>
      <!-- Profile -->
      <li class="basis-1/5 rounded-md text-slate-500">
        <router-link
          :to="{ name: 'account' }"
          class="block text-center py-3 px-1"
          ><fa icon="circle-user" class="fa-fw"></fa
        ></router-link>
      </li>
    </ul>
  </nav>

  <!-- CONTENT -->
  <section id="Content" class="p-6 pb-24">
    <!-- Breadcrumb -->
    <div id="ViewBreadcrumb" class="mb-8">
      <h1 class="text-lg text-center font-light relative px-7">
        <!-- Back -->
        <button
          @click="$router.go(-1)"
          class="text-lg text-center px-2 py-1 absolute -left-2 -top-1"
        >
          <fa icon="chevron-left" class="fa-fw"></fa>
        </button>
        <!-- Context Menu -->
        <a
          href=""
          class="text-lg text-center px-2 py-1 absolute -right-2 -top-1"
          ><fa icon="ellipsis-vertical" class="fa-fw"></fa
        ></a>

        View Project
      </h1>
    </div>

    <!-- Project Details -->
    <div class="bg-slate-100 rounded-md overflow-hidden px-4 py-3 mb-4">
      <!-- Image -->
      <div class="-mx-4 -mt-3 mb-3">
        <img src="https://picsum.photos/800/400" class="w-full" />
      </div>

      <div>
        <h2 class="text-xl font-semibold">Canyon cleanup</h2>
        <div class="flex justify-between gap-4 text-sm mb-3">
          <span><fa icon="user" class="fa-fw text-slate-400"></fa> Rotary</span>
          <span
            ><fa icon="calendar" class="fa-fw text-slate-400"></fa> 8 days
            ago</span
          >
        </div>

        <div class="text-sm text-slate-500">
          Sed ut perspiciatis unde omnis iste natus error sit voluptatem
          accusantium doloremque laudantium…
          <a href="" class="uppercase text-xs font-semibold text-slate-700"
            >Read More</a
          >
        </div>
      </div>
    </div>

    <!-- Commit -->
    <router-link
      :to="{ name: 'new-edit-commitment' }"
      class="block text-center text-lg font-bold uppercase bg-blue-600 text-white px-2 py-3 rounded-md mb-8"
      >Make Commitment</router-link
    >

    <!-- Commitments -->
    <div class="bg-slate-100 px-4 py-3 rounded-md">
      <h3 class="text-sm uppercase font-semibold mb-3">Commitments</h3>

      <ul class="text-sm border-t border-slate-300">
        <li class="flex justify-between gap-4 py-1.5 border-b border-slate-300">
          <span>[Username]</span>
          <span
            >5 hours <fa icon="spinner" class="fa-fw text-slate-400"></fa
          ></span>
        </li>

        <li class="flex justify-between gap-4 py-1.5 border-b border-slate-300">
          <span>[Username]</span>
          <span
            >US$ 20.00 <fa icon="circle-check" class="fa-fw text-lime-500"></fa
          ></span>
        </li>

        <li class="flex justify-between gap-4 py-1.5 border-b border-slate-300">
          <span>[Username]</span>
          <span
            >0.1 BTC <fa icon="spinner" class="fa-fw text-slate-400"></fa
          ></span>
        </li>
      </ul>
    </div>
  </section>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { accessToken } from "@/libs/crypto";
import { db } from "../db";
import { IIdentifier } from "@veramo/core";
import { AppString } from "@/constants/app";

@Options({
  components: {},
})
export default class ProjectViewView extends Vue {
  projectId =
    localStorage.getItem("projectId") === null
      ? ""
      : localStorage.getItem("projectId");
  async LoadProject(identity: IIdentifier) {
    console.log("LoadProject", this.projectId);
    const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
    const url = endorserApiServer + "/api/claim/" + this.projectId;
    const token = await accessToken(identity);
    const headers = {
      "Content-Type": "application/json",
      Authorization: "Bearer " + token,
    };

    try {
      const resp = await this.axios.get(url, { headers });
      console.log(resp.status, resp.data);
    } catch (error) {
      console.log(error);
    }
  }

  async created() {
    await db.open();
    const num_accounts = await db.accounts.count();
    if (num_accounts === 0) {
      console.log("Problem!  Should have a profile!");
    } else {
      const accounts = await db.accounts.toArray();
      const identity = JSON.parse(accounts[0].identity);
      this.LoadProject(identity);
    }
  }
}
</script>
