{% extends "whm/base.html" %}
{% block title %}PHP Error{% 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>PHP Error</h2>
    </div>
    <div class="card-body">
	 <div class="form-group">
            <label for="php_version">Select Domain:</label>
           <select class="form-control" name="php_domain" id="php_domain" onchange="load(this.value)">
    <option value="">Domain</option>
    {% for domain in php_domain %}
        <option value="{{ domain.domain }}">{{ domain.domain }}</option>
    {% endfor %}
</select>

 <div id="responseContainer" style="display: none;" class="sk-wave">
            <div class="rect1"></div>
            <div class="rect2"></div>
            <div class="rect3"></div>
            <div class="rect4"></div>
            <div class="rect5"></div>
          
        </div>
</div>
	       
            <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">
    <label for="error_reporting">Select Error Type:</label>
    <select class="form-control" name="error_reporting" id="error_reporting">
        <option value="0">Off (No Errors)</option>
        <option value="32767">All Errors (E_ALL)</option>
        <option value="32759">All Except Notices (E_ALL & ~E_NOTICE)</option>
        <option value="1">Fatal Errors (E_ERROR)</option>
        <option value="2">Warnings (E_WARNING)</option>
        <option value="4">Parse Errors (E_PARSE)</option>
        <option value="8">Notices (E_NOTICE)</option>
        <option value="8192">Deprecated Notices (E_DEPRECATED)</option>
    </select>
</div>




<div class="form-group">
    <label for="log_errors">Error Log in file:</label>
   <label class="switch switch-primary form-control-label mr-2">
            <input type="checkbox"  name="log_errors" id="log_errors" class="switch-input form-check-input" value="On">
            <span class="switch-label"></span>
            <span class="switch-handle"></span>
          </label>
</div>

<div class="form-group">
    <label for="display_errors">Error Log in display:</label>
   <label class="switch switch-primary form-control-label mr-2">
            <input type="checkbox"  name="display_errors" id="display_errors" class="switch-input form-check-input" value="On">
            <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(`/whm/fatch_php_error_whm/?php_domain=${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
             

if (response.error_reporting) {
        $('#error_reporting').val(response.error_reporting.trim());
    }
if (response.log_errors === 'On' ) {
    $('#log_errors').prop('checked', true);
} else {
    $('#log_errors').prop('checked', false);
}
if (response.display_errors === 'On' ) {
    $('#display_errors').prop('checked', true);
} else {
    $('#display_errors').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 %}
