<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
	CodeBehind="DocumentsView.aspx.cs" Inherits="PDI.Web.DocumentsView" %>

	<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
		<style>
			.item-row-grid {
				display: grid;
				grid-template-columns: 28px 1fr 150px 155px 200px;
				/* Checkbox, Name, Tags, Created, Actions */
				align-items: center;
				gap: 0.75rem;
				padding: 0.75rem 1.5rem 0.75rem 0.75rem;
				border-bottom: 1px solid var(--border-color);
			}

			.item-meta {
				white-space: nowrap;
			}

			.item-row-grid:hover {
				background-color: var(--silver-light);
			}

			.item-check {
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.item-check input[type="checkbox"] {
				width: 16px;
				height: 16px;
				accent-color: #6D214F;
				cursor: pointer;
			}

			.item-name-cell {
				display: flex;
				align-items: center;
			}

			.item-name-cell i {
				width: 30px;
				text-align: center;
				margin-right: 1rem;
				color: var(--wine-light);
				font-size: 1.2rem;
			}

			.item-tags {
				display: flex;
				gap: 0.5rem;
				flex-wrap: wrap;
			}

			.tag {
				background-color: var(--silver-light);
				color: #555;
				padding: 0.25rem 0.5rem;
				border-radius: 12px;
				font-size: 0.8rem;
				padding-top: 5px;
			}

			.item-meta {
				font-size: 0.9rem;
				color: #777;
			}

			.item-actions {
				text-align: right;
				white-space: nowrap;
			}

			.item-actions a {
				margin-left: 1rem;
				text-decoration: none;
				color: var(--wine-light);
			}

			.item-actions a:hover {
				text-decoration: underline;
			}

			.btn-merge {
				display: none;
				align-items: center;
				gap: 0.35rem;
				padding: 5px 14px;
				background: transparent;
				color: #6D214F;
				border: 1.5px solid #6D214F;
				border-radius: 5px;
				font-size: 0.85rem;
				font-weight: 600;
				cursor: pointer;
				transition: all 0.2s;
				font-family: inherit;
				text-decoration: none;
				margin-left: 0.75rem;
			}

			.btn-merge:hover {
				background: #6D214F;
				color: #fff;
			}

			.back-button {
				margin-right: 1rem;
				font-size: 1.2rem;
				color: var(--wine-dark);
				text-decoration: none;
			}

			.back-button:hover {
				color: var(--wine-light);
			}

			.preview-pane .card-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.preview-nav-button {
				font-size: 1.5rem;
				color: var(--wine-dark);
				text-decoration: none;
			}

			.preview-nav-button:hover {
				color: var(--wine-light);
			}

			.preview-title {
				font-weight: bold;
			}
		</style>
	</asp:Content>
	<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
		<script type="text/javascript">
			var hId = 0;
			function openAddToListWindow(documentId) {
				PDIModal.open("AddToList", "AddToListWindow.aspx?DocumentId=" + documentId, "Agregar Documento a Lista", "500px", "600px");
				return false;
			}

			function openShareDocumentWindow(documentId) {
				hId = <%= CurrentHierarchyId %>;
				PDIModal.open("ShareDocument", "ShareDocumentWindow.aspx?DocumentId=" + documentId, "Compartir Documento", "500px", "600px");
				return false;
			}

			function openRenameDocumentWindow(documentId, hierarchyId) {
				hId = hierarchyId;
				PDIModal.open("RenameDocument", "RenameDocumentWindow.aspx?DocumentId=" + documentId, "Renombrar Documento", "500px", "400px");
				return false;
			}

			function openUploadFileWindow(hierarchyId) {
				if (hierarchyId == 0) {
					alert("Por favor seleccione una carpeta del árbol primero.");
					return false;
				}
				hId = hierarchyId;
				PDIModal.open("UploadFile", "UploadFileWindow.aspx?HierarchyId=" + hierarchyId, "Subir Archivo", "500px", "600px");
				return false;
			}

			function openShareHierarchyWindow(hierarchyId) {
				hId = hierarchyId;
				PDIModal.open("ShareHierarchy", "ShareHierarchyWindow.aspx?HierarchyId=" + hierarchyId, "Compartir Carpeta", "500px", "600px");
				return false;
			}

			function openRenameFolderWindow(hierarchyId) {
				hId = <%= CurrentHierarchyId %>;
				PDIModal.open("RenameFolder", "RenameFolderWindow.aspx?HierarchyId=" + hierarchyId, "Renombrar Carpeta", "500px", "250px");
				return false;
			}

			function openAddFolderToListWindow(hierarchyId) {
				PDIModal.open("AddFolderToList", "AddToListWindow.aspx?HierarchyId=" + hierarchyId, "Agregar Carpeta a Lista", "500px", "500px");
				return false;
			}

			function updateMergeButton() {
				var checks = document.querySelectorAll('.doc-check:checked');
				var btn = document.getElementById('btnMergePDFs');
				if (btn) {
					btn.style.display = checks.length >= 2 ? 'inline-flex' : 'none';
				}
			}

			function openMergeWindow() {
				var checks = document.querySelectorAll('.doc-check:checked');
				var ids = [];
				checks.forEach(function(cb) { ids.push(cb.getAttribute('data-docid')); });
				if (ids.length < 2) {
					alert('Seleccione al menos 2 documentos para unir.');
					return false;
				}
				PDIModal.open("MergePDF", "MergePDFWindow.aspx?DocumentIds=" + ids.join(','), "Unir en un Solo PDF", "520px", "420px");
				return false;
			}

			document.addEventListener("DOMContentLoaded", function() {
				PDIModal.onClose("UploadFile", function() { __doPostBack('RefreshContents', hId); });
				PDIModal.onClose("RenameDocument", function() { __doPostBack('RefreshContents', hId); });
				PDIModal.onClose("ShareDocument", function() { __doPostBack('RefreshContents', hId); });
				PDIModal.onClose("RenameFolder", function() { __doPostBack('RefreshContents', hId); });
				PDIModal.onClose("MergePDF", function() { __doPostBack('RefreshContents', hId); });
			});
		</script>
		<section id="page-documents" class="page">
			<h2>Documentos</h2>
			<div class="documents-container">
				<div class="documents-sidebar">
					<div class="search-box">
						<asp:TextBox ID="txtSearch" runat="server" placeholder="Buscar en documentos..."></asp:TextBox>
						<asp:Button ID="btnSearch" runat="server" Text="Buscar" OnClick="btnSearch_Click" />
					</div>
					<h4><i class="fas fa-sitemap"></i>Estructura</h4>
					<div id="document-tree">
						<asp:TreeView ID="tvHierarchies" runat="server"
							OnSelectedNodeChanged="tvHierarchies_SelectedNodeChanged">
						</asp:TreeView>
					</div>
				</div>

				<div class="main-content-area">
					<div class="document-content-viewer card">
						<div class="card-header">
							<asp:LinkButton ID="btnGoBack" runat="server" OnClick="btnGoBack_Click"
								CssClass="back-button" Visible="false">
								<i class="fas fa-arrow-left"></i>
							</asp:LinkButton>
							<asp:Label ID="lblCurrentPath" runat="server" Text="Seleccione una carpeta"></asp:Label>
							<div class="header-tags">
								<asp:Repeater ID="rptHeaderTags" runat="server">
									<itemtemplate>
										<span class="tag">
											<%# Container.DataItem %>
										</span>
									</itemtemplate>
								</asp:Repeater>
								<asp:LinkButton ID="btnAddFile" runat="server" CssClass="add-file-button"
									Visible="false">
									<i class="fas fa-upload"></i> Agregar archivo
								</asp:LinkButton>
								<a href="#" id="btnMergePDFs" class="btn-merge" onclick="return openMergeWindow();">
									<i class="fas fa-object-group"></i> Unir en un solo PDF
								</a>
							</div>
						</div>
						<div class="card-body">
							<asp:Repeater ID="rptContents" runat="server" OnItemCommand="rptContents_ItemCommand"
								OnItemDataBound="rptContents_ItemDataBound">
								<headertemplate>
									<div class="item-row-grid" style="font-weight: bold; border-bottom-width: 2px;">
										<div class="item-check"></div>
										<div class="item-name-cell">Nombre</div>
										<div class="item-tags">Tags</div>
										<div class="item-meta">Fecha de Creaci&oacute;n</div>
										<div class="item-actions">Acciones</div>
									</div>
								</headertemplate>
								<itemtemplate>
									<div class="item-row-grid">
										<div class="item-check">
											<input type="checkbox" class="doc-check"
												data-docid='<%# Eval("Id") %>'
												style='<%# Eval("ItemType").ToString() == "Folder" ? "display:none" : "" %>'
												onclick="updateMergeButton()" />
										</div>
										<div class="item-name-cell">
											<i class='<%# Eval("Icon") %>'></i>
											<asp:LinkButton runat="server" CommandName="OpenFolder"
												CommandArgument='<%# Eval("Id") %>'
												Visible='<%# Eval("ItemType").ToString() == "Folder" %>'
												Text='<%# Eval("Name") %>'>
											</asp:LinkButton>
											&nbsp;&nbsp;
											<span class="item-name" runat="server"
												visible='<%# Eval("ItemType").ToString() == "Document" %>'>
												<%# (Eval("UserFilename")==null || Eval("UserFilename")==DBNull.Value ||
													string.IsNullOrWhiteSpace(Eval("UserFilename").ToString()) ?
													Eval("Name") : Eval("UserFilename")) %>
											</span>
										</div>
										<div class="item-tags">
											<asp:Repeater ID="rptTags" runat="server">
												<itemtemplate>
													<span class="tag">
														<%# Container.DataItem %>
													</span>
												</itemtemplate>
											</asp:Repeater>
										</div>
										<div class="item-meta">
											<%# Eval("Created") %>
										</div>
										<div class="item-actions">
											<asp:LinkButton runat="server" data-tip="Compartir"
												OnClientClick='<%# "return openShareDocumentWindow(" + Eval("Id") + ");" %>'
												Visible='<%# Eval("ItemType").ToString() == "Document" %>'>
												<i class="fas fa-share-nodes"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Compartir"
												OnClientClick='<%# "return openShareHierarchyWindow(" + Eval("Id") + ");" %>'
												Visible='<%# Eval("ItemType").ToString() == "Folder" %>'>
												<i class="fas fa-share-nodes"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Renombrar"
												OnClientClick='<%# "return openRenameFolderWindow(" + Eval("Id") + ");" %>'
												Visible='<%# Eval("ItemType").ToString() == "Folder" %>'>
												<i class="fas fa-edit"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Agregar a lista"
												OnClientClick='<%# "return openAddFolderToListWindow(" + Eval("Id") + ");" %>'
												Visible='<%# Eval("ItemType").ToString() == "Folder" %>'>
												<i class="fas fa-plus"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Borrar" CommandName="DeleteFolder"
												CommandArgument='<%# Eval("Id") %>'
												OnClientClick="return confirm('¿Está seguro de borrar esta carpeta y TODO su contenido (subcarpetas y documentos)? Esta acción no se puede deshacer.');"
												Visible='<%# Eval("ItemType").ToString() == "Folder" && (int)Eval("CreatedBy") == UserId %>'>
												<i class="fas fa-trash"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Vista Previa" CommandName="PreviewDocument"
												CommandArgument='<%# Eval("Id") %>'
												Visible='<%# Eval("ItemType").ToString() == "Document" && ((bool)Eval("IsPrivate") == false || ((bool)Eval("IsPrivate") == true && (Eval("UserFilename") ?? "").ToString().EndsWith(".pdf"))) %>'>
												<i class="fas fa-eye"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Descargar" CommandName="DownloadDocument"
												CommandArgument='<%# Eval("Id") %>'
												Visible='<%# Eval("ItemType").ToString() == "Document" %>'>
												<i class="fas fa-download"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Renombrar"
												OnClientClick='<%# "return openRenameDocumentWindow(" + Eval("Id") + ", " + Eval("HierarchyId") +  ");" %>'
												Visible='<%# Eval("ItemType").ToString() == "Document" %>'>
												<i class="fas fa-edit"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Agregar a lista"
												OnClientClick='<%# "return openAddToListWindow(" + Eval("Id") + ");" %>'
												Visible='<%# Eval("ItemType").ToString() == "Document" %>'>
												<i class="fas fa-plus"></i>
											</asp:LinkButton>
											<asp:LinkButton runat="server" data-tip="Borrar" CommandName="DeleteDocument"
												CommandArgument='<%# Eval("Id") %>'
												OnClientClick="return confirm('¿Está seguro de borrar este documento?');"
												Visible='<%# Eval("ItemType").ToString() == "Document" && (int)Eval("CreatedBy") == UserId %>'>
												<i class="fas fa-trash"></i>
											</asp:LinkButton>
										</div>
									</div>
								</itemtemplate>
							</asp:Repeater>
							<div id="empty_folder_message" runat="server" class="empty-message" visible="false">
								<i class="fas fa-folder-open"></i>
								<asp:Label ID="lblEmptyFolder" runat="server" Text="Esta carpeta está vacía.">
								</asp:Label>
							</div>
						</div>
					</div>

					<div id="previewPane" runat="server" class="document-preview-pane card" visible="false"
						style="width: 700px !important; height: 1000px !important;">
						<div class="card-header">
							<asp:LinkButton ID="btnPrev" runat="server" OnClick="btnPrev_Click" Visible="false"
								CssClass="preview-nav-button">
								<i class="fas fa-chevron-left"></i>
							</asp:LinkButton>
							&nbsp;
							&nbsp;
							&nbsp;
							&nbsp;
							<span class="preview-title">Vista Previa del Documento</span>
							&nbsp;
							&nbsp;
							&nbsp;
							&nbsp;
							<asp:LinkButton ID="btnNext" runat="server" OnClick="btnNext_Click" Visible="false"
								CssClass="preview-nav-button">
								<i class="fas fa-chevron-right"></i>
							</asp:LinkButton>
							<asp:HiddenField ID="hfCurrentPreviewId" runat="server" />
						</div>
						<div class="card-body">
							<iframe id="previewFrame" runat="server"
								style="width: 100%; height: 100%; border: none;"></iframe>
						</div>
					</div>
				</div>
			</div>
		</section>
	</asp:Content>