<!----------------------------------------------------
	File:  ResearchComponent.vue
	Author: Maxime Bartha
	Scope: Extension Publicatons scientifiquess
	Description: Pop Up summarizing
----------------------------------------------------->

<script setup xmlns="http://www.w3.org/1999/html">
import {ref } from "vue";
import {onClickOutside} from '@vueuse/core'
import {patchArticle, deleteArticle, addView} from "@/rest/ScientificPublications/ManageResearch.js";

const restURL = import.meta.env.VITE_CLYDE_MODE === 'container' ? "http://localhost:8000": import.meta.env.DEV ? "http://localhost:5173" : "https://clyde.herisson.ovh/api"
const props = defineProps({
  isOpen: Boolean,
  article: ref(Object),
  manage:Boolean,
});



function format(date){
  let split = date.split("-")
  let month = split[1]
  let day = split[2].split("T")[0]
  let year = split[0]
  return day +"/"+ month +"/"+ year
}

const emit = defineEmits(["modal-close","modified"]);

const target = ref(null)
onClickOutside(target, ()=>emit('modal-close'))

let toModify= Object.assign({}, {});

function cancelChanges(){
  toModify= Object.assign({}, {});
  emit('modal-close')
}

async function confirmChanges(){
  await patchArticle(props.article.id, toModify)
  toModify= Object.assign({}, {});
  emit('modal-close')
  emit("modified")
}

async function deleteThisArticle(){
  await deleteArticle(props.article.id)
  emit('modal-close')
  emit("modified")
}
function downloadPdf(){
  return (restURL + "/" + props.article.pdfLocation)
}

function downloadBibTex(){
  return (restURL + "/" + props.article.bibTexLocation)
}

async function articleClicked(){
  await addView(props.article.pdfLocation)
  emit('modal-close')
  emit('modified')
}

/**
function downloadCoAuthors(){
  //todo
  const data = JSON.stringify(researcher.value);
  const blob = new Blob([data], {type:"application/json"});
  return URL.createObjectURL(blob);
} **/

</script>

<template>
  <div v-if="isOpen" class="modal-mask">
    <div class="modal-wrapper">
      <div class="modal-container" ref="target">
        <div ><ul>
          <li>Article Id : {{article.id}}</li>
          <li>Title : {{article.title}}</li>
          <li>Author : {{article.researcher.user.lastName + " " + article.researcher.user.firstName}}</li>
          <li>Summary : {{article.summary}}</li>
          <li>ReleaseDate: {{format(article.releaseDate)}}</li>
          <li>Language : {{article.language}}</li>
          <li>PaperType : {{article.paperType}}</li>
          <li>Domain : {{article.domain}}</li>
          <li>Views : {{article.views}}</li>
          <li>Access : {{article.access}}</li>
        </ul>
          <div id="downloads" v-if="article.pdfLocation !== null && !manage">
            <a :href=downloadPdf() @click.stop="articleClicked" target="_blank">See Research</a>
            <a v-if="article.bibTexLocation !== null" :href=downloadBibTex() @click.stop="emit('modal-close')"  target="_blank">See bibTex</a> </div>
        </div>
        <div v-if="manage">
          <div>
            <ul>
              <li>Title : <input v-model="toModify.title"></li>
              <li>Language : <input v-model="toModify.language"></li>
              <li>Summary : <input v-model="toModify.summary"></li>
              <li>Domain : <input v-model="toModify.domain"></li>
              <li>Access :
                <select id="classed-select" v-model="toModify.access">
                  <option value="OpenSource">OpenSource</option>
                  <option value="Restricted">Restricted</option>
                  <option value="Private">Private</option>
                </select></li>
            </ul>
          </div>
          <button id="confirmButton" @click="confirmChanges">Confirm Changes</button>
          <button id="cancelButton" @click="cancelChanges">Cancel Changes</button>
          <button id="deleteButton" @click="deleteThisArticle">Delete Research </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-container {
  width: 70%;
  margin: 150px auto;
  padding: 20px 30px;
  background: rgba(157, 99, 205);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
}

.modal-container ul{
  margin-top: 9px;
}


#downloads {
  text-align: end;
}
#downloads a {
  align-self: center;
  margin-left: 2px;
  font-size: large;
  color: white;
  background: rgba(191, 64, 191,0.5);
  border:2px solid black;
  border-radius: 5px;
  text-underline-mode: none;
  text-decoration: none;
}
#downloads button:hover{
  background: rgba(191, 64, 191);
}

#deleteButton{
  margin-left: 80%;
  align-self: center;
  text-align: center;
  border: 2px solid black;
  color: white;
  font-size: large;
  border-radius: 20px;
  background-color: red;
}

#deleteButton:hover{
  background: #ff2d55;
}

#cancelButton{
  align-self: center;
  text-align: center;
  border: 2px solid black;
  color: white;
  font-size: x-large;
  background-color:rgba(191, 64, 191,0.5);
  border-radius: 20px;
}
#cancelButton:hover{
  background:rgba(191,64,191)
}

#confirmButton{
  align-self: center;
  text-align: center;
  border: 2px solid black;
  color: white;
  font-size: x-large;
  background-color: #07bc0c;
  border-radius: 20px;
}
#confirmButton:hover{
  background: #4cd964;
}
</style>
