<?php

/**
 * Admin user edit / create profile fields template.
 *
 * @var \WP_User|null $user            User object (null on create).
 * @var array         $fields          Enabled profile field definitions.
 * @var array         $avatar_settings Avatar settings.
 *
 * @package ArraySubs\Features\ProfileFields
 * @since 1.9.0
 */

defined('ABSPATH') || exit;

$user_id = $user ? $user->ID : 0;
?>

<h2><?php esc_html_e('ArraySubs Profile Fields', 'arraysubs'); ?></h2>

<table class="form-table" role="presentation">
    <?php if (!empty($avatar_settings['enabled']) && $user_id > 0) : ?>
        <?php
        $avatar_data = arraysubs_get_user_avatar($user_id);
        $avatar_src  = $avatar_data['has_avatar'] ? esc_url($avatar_data['url']) : get_avatar_url($user_id, ['size' => 96]);
        ?>
        <tr>
            <th><label><?php esc_html_e('Avatar', 'arraysubs'); ?></label></th>
            <td>
                <div class="arraysubs-admin-avatar">
                    <img
                        src="<?php echo esc_url($avatar_src); ?>"
                        alt="<?php esc_attr_e('Avatar', 'arraysubs'); ?>"
                        width="96"
                        height="96"
                        class="arraysubs-admin-avatar__image" />
                    <input type="file" name="arraysubs_avatar" accept="image/*" />
                    <?php if ($avatar_data['has_avatar']) : ?>
                        <label class="arraysubs-admin-avatar__remove-label">
                            <input type="checkbox" name="arraysubs_remove_avatar" value="1" />
                            <?php esc_html_e('Remove avatar', 'arraysubs'); ?>
                        </label>
                    <?php endif; ?>
                    <p class="description">
                        <?php
                        /* translators: %d: max file size in MB */
                        printf(esc_html__('Max file size: %d MB', 'arraysubs'), absint($avatar_settings['max_file_size']));
                        ?>
                    </p>
                </div>
            </td>
        </tr>
    <?php endif; ?>

    <?php foreach ($fields as $field) : ?>
        <?php
        $field_name  = 'arraysubs_pf_' . esc_attr($field['key']);
        $field_id    = 'arraysubs-admin-pf-' . esc_attr($field['key']);
        $value       = $user_id > 0 ? arraysubs_get_user_profile_field($user_id, $field['key']) : '';
        $required    = !empty($field['required']);
        $placeholder = esc_attr($field['placeholder'] ?? '');
        ?>
        <tr>
            <th>
                <label for="<?php echo esc_attr($field_id); ?>">
                    <?php echo esc_html($field['label']); ?>
                    <?php if ($required) : ?>
                        <span class="description"><?php esc_html_e('(required)', 'arraysubs'); ?></span>
                    <?php endif; ?>
                </label>
            </th>
            <td>
                <?php
                switch ($field['type']) {
                    case 'text':
                ?>
                        <input
                            type="text"
                            name="<?php echo esc_attr($field_name); ?>"
                            id="<?php echo esc_attr($field_id); ?>"
                            value="<?php echo esc_attr($value); ?>"
                            placeholder="<?php echo esc_attr($placeholder); ?>"
                            class="regular-text"
                            <?php echo $required ? 'required' : ''; ?> />
                    <?php
                        break;

                    case 'textarea':
                    ?>
                        <textarea
                            name="<?php echo esc_attr($field_name); ?>"
                            id="<?php echo esc_attr($field_id); ?>"
                            rows="4"
                            cols="30"
                            placeholder="<?php echo esc_attr($placeholder); ?>"
                            <?php echo $required ? 'required' : ''; ?>><?php echo esc_textarea($value); ?></textarea>
                    <?php
                        break;

                    case 'select':
                    ?>
                        <select
                            name="<?php echo esc_attr($field_name); ?>"
                            id="<?php echo esc_attr($field_id); ?>"
                            <?php echo $required ? 'required' : ''; ?>>
                            <option value=""><?php esc_html_e('Select an option', 'arraysubs'); ?></option>
                            <?php if (!empty($field['options']) && is_array($field['options'])) : ?>
                                <?php foreach ($field['options'] as $option) : ?>
                                    <option
                                        value="<?php echo esc_attr($option['value']); ?>"
                                        <?php selected($value, $option['value']); ?>>
                                        <?php echo esc_html($option['label']); ?>
                                    </option>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    <?php
                        break;

                    case 'date':
                    ?>
                        <input
                            type="date"
                            name="<?php echo esc_attr($field_name); ?>"
                            id="<?php echo esc_attr($field_id); ?>"
                            value="<?php echo esc_attr($value); ?>"
                            class="regular-text"
                            <?php echo $required ? 'required' : ''; ?> />
                    <?php
                        break;

                    case 'checkbox':
                    ?>
                        <label>
                            <input
                                type="checkbox"
                                name="<?php echo esc_attr($field_name); ?>"
                                id="<?php echo esc_attr($field_id); ?>"
                                value="1"
                                <?php checked($value, '1'); ?> />
                            <?php echo esc_html($field['placeholder'] ?? ''); ?>
                        </label>
                        <?php
                        break;

                    case 'upload':
                        if (!empty($value) && $user_id > 0) {
                            $upload_dir = arraysubs_get_profile_upload_dir($user_id);
                            $file_url   = $upload_dir['url'] . '/' . $value;
                        ?>
                            <p>
                                <a href="<?php echo esc_url($file_url); ?>" target="_blank" rel="noopener noreferrer">
                                    <?php echo esc_html(basename($value)); ?>
                                </a>
                            </p>
                            <label>
                                <input type="checkbox" name="arraysubs_remove_file_<?php echo esc_attr($field['key']); ?>" value="1" />
                                <?php esc_html_e('Remove file', 'arraysubs'); ?>
                            </label>
                            <br />
                        <?php
                        }
                        ?>
                        <input
                            type="file"
                            name="<?php echo esc_attr($field_name); ?>"
                            id="<?php echo esc_attr($field_id); ?>" />
                <?php
                        if (!empty($field['allowed_types'])) {
                            echo '<p class="description">';
                            /* translators: %s: comma-separated list of allowed file extensions */
                            printf(esc_html__('Allowed types: %s', 'arraysubs'), esc_html(implode(', ', $field['allowed_types'])));
                            echo '</p>';
                        }
                        break;
                }

                if (!empty($field['help_text'])) {
                    echo '<p class="description">' . esc_html($field['help_text']) . '</p>';
                }
                ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

<?php wp_nonce_field('arraysubs_admin_save_profile_fields', 'arraysubs_admin_profile_nonce'); ?>