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

    <div class="container">
        <h1>Safety Scores</h1>
        
        <div class="table-responsive">
            <asp:GridView ID="gvScores" runat="server" DataSourceID="dsScores" AutoGenerateColumns="false" AllowSorting="true"
                    CssClass="table floating-hscroll" CellSpacing="-1" GridLines="None" data-fixed-columns="true" data-fixed-number="1">
                    <HeaderStyle CssClass="thead-light" />
                <Columns>
                    <asp:BoundField HeaderText="Date" SortExpression="ScoreDate" DataField="ScoreDate" DataFormatString="{0:d}" />
                    <asp:BoundField HeaderText="Score1" SortExpression="Score1" DataField="Score1" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score1Limit" SortExpression="Score1Limit" DataField="Score1Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score7" SortExpression="Score7" DataField="Score7" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score7Limit" SortExpression="Score7Limit" DataField="Score7Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score2" SortExpression="Score2" DataField="Score2" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score2Limit" SortExpression="Score2Limit" DataField="Score2Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score5" SortExpression="Score5" DataField="Score5" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score5Limit" SortExpression="Score5Limit" DataField="Score5Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score4" SortExpression="Score4" DataField="Score4" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score4Limit" SortExpression="Score4Limit" DataField="Score4Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score6" SortExpression="Score6" DataField="Score6" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score6Limit" SortExpression="Score6Limit" DataField="Score6Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score3" SortExpression="Score3" DataField="Score3" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score3Limit" SortExpression="Score3Limit" DataField="Score3Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Score8" SortExpression="Score8" DataField="Score8" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="score" />
                    <asp:BoundField HeaderText="Score8Limit" SortExpression="Score8Limit" DataField="Score8Limit" HeaderStyle-CssClass="d-none" ItemStyle-CssClass="d-none" />
                    <asp:BoundField HeaderText="Notes" SortExpression="Notes" DataField="Notes" />
                </Columns>
                <EmptyDataTemplate>
                    <p>There are no safety scores entered.</p>
                </EmptyDataTemplate>
            </asp:GridView>
            <asp:SqlDataSource ID="dsScores" runat="server" SelectCommand="
                SELECT s.ID,s.ScoreDate,s.Score1,s.Score2,s.Score3,s.Score4,s.Score5,s.Score6,s.Score7,s.Score8,s.Notes,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score1Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score1Hazmat ELSE setup.Score1 END END Score1Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score2Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score2Hazmat ELSE setup.Score2 END END Score2Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score3Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score3Hazmat ELSE setup.Score3 END END Score3Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score4Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score4Hazmat ELSE setup.Score4 END END Score4Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score5Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score5Hazmat ELSE setup.Score5 END END Score5Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score6Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score6Hazmat ELSE setup.Score6 END END Score6Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score7Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score7Hazmat ELSE setup.Score7 END END Score7Limit,
	                CASE WHEN cust.PassengerCarrier=1 THEN setup.Score8Passenger ELSE CASE WHEN cust.HASMAT=1 THEN setup.Score8Hazmat ELSE setup.Score8 END END Score8Limit
                FROM SafetyScore s
                LEFT JOIN ServiceContract sc ON sc.ID=s.ContractID
                LEFT JOIN Customer cust ON cust.ID=sc.CustomerID
                LEFT JOIN SafetyScoreSetup setup ON 1=1
                WHERE s.ScoreDate >= DATEADD(YEAR,-1,GETDATE()) AND s.ContractID=@contractId
                ORDER BY s.ScoreDate DESC,s.ID">
                <SelectParameters>
                    <asp:Parameter Name="contractId" />
                </SelectParameters>
            </asp:SqlDataSource>
            <script>
                $(document).ready(function () {
                    var gvScores = $('#<%= gvScores.ClientID %>');
                    gvScores.find('td').each(function () {
                        if (!$(this).hasClass('score'))
                            return;
                        var value = $(this).text();
                        var limit = $(this).next().text();
                        if (Number.parseInt(value) >= limit)
                            $(this).addClass('text-danger');
                    });
                });
            </script>
        </div>

    </div>

</asp:Content>
