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

?>
<?php if (!empty($integrazo_wz_common_top_note)) : ?>
    <div class="integrazo-wz-setup-step2-note-bottom" style="margin-left: 170px !important;max-width: 71.5% !important;">
        <?php echo wp_kses_post($integrazo_wz_common_top_note); ?>
    </div>
<?php endif; ?>
<form method="post" id="integrazo-wz-setup-mapping-area" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
    <?php
    // WordPress nonce and action field for security
    wp_nonce_field('integrazo_wz_save_mapping_action', 'integrazo_wz_save_mapping_nonce');
    ?>
    <input type="hidden" name="action" value="integrazo_wz_save_mapping" />
    <input type="hidden" name="integration_id" value="<?php echo esc_attr($integrazo_wz_integration_id); ?>" />
    <input type="hidden" name="record_action" value="" />
    <input type="hidden" name="primary_key" value="" />
    <input type="hidden" name="filter_criteria" value="" />
    <input type="hidden" name="filter_criteria_json" value="" />
    <input type="hidden" name="final_json" value="" />

    <?php
    $integrazo_wz_raw_form_fields = INTEGRAZO_WZ_Fields_Details::render($integrazo_wz_integration_details);

    if (! is_array($integrazo_wz_raw_form_fields)) {
        echo '<p style="text-align: center;">' . esc_html($integrazo_wz_raw_form_fields) . '</p>';
        return;
    }

    $integrazo_wz_normalized_form_fields = [];
    if (!empty($integrazo_wz_raw_form_fields)) {
        foreach ($integrazo_wz_raw_form_fields as $integrazo_wz_field) {
            $integrazo_wz_normalized_form_fields[] = [
                'name'      => $integrazo_wz_field['name'] ?? '',
                'label'     => $integrazo_wz_field['label'] ?? ($integrazo_wz_field['name'] ?? ''),
                'data_type' => $integrazo_wz_field['type'] ?? '',
                'identifier' => $integrazo_wz_field['identifier'] ?? '',
            ];
        }
    }

    $integrazo_wz_crm_fields = $integrazo_wz_normalized_crm_fields;
    $integrazo_wz_all_fields = $integrazo_wz_normalized_form_fields;

    // Prepare form fields including system fields
    $integrazo_wz_form_fields = [
        'form_fields'   => [],
        'system_fields' => [
            ['name' => 'custom_value', 'label' => 'Custom Value'],
            // ['name' => 'submitted_url', 'label' => 'Submitted Url'],
        ],
    ];

    foreach ($integrazo_wz_all_fields as $integrazo_wz_field) {
        $integrazo_wz_form_fields['form_fields'][] = [
            'name'      => $integrazo_wz_field['name'],
            'label'     => $integrazo_wz_field['label'],
            'data_type' => $integrazo_wz_field['data_type'],
            'identifier' => $integrazo_wz_field['identifier'] ?? '',
        ];
    }

    // Pass form fields to JS
    wp_localize_script('integrazo-wz-custom-js', 'integrazoFCData', [
        'form_fields' => $integrazo_wz_form_fields['form_fields'],
    ]);

    $integrazo_wz_field_mapping = json_decode($integrazo_wz_integration_details['field_mapping'] ?? '', true);

    if (empty($integrazo_wz_field_mapping)) {
        require_once INTEGRAZO_WZ_PLUGIN_DIR . 'src/products/mapping.php';
    } else {
        $integrazo_wz_mandatory_mappings     = [];
        $integrazo_wz_non_mandatory_mappings = [];

        if (!empty($integrazo_wz_field_mapping)) {
            $integrazo_wz_mandatory_crm_fields = array_filter($integrazo_wz_crm_fields, function ($field) {
                return !empty($field['system_mandatory']);
            });

            $integrazo_wz_mandatory_crm_api_names = array_column($integrazo_wz_mandatory_crm_fields, 'api_name');

            foreach ($integrazo_wz_field_mapping as $map) {
                if (in_array($map['product_field'], $integrazo_wz_mandatory_crm_api_names, true)) {
                    $integrazo_wz_mandatory_mappings[] = $map;
                } else {
                    $integrazo_wz_non_mandatory_mappings[] = $map;
                }
            }
        }

        require_once INTEGRAZO_WZ_PLUGIN_DIR . 'src/products/ex-mapping.php';
    }


    // Load filter section
    require_once INTEGRAZO_WZ_PLUGIN_DIR . 'src/products/filter.php';
    ?>

    <!-- Submit Button -->
    <div class="integrazo-wz-setup-next-btn-wrapper">
        <button type="submit" class="button button-primary" id="integrazo-wz-save-mapping"
            style="<?php echo empty($integrazo_wz_integration_data) ? 'display:none;' : ''; ?>">
            <?php esc_html_e('Save Mapping', 'integrazo-integration-for-zoho-apps'); ?>
        </button>
    </div>

</form>