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

$integration_name = "Integration #" . ($integrazo_fwcrm_form_fieldMappingDBInstance->getFieldMappingTotalCount() + 1);

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

        <h3 class="integrazo_fwcrm_form-setup-popup-title">
            <?php esc_html_e('Add Integration', 'crm-integration-freshworks-any-form'); ?>
        </h3>

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

                <!-- Form Name -->
                <tr class="integrazo_fwcrm_form-setup-row">
                    <td class="integrazo_fwcrm_form-setup-title"><?php esc_html_e('Select Form:', 'crm-integration-freshworks-any-form'); ?>
                        <div class="integrazo_fwcrm_form-help-icon-wrapper">
                            <span class="integrazo_fwcrm_form-help-icon">?</span>
                            <div class="integrazo_fwcrm_form-help-tooltip">
                                <span>Select a form to integrate with Freshworks CRM for data syncing.</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <select name="formName" class="integrazo_fwcrm_form-setup-input" id="integrazo_fwcrm_form-setup-formName" onchange="integrazo_fwcrm_form_handleFormChange()">
                            <option value=""><?php esc_html_e('Select', 'crm-integration-freshworks-any-form'); ?></option>
                            <?php foreach ($form_group_list as $group_id => $group_data): ?>
                                <optgroup label="<?php echo esc_attr($group_data['form_group_name']); ?>">
                                    <?php foreach ($group_data['forms'] as $form): ?>
                                        <option value="<?php echo esc_attr($group_id . '_' . $form['id']); ?>">
                                            <?php echo esc_html($form['name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </optgroup>
                            <?php endforeach; ?>
                        </select>
                    </td>
                </tr>

                <!-- Account Name -->
                <tr class="integrazo_fwcrm_form-setup-row">
                    <td class="integrazo_fwcrm_form-setup-title"><?php esc_html_e('Select Account:', 'crm-integration-freshworks-any-form'); ?>
                        <div class="integrazo_fwcrm_form-help-icon-wrapper">
                            <span class="integrazo_fwcrm_form-help-icon">?</span>
                            <div class="integrazo_fwcrm_form-help-tooltip">
                                <span>
                                    Select the Freshworks CRM account to push data.</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <select name="accountName" class="integrazo_fwcrm_form-setup-input" id="integrazo_fwcrm_form-setup-accountName" onchange="integrazo_fwcrm_form_handleAccountChange()">
                            <option value=""><?php esc_html_e('Select', 'crm-integration-freshworks-any-form'); ?></option>
                            <?php
                            // Iterate through the account list and add options
                            foreach ($account_group_list as $account): ?>
                                <option value="<?php echo esc_attr($account['account_id']); ?>">
                                    <?php echo esc_html($account['account_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>

                    </td>
                </tr>

                <!-- Module Name -->
                <tr class="integrazo_fwcrm_form-setup-row">
                    <td class="integrazo_fwcrm_form-setup-title"><?php esc_html_e('Select Module:', 'crm-integration-freshworks-any-form'); ?>
                        <div class="integrazo_fwcrm_form-help-icon-wrapper">
                            <span class="integrazo_fwcrm_form-help-icon">?</span>
                            <div class="integrazo_fwcrm_form-help-tooltip">
                                <span>Choose the module in Freshworks CRM to store the submitted data.</span>
                            </div>
                        </div>
                    </td>
                    <td>
                        <select name="moduleName"
                            class="integrazo_fwcrm_form-setup-input"
                            id="integrazo_fwcrm_form-setup-moduleName"
                            onchange="integrazo_fwcrm_form_handleModuleChange()">
                            <option value=""><?php echo esc_html__('Select', 'crm-integration-freshworks-any-form'); ?></option>
                            <option value="contacts"><?php echo esc_html__('Leads', 'crm-integration-freshworks-any-form'); ?></option>
                            <option value="sales_accounts"><?php echo esc_html__('Accounts', 'crm-integration-freshworks-any-form'); ?></option>
                        </select>

                    </td>
                </tr>
            </table>
        </div>

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

        </div>


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

            <!-- Action -->
            <div class="integrazo_fwcrm_form-setup-action-container">
                <div class="integrazo_fwcrm_form-setup-action-popup-container">
                    <div style="padding-left: 22px; padding-top: 20px;">
                        <div>
                            <p class="integrazo_fwcrm_form-setup-action-heading"><?php esc_html_e('Choose an action for CRM records:', 'crm-integration-freshworks-any-form'); ?></p>
                            <div class="integrazo_fwcrm_form-help-icon-wrapper">
                                <span class="integrazo_fwcrm_form-help-icon">?</span>
                                <div class="integrazo_fwcrm_form-help-tooltip">
                                    <span>Choose how to process form submissions in Freshworks CRM, such as creating new records or updating existing ones.</span>
                                </div>
                            </div>
                        </div>
                        <div style="padding: 9px;">
                            <label class="integrazo_fwcrm_form-setup-action-label">
                                <input type="radio" name="crm_action" value="add_ignore_existing">
                                <?php esc_html_e('Always add as a new record, ignoring existing records', 'crm-integration-freshworks-any-form'); ?>
                            </label>
                            <label class="integrazo_fwcrm_form-setup-action-label">
                                <input type="radio" name="crm_action" value="add_or_update">
                                <?php esc_html_e('Add if record is not existing, or update if it already exists', 'crm-integration-freshworks-any-form'); ?>
                            </label>
                        </div>
                    </div>
                </div>
            </div>



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