<!--  $Id$  -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:include href="/app/tools/support/templates/layout.xsl" />

  <xsl:variable name="importerType">
    <xsl:choose>
        <xsl:when test="/Root/Params/c = 'importers'">rps</xsl:when>
        <xsl:otherwise>dtmv</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:template name="mainBody" >
    <style>
        .importerRow1 {
          background: #f7f7f7;
        }

        .toggleCell, #hideToggles {
          display: none;
        }

         /* The switch - the box around the slider */
        .switch {
          position: relative;
          display: inline-block;
          width: 30px;
          height: 17px;
        }

        /* Hide default HTML checkbox */
        .switch input {
          opacity: 0;
          width: 0;
          height: 0;
        }

        /* The slider */
        .slider {
          position: absolute;
          cursor: pointer;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: #ccc;
          -webkit-transition: .4s;
          transition: .4s;
        }

        .slider:before {
          position: absolute;
          content: "";
          height: 13px;
          width: 13px;
          left: 2px;
          bottom: 2px;
          background-color: white;
          -webkit-transition: .4s;
          transition: .4s;
        }

        input:checked + .slider {
          background-color: #026ead;
        }

        input:focus + .slider {
          box-shadow: 0 0 1px #026ead;
        }

        input:checked + .slider:before {
          -webkit-transform: translateX(13px);
          -ms-transform: translateX(13px);
          transform: translateX(13px);
        }

        /* Rounded sliders */
        .slider.round {
          border-radius: 17px;
        }

        .slider.round:before {
          border-radius: 50%;
        }
    </style>
    <script src="/production/javascript/jquery/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("input:checkbox").on('change', function() {
                var isChecked = $(this).is(":checked") ? 1:0;
                $.ajax({
                    url: '/support/client?c=importer_toggle',
                    type: 'GET',
                    data: { serviceVersion:$(this).attr("id"), status:isChecked, importerType:"<xsl:value-of select="$importerType" />" }
                });
            });
        });
        function showToggles() {
            $('.toggleCell').show();
            $('#showToggles').hide();
            $('#hideToggles').show();
        }
        function hideToggles() {
            $('.toggleCell').hide();
            $('#showToggles').show();
            $('#hideToggles').hide();
        }
    </script>
    <xsl:call-template name="bodyTitle" >
      <xsl:with-param name="title">Sales File Importers</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="displayList" />
  </xsl:template>

  <xsl:template name="displayList" >
    <div style="float:right">
        <a href="javascript:void(0)" onClick="showToggles()" id="showToggles">Show Importer Toggles</a>
        <a href="javascript:void(0)" onClick="hideToggles()" id="hideToggles">Hide Importer Toggles</a>
    </div>
    <xsl:choose>

      <xsl:when test="Form/Service" >
        <xsl:call-template name="displayListBody" />
      </xsl:when>

      <xsl:otherwise>
        <xsl:call-template name="collectionListEmpty" />
      </xsl:otherwise>

    </xsl:choose>

  </xsl:template>

  <xsl:template name="displayListBody" >
    <xsl:call-template name="displayImporterList" />
  </xsl:template>

  <xsl:template name="displayImporterList" >
    <table>
      <tbody>

        <xsl:for-each select="Form/Service[normalize-space(SaleImporterList/Version/Version)]" >
          <xsl:sort select="translate(ServiceName, 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
          <xsl:call-template name="displayImporterRow" />
        </xsl:for-each>

      </tbody>
    </table>
  </xsl:template>

  <xsl:template name="displayImporterRow" >
    <tr class="importerRow{position() mod 2}" style="border-top: 1px solid grey">
      <td><b><xsl:value-of select="ServiceName" /></b> (<xsl:value-of select="ServiceID/@raw" />)<br />
        <xsl:call-template name="displayImporterVersion" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="displayImporterVersion" >
    <table>
      <xsl:for-each select="SaleImporterList/Version" >
        <xsl:sort select="Type" />
        <xsl:sort select="Version" data-type="number" />
        <xsl:call-template name="displayImporterVersionRow" />
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template name="displayImporterVersionRow" >
    <tr>
      <xsl:if test="position() != last()">
        <xsl:attribute name="style">border-bottom: 1px solid grey</xsl:attribute>
      </xsl:if>
      <td class="versionColumn" style="vertical-align: top">Version <xsl:value-of select="Version" /></td>
      <td style="vertical-align: top; width: 20px" class="toggleCell">
        <label class="switch">
            <input type="checkbox" id="{ServiceID}_{Version}">
                <xsl:if test="Status = 1">
                    <xsl:attribute name="checked">checked</xsl:attribute>
                </xsl:if>
            </input>
            <span class="slider round"></span>
        </label>
      </td>
      <td class="sampleFileColumn" style="vertical-align: top; text-align: left"><xsl:value-of select="Importer" /></td>
      <td style="vertical-align: top">
        <xsl:call-template name="displayImporterSampleFiles" />
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="displayImporterSampleFiles" >
    <xsl:for-each select="SampleFileList/SampleFile" >
      <xsl:sort select="translate(Filename, 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
      <a>
        <xsl:attribute name="href"><xsl:value-of select="$moduleURL" />?c=download_sample&amp;file=<xsl:value-of disable-output-escaping="yes" select="EscapedPath" />&amp;type=<xsl:value-of select="../../Type" /></xsl:attribute>
        <xsl:value-of disable-output-escaping="no" select="Filename" />
      </a><br />
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
