<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" encoding="utf-8" indent="yes"/>

    <!-- Parameters -->

    <xsl:param name="VERSION">1.0</xsl:param>
    <xsl:param name="PAGE-TITLE">Permits</xsl:param>
    <xsl:param name="PAGE-NAME">managePermits</xsl:param>

    <!-- Variables --> 

    <xsl:variable name="PAGINATION_COUNT" select="//permits/@count"/>
    <xsl:variable name="PAGINATION_START" select="//permits/@start"/>
    <xsl:variable name="PAGINATION_END"   select="//permits/@end"/>
    <xsl:variable name="PAGINATION_PAGE"  select="//permits/@page"/>

    <!-- Main Template -->

    <xsl:template match="/">

        <script src="js/{$PAGE-NAME}.js?version={$VERSION}"/>   

        <div class="{$PAGE-NAME}">
      
            <div class="card">
        
                <div class="card-header bg-primary text-white p-1 ps-3">
                    <i class="bi bi-tools me-2"/>
                    <xsl:value-of select="$PAGE-TITLE"/>
                    <xsl:if test="$PAGINATION_COUNT > 0">
                        <xsl:text> | Showing </xsl:text>
                        <xsl:value-of select="format-number($PAGINATION_START, '###,##0')"/>
                        <xsl:text> to </xsl:text>
                        <xsl:value-of select="format-number($PAGINATION_END, '###,##0')"/>
                        <xsl:text> of </xsl:text>
                        <xsl:value-of select="format-number($PAGINATION_COUNT, '###,##0')"/>
                    </xsl:if>
                </div>
        
                <div class="card-body">

                    <div class="row align-items-center mt-2 mb-2">
            
                        <div class="col-lg-4 mb-2">
                            <div class="input-group input-group-sm">
                                <input type="text" class="form-control autosave" name="find_permit" id="find_permit" maxlength="100" autocomplete="off" placeholder="{//popovers/record[popover_name = 'find_permit']/popover_placeholder}" autofocus="autofocus"/>
                                <button type="button" class="btn btn-primary fw-bold btnFind" data-bs-toggle="tooltip" data-bs-title="{//popovers/record[popover_name = 'find_permit']/popover_text}" data-bs-html="true" data-bs-custom-class="custom-tooltip">
                                    <i class="bi bi-search"/>
                                </button>
                                <button type="button" class="btn btn-primary fw-bold btnClearFind" data-bs-toggle="tooltip" data-bs-title="Clear Find" data-bs-custom-class="custom-tooltip">
                                    <i class="bi bi-arrow-clockwise"/>
                                </button>
                            </div>
                        </div>

                        <div class="col-auto mb-2">
                            <div class="input-group input-group-sm" data-bs-toggle="tooltip" data-bs-title="Show Per Page" data-bs-custom-class="custom-tooltip">
                                <div class="input-group-text">
                                    <i class="bi bi-list-ol"/>
                                </div>
                                <select class="form-select" name="permits_per_page" id="permits_per_page">
                                    <option value="20">20</option>
                                    <option value="40">40</option>
                                    <option value="60">60</option>
                                    <option value="80">80</option>
                                    <option value="100">100</option>
                                </select>
                            </div>
                        </div>

                    </div>
 
                    <!-- Action Buttons --> 
      
                    <div class="buttons mt-1 mb-1">
            
                        <xsl:if test="not($USER_ROLE = 'Guest')">
                            <button type="button" class="btn btn-sm btn-primary me-2 mb-2 btnAdd">
                                <i class="bi bi-plus-lg me-2"/>Add</button>
                        </xsl:if>
                        
                        <button type="button" class="btn btn-sm btn-primary me-2 mb-2 btnDone">
                            <i class="bi bi-box-arrow-left me-2"/>Done</button>
                       
                        <button type="button" class="btn btn-sm btn-primary me-2 mb-2 btnRefresh">
                            <i class="bi bi-arrow-clockwise me-2"/>Refresh</button>
                            
                        <xsl:if test="count(//permits/record) > 0">
                            <button type="button" class="btn btn-sm btn-primary me-2 mb-2 btnSelectAll"  >
                                <i class="bi bi-check2-square me-2"/>Select All</button>
                        </xsl:if>
                        
                        <button type="button" class="btn btn-sm btn-primary me-2 mb-2 btnDeselectAll d-none">
                            <i class="bi bi-square me-2"/>Deselect All</button>
                            
                        <button type="button" class="btn btn-sm btn-primary me-2 mb-2 btnPrintSelected d-none">
                            <i class="bi bi-printer me-2"/>Print Selected</button>

                        <div class="btn-group ms-2 mb-2" role="group">
                            <xsl:if test="$PAGINATION_PAGE > 1">
                                <button type="button" class="btn btn-sm btn-primary btnPagination" data-pagination="first" data-bs-toggle="tooltip" data-bs-title="First Page" data-bs-custom-class="custom-tooltip">
                                    <i class="bi bi-chevron-bar-left me-1"/>First</button>
                                <button type="button" class="btn btn-sm btn-primary btnPagination" data-pagination="previous" data-bs-toggle="tooltip" data-bs-title="Previous Page" data-bs-custom-class="custom-tooltip">
                                    <i class="bi bi-chevron-left me-1"/>Previous</button>
                            </xsl:if>
                            <xsl:if test="$PAGINATION_COUNT > $PAGINATION_END">
                                <button type="button" class="btn btn-sm btn-primary btnPagination" data-pagination="next" data-bs-toggle="tooltip" data-bs-title="Next Page" data-bs-custom-class="custom-tooltip">
                                    <i class="bi bi-chevron-right me-1"/>Next</button>
                                <button type="button" class="btn btn-sm btn-primary btnPagination" data-pagination="last" data-bs-toggle="tooltip" data-bs-title="Last Page" data-bs-custom-class="custom-tooltip">
                                    <i class="bi bi-chevron-bar-right me-1"/>Last</button>
                            </xsl:if>
                        </div>
            
                    </div>

                    <hr class="mt-2 mb-2"/> 

                    <xsl:choose>
            
                        <xsl:when test="count(//permits/record) = 0">
          
                            <div class="fs-5 text-danger fw-bold fst-italic mt-3 mb-1">
                                <xsl:text>There are no Permit Records to view...</xsl:text>
                            </div>
              
                        </xsl:when>
            
                        <xsl:otherwise>

                            <div class="table-responsive scrollable">
                
                                <table class="table table-sm table-striped table-hover base-table" id="permits-table">
                  
                                    <thead class="sticky-header" data-theme="{$THEME}">

                                        <th class="text-center">Select</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_permit_number">Permit Number</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_project_addr">Project Address</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_sub_div_name">Subdivision Name</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_project_type">Project Type</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_company">Company</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_permit_date">Permit Date</th>
                                        <th class="sortable text-nowrap" data-order-by="permit_entry_date">Entry Date</th>
                    
                                    </thead>
                  
                                    <tbody>
                    
                                        <xsl:apply-templates select="//permits"/>
                    
                                    </tbody>
                  
                                </table>
                
                            </div>
            
                        </xsl:otherwise>
            
                    </xsl:choose>
          
                </div>
        
            </div>
      
        </div>

        <!-- Form to Print Selected Permit -->
    
        <form style="display:inline;" id="PrintPermitForm" method="post" action="" target="PrintTab">

            <input type="hidden" name="action"            value="print"/>
            <input type="hidden" name="report"            value="Print_Selected_Permits"/>
            <input type="hidden" name="selected_permits"  value=""/>

        </form>
                        
    </xsl:template>

    <!-- Template for Permit tows -->

    <xsl:template match="permits/record">
    
        <tr class="hoverable permit-row text-nowrap" data-permit-serial="{permit_serial}">
            
            <td class="checkbox text-center selection-checkboxes selection-area">
                <input type="checkbox" class="selection-checkbox selection-area" name="selected[]" value="{permit_serial}">
                </input>
                <span class="selection-area user-select-none">&#160;</span>
            </td>
            <td class="permit_permit_number">
                <xsl:value-of select="permit_permit_number"/>
            </td>
            <td class="permit_project_addr text-nowrap">
                <xsl:value-of select="permit_project_addr"/>
            </td>
            <td class="permit_sub_div_name text-nowrap">
                <xsl:value-of select="permit_sub_div_name"/>
            </td>
            <td class="permit_project_type text-nowrap">
                <xsl:value-of select="permit_project_type"/>
            </td>
            <td class="permit_company">
                <xsl:value-of select="permit_company"/>
            </td>
            <td class="permit_permit_date">
                <xsl:value-of select="permit_permit_date_verbose"/>
            </td>
            <td class="permit_entry_date">
                <xsl:value-of select="permit_entry_date_verbose"/>
            </td>

        </tr>
    
    </xsl:template>
  
</xsl:stylesheet>
