<?php
/**
 * Form Integration Settings Page
 *
 * @package Divine_Agreement_System
 */

if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="wrap divine-integration-settings">
    <h1><?php echo esc_html__('Form Integration Settings', 'divine-agreement-system'); ?></h1>
    <p class="description">
        <?php echo esc_html__('Automatically create and send agreements when users submit forms from Divine Forms plugin.', 'divine-agreement-system'); ?>
    </p>

    <hr class="wp-header-end">

    <?php if (empty($forms)): ?>
        <div class="notice notice-warning">
            <p>
                <strong><?php echo esc_html__('No forms found.', 'divine-agreement-system'); ?></strong>
                <?php echo esc_html__('Please create forms in the Divine Forms plugin first.', 'divine-agreement-system'); ?>
            </p>
        </div>
    <?php elseif (empty($templates)): ?>
        <div class="notice notice-warning">
            <p>
                <strong><?php echo esc_html__('No templates found.', 'divine-agreement-system'); ?></strong>
                <?php echo esc_html__('Please create agreement templates first.', 'divine-agreement-system'); ?>
                <a href="<?php echo esc_url(admin_url('edit.php?post_type=divine_template')); ?>" class="button button-small">
                    <?php echo esc_html__('Create Template', 'divine-agreement-system'); ?>
                </a>
            </p>
        </div>
    <?php else: ?>

        <?php if (!empty($integrations)): ?>
            <div class="divine-existing-integrations">
                <h2><?php echo esc_html__('Configured Integrations', 'divine-agreement-system'); ?></h2>

                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th><?php echo esc_html__('Form', 'divine-agreement-system'); ?></th>
                            <th><?php echo esc_html__('Template', 'divine-agreement-system'); ?></th>
                            <th><?php echo esc_html__('Status', 'divine-agreement-system'); ?></th>
                            <th><?php echo esc_html__('Auto Email', 'divine-agreement-system'); ?></th>
                            <th><?php echo esc_html__('Actions', 'divine-agreement-system'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($integrations as $divine_form_id => $divine_config): ?>
                            <?php
                            $divine_form_name = '';
                            foreach ($forms as $divine_form) {
                                if ($divine_form['id'] == $divine_form_id) {
                                    $divine_form_name = $divine_form['title'];
                                    break;
                                }
                            }

                            $divine_template = get_post($divine_config['template_id']);
                            $divine_template_title = $divine_template ? $divine_template->post_title : __('(Template Deleted)', 'divine-agreement-system');

                            $divine_is_enabled = isset($divine_config['enabled']) ? $divine_config['enabled'] : false;
                            $divine_send_email = isset($divine_config['send_email']) ? $divine_config['send_email'] : false;
                            ?>
                            <tr data-form-id="<?php echo esc_attr($divine_form_id); ?>">
                                <td><strong><?php echo esc_html($divine_form_name); ?></strong></td>
                                <td><?php echo esc_html($divine_template_title); ?></td>
                                <td>
                                    <?php if ($divine_is_enabled): ?>
                                        <span class="divine-status-badge divine-status-active">
                                            <?php echo esc_html__('Active', 'divine-agreement-system'); ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="divine-status-badge divine-status-inactive">
                                            <?php echo esc_html__('Inactive', 'divine-agreement-system'); ?>
                                        </span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $divine_send_email ? '&#10003;' : '&mdash;'; ?></td>
                                <td>
                                    <button type="button" class="button button-small divine-edit-integration" data-form-id="<?php echo esc_attr($divine_form_id); ?>">
                                        <?php echo esc_html__('Edit', 'divine-agreement-system'); ?>
                                    </button>
                                    <button type="button" class="button button-small button-link-delete divine-delete-integration" data-form-id="<?php echo esc_attr($divine_form_id); ?>">
                                        <?php echo esc_html__('Delete', 'divine-agreement-system'); ?>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <hr>
        <?php endif; ?>

        <div class="divine-new-integration">
            <h2><?php echo esc_html__('Add New Integration', 'divine-agreement-system'); ?></h2>

            <button type="button" class="button button-primary" id="divine-add-integration-btn">
                <span class="dashicons dashicons-plus-alt2"></span>
                <?php echo esc_html__('Configure New Form', 'divine-agreement-system'); ?>
            </button>
        </div>

        <div id="divine-integration-modal" class="divine-modal" style="display: none;">
            <div class="divine-modal-overlay"></div>
            <div class="divine-modal-content">
                <div class="divine-modal-header">
                    <h2><?php echo esc_html__('Configure Form Integration', 'divine-agreement-system'); ?></h2>
                    <button type="button" class="divine-modal-close">
                        <span class="dashicons dashicons-no-alt"></span>
                    </button>
                </div>

                <div class="divine-modal-body">
                    <form id="divine-integration-form">
                        <input type="hidden" name="form_id" id="divine-form-id" value="">

                        <div class="divine-form-section">
                            <h3><?php echo esc_html__('1. Select Form', 'divine-agreement-system'); ?></h3>

                            <table class="form-table">
                                <tr>
                                    <th scope="row">
                                        <label for="divine-select-form">
                                            <?php echo esc_html__('Form', 'divine-agreement-system'); ?>
                                            <span class="required">*</span>
                                        </label>
                                    </th>
                                    <td>
                                        <select name="form_id" id="divine-select-form" class="regular-text" required>
                                            <option value=""><?php echo esc_html__('-- Select Form --', 'divine-agreement-system'); ?></option>
                                            <?php foreach ($forms as $divine_form): ?>
                                                <option value="<?php echo esc_attr($divine_form['id']); ?>">
                                                    <?php echo esc_html($divine_form['title']); ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                        <p class="description">
                                            <?php echo esc_html__('Select which form should trigger agreement creation.', 'divine-agreement-system'); ?>
                                        </p>
                                    </td>
                                </tr>
                            </table>
                        </div>

                        <div class="divine-form-section divine-config-section" style="display: none;">
                            <h3><?php echo esc_html__('2. Configuration', 'divine-agreement-system'); ?></h3>

                            <table class="form-table">
                                <tr>
                                    <th scope="row"><?php echo esc_html__('Enable Integration', 'divine-agreement-system'); ?></th>
                                    <td>
                                        <label>
                                            <input type="checkbox" name="enabled" id="divine-enabled" value="1">
                                            <?php echo esc_html__('Automatically create agreements when this form is submitted', 'divine-agreement-system'); ?>
                                        </label>
                                    </td>
                                </tr>

                                <tr>
                                    <th scope="row">
                                        <label for="divine-template-id">
                                            <?php echo esc_html__('Agreement Template', 'divine-agreement-system'); ?>
                                            <span class="required">*</span>
                                        </label>
                                    </th>
                                    <td>
                                        <select name="template_id" id="divine-template-id" class="regular-text" required>
                                            <option value=""><?php echo esc_html__('-- Select Template --', 'divine-agreement-system'); ?></option>
                                            <?php foreach ($templates as $divine_template): ?>
                                                <option value="<?php echo esc_attr($divine_template->ID); ?>">
                                                    <?php echo esc_html($divine_template->post_title); ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                        <p class="description">
                                            <?php echo esc_html__('Select the agreement template to use.', 'divine-agreement-system'); ?>
                                        </p>
                                    </td>
                                </tr>

                                <tr>
                                    <th scope="row"><?php echo esc_html__('Auto Send Email', 'divine-agreement-system'); ?></th>
                                    <td>
                                        <label>
                                            <input type="checkbox" name="send_email" id="divine-send-email" value="1">
                                            <?php echo esc_html__('Automatically send signing link to user after agreement creation', 'divine-agreement-system'); ?>
                                        </label>
                                    </td>
                                </tr>

                                <tr>
                                    <th scope="row">
                                        <label for="divine-email-field">
                                            <?php echo esc_html__('Email Field', 'divine-agreement-system'); ?>
                                            <span class="required">*</span>
                                        </label>
                                    </th>
                                    <td>
                                        <select name="email_field" id="divine-email-field" class="regular-text" required>
                                            <option value=""><?php echo esc_html__('-- Select Email Field --', 'divine-agreement-system'); ?></option>
                                        </select>
                                        <p class="description">
                                            <?php echo esc_html__('Select which form field contains the collaborator\'s email address.', 'divine-agreement-system'); ?>
                                        </p>
                                    </td>
                                </tr>
                            </table>
                        </div>

                        <div class="divine-form-section divine-mapping-section" style="display: none;">
                            <h3><?php echo esc_html__('3. Field Mapping', 'divine-agreement-system'); ?></h3>
                            <p class="description">
                                <?php echo esc_html__('Map template placeholders to form fields. Placeholders will be replaced with form data.', 'divine-agreement-system'); ?>
                            </p>

                            <div id="divine-field-mapping-container">
                                <div class="divine-mapping-loading">
                                    <span class="spinner is-active"></span>
                                    <?php echo esc_html__('Loading fields...', 'divine-agreement-system'); ?>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="divine-modal-footer">
                    <button type="button" class="button" id="divine-test-mapping-btn">
                        <span class="dashicons dashicons-visibility"></span>
                        <?php echo esc_html__('Test Mapping', 'divine-agreement-system'); ?>
                    </button>

                    <div class="divine-modal-footer-right">
                        <button type="button" class="button" id="divine-cancel-integration-btn">
                            <?php echo esc_html__('Cancel', 'divine-agreement-system'); ?>
                        </button>
                        <button type="button" class="button button-primary" id="divine-save-integration-btn">
                            <?php echo esc_html__('Save Integration', 'divine-agreement-system'); ?>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div id="divine-test-preview-modal" class="divine-modal" style="display: none;">
            <div class="divine-modal-overlay"></div>
            <div class="divine-modal-content">
                <div class="divine-modal-header">
                    <h2><?php echo esc_html__('Mapping Test Preview', 'divine-agreement-system'); ?></h2>
                    <button type="button" class="divine-modal-close">
                        <span class="dashicons dashicons-no-alt"></span>
                    </button>
                </div>

                <div class="divine-modal-body">
                    <div id="divine-test-preview-content"></div>
                </div>

                <div class="divine-modal-footer">
                    <button type="button" class="button button-primary divine-modal-close">
                        <?php echo esc_html__('Close', 'divine-agreement-system'); ?>
                    </button>
                </div>
            </div>
        </div>

    <?php endif; ?>
</div>
