<?php

namespace GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormControls;

class InputPhone extends \GriffinForms\Admin\App\Html\Widgets\PresentationArea\FormControls\Format
{
    public function getHtml()
    {
        $this->renderFieldStyles(array('text_input', 'select_input'));

        $control_id = $this->getFieldControlId('phone');
        $select_id = 'gf-admin-phone-country-' . $this->item_id;
        $attributes = $this->getFieldStyleAttributes('text_input');
        $placeholder = $this->getFieldPlaceholder();
        if ($placeholder !== '') {
            $attributes .= ' placeholder="' . esc_attr($placeholder) . '"';
        }
        $field_settings = $this->item->getProp('field_settings') ?? array();
        if (is_string($field_settings)) {
            $decoded = maybe_unserialize($field_settings);
            if (is_array($decoded)) {
                $field_settings = $decoded;
            }
        }
        $display_mode = is_array($field_settings) && !empty($field_settings['phone_display_mode'])
            ? $field_settings['phone_display_mode']
            : 'flag_code';

        $validations = $this->item->getProp('validations') ?? array();
        if (is_string($validations)) {
            $decoded = maybe_unserialize($validations);
            if (is_array($decoded)) {
                $validations = $decoded;
            }
        }
        $allowed_codes = isset($validations['allowed_country_codes']) && is_array($validations['allowed_country_codes'])
            ? $validations['allowed_country_codes']
            : array();
        $countries = \GriffinForms\Includes\Helpers\PhoneCountries::getCountries($allowed_codes);
        $default_code = '';
        if (!empty($allowed_codes) && count($allowed_codes) === 1) {
            $default_code = '+' . ltrim((string) $allowed_codes[0], '+');
        }

        echo '<div class="input-group griffinforms-phone-input-group">';
        $select_attributes = $this->getFieldStyleAttributes('select_input');
        echo '<select class="form-select griffinforms-phone-country-select" id="' . esc_attr($select_id) . '" data-gf-phone-display-mode="' . esc_attr($display_mode) . '"' . $select_attributes . '>';
        echo '<option value="">' . esc_html__('Country', 'griffinforms-form-builder') . '</option>';
        foreach ($countries as $country) {
            $selected = ($default_code !== '' && $country['dial_code'] === $default_code) ? ' selected' : '';
            $label = $country['dial_code'];
            if ($display_mode === 'flag_code') {
                $label = $country['flag'] . ' ' . $country['dial_code'];
            } elseif ($display_mode === 'flag_code_name') {
                $label = $country['flag'] . ' ' . $country['dial_code'] . ' ' . $country['name'];
            }
            echo '<option value="' . esc_attr($country['dial_code']) . '" data-iso="' . esc_attr($country['iso']) . '" data-flag="' . esc_attr($country['flag']) . '" data-name="' . esc_attr($country['name']) . '"' . $selected . '>';
            echo esc_html($label);
            echo '</option>';
        }
        echo '</select>';
        echo '<input type="tel" id="' . esc_attr($control_id) . '" class="form-control"' . $attributes . '>';
        echo '</div>';
    }
}
