<?php
session_start();
if (!isset($_SESSION['user'])) {
	header('Location: https://glink.zip/');
}
?>
<!DOCTYPE html>
<html id="html-tag" lang="en" class="py-[0%] my-[0%]">
<head class="py-0 my-0 h-0">
    <meta charset="UTF-8">
    <title>Dashboard - GLink</title>
  <link rel="stylesheet" href="output.css">
</head>
<body class="w-full my-[0%] py-[0%] px-[10%] min-h-screen bg-gray-300 dark:bg-black">
<script src="htmx.min.js"></script>

<main class="h-auto w-auto py-[5%]">

<?php include 'header.php'; ?>

  <div class="overflow-x-auto ">
    <table class="table text-gray-800 brightness-125 italic my-4 bg-green-100 dark:bg-gray-800">
      <!-- head -->
      <thead>
      <tr class="text-gray-800 dark:text-gray-50 brightness-125 text-xl font-bold text-center border-none">
        <th></th>
        <th>GLink</th>
        <th>URL</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
      </thead>
      <tbody>
	<?php
	require_once 'db.inc.php';
	$session = init_cass_db();
	$statement = $session->prepare("SELECT * FROM data WHERE user=? AND hell=false ALLOW FILTERING;");
	$result = $session->execute($statement,array('arguments' => array($_SESSION['user'])));

	for ($i=0; $i < $result->count(); $i++) { ?>
      <!-- row 1 -->
      <tr class="data_row text-center border-none" >
        <th class="dark:bg-transparent dark:text-white"><?php echo($i+1) ?></th>
        <td><input type="text" class="dark:bg-transparent dark:text-white input input-bordered input-success glink_str text-center italic border-none rounded-lg focus:shadow-lg" readonly value="<?php echo($result[$i]['shortlink']);?>"></td>
        <td><input type="url" class="dark:bg-transparent dark:text-white input input-bordered input-success url_str italic text-center border-none rounded-lg focus:shadow-lg" readonly value="<?php echo($result[$i]['url']);?>"></td>
        <td>
          <button class="edit_btn btn bg-transparent hover:bg-transparent border-none p-0 m-0">
            <svg width="24"
                 height="24"
                 class="bi bi-pencil dark:fill-blue-500 fill-[#0304f7]"
                 viewBox="0 0 16 16">
              <path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/>
            </svg></button>
          <button class="save_btn btn mx-8 bg-transparent hover:bg-transparent border-none p-0 m-0 hidden" hx-trigger='click[checkURL(event.currentTarget.parentElement.parentElement.getElementsByClassName("url_str")[0].value)]' hx-post="/updatelink.php" hx-vals='js:{"old_link": "<?php echo($result[$i]['shortlink']); ?>", "url": event.currentTarget.parentElement.parentElement.getElementsByClassName("url_str")[0].value}'>
            <div class="inline-block rotate-45 h-[24px] w-[12px] border-b-4 border-b-green-500 border-r-green-500 border-r-4 hover:border-b-green-300 hover:border-r-green-300"></div>
          </button>
          <button class="cancel_btn btn bg-transparent hover:bg-transparent border-none p-0 m-0 hidden">
            <div class="w-[24px] h-[24px] before:absolute before:content-[' '] before:h-[32px] before:w-[2px] before:bg-red-600 before:rotate-45 after:absolute after:content-[' '] after:h-[32px] after:w-[2px] after:bg-red-600 after:-rotate-45 hover:before:bg-red-400 hover:after:bg-red-400"></div>
          </button></td>
        <td>
          <button class="delete_btn btn bg-transparent hover:bg-transparent border-none p-0 m-0" hx-post="/deletelink.php" hx-vals='{"link": "<?php echo($result[$i]['shortlink']);?>"}' >
            <svg xmlns="http://www.w3.org/2000/svg" fill="red" x="0px" y="0px" viewBox="0 0 25 24.8" width="25" height="24" xml:space="preserve" class= "group icon-trashcan ct-delete" data-ember-action="" data-ember-action-1015="1015">
  <g class="trashcan-open invisible group-hover:visible">
    <path d="M18.7,24.4H5.9L4.9,7h14.9L18.7,24.4z M7.6,22.6H17l0.8-13.7h-11L7.6,22.6z"></path>
    <polygon points="13.6,10.3 13.1,21.2 14.9,21.2 15.4,10.3 "></polygon>
    <polygon points="11.5,21.2 11,10.3 9.2,10.3 9.7,21.2 "></polygon>
    <path d="M19.1,0.7l-4.7,0.9l-0.8-1.4L8.2,1.3L8,3l-4.7,1l0.2,4.7l17.3-3.5L19.1,0.7z

             M8.8,1.9l4.4 -1.0 l0.5,0.8
             L8.7,2.8z

             M5.2,6.4l0-1L18,2.8l0.3,0.9L5.2,6.4z"></path>
  </g>
              <g class="trashcan-closed group-hover:invisible">
    <path d="M6.8,8.8h11L17,22.6
             H7.6L6.8,8.8z
             M4.9,7l1,17.4h12.8
             l1-17.4
             H4.9z"></path>
                <polygon points="13.6,10.3 13.1,21.2 14.9,21.2 15.4,10.3 "></polygon>
                <polygon points="11.5,21.2 11,10.3 9.2,10.3 9.7,21.2 "></polygon>
                <path d="M20.4,4h-4.8l-0.5-1.6
             H9.5L9,4
             H4.2
             L3.5,8.6h17.6
             L20.4,4z

             M9.9,3.2h4.8
             L14.9,3.9h-5.2z

             M5.6,6.7l0.2-1 h13l0.2,1
             H5.6z"></path>
  </g>
</svg>
          </button>
        </td>
      </tr>
      <tr class="notification_row text-center w-full hidden border-none">
        <td colspan="5"><div class="alert alert-error text-lg">
          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="stroke-current shrink-0 w-8 h-8"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
          <span>You have just deleted a row. Press <button class="undo_btn btn bg-transparent m-0 p-0 h-fit hover:bg-transparent border-none" hx-post="/undodeletelink.php" hx-vals='{"link": "<?php echo($result[$i]['shortlink']); ?>"}'><kbd class="px-2 py-1.5 text-xs font-semibold text-gray-800 bg-gray-100 border border-gray-200 rounded-lg dark:bg-gray-600 dark:text-gray-100 dark:border-gray-500">undo</kbd></button> to undo the action</span>
          <button class="close_btn btn bg-transparent hover:bg-transparent broder-none p-0 m-0 "><svg xmlns="http://www.w3.org/2000/svg" class="stroke-black shrink-0 h-8 w-8" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></button>
        </div></td>
      </tr>
	<?php } ?>
      </tbody>
      <script>
        let allButtons = document.getElementsByClassName('btn');
        for (let btn of allButtons) {
          var url_value;
          var glink_value;

          btn.addEventListener('click', () => {
            if (btn.classList.contains("close_btn")) {
              let notificationRow = btn.parentElement.parentElement.parentElement;
              notificationRow.classList.add("hidden");
            } else if (btn.classList.contains("undo_btn")) {
              let notificationRow = btn.parentElement.parentElement.parentElement.parentElement;
              let row = notificationRow.previousElementSibling;
              row.classList.remove("hidden");
              notificationRow.classList.add("hidden");
            }
            else {
              var clickedElement = btn;
              var clickedRow = clickedElement.parentElement.parentElement;
              let txt = clickedRow.getElementsByClassName("glink_str")[0];
              let url = clickedRow.getElementsByClassName("url_str")[0];
              let edit = clickedRow.getElementsByClassName("edit_btn")[0];
              let save = clickedRow.getElementsByClassName("save_btn")[0];
              let cancel = clickedRow.getElementsByClassName("cancel_btn")[0];
              if (btn.classList.contains("edit_btn") || btn.classList.contains("save_btn")) {
                if (url.readOnly) { // Edit button
                  url_value = url.value;
//                  glink_value = txt.value;
//                  txt.readOnly = false;
                  url.readOnly = false;
//                  txt.classList.add("outline-none", "ring-4", "ring-emerald-400", "shadow-lg", "focus:outline-none", "focus:ring-4", "focus:ring-emerald-400", "focus:shadow-lg");
                  url.classList.add("outline-none", "ring-4", "ring-emerald-400", "shadow-lg", "focus:outline-none", "focus:ring-4", "focus:ring-emerald-400", "focus:shadow-lg");
                  edit.classList.add("hidden");
                  save.classList.remove("hidden");
                  cancel.classList.remove("hidden");
                  url.focus();
                } else { // Save button
		    if (!checkURL(url.value)) {
			url.classList.replace("ring-emerald-400","ring-red-400");
			url.classList.replace("focus:ring-emerald-400","focus:ring-red-400");
		    } else {
			url.classList.replace("ring-red-400","ring-emerald-400");
			url.classList.replace("focus:ring-red-400","focus:ring-emerald-400");
//                  	txt.readOnly = true;
                  	url.readOnly = true;
//                  	txt.classList.remove("outline-none", "ring-4", "ring-emerald-400", "shadow-lg", "focus:outline-none", "focus:ring-4", "focus:ring-emerald-400", "focus:shadow-lg");
                  	url.classList.remove("outline-none", "ring-4", "ring-emerald-400", "shadow-lg", "focus:outline-none", "focus:ring-4", "focus:ring-emerald-400", "focus:shadow-lg");
                  	edit.classList.remove("hidden");
	                save.classList.add("hidden");
        	        cancel.classList.add("hidden");
		    }
                }
              }
              if (btn.classList.contains("cancel_btn")) {
//                txt.value = glink_value;
                url.value = url_value;
//                txt.readOnly = true;
                url.readOnly = true;
//                txt.classList.remove("outline-none", "ring-4", "ring-blue-500/50", "shadow-lg", "focus:outline-none", "focus:ring-4", "focus:ring-blue-500/50", "focus:shadow-lg");
                url.classList.remove("outline-none", "ring-4", "ring-blue-500/50", "shadow-lg", "focus:outline-none", "focus:ring-4", "focus:ring-blue-500/50", "focus:shadow-lg");
                edit.classList.remove("hidden");
                save.classList.add("hidden");
                cancel.classList.add("hidden");
              }
              if (btn.classList.contains("delete_btn")) {
                let row = clickedRow;
//                let notification = row.parentElement.getElementsByClassName("notification_row")[0];
		let notification = row.nextElementSibling;
                row.classList.add("hidden");
                notification.classList.remove("hidden");
                setTimeout(function () {
                  notification.classList.add("hidden");
                }, 4000);
              }
            }
          });
        }

	function checkURL(url) {
		try {
			new URL(url);
			return true;
		} catch (err) {
			return false;
		}
	}
      </script>
    </table>
  </div>
  <!-- Table end -->
</main>

</body>
</html>