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

    <div class="container">

<asp:PlaceHolder ID="phTabs1" runat="server">
        <ul id="tabList" class="nav nav-tabs">
            <li class="nav-item">
                <a class="nav-link active" href="#vehicles" data-toggle="tab">Vehicles</a>
            </li>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phSchedulesButton" runat="server">
            <li class="nav-item">
                <a class="nav-link" href="#schedules" data-toggle="tab">Maintenance Schedules</a>
            </li>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phRecordsButton" runat="server">
            <li class="nav-item">
                <a class="nav-link" href="#records" data-toggle="tab">Maintenance Records</a>
            </li>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phTabs2" runat="server">
        </ul>

        <div class="tab-content bg-white border border-top-0 p-3">
</asp:PlaceHolder>
        
<asp:PlaceHolder ID="phTabs3" runat="server">
            <!-- Vehicles tab -->
            <div id="vehicles" class="tab-pane show active">
</asp:PlaceHolder>
                <div class="form-group">
                    <asp:CheckBox ID="chkActiveVehicles" runat="server" AutoPostBack="true" Checked="true" CssClass="mr-1" />
                    <asp:Label runat="server" AssociatedControlID="chkActiveVehicles" 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="btnAddVehicle" runat="server" CssClass="btn btn-primary pull-right" Text="Add Vehicle" Width="150px" />

                    </div>
                </div>
        
                <div class="table-responsive">
                    <asp:GridView ID="gvVehicles" runat="server" DataSourceID="dsVehicles" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table clickable-rows table-hover floating-hscroll" CellSpacing="-1" GridLines="None" data-fixed-columns="true" data-fixed-right-number="2" data-fixed-number="1">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Unit" SortExpression="Unit" DataField="Unit" />
                            <asp:BoundField HeaderText="UnitYear" SortExpression="UnitYear" DataField="UnitYear" />
                            <asp:BoundField HeaderText="Make" SortExpression="Make" DataField="Make" />
                            <asp:BoundField HeaderText="Model" SortExpression="Model" DataField="Model" />
                            <asp:BoundField HeaderText="VIN" SortExpression="VIN" DataField="VIN" />
                            <asp:BoundField HeaderText="PAIDue" SortExpression="PAIDue" DataField="PAIDue" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="TagNumber" SortExpression="TagNumber" DataField="TagNumber" />
                            <asp:BoundField HeaderText="TagExpiration" SortExpression="TagExpiration" DataField="TagExpiration" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Type" SortExpression="Type" DataField="Type" />
                            <asp:BoundField HeaderText="Location" SortExpression="Location" DataField="Location" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="DocumentsNew.aspx?ctx=Vehicle&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="RemoveVehicle.aspx?id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" class="btn btn-outline-danger">
                                            <i class="fa fa-trash-alt fa-fw" title="delete"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no vehicles.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsVehicles" runat="server"
                        SelectCommand="
                            SELECT Vehicle.ID, Vehicle.VIN, ContractID,
                                Vehicle.Make, Vehicle.Model, Vehicle.UnitYear, Type.Name AS Type,
                                Vehicle.Unit, Vehicle.PAIDue, Vehicle.TagNumber,
                                CASE WHEN Vehicle.TagExpiration >= '9/9/2099' THEN NULL ELSE Vehicle.TagExpiration END AS TagExpiration
                                ,ISNULL(Vehicle.Location,'') AS Location
                            FROM Vehicle LEFT JOIN VehicleType Type ON Vehicle.TypeID=Type.ID
                            WHERE ContractID=@contractId AND (@activeOnly=0 OR NotInService=0)
                            ORDER BY Unit">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                            <asp:ControlParameter Name="activeOnly" ControlID="chkActiveVehicles" PropertyName="Checked" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
<asp:PlaceHolder ID="phTabs4" runat="server">
            </div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="phSchedulesTab" runat="server">
            <!-- Schedules tab -->
            <div id="schedules" class="tab-pane">
                <asp:Button ID="btnAddSchedule" runat="server" CssClass="btn btn-primary float-right mb-2" Text="Add Schedule" />

                <button type="button" class="btn btn-link" data-toggle="modal" data-target="#dlgGuidance">Guidance</button>
                <div id="dlgGuidance" class="modal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">Maintenance Schedules Guidance</h5>
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                            </div>
                            <div class="modal-body">
                                <iframe id="frmGuidance" src="MaintenanceSchedulesGuidance.aspx" style="width: 100%; height: 20em;"></iframe>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="table-responsive">
                    <asp:GridView ID="gvSchedules" runat="server" DataSourceID="dsSchedules" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table clickable-rows table-hover floating-hscroll" CellSpacing="-1" GridLines="None">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Label" SortExpression="label" DataField="label" />
                            <asp:BoundField HeaderText="Date Schedule" SortExpression="daySchedule" DataField="daySchedule" />
                            <asp:BoundField HeaderText="Mileage Schedule" SortExpression="mileageSchedule" DataField="mileageSchedule" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="RemoveSchedule.aspx?id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" class="btn btn-outline-danger">
                                            <i class="fa fa-trash-alt fa-fw" title="delete"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no maintenance schedules.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsSchedules" runat="server"
                        SelectCommand="
                            SELECT id, isActive, label, contractId,
                                CONVERT(VARCHAR, daySchedule) + ' days' daySchedule, 
                                CONVERT(VARCHAR, mileageSchedule) + ' miles' mileageSchedule,
                                enteredBy, enteredDate, editedBy, editedDate
                            FROM MaintenanceSchedules 
                            WHERE contractId=@contractId AND isActive=1
                            ORDER BY label">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="phRecordsTab" runat="server">
            <!-- Records tab -->
            <div id="records" class="tab-pane">
                <asp:Button ID="btnAddRecord" runat="server" CssClass="btn btn-primary float-right mb-2" Text="Add Service Entry" />
                
                <div class="table-responsive">
                    <asp:GridView ID="gvRecords" runat="server" DataSourceID="dsRecords" AutoGenerateColumns="false" AllowSorting="true"
                        CssClass="table clickable-rows table-hover floating-hscroll" CellSpacing="-1" GridLines="None" data-fixed-columns="true" data-fixed-right-number="1">
                        <HeaderStyle CssClass="thead-light" />
                        <Columns>
                            <asp:BoundField HeaderText="Unit" SortExpression="unit" DataField="unit" />
                            <asp:BoundField HeaderText="Service Date" SortExpression="serviceDate" DataField="serviceDate" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Next Service Date" SortExpression="nextServiceDate" DataField="nextServiceDate" DataFormatString="{0:d}" />
                            <asp:BoundField HeaderText="Service Type" SortExpression="label" DataField="label" HeaderStyle-Width="200px" />
                            <asp:BoundField HeaderText="Vendor/Mechanic" SortExpression="mechanic" DataField="mechanic" />
                            <asp:BoundField HeaderText="Description" SortExpression="description" DataField="description" />
                            <asp:BoundField HeaderText="Cost" SortExpression="cost" DataField="cost" DataFormatString="{0:c}" />
                            <asp:BoundField HeaderText="Odometer" SortExpression="miles" DataField="miles" />
                            <asp:TemplateField HeaderText="">
                                <ItemTemplate>
                                    <div class="text-right">
                                        <a href="RemoveServiceItem.aspx?id=<%# Eval("id") %>&cid=<%# Eval("ContractID") %>" class="btn btn-outline-danger">
                                            <i class="fa fa-trash-alt fa-fw" title="delete"></i>
                                        </a>
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <p>There are no maintenance records.</p>
                        </EmptyDataTemplate>
                    </asp:GridView>
                    <asp:SqlDataSource ID="dsRecords" runat="server"
                        SelectCommand="
                            SELECT vsi.id, vsi.contractId, ISNULL(v.Unit, '(All)') unit,
                                vsi.serviceDate, vsi.nextServiceDate, vsi.label, vsi.description, vsi.cost, vsi.miles,
                                vsi.mechanic, vsi.enteredDate, vsi.enteredBy, vsi.editedDate, vsi.editedBy
                            FROM VehicleServiceItems vsi LEFT JOIN Vehicle v ON v.id=vsi.vehicleId
                            WHERE v.ContractId=@contractId or vsi.contractId=@contractId
                            ORDER BY vsi.serviceDate DESC">
                        <SelectParameters>
                            <asp:Parameter Name="contractId" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </div>
            </div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="phTabs5" runat="server">
        </div>
</asp:PlaceHolder>

    </div>

</asp:Content>
