{% extends "whm/base.html" %}
{% block title %}LiteSpeed Configuration{% endblock title %}
{% block content %}


<div id="alertContainer" class="alert alert-success alert-dismissible fade show" role="alert" style="display: none;">
    <div id="message"></div>
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                  <span aria-hidden="true">×</span>
                </button>
</div>	





<form method="post" action="">
            {% csrf_token %}
<div class="row">
                            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">  
<div class="card card-default">
    <div class="card-header">
        <h2>LiteSpeed Configuration</h2>
    </div>
    <div class="card-body">
	 
	       
            <div class="tab-content mt-5" id="nav-tabContent">
              <div class="tab-pane fade show active" id="nav-tabs-home" role="tabpanel" aria-labelledby="nav-home-tab">
                
           

<div class="form-group mt-3">
    <label for="maxConnections">Max Connections:</label>
    <input type="text" class="form-control" id="maxConnections" name="maxConnections" placeholder="e.g., 128M" value="{{ current_settings.maxConnections }}">
</div>

<div class="form-group">
    <label for="maxSSLConnections">Max SSL Connections:</label>
    <input type="text" class="form-control" id="maxSSLConnections" name="maxSSLConnections" placeholder="e.g., 64M" value="{{ current_settings.maxSSLConnections }}">
</div>

<div class="form-group">
    <label for="connTimeout">Connection Timeout:</label>
    <input type="text" class="form-control" id="connTimeout" name="connTimeout" placeholder="e.g., 64M" value="{{ current_settings.connTimeout }}">
</div>

<div class="form-group">
    <label for="keepAliveTimeout">Keep Alive Timeout:</label>
    <input type="text" class="form-control" id="keepAliveTimeout" name="keepAliveTimeout" placeholder="e.g., 30" value="{{ current_settings.keepAliveTimeout }}">
</div>

<div class="form-group">
    <label for="enableGzipCompress">Enable GZIP Compression:</label>
    <label class="switch switch-primary form-control-label mr-2">
        <input type="checkbox" name="enableGzipCompress" id="enableGzipCompress" class="switch-input form-check-input" value="1"
               {% if current_settings.enableGzipCompress == '1' %}checked{% endif %}>
        <span class="switch-label"></span>
        <span class="switch-handle"></span>
    </label>
</div>

<div class="form-group">
    <label for="ls_enabled">Cache Module:</label>
    <label class="switch switch-primary form-control-label mr-2">
        <input type="checkbox" name="ls_enabled" id="ls_enabled" class="switch-input form-check-input" value="1"
               {% if current_settings.ls_enabled == '1' %}checked{% endif %}>
        <span class="switch-label"></span>
        <span class="switch-handle"></span>
    </label>
</div>

<div class="form-group">
    <label for="totalInMemCacheSize">Cache Size in memory:</label>
    <input type="text" class="form-control" id="totalInMemCacheSize" name="totalInMemCacheSize" placeholder="e.g., 30" value="{{ current_settings.totalInMemCacheSize }}">
</div>

<div class="form-group">
    <label for="expireInSeconds">Cache expire In Seconds:</label>
    <input type="text" class="form-control" id="expireInSeconds" name="expireInSeconds" placeholder="e.g., 30" value="{{ current_settings.expireInSeconds }}">
</div>

<div class="form-group">
    <label for="privateExpireInSeconds">Private Cache expire In Seconds:</label>
    <input type="text" class="form-control" id="privateExpireInSeconds" name="privateExpireInSeconds" placeholder="e.g., 30" value="{{ current_settings.privateExpireInSeconds }}">
</div>

<div class="form-group">
    <label for="enableCache">Public Cache:</label>
    <label class="switch switch-primary form-control-label mr-2">
        <input type="checkbox" name="enableCache" id="enableCache" class="switch-input form-check-input" value="1"
               {% if current_settings.enableCache == '1' %}checked{% endif %}>
        <span class="switch-label"></span>
        <span class="switch-handle"></span>
    </label>
</div>

<div class="form-group">
    <label for="enablePrivateCache">Private Cache:</label>
    <label class="switch switch-primary form-control-label mr-2">
        <input type="checkbox" name="enablePrivateCache" id="enablePrivateCache" class="switch-input form-check-input" value="1"
               {% if current_settings.enablePrivateCache == '1' %}checked{% endif %}>
        <span class="switch-label"></span>
        <span class="switch-handle"></span>
    </label>
</div>





            <button type="submit" class="btn btn-primary mt-3">Save Settings</button>
       
    
	    </div>
              

          </div>
	
	
	
     
	
</div>

 </div>
</div>

 </form>
 
 <script>
   

       const alertContainer = document.getElementById('alertContainer');
       const messageDisplay = document.getElementById('message');
      const responseDisplay = document.getElementById('responseContainer');
      

   function load(selectedVersion) {
    if (selectedVersion) {  // Ensuring selectedVersion is available
        console.log('Selected version:', selectedVersion);
       
        setTimeout(function() {
            responseDisplay.style.display = 'inline-block';

            // Call the URL with GET method, appending php_version as a query parameter
            fetch(`{% url 'fetch_php_settings' %}?php_version=${selectedVersion}`, {
                method: 'GET',  // Using GET to pass php_version as a query parameter
                headers: {
                    'Content-Type': 'application/json'
                },
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok: ' + response.statusText);
                }
                return response.json(); // Retrieve response as JSON
            })
            .then(response => {
                console.log('Response:', response);
               responseDisplay.style.display = 'none';

                // Populate form fields with the response data
                $('#memory_limit').val(response.memory_limit || '');
                $('#upload_max_filesize').val(response.upload_max_filesize || '');
                $('#post_max_size').val(response.post_max_size || '');
                $('#max_execution_time').val(response.max_execution_time || '');
				$('#max_input_time').val(response.max_input_time || '');
				$('#allow_url_fopen').val(response.allow_url_fopen || '');
				$('#allow_url_include').val(response.allow_url_include || '');
				$('#display_errors').val(response.display_errors || '');
				$('#file_uploads').val(response.file_uploads || '');
				
editor.setValue(response.content, -1);
// Toggle checkbox if the value matches '1' or 'on'
if (response.allow_url_fopen === '1' || response.allow_url_fopen.toString().toLowerCase() === 'on') {
    $('#allow_url_fopen').prop('checked', true);
} else {
    $('#allow_url_fopen').prop('checked', false);
}

// Toggle checkbox if the value matches '1' or 'on'
if (response.allow_url_include === '1' || response.allow_url_include.toString().toLowerCase() === 'on') {
    $('#allow_url_include').prop('checked', true);
} else {
    $('#allow_url_include').prop('checked', false);
}

// Toggle checkbox if the value matches '1' or 'on'
if (response.display_errors === '1' || response.display_errors.toString().toLowerCase() === 'on') {
    $('#display_errors').prop('checked', true);
} else {
    $('#display_errors').prop('checked', false);
}

// Toggle checkbox if the value matches '1' or 'on'
if (response.file_uploads === '1' || response.file_uploads.toString().toLowerCase() === 'on') {
    $('#file_uploads').prop('checked', true);
} else {
    $('#file_uploads').prop('checked', false);
}


            })
            .catch(error => {
                console.error('Error:', error);
                // You can show an alert to notify the user about the error
               responseDisplay.style.display = 'none';
               // messageDisplay.innerText = 'Failed to fetch PHP settings. Please try again.';
            });
        }, 0); // Delay set to 0 milliseconds to avoid any race conditions
    } else {
        console.log('No PHP version selected.');
    }
}


   
</script>

 
{% include "whm/footer.html" %}
{% endblock content %}
