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

    <div class="container">

        <h1>Drivers</h1>

        <div class="form-group">
            <asp:CheckBox ID="chkActive" runat="server" AutoPostBack="true" Checked="true" CssClass="mr-1" />
            <asp:Label runat="server" AssociatedControlID="chkActive" Text="Active Only" />

                    <div class="float-right">
                        <asp:Button ID="btnExport" runat="server" CssClass="btn btn-primary pull-right" Text="Export to Excel" Width="150px" />
                        <span style="width:3em;"> </span>
                        <asp:Button ID="btnAdd" runat="server" CssClass="btn btn-primary pull-right" Text="Add Driver" Width="150px" />
                    </div>

        </div>

        <div class="table-responsive">
            <asp:GridView ID="gvDrivers" runat="server" DataSourceID="dsDrivers" AutoGenerateColumns="false" AllowSorting="true"
                CssClass="table clickable-rows table-hover floating-hscroll" CellSpacing="-1" GridLines="None" data-fixed-columns="true" data-fixed-number="2">
                <HeaderStyle CssClass="thead-light" />
                <Columns>
                    <asp:BoundField HeaderText="First Name" SortExpression="FirstName" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" SortExpression="LastName" DataField="LastName" />
                    <asp:BoundField HeaderText="Hire Date" SortExpression="HireDate" DataField="HireDate" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="DL Number" SortExpression="DLNumber" DataField="DLNumber" />
                    <asp:BoundField HeaderText="DL State" SortExpression="DLState" DataField="DLState" />
                    <asp:BoundField HeaderText="DL Expire" SortExpression="DLExpire" DataField="DLExpire" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="MC Expire" SortExpression="MCExpire" DataField="MCExpire" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="MVR Expire" SortExpression="MVRExpire" DataField="MVRExpire" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="COV Expire" SortExpression="COVExpire" DataField="COVExpire" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="DACH Expire" SortExpression="AnnualDashExpire" DataField="AnnualDashExpire" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="Location" SortExpression="Location" DataField="Location" />
                    <asp:HyperLinkField DataNavigateUrlFields="id,ContractID" DataNavigateUrlFormatString="MoveDriver.aspx?id={0}&cid={1}" Text="Move" />
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <div class="text-right">
                                <a href="DocumentsNew.aspx?ctx=Driver&id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" >
                                    <i class="fa fa-fw fa-file"></i>
                                </a>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <div class="text-right">
                                <a href="RemoveDriver.aspx?id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" class="btn btn-outline-danger <%# If(Eval("Terminated") = 0, "", "d-none") %>">
                                    <i class="fa fa-trash-alt fa-fw" title="delete"></i>
                                </a>
                                <small><%# If(Eval("Terminated") = 0, "", "(terminated)") %></small>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <p>There are no drivers.</p>
                </EmptyDataTemplate>
            </asp:GridView>
            <asp:SqlDataSource ID="dsDrivers" runat="server"
                SelectCommand="
                    SELECT ID, ContractID,
                        FirstName, MiddleName, LastName,
                        'xxx-xx-' + RIGHT(SSN, 4) AS SSN,
                        (   CASE WHEN CDL=1 THEN 'CDL' ELSE '' END +
		                    CASE WHEN H=1 THEN '/H' ELSE '' END +
		                    CASE WHEN T=1 THEN '/T' ELSE '' END +
		                    CASE WHEN N=1 THEN '/N' ELSE '' END +
		                    CASE WHEN X=1 THEN '/X' ELSE '' END +
		                    CASE WHEN P=1 THEN '/P' ELSE '' END +
		                    CASE WHEN S=1 THEN '/S' ELSE '' END
	                        ) AS Endorsements,
                        CASE WHEN HireDate >= '9/9/9998' THEN NULL ELSE HireDate END HireDate, 
                        CASE WHEN DOB >= '9/9/9998' THEN NULL ELSE DOB END DOB, 
                        Terminated,
                        Location,
                        DLNumber,DLState,
                        CASE WHEN DLExpire >= '9/9/9998' THEN NULL ELSE DLExpire END DLExpire, 
                        CASE WHEN MCExpire >= '9/9/9998' THEN NULL ELSE MCExpire END MCExpire, 
                        CASE WHEN MVRExpire >= '9/9/9998' THEN NULL ELSE MVRExpire END MVRExpire, 
                        CASE WHEN COVExpire >= '9/9/9998' THEN NULL ELSE COVExpire END COVExpire, 
                        CASE WHEN AnnualDashExpire >= '9/9/9998' THEN NULL ELSE AnnualDashExpire END AnnualDashExpire,
	                    CASE WHEN (SELECT COUNT(id) FROM DocumentMap WHERE MapType='Driver' AND MapID=Driver.ID) > 0 THEN 'show docs' ELSE '' END ShowDocsText
                    FROM Driver
                    WHERE ContractID=@contractId AND (@activeOnly=0 OR Terminated=0)
                    AND ((ISNULL(DRIVER.PREQUAL,0)=0 ) OR (ISNULL(DRIVER.PREQUAL,0)=1 AND Driver.HireDate < '9/9/9998'))

                    ORDER BY LastName, FirstName, MiddleName">
                <SelectParameters>
                    <asp:Parameter Name="contractId" />
                    <asp:ControlParameter Name="activeOnly" ControlID="chkActive" PropertyName="Checked" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>

    </div>

</asp:Content>
