const { invoke } = window.__TAURI__.tauri;
const { listen } = window.__TAURI__.event;
const { ask, message } = window.__TAURI__.dialog;
const downBar = document.querySelector(".progressFinished");
//import { listen } from '@tauri-apps/api';

const error = listen("Error", (error) => {
  message(error.payload, {title: "Error", type: "error"});
});

window.addEventListener("DOMContentLoaded", () => {

  document.getElementById("back").addEventListener("click", back);
  document.getElementById("Cancel").addEventListener("click", back);
  document.getElementById("Login").addEventListener("click", login);
  document.getElementById("Password").addEventListener("keypress", keypress);
  
});

function back(){
  invoke("drop_session");
  window.location.href = "index.html";
}

function login(){
  invoke("login", { username: document.getElementById("Username").value, password: document.getElementById("Password").value});
}

function keypress(e){
  if(e.keyCode === 13){
    e.preventDefault();
    login();
  }
}

const failed = listen("Login_Failed", (event) => {
  document.getElementById("Incorrect").style.visibility = "visible";
})

const success = listen("Login_Success", (event) => {
  window.location.href = "Admin.html";
})