<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Client/ClientMaster.master" CodeBehind="DocumentsNew.aspx.vb" Inherits="Web.DocumentsNew" %>
<%@ MasterType VirtualPath="ClientMaster.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="phContent" runat="server">

    <div class="container">
        <h1><asp:Literal ID="litContext" runat="server" /> Docs</h1>
        <p>Name: <asp:Literal ID="litItemName" runat="server" /></p>
        <asp:Button ID="btnCancel" runat="server" CausesValidation="false" Text="Cancel" CssClass="btn btn-secondary mb-3" />
        <asp:Button ID="btnDownload" runat="server" CausesValidation="false" Text="Download All" CssClass="btn btn-secondary mb-3" />
        <asp:Button ID="btnUploadDoc" runat="server" CausesValidation="false" Text="Upload Document" CssClass="btn btn-secondary mb-3" />

        <div class="table-responsive">
            <asp:GridView ID="gvDocs" runat="server" DataSourceID="dsDocs" AutoGenerateColumns="false" AllowSorting="true"
                CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None">
                <HeaderStyle CssClass="thead-light" />
                <Columns>
                    <asp:BoundField HeaderText="Document Type" SortExpression="DocSubCategory" DataField="DocSubCategory" />
                    <asp:BoundField HeaderText="File name" SortExpression="DocName" DataField="DocName" />
                    <asp:BoundField HeaderText="Date" SortExpression="EnteredDate" DataField="EnteredDate" DataFormatString="{0:d}" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                      <asp:HyperLink ID="lnkUpdate" NavigateUrl='<%#String.Format("DocumentEdit.aspx?cid={0}&docid={1}&ctx={2}&id={3}", Eval("ContractID"), Eval("ID"), Eval("Ctx"), Eval("ItemID"))%>'   runat="server" Text='<%#Eval("EditText") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>

                    <asp:HyperLinkField DataNavigateUrlFields="ctx2,ID" DataNavigateUrlFormatString="/_assets/Document.aspx?context={0}&id={1}" Target="_blank" Text="View" />
                </Columns>
                <EmptyDataTemplate>
                    <p>There are no documents attached.</p>
                </EmptyDataTemplate>
            </asp:GridView>
            <asp:SqlDataSource ID="dsDocs" runat="server" SelectCommand="
SELECT DocumentsNew.ContractID,DocumentsNew.ID,DocumentsNew.DocName,ISNULL(DocumentsNew.ItemID,0) AS ItemID,DocumentsNew.EnteredDate,ISNULL(DocumentsSubCategory.Name,'') AS DocSubCategory,DocumentsNew.UploadNotes,@ctx as Ctx,'' as pageUrl,'DocumentNew' as ctx2,0 AS OrderByField,'Edit' AS EditText
FROM DocumentsNew LEFT JOIN DocumentsSubCategory ON DocumentsNew.SubCategoryID=DocumentsSubCategory.Id
WHERE (@ItemID=0 OR (@ItemID<>0 AND DocumentsNew.ItemID=@ItemId)) AND DocumentsNew.ContractID=@ContractID AND DocumentsNew.CategoryID=@CatID AND (ISNULL(DocumentsNew.Deleted,0) = 0) 
UNION ALL
SELECT ContractID,WebAppId AS ID,'All application documents' DocName,ID AS Itemid,EnteredDate,'Web Application' AS DocSubCategory, '' AS UploadNotes,'Driver' AS Ctx,CONCAT('/_assets/Document.aspx?context=FullApplication&id=', WebAppId) pageUrl,'FullApplication' AS ctx2,0 AS OrderByField,'' AS EditText
FROM Driver WHERE ID=@ItemID AND ISNULL(webAppID,0)>0 AND @ctx IN ('Driver','DriverDPQ')
UNION ALL
Select ContractID,web_DriverApplicationPDF.ID AS ID,pageTitle AS DOcName,Driver.ID AS ItemID,Driver.EnteredDate,'Web Application' AS DocSubCategory,'' AS UploadNotes,'Driver' AS CTX,CONCAT('/_assets/Document.aspx?context=Application&id=', web_DriverApplicationPDF.id) pageUrl,'Application' AS Ctx2,web_DriverApplicationPDF.ID AS OrderByField,'' AS EditText
FROM Driver LEFT JOIN web_DriverApplicationPDF ON Driver.WebAppId=web_DriverApplicationPdf.appId
WHERE Driver.ID=@ItemID And Not web_DriverApplicationPdf.ID Is NULL AND @ctx IN ('Driver','DriverDPQ')
ORDER BY OrderByField,DocSubCategory,DocName,ID                
                ">
                <SelectParameters>
                    <asp:Parameter Name="CatID" />
                    <asp:Parameter Name="ItemId" />
                    <asp:Parameter Name="ContractId" />
                    <asp:Parameter Name="ctx" />
                    <asp:Parameter Name="appID" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
    </div>

</asp:Content>