<div class="matewa-settings-card">
    <h2><?php echo esc_html($settings_heading); ?></h2>
    <p class="description">
        <?php echo esc_html($settings_description); ?>
    </p>

    <table class="form-table" role="presentation">
        <tr>
            <th scope="row">
                <label for="matewa_number">WhatsApp Number</label>
            </th>
            <td>
                <input
                    type="text"
                    id="matewa_number"
                    name="matewa_number"
                    class="regular-text"
                    value="<?php echo esc_attr($number); ?>"
                    placeholder="e.g. 447900123456"
                />
                <p class="description">
                    Enter the full number with country code.<br>
                    Example: 447900123456, not 07900123456.
                </p>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="matewa_business_name">Business Name</label>
            </th>
            <td>
                <input
                    type="text"
                    id="matewa_business_name"
                    name="matewa_business_name"
                    class="regular-text"
                    value="<?php echo esc_attr($business); ?>"
                />
                <p class="description">Used wherever your business name appears inside the widget experience.</p>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="matewa_leads_email">Lead destination inbox</label>
            </th>
            <td>
                <input
                    type="email"
                    id="matewa_leads_email"
                    name="matewa_leads_email"
                    class="regular-text"
                    value="<?php echo esc_attr($leads_email); ?>"
                    placeholder="e.g. jobs@yourbusiness.com"
                />
                <p class="description">
                    New website leads will be emailed to this inbox. This is not the sender address. Use your sales or enquiries inbox, such as sales@yourdomain.com or hello@yourdomain.com. To send leads somewhere else as well, set up forwarding in your email provider, hosting control panel, Microsoft 365, Google Workspace, or CRM email parser.
                </p>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="matewa_marketing_email">Account / Marketing Email</label>
            </th>
            <td>
                <input
                    type="email"
                    id="matewa_marketing_email"
                    name="matewa_marketing_email"
                    class="regular-text"
                    value="<?php echo esc_attr($marketing_email); ?>"
                    placeholder="e.g. hello@yourbusiness.com"
                />
                <p class="description">
                    Used for account, product, trial, and future 247ChatMate marketing communications. It is not used for customer lead notifications.
                </p>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="matewa_license_key">Licence Key</label>
            </th>
            <td>
                <input
                    type="password"
                    id="matewa_license_key"
                    name="matewa_license_key"
                    class="regular-text"
                    value=""
                    placeholder="<?php echo '' !== $masked_license ? esc_attr('Saved key hidden - enter a new key to replace') : esc_attr('Enter your 247ChatMate licence or access key'); ?>"
                    autocomplete="new-password"
                    spellcheck="false"
                />
                <p class="description">
                    For security, saved keys are hidden after saving and are not printed back into the page. Leave this field blank to keep the saved key, or enter a new key to replace it.
                </p>
                <?php if ('' !== $masked_license) : ?>
                    <div class="matewa-license-meta">
                        Saved key hidden for security.
                    </div>
                <?php endif; ?>
            </td>
        </tr>

        <tr>
            <th scope="row">
                <label for="matewa_industry">Industry</label>
            </th>
            <td>
                <input
                    type="text"
                    id="matewa_industry"
                    name="matewa_industry"
                    class="regular-text"
                    value="<?php echo esc_attr($industry); ?>"
                    placeholder="e.g. Plumber"
                />
                <p class="description">
                    Helps us understand your business category and support future setup improvements.
                </p>
            </td>
        </tr>

        <tr>
            <th scope="row">Enable Widget</th>
            <td>
                <label>
                    <input
                        type="checkbox"
                        name="matewa_widget_settings[enabled]"
                        value="1"
                        <?php checked(($widget['enabled'] ?? '0'), '1'); ?>
                    >
                    Enable chat widget
                </label>
                <p class="description">Turn the widget on or off across your site.</p>
            </td>
        </tr>

        <tr>
            <th scope="row">Widget Page Display</th>
            <td>
                <fieldset class="matewa-display-mode-options">
                    <legend class="screen-reader-text">Widget Page Display</legend>

                    <label class="matewa-checkbox-row">
                        <input
                            type="checkbox"
                            class="matewa-display-mode-checkbox"
                            name="matewa_widget_settings[display_mode]"
                            value="selected"
                            <?php checked($widget_display_mode ?? 'all', 'selected'); ?>
                        >
                        Show widget only on selected pages
                    </label>
                </fieldset>

                <p class="description">Turn this on to show the widget only on selected pages. Leave it off to show the widget everywhere.</p>

                <div class="matewa-page-targeting-box">
                    <strong>Detected pages</strong>
                    <p class="description">Tick the pages where the widget should appear.</p>

                    <div class="matewa-page-checkbox-list" role="group" aria-label="Detected pages">
                        <label class="matewa-page-checkbox-row">
                            <input
                                type="checkbox"
                                name="matewa_widget_settings[include_homepage]"
                                value="1"
                                <?php checked($widget_include_homepage ?? '0', '1'); ?>
                            >
                            <span class="matewa-page-checkbox-main">
                                <span class="matewa-page-checkbox-title">Homepage</span>
                                <span class="matewa-page-checkbox-url"><?php echo esc_html(untrailingslashit(home_url('/'))); ?></span>
                            </span>
                        </label>

                        <?php if (!empty($available_pages ?? [])) : ?>
                            <?php $home_page_id = (int) get_option('page_on_front'); ?>
                            <?php foreach (($available_pages ?? []) as $available_page) : ?>
                                <?php
                                $available_page_id = (int) $available_page->ID;

                                if ($home_page_id > 0 && $available_page_id === $home_page_id) {
                                    continue;
                                }

                                $available_page_title = $available_page->post_title ?: ('Page #' . $available_page_id);
                                $available_page_url = get_permalink($available_page_id);
                                ?>
                                <label class="matewa-page-checkbox-row">
                                    <input
                                        type="checkbox"
                                        name="matewa_widget_settings[page_ids][]"
                                        value="<?php echo esc_attr((string) $available_page_id); ?>"
                                        <?php checked(in_array($available_page_id, ($widget_page_ids ?? []), true)); ?>
                                    >
                                    <span class="matewa-page-checkbox-main">
                                        <span class="matewa-page-checkbox-title"><?php echo esc_html($available_page_title); ?></span>
                                        <span class="matewa-page-checkbox-url"><?php echo esc_html($available_page_url); ?></span>
                                    </span>
                                </label>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
                </div>
            </td>
        </tr>
    </table>
</div>