<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Client/ClientMaster.master" CodeBehind="Recruiting.aspx.vb" Inherits="Web.Recruiting" %>
<%@ MasterType VirtualPath="ClientMaster.Master" %>
<%@ Register TagPrefix="ctrl" Namespace="Web" Assembly="Web" %>
<%@ Register src="~/_include/controls/CopyableText.ascx" TagPrefix="ctrl" TagName="CopyableText" %>
<asp:Content ID="Content1" ContentPlaceHolderID="phContent" runat="server">

    <div class="container">
        <h1>Driver Recruiting</h1>
        
<asp:PlaceHolder ID="phApplicationLinks1" runat="server">
        <ul id="tabList" class="nav nav-tabs">
            <li class="nav-item">
                <a class="nav-link active" href="#applications" data-toggle="tab">Applications</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#dpq" data-toggle="tab">Driver Pre-Qualification (DPQ)</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#rejected" data-toggle="tab">Rejected Applications</a>
            </li>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phQualifiedLink" runat="server">
            <li class="nav-item">
                <a class="nav-link" href="#drivers" data-toggle="tab">Qualified Drivers</a>
            </li>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phApplicationLinks2" runat="server">
        </ul>
</asp:PlaceHolder>

<asp:PlaceHolder ID="phApplicationTabs1" runat="server">
        <div class="tab-content bg-white border border-top-0 p-3">

            <!-- Applications tab -->
            <div id="applications" class="tab-pane show active">

                <p>New drivers may start an application for your company at the URL below:</p>
                <div class="input-group mb-3">
                    <asp:TextBox ID="txtAppLink" runat="server" CssClass="form-control" Enabled="false" />
                    <div class="input-group-append">
                        <button id="btnEmailLink" class="btn btn-outline-secondary" type="button" data-toggle="modal" data-target="#dlgEmail">Email Link</button>
                        <button id="btnCopyLink" class="btn btn-outline-secondary" type="button">Copy Link</button>
                    </div>
                </div>
                <div id="dlgEmail" class="modal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">Email Application Link</h5>
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                            </div>
                            <div class="modal-body">
                                <div id="errorResponse" class="alert alert-danger mb-3 d-none"></div>
                                <div class="form-group">
                                    <asp:Label runat="server" Text="Applicant Name:" AssociatedControlID="txtApplicantName" />
                                    <asp:TextBox ID="txtApplicantName" runat="server" MaxLength="100" CssClass="form-control" />
                                    <asp:RequiredFieldValidator runat="server" ControlToValidate="txtApplicantName"
                                        Text="required" Display="Dynamic" CssClass="text-danger" />
                                </div>
                                <div class="form-group">
                                    <asp:Label runat="server" Text="Email address:" AssociatedControlID="txtApplicantEmail" />
                                    <asp:TextBox ID="txtApplicantEmail" runat="server" MaxLength="100" CssClass="form-control" />
                                    <asp:RequiredFieldValidator runat="server" ControlToValidate="txtApplicantEmail"
                                        Text="required" Display="Dynamic" CssClass="text-danger" />
                                </div>
                                <div class="form-group">
                                    <label for="txtPreview">Preview:</label>
                                    <textarea id="txtPreview" runat="server" class="form-control form-control-sm" rows="5" disabled="disabled"></textarea>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                <button id="btnSubmitEmail" type="button" class="btn btn-primary">Send</button>
                            </div>
                        </div>
                    </div>
                </div>
                <script>
                    (function () {
                        var template = $('#<%= txtPreview.ClientID %>').text();
                        function updatePreview() {
                            $('#<%= txtPreview.ClientID %>').text(template
                                .replace('#CONTRACTID#', '<asp:Literal ID="litContractId" runat="server" />')
                                .replace('#CUSTOMERNAME#', '<asp:Literal ID="litCustomerName" runat="server" />')
                                .replace('#DRIVERNAME#', $('#<%= txtApplicantName.ClientID %>').val() || "#DRIVERNAME#")
                                .replace('#STARTURL#', $('#<%= txtAppLink.ClientID %>').val())
                            );
                        };
                        updatePreview();
                        $('#<%= txtApplicantName.ClientID %>').on('input', updatePreview);

                        $('#btnSubmitEmail').click(function (event) {
                            if (!Page_ClientValidate())
                                return;
                            $.ajax({
                                method: 'POST',
                                url: '/Services/internal/EmailAppLink.aspx',
                                data: {
                                    contractId: parseInt('<asp:Literal ID="litContractId2" runat="server" />'),
                                    driverName: $('#<%= txtApplicantName.ClientID %>').val(),
                                    driverEmail: $('#<%= txtApplicantEmail.ClientID %>').val()
                                }
                            })
                            .done(function () {
                                $('#errorResponse').addClass('d-none');
                                $('#<%= txtApplicantName.ClientID %>').val('');
                                $('#<%= txtApplicantEmail.ClientID %>').val('');
                                updatePreview();
                                $('#dlgEmail').modal('hide');
                                $('#btnEmailLink').tooltip({ title: 'Emailed!', placement: 'bottom', trigger: 'manual' }).tooltip('show');
                                setTimeout(function () { $('#btnEmailLink').tooltip('hide') }, 1600);
                            })
                            .fail(function (xhr) {
                                $('#errorResponse').removeClass('d-none').text(xhr.responseText)
                            });
                        });
                        
                        $('#btnCopyLink').click(function (event) {
                            navigator.clipboard.writeText('<%= txtAppLink.Text %>');
                            event.preventDefault();
                            $(this).tooltip({ title: 'Copied!', placement: 'bottom', trigger: 'manual' }).tooltip('show');
                            setTimeout($.proxy(function () { $(this).tooltip('hide') }, this), 1600);
                        });
                    })();
                </script>

                <asp:Repeater ID="repPending" runat="server" DataSourceID="dsPending">
                    <HeaderTemplate>
                        <section class="mb-3">
                            <h2>Application Submissions</h2>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <div class="card" style="width: 20rem;">
                            <div class="card-header"><%# Eval("applicantName") %> <br />
                            <%# Eval("applicantCity") %>, <%# Eval("applicantState") %></div>
                            <div class="card-body">
                                <p>App Id: <%# Eval("id") %></p>
                                <p>Submitted: <%# Eval("dateSubmitted") %></p>
                                <a class="btn btn-primary mr-3" href="SendApplication.aspx?id=<%# Eval("id") %>&cid=<%# Eval("clientId") %>">Send to TCS</a>
                                <a class="card-link" href="ViewApplication.aspx?id=<%# Eval("id") %>&cid=<%# Eval("clientId") %>">View</a>
                                <a class="card-link text-danger" href="RejectApplication.aspx?id=<%# Eval("id") %>&cid=<%# Eval("clientId") %>">Reject</a>
                            </div>
                        </div>
                    </ItemTemplate>
                    <FooterTemplate>
                        </section>
                    </FooterTemplate>
                </asp:Repeater>
                <asp:SqlDataSource ID="dsPending" runat="server"
                    SelectCommand="
                        SELECT id, dateSubmitted, clientId,
	                        applicantNameLast + ', ' + applicantNameFirst + 
	                        CASE WHEN NOT NULLIF(applicantNameMiddle, '') IS NULL THEN ' ' + applicantNameMiddle ELSE '' END +
	                        CASE WHEN NOT NULLIF(applicantNameSuffix, '') IS NULL THEN ', ' + applicantNameSuffix ELSE '' END AS applicantName,
                            ISNULL(web_DriverApplication.applicantCity,'') AS applicantCity,ISNULL(web_DriverApplication.applicantState,'') AS applicantState
                        FROM web_DriverApplication
                        WHERE clientId=@contractId AND status='submitted'
                        ORDER BY dateSubmitted DESC">
                    <SelectParameters>
                        <asp:Parameter Name="contractId" />
                    </SelectParameters>
                </asp:SqlDataSource>    
            </div>

            <!-- Driver Prequal tab -->
            <div id="dpq" class="tab-pane">
                <h2>Driver Pre-Qualification (DPQ)</h2>
                <div class="table-responsive">
                    <asp:GridView ID="gvReview" runat="server" DataSourceID="dsReview" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table text-center floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Applicant" DataField="applicantName" SortExpression="applicantName" />
                            <asp:BoundField HeaderText="Location" DataField="CityState" SortExpression="CityState" />
                            <asp:BoundField HeaderText="Date" DataField="dpqRequestMade" DataFormatString="{0:d}" SortExpression="dpqRequestMade" />
                            <asp:TemplateField HeaderText="Docs" SortExpression="DriverID">
                                <ItemTemplate>
                                    <div class="text-middle">
                                        <a href="DocumentsNew.aspx?ctx=DriverDPQ&id=<%# Eval("DriverID") %>&appid=<%# Eval("AppID") %>&cid=<%# Eval("CID") %>" >
                                            <i class="fa fa-fw fa-file" title="Docs"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <ctrl:PrettyCheckBoxField HeaderText="Application Review" DataField="dpqInitialReviewSent" SortExpression="dpqInitialReviewSent" />
                            <ctrl:PrettyCheckBoxField HeaderText="MVR" DataField="dpqMVRCompleted" SortExpression="dpqMVRCompleted" />
                            <ctrl:PrettyCheckBoxField HeaderText="PSP" DataField="dpqPSPCompleted" SortExpression="dpqPSPCompleted" />
                            <ctrl:PrettyCheckBoxField HeaderText="CBC" DataField="dpqCriminalBackgroundCheckCompleted" SortExpression="dpqCriminalBackgroundCheckCompleted" />
                            <ctrl:PrettyCheckBoxField HeaderText="DTR" DataField="DPQDrugTestResultsReceived" SortExpression="DPQDrugTestResultsReceived" />
                            <ctrl:PrettyCheckBoxField HeaderText="SPH" DataField="dpqSPH3" SortExpression="dpqSPH3" />

                            <asp:TemplateField HeaderText="DACH" SortExpression="DPQDACHStatus">
                                <ItemTemplate>
                                    <div class="text-middle">
                                        <%# If(Eval("DPQDACHStatus") = "Pending", "<i class=""far fa-stop-circle"" title=""Drug & Alcohol Clearinghouse Pre-Employment Full Query Pending""></i>", "") %>
                                        <%# If(Eval("DPQDACHStatus") = "Electronic Consent Requested", "<span style=""font-size: 1em; color: orange;""><i class=""fas fa-exclamation-triangle"" title=""Drug & Alcohol Clearinghouse Pre-Employment Full Query Pending: Please have the applicant provide electronic consent in the Drug & Alcohol Clearinghouse. An applicant cannot drive a commercial motor vehicle until this is completed.""></i></span>", "") %>
                                        <%# If(Eval("DPQDACHStatus") = "DACH Completed - Driver Not Prohibited", "<span style=""font-size: 1em; color: Green;""><i class=""fas fa-check-circle"" title=""Drug & Alcohol Clearinghouse Pre-Employment Full Query Results: Driver Not Prohibited""></i></span>", "") %>
                                        <%# If(Eval("DPQDACHStatus") = "DACH Completed - Driver Prohibited", "<span style=""font-size: 1em; color: red;""><i class=""fas fa-user-slash"" title=""Drug & Alcohol Clearinghouse Pre-Employment Full Query Results: DRIVER PROHIBITED. The applicant cannot drive a commercial motor vehicle for your company until certain requirements are met.""></i></span>", "") %>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                            
                            
                            <ctrl:PrettyCheckBoxField HeaderText="DPQ Process Completed" DataField="dpqComplete" SortExpression="dpqComplete" />


                            <asp:TemplateField HeaderText="Hired?" SortExpression="HireDate">
                                <ItemTemplate>
                                    <div class="text-middle">
                                        <a href="DriverHire.aspx?id=<%# Eval("DriverID") %>&cid=<%# Eval("CID") %>" class="btn btn-outline-success <%# If(Eval("DPQHired") = 0, "", "d-none") %>">
                                            <i class="fas fa-check-square" title="Hired"></i>
                                        </a>
                                        <a href="DriverNotHire.aspx?id=<%# Eval("DriverID") %>&cid=<%# Eval("CID") %>" class="btn btn-outline-danger <%# If(Eval("DPQHired") = 0, "", "d-none") %>">
                                            <i class="fas fa-times" title="Not Hired"></i>
                                        </a>
                                        <%# If(Eval("DPQHired") = 0, "", "HIRED [" & Eval("HireDate") & "]") %>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no applications in review.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsReview" runat="server"
                        SelectCommand="
                            SELECT dpqRequestMade,dpqMVRCompleted,dpqPSPCompleted,DPQCriminalBackgroundCheckCompleted,dpqSPH3,dpqInitialReviewSent,
	                            ISNULL((dpqComplete | dpqComplete_NoEmail),0) dpqComplete,
	                            ISNULL((SELECT TOP 1 CASE WHEN (test.Completed = 1 AND test.results = 1) THEN 1 ELSE 0 END FROM DrugAlcoholTest test WHERE test.DriverID=Driver.ID AND DPQ=1),0) dpqTestCompleted,
	                            LastName + ', ' + FirstName + (CASE WHEN NOT NULLIF(MiddleName, '') IS NULL THEN ' ' + MiddleName ELSE '' END) + (CASE WHEN NOT NULLIF(Suffix, '') IS NULL THEN ', ' + Suffix ELSE '' END) applicantName
                        ,Driver.ID AS DriverID,@ContractID AS CID,Driver.WebAppID AS AppID,ISNULL(DPQDrugTestResultsReceived,0) AS DPQDrugTestResultsReceived,ISNULL(DPQHired,0) AS DPQHired,HireDate,ISNULL(Driver.DPQDACHStatus,'') AS DPQDACHStatus,
                        ISNULL(Driver.City,'') + ', ' + ISNULL(Driver.State,'') AS CityState
                            FROM Driver WHERE contractId=@contractId AND PreQual=1 AND Suspended=0
                            UNION ALL
                            SELECT NULL AS dpqRequestMade,NULL,NULL,NULL,NULL,NULL,
	                            NULL,
                                NULL,
	                            applicantNameLast + ', ' + applicantNameFirst + CASE WHEN NOT NULLIF(applicantNameMiddle, '') IS NULL THEN ' ' + applicantNameMiddle ELSE '' END + CASE WHEN NOT NULLIF(applicantNameSuffix, '') IS NULL THEN ', ' + applicantNameSuffix ELSE '' END AS applicantName
                        ,-1 as DriverID,@ContractID AS CID,ID as AppID,NULL,0,GETDATE(),'' AS DPQDACHStatus,ISNULL(web_DriverApplication.applicantCity,'') + ', ' + ISNULL(web_DriverApplication.applicantState,'') AS CityState
                            FROM web_DriverApplication WHERE clientId=@contractId AND status IN ('in review', 'transfer pending')
                        ORDER BY applicantname,dpqRequestMade
	                    ">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <script>
                        $(document).ready(function () {
                            var headers = $('#<%= gvReview.ClientID %> th');
                            $(headers[2]).attr('title', 'DPQ Request Date');
                            $(headers[5]).attr('title', 'Motor Vehicle Report');
                            $(headers[6]).attr('title', 'Pre-Employment Screening Program Report');
                            $(headers[7]).attr('title', 'Criminal Background Check');
                            $(headers[8]).attr('title', 'Pre-Employment Drug Screen');
                            $(headers[9]).attr('title', 'Safety Performance History Investigation');
                            $(headers[10]).attr('title', 'Drug & Alcohol Clearinghouse Pre-Employment Full Query');
                            headers.filter('[title]').tooltip({ placement: 'top', trigger: 'hover' });
                        });
                    </script>
                </div>
            </div>

            <!-- Rejected Apps tab -->
            <div id="rejected" class="tab-pane">
                <h2>Rejected Applications</h2>
                <div class="table-responsive">
                    <asp:GridView ID="gvPast" runat="server" DataSourceID="dsPast" AutoGenerateColumns="false" 
                        CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Date Submitted" DataField="dateSubmitted" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Applicant" DataField="applicantName" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-middle">
                                        <a href="Documents.aspx?ctx=DriverPQ&did=<%# Eval("DriverID") %>&aid=<%# Eval("AppID") %>&cid=<%# Eval("CID") %>" >
                                            <i class="fa fa-fw fa-file" title="Docs"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no past applications.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsPast" runat="server"
                        SelectCommand="
                            SELECT id, dateSubmitted, clientId,
	                            applicantNameLast + ', ' + applicantNameFirst + 
	                            CASE WHEN NOT NULLIF(applicantNameMiddle, '') IS NULL THEN ' ' + applicantNameMiddle ELSE '' END +
	                            CASE WHEN NOT NULLIF(applicantNameSuffix, '') IS NULL THEN ', ' + applicantNameSuffix ELSE '' END AS applicantName
                                ,-1 as DriverID,@ContractID AS CID,ID as AppID
                            FROM web_DriverApplication
                            WHERE clientId=@contractId AND status='rejected'
                            ORDER BY dateSubmitted DESC">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="phQualifiedTab" runat="server">
            <!-- Qualified Drivers tab -->
            <div id="drivers" class="tab-pane">
                <h2>Qualified Drivers</h2>
                <div class="form-group form-inline">
                    <asp:Label runat="server" AssociatedControlID="ddlAgency" Text="Agency:" />
                    <asp:DropDownList ID="ddlAgency" runat="server" CssClass="form-control ml-3" AutoPostBack="true" />
                </div>
                <div class="table-responsive">
                    <asp:GridView ID="gvDrivers" runat="server" DataSourceID="dsDrivers" AutoGenerateColumns="false" AllowSorting="true" 
                        CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Name" SortExpression="FullName" DataField="FullName" ItemStyle-CssClass="big" />
                        </Columns>
                        <EmptyDataTemplate>
                            There are no pre-qualified drivers currently available.
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsDrivers" runat="server" SelectCommand=
                        "SELECT ID Id, LastName+', '+FirstName+' '+MiddleName+' '+Suffix FullName, @contractId ContractId, ContractID AgencyId
                        FROM Driver
                        WHERE (@agencyId=0 AND ContractID IN (SELECT agencyId FROM ContractAgencyMap WHERE contractId=@contractId))
                           OR (@agencyId<>0 AND ContractID=@agencyId)
                        ORDER BY FullName">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                            <asp:ControlParameter Name="agencyId" ControlID="ddlAgency" PropertyName="SelectedValue" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>
</asp:PlaceHolder>
        
<asp:PlaceHolder ID="phApplicationTabs2" runat="server">
        </div>
</asp:PlaceHolder>

    </div>

</asp:Content>
