<?php
// Exit if accessed directly to ensure security.
if (!defined('ABSPATH')) {
    exit;
}

$iafwzc_integration_name = "Integration #" . ($IAFWZC_Field_MappingDBInstance->getFieldMappingTotalCount() + 1);

?>
<!-- Setup Popup Overlay -->
<div id="iafwzc-setup-popupOverlay" class="iafwzc-setup-popup-overlay">
    <div class="iafwzc-setup-popup-content">
        <!-- Close Button -->
        <span class="iafwzc-setup-close" onclick="window.location.href='<?php echo esc_url(admin_url('admin.php?page=integrate-any-form-with-zoho-crm&tab=setup')); ?>'">&times;</span>

        <h3 class="iafwzc-setup-popup-title">
            <?php esc_html_e('Add Integration', 'integrate-any-form-with-zoho-crm'); ?>
        </h3>

        <div class="iafwzc-setup-menu">
            <table class="iafwzc-setup-table">
                <!-- Integration Name -->
                <tr class="iafwzc-setup-row">
                    <td class="iafwzc-setup-title"><?php esc_html_e('Integration Name:', 'integrate-any-form-with-zoho-crm'); ?>
                        <div class="iafwzc-help-icon-wrapper">
                            <span class="iafwzc-help-icon">?</span>
                            <div class="iafwzc-help-tooltip">
                                <p>A unique name to identify this integration.</p>
                            </div>
                        </div>
                    </td>
                    <td>
                        <input type="text" class="iafwzc-setup-input" id="iafwzc-setup-integrationName" placeholder="Enter integration name" value="<?php echo esc_attr($iafwzc_integration_name); ?>">
                    </td>
                </tr>

                <!-- Form Name -->
                <tr class="iafwzc-setup-row">
                    <td class="iafwzc-setup-title"><?php esc_html_e('Select Form:', 'integrate-any-form-with-zoho-crm'); ?>
                        <div class="iafwzc-help-icon-wrapper">
                            <span class="iafwzc-help-icon">?</span>
                            <div class="iafwzc-help-tooltip">
                                <span>Select a form to integrate with Zoho CRM or Zoho Bigin for data syncing.</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <select name="formName" class="iafwzc-setup-input" id="iafwzc-setup-formName" onchange="iafwzc_handleFormChange()">
                            <option value=""><?php esc_html_e('Select', 'integrate-any-form-with-zoho-crm'); ?></option>
                            <?php foreach ($iafwzc_form_group_list as $iafwzc_group_id => $iafwzc_group_data): ?>
                                <optgroup label="<?php echo esc_attr($iafwzc_group_data['form_group_name']); ?>">
                                    <?php foreach ($iafwzc_group_data['forms'] as $iafwzc_form): ?>
                                        <option value="<?php echo esc_attr($iafwzc_group_id . '_' . $iafwzc_form['id']); ?>">
                                            <?php echo esc_html($iafwzc_form['name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </optgroup>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>

                <!-- Account Name -->
                <tr class="iafwzc-setup-row">
                    <td class="iafwzc-setup-title"><?php esc_html_e('Select Account:', 'integrate-any-form-with-zoho-crm'); ?>
                        <div class="iafwzc-help-icon-wrapper">
                            <span class="iafwzc-help-icon">?</span>
                            <div class="iafwzc-help-tooltip">
                                <span>
                                    Select the Zoho CRM or Zoho Bigin account to push data.</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <select name="accountName" class="iafwzc-setup-input" id="iafwzc-setup-accountName" onchange="iafwzc_handleAccountChange()">
                            <option value=""><?php esc_html_e('Select', 'integrate-any-form-with-zoho-crm'); ?></option>
                            <?php
                            // Group accounts by service_name
                            $iafwzc_grouped_accounts = [];
                            foreach ($iafwzc_account_group_list as $iafwzc_account) {
                                $iafwzc_service_label = ($iafwzc_account['service_name'] === 'zoho_crm') ? 'Zoho CRM' : (($iafwzc_account['service_name'] === 'zoho_bigin') ? 'Zoho Bigin' : 'Other Service');
                                $iafwzc_grouped_accounts[$iafwzc_service_label][] = $iafwzc_account;
                            }

                            foreach ($iafwzc_grouped_accounts as $iafwzc_service_label => $iafwzc_accounts): ?>
                                <optgroup label="<?php echo esc_attr($iafwzc_service_label); ?>">
                                    <?php foreach ($iafwzc_accounts as $iafwzc_account): ?>
                                        <option value="<?php echo esc_attr($iafwzc_account['account_id']); ?>">
                                            <?php echo esc_html($iafwzc_account['account_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </optgroup>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>

                <!-- Module Name -->
                <tr class="iafwzc-setup-row">
                    <td class="iafwzc-setup-title"><?php esc_html_e('Select Module:', 'integrate-any-form-with-zoho-crm'); ?>
                        <div class="iafwzc-help-icon-wrapper">
                            <span class="iafwzc-help-icon">?</span>
                            <div class="iafwzc-help-tooltip">
                                <span>Choose the module in Zoho CRM or Zoho Bigin (e.g., Leads, Contacts, etc.) to store the submitted data.</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <select name="moduleName" class="iafwzc-setup-input" id="iafwzc-setup-moduleName" onchange="iafwzc_handleModuleChange()">
                            <option value=""><?php esc_html_e('Select', 'integrate-any-form-with-zoho-crm'); ?></option>
                        </select>
                    </td>
                </tr>
            </table>
        </div>

        <!-- Alert Text -->
        <div class="iafwzc-setup-loading-text">
            <p></p>
        </div>


        <div id="iafwzc-setup-fieldMapping-area" style="display:none;">
            <!-- Field Mapping -->
            <div id="iafwzc-setup-fieldMapping" style="margin-top:30px;" class="iafwzc-setup-menu">
                <table id="iafwzc-setup-fieldMapping-table" class="iafwzc-setup-table">
                    <thead>
                        <tr>
                            <th class="iafwzc-setup-th" width="43%"><?php esc_html_e('Select Form Fields', 'integrate-any-form-with-zoho-crm'); ?>
                                <div class="iafwzc-help-icon-wrapper">
                                    <span class="iafwzc-help-icon">?</span>
                                    <div class="iafwzc-help-tooltip">
                                        <span>Select the form field to map with Zoho CRM or Zoho Bigin fields.</span>
                                    </div>
                                </div>
                            </th>
                            <th class="iafwzc-setup-th" width="43%"><?php esc_html_e('Select CRM Fields', 'integrate-any-form-with-zoho-crm'); ?>
                                <div class="iafwzc-help-icon-wrapper">
                                    <span class="iafwzc-help-icon">?</span>
                                    <div class="iafwzc-help-tooltip">
                                        <span>Choose the Zoho CRM or Zoho Bigin field to map with the selected form field.</span>
                                    </div>
                                </div>
                            </th>
                            <th class="iafwzc-setup-th" style="border: none; background: none;"></th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                </table>
            </div>

            <!-- Action -->
            <div class="iafwzc-setup-action-container">
                <div class="iafwzc-setup-action-popup-container">
                    <div style="padding-left: 22px; padding-top: 20px;">
                        <div>
                            <p class="iafwzc-setup-action-heading"><?php esc_html_e('Choose an action for CRM records:', 'integrate-any-form-with-zoho-crm'); ?></p>
                            <div class="iafwzc-help-icon-wrapper">
                                <span class="iafwzc-help-icon">?</span>
                                <div class="iafwzc-help-tooltip">
                                    <span>Choose how to process form submissions in Zoho CRM or Bigin, such as creating or updating records.</span>
                                </div>
                            </div>
                        </div>
                        <?php
                        // Common variables to handle dynamic behavior
                        $iafwzc_is_disabled = !$iafwzc_session ? 'disabled' : '';
                        $iafwzc_dynamic_class = !$iafwzc_session ? 'iafwzc-setup-action-label-disable' : '';
                        $iafwzc_dynamic_title = !$iafwzc_session ? esc_attr__('This option is available in the premium version', 'integrate-any-form-with-zoho-crm') : '';
                        ?>

                        <div style="padding: 9px;">
                            <label class="iafwzc-setup-action-label">
                                <input type="radio" name="crm_action" value="add_ignore_existing" checked>
                                <?php esc_html_e('Always add as a new record, ignoring existing records', 'integrate-any-form-with-zoho-crm'); ?>
                            </label>
                            <label class="iafwzc-setup-action-label <?php echo esc_attr($iafwzc_dynamic_class); ?>" <?php echo $iafwzc_dynamic_title ? 'title="' . esc_attr($iafwzc_dynamic_title) . '"' : ''; ?>>
                                <input type="radio" name="crm_action" value="skip_if_exists" <?php echo esc_attr($iafwzc_is_disabled); ?>>
                                <?php esc_html_e('Skip if record already exists', 'integrate-any-form-with-zoho-crm'); ?>
                            </label>
                            <label class="iafwzc-setup-action-label <?php echo esc_attr($iafwzc_dynamic_class); ?>" <?php echo $iafwzc_dynamic_title ? 'title="' . esc_attr($iafwzc_dynamic_title) . '"' : ''; ?>>
                                <input type="radio" name="crm_action" value="add_or_update" <?php echo esc_attr($iafwzc_is_disabled); ?>>
                                <?php esc_html_e('Add if record is not existing, or update if it already exists', 'integrate-any-form-with-zoho-crm'); ?>
                            </label>
                            <label class="iafwzc-setup-action-label <?php echo esc_attr($iafwzc_dynamic_class); ?>" <?php echo $iafwzc_dynamic_title ? 'title="' . esc_attr($iafwzc_dynamic_title) . '"' : ''; ?>>
                                <input type="radio" name="crm_action" value="update_only" <?php echo esc_attr($iafwzc_is_disabled); ?>>
                                <?php esc_html_e('Update only if record exists', 'integrate-any-form-with-zoho-crm'); ?>
                            </label>
                            <label class="iafwzc-setup-action-label <?php echo esc_attr($iafwzc_dynamic_class); ?>" <?php echo $iafwzc_dynamic_title ? 'title="' . esc_attr($iafwzc_dynamic_title) . '"' : ''; ?>>
                                <input type="radio" name="crm_action" value="add_tag_if_duplicate" <?php echo esc_attr($iafwzc_is_disabled); ?>>
                                <?php esc_html_e('Add new or if already existing then add a tag as duplicate', 'integrate-any-form-with-zoho-crm'); ?>
                            </label>
                        </div>
                        <div id="iafwzc-setup-updateActionName-area">
                            <div>
                                <p class="iafwzc-setup-action-heading"><?php esc_html_e('Select Primary Key to Match Records:', 'integrate-any-form-with-zoho-crm'); ?></p>
                                <div class="iafwzc-help-icon-wrapper">
                                    <span class="iafwzc-help-icon">?</span>
                                    <div class="iafwzc-help-tooltip">
                                        <span>Select the primary key to match records. If a match is found, the system will perform the selected action, such as add, update, or skip.</span>
                                    </div>
                                </div>
                            </div>
                            <select class="iafwzc-setup-input" id="iafwzc-setup-updateActionName">
                                <option value=""><?php esc_html_e('Select', 'integrate-any-form-with-zoho-crm'); ?></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Note -->
            <div id="iafwzc-setup-note-area">
                <div class="iafwzc-setup-note-row">

                    <p class="iafwzc-setup-note-heading"><?php esc_html_e('Do you want to add a note?', 'integrate-any-form-with-zoho-crm'); ?></p>
                    <div class="iafwzc-help-icon-wrapper">
                        <span class="iafwzc-help-icon">?</span>
                        <div class="iafwzc-help-tooltip">
                            <span>Add a note to include additional information or context for the CRM record.</span>
                        </div>
                    </div>
                    <label class="iafwzc-setup-note-label">
                        <input type="radio" name="note_value" value="1">
                        <?php esc_html_e('Yes', 'integrate-any-form-with-zoho-crm'); ?>
                    </label>
                    <label class="iafwzc-setup-note-label">
                        <input type="radio" name="note_value" value="0">
                        <?php esc_html_e('No', 'integrate-any-form-with-zoho-crm'); ?>
                    </label>



                </div>
                <textarea id="iafwzc-setup-note" name="noteContent" rows="4" cols="50" placeholder="<?php esc_attr_e('Enter your note here', 'integrate-any-form-with-zoho-crm'); ?>"></textarea>
            </div>

            <!-- Filter Container -->
            <div class="iafwzc-setup-filter-container">
                <!-- Filter Header -->
                <div class="iafwzc-setup-filter-header" onclick="iafwzc_toggleFilterArea()">
                    <p class="iafwzc-setup-note-heading">Filter Criteria</p>
                    <span id="iafwzc-setup-filter-arrow" class="iafwzc-setup-filter-header-arrow">▼</span>
                </div>

                <!-- Filter Content -->
                <div class="iafwzc-setup-filter-content">
                    <div class="iafwzc-setup-filter-clear-all" onclick="iafwzc_clearAll()">Clear All</div>
                    <div id="iafwzc-setup-filter-group-container" class="iafwzc-setup-filter-group-container">
                        <div class="iafwzc-setup-filter-group">
                            <div class="iafwzc-setup-filter-row">
                                <select class="iafwzc-setup-filter-field">
                                    <option value="" disabled selected>Select Field</option>
                                </select>
                                <select class="iafwzc-setup-filter-condition">
                                    <option value="" disabled selected>Choose Condition</option>
                                    <option value="contains">Contains</option>
                                    <option value="not_contains">Does Not Contain</option>
                                    <option value="equals">Equals</option>
                                    <option value="not_equals">Does Not Equal</option>
                                    <option value="starts_with">Starts With</option>
                                    <option value="ends_with">Ends With</option>
                                    <option value="greater_than">Greater Than</option>
                                    <option value="less_than">Less Than</option>
                                    <option value="is_empty">Is Empty</option>
                                    <option value="is_not_empty">Is Not Empty</option>
                                </select>
                                <input type="text" class="iafwzc-setup-filter-value" placeholder="Enter value" />
                                <div class="iafwzc-setup-filter-actions">
                                    <button class="iafwzc-setup-filter-add-icon" onclick="iafwzc_addFilter(this)">+</button>
                                    <button class="iafwzc-setup-filter-close-icon" onclick="iafwzc_removeFilter(this)">×</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Submit Button -->
            <div class="iafwzc-auth-button-container" id="iafwzc-setup-button" style="margin-top: 40px;">
                <button type="submit" class="iafwzc-auth-button" onclick="iafwzc_handleButtonClick(this)">
                    <?php esc_html_e('Save Mapping', 'integrate-any-form-with-zoho-crm'); ?>
                </button>
            </div>
        </div>
    </div>
</div>
