<script setup>
import i18n from "@/i18n.js"
import {getSelf, getUser} from '../../rest/Users.js'
import {getcurriculum,getSomeonesCurriculumList} from "@/rest/curriculum.js";
import {getRegisters} from "@/rest/ServiceInscription.js";
import {get} from "jsdom/lib/jsdom/named-properties-tracker.js";
import {getExternalCurriculumByInscrReq} from "@/rest/externalCurriculum.js";
import {ref} from "vue";
import ExternalCurriculumList from "@/Apps/Inscription/ExternalCurriculumList.vue";
import {editEquivalenceState} from "@/rest/requests.js";

const props = defineProps(['target']);
const request = await getRegisters(props.target);
const cursus = await getcurriculum(request.curriculum);
const user = await getSelf();
const list = ref(false);
const externalCurriculum = await getExternalCurriculumByInscrReq(request.id)

//Get the parent page windowState to display the correct button
const windowState = defineModel("windowState")
function getPP(){
  if(request.profilePictureUrl === null){
    return "/Clyde.png"
  }
  return request.profilePictureUrl;
}

async function editEquivalence(id, newstate){
  await editEquivalenceState(id, newstate)
}
</script>

<template>
  <div class="body" v-if="list == false">
    <div class="container">
      <div class="profilPic">
        <img class="subContainter" :src=getPP()>
      </div>
      <div class = "globalInfos">
        <div class="infosContainer">
          <div>
            FirstName/Name : {{request.firstName}} {{request.lastName}}
          </div>
          <div>
            E-mail: {{request.email}}
          </div>
          <div>
            Adresse : {{request.address}}
          </div>
          <div>
            Pays : {{request.country}}
          </div>
          <div>
            Date de naissance : {{request.birthDate}}
          </div>
          <div>
            Cursus voulu : BAB {{cursus.year}} {{cursus.option}}
          </div>
          <div>
            <a :href="downloadPdf(request.identityCard)">Download identity card</a>
            <button v-if="request.admissionDocUrl != null">Download admission document</button>
          </div>
          <div v-if="cursus.year > 1">
            <button style="background-color:rgb(105,05,105);margin-left: 5%" @click="list=!list" v-if="(user.role == 'Teacher' || user.role == 'Admin')&& request.equivalenceState == 'Pending'">See external curriculums</button>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div v-if="list == false">
    <button @click="windowState = 0">Back</button>
  </div>
  <div v-if="list==true">
    <ExternalCurriculumList :ext-curr-list="externalCurriculum" :mode="0"></ExternalCurriculumList>
    <div style="margin-left: 15%;margin-top: 5%;">
      <button style="margin-left: 2%" @click="list = false;editEquivalence(request.id, 'Accepted'); request.equivalenceState='Accepted'">Accept Equivalence</button>
      <button style="margin-left: 2%" @click="list = false;editEquivalence(request.id, 'Refused'); request.equivalenceState='Refused'">Refuse Equivalence</button>
      <button style="margin-left: 2%" @click="list=false">Back</button>
    </div>
  </div>
</template>


<style scoped>
.container{
  min-width:675px;
  display:grid;
  grid-template-columns:10vw 50vw;
  grid-template-rows:200px auto;
  column-gap:2.7%;
  row-gap:45px;
  grid-template-areas:
  "profilPic globalInfos"
  "minfos minfos";
}

.profilPic{
  grid-area:profilPic;
  width:100%;
}

.globalInfos {
  grid-area:globalInfos;
  align-self :center;

}

.body {
  min-width:960px;
  width:100%;
  display:flex;
  align-items:center;
  justify-content:center;
  margin-top:5%;
}

.subContainter{
  width:100%;
  background-color:rgb(50,50,50);
  border-radius:20px;
  border:4px solid black;
}

.infosContainer {
  padding-bottom:50px;
  border:2px solid black;
  font-size:25px;
  color:white;
  padding:20px;
  background-color:rgb(50,50,50);
  border-radius:20px;
}

button{
  font-size:15px;
  height:50px;
  width:100px;
  border:none;
  border-radius:20px;
}
</style>