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

    <div class="container">
        <h1>Drug &amp; Alcohol Tests</h1>
        <asp:Button ID="btnSchedule" runat="server" CssClass="btn btn-primary mb-3" Text="Schedule Test" />
        
        <!-- Filter -->
        <div class="form-group form-inline">
            <asp:Label runat="server" AssociatedControlID="txtPendingStart" CssClass="mr-3" Text="Date Range:" />
            <div class="input-group">
                <asp:TextBox ID="txtPendingStart" runat="server" MaxLength="50" AutoPostBack="true" CssClass="form-control" TextMode="Date" />
                <asp:Label runat="server" AssociatedControlID="txtPendingTo" CssClass="input-group-text border-left-0 border-right-0 rounded-0" Text="To" />
                <asp:TextBox ID="txtPendingTo" runat="server" MaxLength="50" AutoPostBack="true" CssClass="form-control" TextMode="Date" />
            </div>
        </div>

        <!-- Tabs -->
        <ul id="tabList" class="nav nav-tabs">
            <li class="nav-item">
                <a class="nav-link active" href="#awaitingTesting" data-toggle="tab">Awaiting Testing</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#pendingResults" data-toggle="tab">Pending Results</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#completed" data-toggle="tab">Completed Tests</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#notTested" data-toggle="tab">Not Tested</a>
            </li>
        </ul>

        <div class="tab-content bg-white border border-top-0 p-3">

            <!-- Awaiting testing tab -->
            <div id="awaitingTesting" class="tab-pane show active">
                <div class="table-responsive">
                    <asp:GridView ID="gvAwaitingTests" runat="server" DataSourceID="dsAwaitingTests" AutoGenerateColumns="false"
                        CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Date" SortExpression="TestDate" DataField="TestDate" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Name" SortExpression="DriverName" DataField="DriverName" />
                            <asp:BoundField HeaderText="Type" SortExpression="TestType" DataField="TestType" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="DocumentsNew.aspx?ctx=DrugAlcoholTest&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="RequestTest.aspx?tid=<%# Eval("ID") %>&cid=<%# Eval("ContractID") %>" class="btn btn-primary <%# Eval("ScheduleCss") %>">
                                            Schedule Test
                                        </a>
                                        <a href="#" class="btn btn-secondary not-tested-button" data-id="<%# Eval("ID") %>">
                                            Not Tested
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no tests in the date range that are awaiting testing.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsAwaitingTests" runat="server"
                        SelectCommand="
                            SELECT Test.ID, Test.TestDate, Test.DriverName, Type.Name AS TestType, ContractID,
                                CASE WHEN Type.IsDrugTest = 1 THEN '' ELSE 'd-none' END ScheduleCss
                            FROM DrugAlcoholTest Test LEFT JOIN DrugAlcoholTestType Type ON Test.TestTypeID=Type.ID
                            WHERE Status='Awaiting Test'
                            AND ContractID=@contractId AND TestDate>=TRY_PARSE(@filterStart AS DATE) AND TestDate<=TRY_PARSE(@filterEnd AS DATE)">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                            <asp:ControlParameter Name="filterStart" ControlId="txtPendingStart" PropertyName="Text" DefaultValue="1970-01-01" ConvertEmptyStringToNull="true" />
                            <asp:ControlParameter Name="filterEnd" ControlId="txtPendingTo" PropertyName="Text" DefaultValue="2999-01-01" ConvertEmptyStringToNull="true" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
                <div id="dlgNotTested" class="modal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">Reason Not Being Tested</h5>
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                            </div>
                            <div class="modal-body">
                                <p>Guidance Regarding Selecting Alternates: It is permissible to select alternates. However, it is only permissible if the primary driver selected will not be available for testing during the selection period because of long-term absence due to layoff, illness, injury, vacation or other circumstances. In the event the initial driver selected is not available for testing, the employer and/or C/TPA must document the reason why an alternate driver was tested. The documentation must be maintained and readily available when requested by the Secretary of Transportation, any DOT agency, or any State or local officials with regulatory authority over the employer or any of its drivers.</p>
                                <div id="errorResponse" class="alert alert-danger mb-3 d-none">test</div>
                                <div class="form-group">
                                    <label for="txtReason">Reason Not Being Tested:</label>
                                    <textarea id="txtReason" class="form-control"></textarea>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                <button id="btnSubmitNotTested" type="button" class="btn btn-primary">Submit</button>
                            </div>
                        </div>
                    </div>
                </div>
                <script>
                    $(document).ready(function () {

                        $('.not-tested-button').click(function () {
                            var id = $(this).data('id');
                            $('#dlgNotTested').data('id', id);
                            $('#dlgNotTested').modal('show');
                        });

                        $('#btnSubmitNotTested').click(function () {
                            var reason = $('#txtReason').val();
                            if (reason.match(/^\s*$/) !== null) {
                                $('#errorResponse').removeClass('d-none').text('Reason for not being tested is required.');
                                return;
                            }

                            $.ajax({
                                method: 'POST',
                                url: '/Services/internal/SetNotTested.aspx',
                                data: {
                                    testId: $('#dlgNotTested').data('id'),
                                    reason: reason
                                }
                            })
                                .done(function () {
                                    location.reload();
                                })
                                .fail(function (xhr) {
                                    $('#errorResponse').removeClass('d-none').text(xhr.responseText)
                                })
                        });

                    });
                </script>
            </div>

            <!-- Pending Results tab -->
            <div id="pendingResults" class="tab-pane">
                <div class="table-responsive">
                    <asp:GridView runat="server" DataSourceID="dsAwaitingResults" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Date" SortExpression="TestDate" DataField="TestDate" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Name" SortExpression="DriverName" DataField="DriverName" />
                            <asp:BoundField HeaderText="Type" SortExpression="TestType" DataField="TestType" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="DocumentsNew.aspx?ctx=DrugAlcoholTest&id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" >
                                            <i class="fa fa-fw fa-file"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no completed drug &amp; alcohol tests in the date range.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsAwaitingResults" runat="server"
                        SelectCommand="
                            SELECT Test.ID, Test.TestDate, Test.DriverName, Type.Name AS TestType, Status AS TestResult, ContractID
                            FROM DrugAlcoholTest Test LEFT JOIN DrugAlcoholTestType Type ON Test.TestTypeID=Type.ID
                            WHERE (Status='Awaiting Test Results' OR Status='Scheduled')
                            AND ContractID=@contractId AND TestDate>=TRY_PARSE(@filterStart AS DATE) AND TestDate<=TRY_PARSE(@filterEnd AS DATE)">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                            <asp:ControlParameter Name="filterStart" ControlId="txtPendingStart" PropertyName="Text" DefaultValue="1970-01-01" ConvertEmptyStringToNull="true" />
                            <asp:ControlParameter Name="filterEnd" ControlId="txtPendingTo" PropertyName="Text" DefaultValue="2999-01-01" ConvertEmptyStringToNull="true" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>

            <!-- Completed Tests tab -->
            <div id="completed" class="tab-pane">
                <div class="table-responsive">
                    <asp:GridView ID="gvCompleted" runat="server" DataSourceID="dsCompleted" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table clickable-rows table-hover floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Date" SortExpression="TestDate" DataField="TestDate" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Name" SortExpression="DriverName" DataField="DriverName" />
                            <asp:BoundField HeaderText="Type" SortExpression="TestType" DataField="TestType" />
                            <asp:BoundField HeaderText="Status" SortExpression="Status" DataField="Status" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="DocumentsNew.aspx?ctx=DrugAlcoholTest&id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" >
                                            <i class="fa fa-fw fa-file"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no tests in the date range that are completed.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsCompleted" runat="server"
                        SelectCommand="
                            SELECT Test.ID, Test.TestDate, Test.DriverName, Test.Status, Type.Name AS TestType, ContractID
                            FROM DrugAlcoholTest Test LEFT JOIN DrugAlcoholTestType Type ON Test.TestTypeID=Type.ID
                            WHERE Completed=1
                            AND ContractID=@contractId AND TestDate>=TRY_PARSE(@filterStart AS DATE) AND TestDate<=TRY_PARSE(@filterEnd AS DATE)">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                            <asp:ControlParameter Name="filterStart" ControlId="txtPendingStart" PropertyName="Text" DefaultValue="1970-01-01" ConvertEmptyStringToNull="true" />
                            <asp:ControlParameter Name="filterEnd" ControlId="txtPendingTo" PropertyName="Text" DefaultValue="2999-01-01" ConvertEmptyStringToNull="true" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>

            <!-- Not Tested tab -->
            <div id="notTested" class="tab-pane">
                <div class="table-responsive">
                    <asp:GridView runat="server" DataSourceID="dsNotTested" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Date" SortExpression="TestDate" DataField="TestDate" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Name" SortExpression="DriverName" DataField="DriverName" />
                            <asp:BoundField HeaderText="Type" SortExpression="TestType" DataField="TestType" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="DocumentsNew.aspx?ctx=DrugAlcoholTest&id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" >
                                            <i class="fa fa-fw fa-file"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no tests in the date range that are not tested.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsNotTested" runat="server"
                        SelectCommand="
                            SELECT Test.ID, Test.TestDate, Test.DriverName, Type.Name AS TestType, ContractID
                            FROM DrugAlcoholTest Test LEFT JOIN DrugAlcoholTestType Type ON Test.TestTypeID=Type.ID
                            WHERE Status='Not Tested'
                            AND ContractID=@contractId AND TestDate>=TRY_PARSE(@filterStart AS DATE) AND TestDate<=TRY_PARSE(@filterEnd AS DATE)">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                            <asp:ControlParameter Name="filterStart" ControlId="txtPendingStart" PropertyName="Text" DefaultValue="1970-01-01" ConvertEmptyStringToNull="true" />
                            <asp:ControlParameter Name="filterEnd" ControlId="txtPendingTo" PropertyName="Text" DefaultValue="2999-01-01" ConvertEmptyStringToNull="true" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>

        </div>

    </div>

</asp:Content>
