<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : forms-tpl.php
 * @location     : /modules/components/forms-tpl.php
 * @type         : Component
 * @scope        : Cross-Module
 * @allowed      : Form rendering, validation, data processing
 * @reusability  : Shared
 * @notes        : Dynamic form component for all modules
 */

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

function igny8_render_inline_form_row($table_id, $mode = 'add', $record_data = []) {
    $form_config = igny8_get_form_config($table_id);

    if (!$form_config) {
        return '<tr><td colspan="100%">Form not configured for table: ' . esc_html($table_id) . '</td></tr>';
    }

    $row_id_attr = ($mode === 'edit' && !empty($record_data['id']))
        ? ' data-id="' . esc_attr($record_data['id']) . '"'
        : '';

    ob_start(); ?>
    
    <tr class="igny8-inline-form-row" data-mode="<?php echo esc_attr($mode); ?>"<?php echo $row_id_attr; ?>>
        <td><input type="checkbox" disabled></td>

        <?php foreach ($form_config['fields'] as $field): ?>
            <td>
                <?php echo igny8_render_form_field($field, $record_data, $mode); ?>
            </td>
        <?php endforeach; ?>

        <td class="igny8-align-center igny8-actions">
            <button 
                type="button"
                class="igny8-btn igny8-btn-success igny8-btn-sm igny8-form-save"
                data-table-id="<?php echo esc_attr($table_id); ?>"
                data-nonce="<?php echo esc_attr(wp_create_nonce('igny8_ajax_nonce')); ?>"
                title="Save"
            >
                <svg width="20" height="20" viewBox="0 0 26 26" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
                </svg>
            </button>

            <button 
                type="button"
                class="igny8-btn igny8-btn-secondary igny8-btn-sm igny8-form-cancel"
                title="Cancel"
            >
                <svg width="20" height="20" viewBox="0 0 26 26" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                    <line x1="5" y1="5" x2="20" y2="20"></line>
                    <line x1="5" y1="20" x2="20" y2="5"></line>
                </svg>
            </button>
        </td>
    </tr>

    <style>
        tr.igny8-inline-form-row {
            animation: slideInForm 0.3s ease-out;
        }
        @keyframes slideInForm {
            from { opacity: 0; transform: translateY(-10px); }
            to   { opacity: 1; transform: translateY(0); }
        }
        tr.igny8-inline-form-row td {
            transition: all 0.2s ease;
        }
        tr.igny8-inline-form-row:hover td {
            background-color: rgba(0, 123, 255, 0.05);
        }
    </style>

    <?php
    return ob_get_clean();
}

function igny8_render_form_field($field, $record_data = [], $mode = 'add') {
    $field_name  = $field['name'];
    $field_type  = $field['type'];
    $field_label = $field['label'] ?? ucfirst($field_name);
    $field_value = isset($record_data[$field_name]) ? $record_data[$field_name] : ($field['default'] ?? '');
    $is_required = !empty($field['required']);

    switch ($field_type) {
        case 'number':
            return igny8_render_number_field($field_name, $field_label, $field_value, $is_required, $field['step'] ?? '');
        case 'select':
            return igny8_render_select_field($field_name, $field_label, $field_value, $field, $is_required);
        case 'textarea':
            return igny8_render_textarea_field($field_name, $field_label, $field_value, $is_required);
        default:
            return igny8_render_text_field($field_name, $field_label, $field_value, $is_required);
    }
}

function igny8_render_text_field($name, $label, $value, $required = false) {
    ob_start();
    ?>
    <input type="text" name="<?php echo esc_attr($name); ?>" placeholder="<?php echo esc_attr($label); ?>" 
           class="igny8-input-sm" value="<?php echo esc_attr($value); ?>"<?php echo $required ? ' required' : ''; ?> />
    <?php
    return ob_get_clean();
}

function igny8_render_number_field($name, $label, $value, $required = false, $step = '') {
    ob_start();
    ?>
    <input type="number" name="<?php echo esc_attr($name); ?>" placeholder="<?php echo esc_attr($label); ?>"
           class="igny8-input-sm" value="<?php echo esc_attr($value); ?>"<?php 
           echo $step ? ' step="' . esc_attr($step) . '"' : '';
           echo $required ? ' required' : ''; ?> />
    <?php
    return ob_get_clean();
}

function igny8_render_select_field($name, $label, $value, $field_config, $required = false) {
    $display_text = 'Select ' . esc_html($label);
    $options = [];

    // Get options
    if (isset($field_config['source']) && function_exists($field_config['source'])) {
        try {
            $dynamic_options = call_user_func($field_config['source']);
            foreach ($dynamic_options as $option) {
                $val = $option['value'] ?? $option;
                $lbl = $option['label'] ?? $option;
                $options[$val] = $lbl;
                if ($value == $val) $display_text = esc_html($lbl);
            }
        } catch (Exception $e) {
            $options = [];
        }
    } else {
        $options = $field_config['options'] ?? [];
        foreach ($options as $val => $lbl) {
            if ($value == $val) $display_text = esc_html($lbl);
        }
    }

    ob_start();
    ?>
    <div class="select">
        <button type="button" class="select-btn" name="<?php echo esc_attr($name); ?>" data-value="<?php echo esc_attr($value); ?>">
            <span class="select-text"><?php echo $display_text; ?></span>
            <span>▼</span>
        </button>
        <div class="select-list" style="max-height:200px;overflow-y:auto;">
            <div class="select-item" data-value="">Select <?php echo esc_html($label); ?></div>
            <?php foreach ($options as $val => $lbl): ?>
                <div class="select-item" data-value="<?php echo esc_attr($val); ?>"><?php echo esc_html($lbl); ?></div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php
    return ob_get_clean();
}

function igny8_render_textarea_field($name, $label, $value, $required = false) {
    ob_start();
    ?>
    <textarea name="<?php echo esc_attr($name); ?>" placeholder="<?php echo esc_attr($label); ?>"
              class="igny8-input-sm" rows="3"<?php echo $required ? ' required' : ''; ?>><?php echo esc_textarea($value); ?></textarea>
    <?php
    return ob_get_clean();
}
?>
