<?php
    if (!defined('pp_allowed_access')) {
        die('Direct access not allowed');
    }

    $plugin_slug = 'whatsapp-notification';
    $settings = pp_get_plugin_setting($plugin_slug);
?>

<form id="whatsappNotificationSettingsForm" method="post" action="">
    <div class="page-header">
      <div class="row align-items-end">
        <div class="col-sm mb-2 mb-sm-0">
          <h1 class="page-header-title">WhatsApp Notification Settings</h1>
        </div>
      </div>
    </div>

    <div class="row justify-content-center">
      <div class="col-lg-8">
        <div class="card">
          <div class="card-header">
            <h2 class="card-title h4">Configuration</h2>
          </div>

          <div class="card-body">
            <input type="hidden" name="action" value="plugin_update-submit">
            <input type="hidden" name="plugin_slug" value="<?php echo $plugin_slug?>">

            <div class="row mb-4">
              <div class="col-sm-12">
                <label for="whatsapp_api_token" class="col-sm-12 col-form-label form-label">API Token</label>
                <input type="password" class="form-control" name="whatsapp_api_token" id="whatsapp_api_token" value="<?php echo htmlspecialchars($settings['whatsapp_api_token'] ?? '') ?>">
                <small class="text-muted">Add your WhatsApp notification provider API token.</small>
              </div>
            </div>

            <div class="row mb-4">
              <div class="col-sm-12">
                <label for="whatsapp_sender_number" class="col-sm-12 col-form-label form-label">Sender Number</label>
                <input type="text" class="form-control" name="whatsapp_sender_number" id="whatsapp_sender_number" value="<?php echo htmlspecialchars($settings['whatsapp_sender_number'] ?? '') ?>">
              </div>
            </div>

            <div class="form-check form-switch mb-4">
              <input class="form-check-input" type="checkbox" name="enable_whatsapp_notifications" id="enable_whatsapp_notifications" <?php echo (isset($settings['enable_whatsapp_notifications']) && $settings['enable_whatsapp_notifications']) ? 'checked' : ''; ?>>
              <label class="form-check-label" for="enable_whatsapp_notifications">Enable WhatsApp notifications</label>
            </div>

            <div id="whatsappAjaxResponse" class="mb-3"></div>

            <button type="submit" class="btn btn-primary btn-whatsapp-save">Save Settings</button>
          </div>
        </div>
      </div>
    </div>
</form>

<script>
    $(document).ready(function() {
        $('#whatsappNotificationSettingsForm').on('submit', function(e) {
            e.preventDefault();

            document.querySelector(".btn-whatsapp-save").innerHTML = '<div class="spinner-border text-light spinner-border-sm" role="status"> <span class="visually-hidden">Loading...</span> </div>';

            $.ajax({
                url: $(this).attr('action'),
                type: 'POST',
                data: $(this).serialize(),
                dataType: 'json',
                success: function(response) {
                    document.querySelector(".btn-whatsapp-save").innerHTML = 'Save Settings';
                    $('#whatsappAjaxResponse').removeClass('alert-success alert-danger').addClass(response.status ? 'alert alert-success' : 'alert alert-danger').html(response.message);
                },
                error: function() {
                    document.querySelector(".btn-whatsapp-save").innerHTML = 'Save Settings';
                    $('#whatsappAjaxResponse').removeClass('alert-success').addClass('alert alert-danger').html('An error occurred. Please try again.');
                }
            });
        });
    });
</script>
