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

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
	<style>
		.search-container {
			margin: 0 0 1rem;
			padding: 0.6rem 1rem;
			background-color: #fff;
			border-radius: 8px;
			box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
			display: flex;
			align-items: center;
			gap: 0.75rem;
			flex-wrap: wrap;
		}

		.search-box {
			display: flex;
			align-items: center;
			gap: 0.5rem;
			flex: 1;
			min-width: 250px;
		}

		.search-box input[type="text"] {
			flex: 1;
			padding: 6px 12px;
			border: 1px solid #ccc;
			border-radius: 4px;
			font-size: 0.9rem;
		}

		.search-box input[type="submit"] {
			padding: 6px 20px;
			background: var(--wine-dark, #6D214F);
			color: #fff;
			border: none;
			border-radius: 4px;
			cursor: pointer;
			font-size: 0.9rem;
		}

		.search-box input[type="submit"]:hover { opacity: 0.9; }

		.tag-options {
			display: flex;
			align-items: center;
			gap: 0.75rem;
			font-size: 0.85rem;
			color: #555;
			border-left: 1px solid #ddd;
			padding-left: 0.75rem;
		}

			.tag-options table { /* RadioButtonList table */
				display: flex;
				gap: 0.5rem;
			}

			.tag-options table td { padding: 0 4px; }

		.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;
		}

		/* Search results grid: 6 columns */
		#page-search .item-row-grid {
			grid-template-columns: 2fr 2fr 0.8fr 1fr 1fr 2.2fr;
		}

		#page-search .item-actions a {
			font-size: 0.9rem;
		}

		.ua-badge {
			display: inline-block;
			background: var(--wine-dark, #6D214F);
			color: #fff;
			padding: 2px 8px;
			border-radius: 3px;
			font-size: 0.75rem;
			font-weight: bold;
		}
	</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
	<script type="text/javascript">
		function openShareDocumentWindow(documentId) {
			PDIModal.open("ShareDocumentFromSearch", "ShareDocumentWindow.aspx?DocumentId=" + documentId, "Compartir Documento", "600px", "400px");
			return false;
		}

		function openAddToListWindow(documentId) {
			PDIModal.open("AddToList", "AddToListWindow.aspx?DocumentId=" + documentId, "Agregar Documento a Lista", "500px", "600px");
			return false;
		}

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

		document.addEventListener("DOMContentLoaded", function() {
			PDIModal.onClose("RenameDocument", function() { window.location.reload(); });
		});
	</script>

	<section id="page-search" class="page">
		<h2>Búsqueda Avanzada</h2>

		<div class="search-container">
			<div class="search-box">
				<asp:TextBox ID="txtSearchQuery" runat="server" placeholder="Buscar documentos..." />
				<asp:Button ID="btnSearch" runat="server" Text="Buscar" OnClick="btnSearch_Click" />
			</div>
			<div class="tag-options">
				<asp:CheckBox ID="chkFilterByTag" runat="server" Text="Filtro por tag" AutoPostBack="true" OnCheckedChanged="chkFilterByTag_CheckedChanged" />
				<asp:RadioButtonList ID="rblTagSearchOptions" runat="server" Visible="false" RepeatDirection="Horizontal">
					<asp:ListItem Value="Also" Text="Tambi&eacute;n en tags" Selected="True" />
					<asp:ListItem Value="Only" Text="Solo en tags" />
				</asp:RadioButtonList>
			</div>
		</div>

		<div class="main-content-area">
			<div class="search-results-container card">
				<div class="card-header">
					Resultados
				</div>
				<div class="card-body">
					<asp:Repeater ID="rptSearchResults" runat="server" OnItemCommand="rptSearchResults_ItemCommand" OnItemDataBound="rptSearchResults_ItemDataBound">
						<HeaderTemplate>
							<div class="item-row-grid" style="font-weight: bold; border-bottom-width: 2px;">
								<div class="item-name-cell">Nombre</div>
								<div class="item-meta">Ruta</div>
								<div>UA</div>
								<div class="item-tags">Tags</div>
								<div class="item-meta">Creaci&oacute;n</div>
								<div class="item-actions">Acciones</div>
							</div>
						</HeaderTemplate>
						<ItemTemplate>
							<div class="item-row-grid">
								<div class="item-name-cell">
									<i class='<%# Eval("ItemType").ToString() == "Hierarchy" ? "fas fa-folder" : "fas fa-file-alt" %>'></i>
									<asp:HyperLink runat="server"
										NavigateUrl='<%# "~/DocumentsView.aspx?id=" + Eval("Id") %>'
										Visible='<%# Eval("ItemType").ToString() == "Hierarchy" %>'
										Text='<%# Eval("Name") %>'></asp:HyperLink>
									<span 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-meta">
									<%# Eval("Path") %>
								</div>
								<div>
									<span class="ua-badge" runat="server" visible='<%# !string.IsNullOrEmpty((string)Eval("UANombreCorto")) %>'><%# Eval("UANombreCorto") %></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">
									<%# ((DateTime)Eval("Created")).ToString("yyyy-MM-dd HH:mm") %>
								</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="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>
								</div>
							</div>
							<div class="ocr-snippet" runat="server" visible='<%# !string.IsNullOrEmpty((string)Eval("OcrSnippet")) %>'>
								<asp:Literal runat="server" Mode="PassThrough" Text='<%# Eval("OcrSnippet") %>'></asp:Literal>
							</div>
						</ItemTemplate>
						<FooterTemplate>
							<% if (rptSearchResults.Items.Count == 0)
								{ %>
							<div class="empty-message">
								<i class="fas fa-search"></i>
								<p>No se encontraron resultados. Intente con otros términos de búsqueda.</p>
							</div>
							<% } %>
						</FooterTemplate>
					</asp:Repeater>
				</div>
			</div>

			<div id="previewPane" runat="server" class="search-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>
	</section>
</asp:Content>
