<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
$languages_array = doobert_load_all_languages_for_user($doobert_api_token_value);
$is_true = 0;
if (!empty($languages_array) && isset($languages_array->status)) {
    $is_true = (int) $languages_array->status;
}
$saved_email = get_option('doobert_audit_email');
if ($is_true == 1) {
    $total_allowed_languages = isset($languages_array->allowed_languages) ? $languages_array->allowed_languages : 1;
    $uploaded_languages = isset($languages_array->uploaded_languages) ? $languages_array->uploaded_languages : '';
    global $wpdb;
    $table_name = $wpdb->prefix . 'doobert_translations_data';
    $existing_language = $wpdb->get_results(
        "SELECT * FROM {$wpdb->prefix}doobert_translations_data"
    );
    $already_saved_languages = array();
    $allowed_languages = (array) $languages_array->available_languages;
    $exisiting_languages_count = 0;
    $pageurl = admin_url('admin.php') . '?page=doobert-chatbot-settings&tab=premium-settings';
    $pageurl = 'https://app.doobert.com/external-call?redirect_url=' . esc_url($pageurl) . '&dbrt_url=https://app.doobert.com/chatbot-additional-languages';
?>
    <div id="langaugeModal" class="langauge-modal">
        <div class="langauge-modal-content">
            <div class="success_response">
                <div style="margin-bottom: 45px;text-align:center;margin-right:40px;"><span class="dashicons dashicons-yes-alt"></span></div>
                <p><b><?php echo esc_html__('Update in progress!', 'doobert-chatbot'); ?></b></p>
                <p><?php echo esc_html__('Please note that these changes may take up to', 'doobert-chatbot'); ?>
                    <?php echo esc_html__('2 hours to be reflected in the chatbot.', 'doobert-chatbot'); ?>
                </p>
            </div>
            <div class="error_response">
                <div style="margin-bottom: 45px;text-align:center;margin-right:40px;"><span class="dashicons dashicons-no"></span></div>
                <p><b><?php echo esc_html__('Error!', 'doobert-chatbot'); ?></b></p>
                <p class="error_msgs_response"></p>
            </div>

            <div class="close_popup">
                <div class="close_popup_btn button button-primary" style="background: #ec5832; border-color: #ec5832;">Ok, got it!</div>
            </div>
        </div>
    </div>
    <div class="wrap_doobert">
        <h3><?php echo esc_html__('PetCo Love Settings', 'doobert-chatbot'); ?></h3>
        <div class="sub_div_premium">
            <table class="form-table">
                <tr>
                    <td colspan="2" style="padding-left: 25px;">
                        <?php echo esc_html__('By enabling Petco Love, your chatbot will automatically help your users by posting their lost or found pets on the Petco Love website and deliver search results for pets they’ve reported as lost or found.', 'doobert-chatbot'); ?>
                    </td>
                </tr>
                <tr>
                    <th class="th_padding">
                        <?php echo esc_html__('Disable/Enable', 'doobert-chatbot'); ?>
                    </th>
                    <td style="vertical-align: middle;">
                        <select name="petco_love" id="petco_love_id">
                            <option <?php selected($petco_love, '1'); ?> value="1"><?php echo esc_html__('Enable', 'doobert-chatbot'); ?></option>
                            <option <?php selected($petco_love, '0'); ?> value="0"><?php echo esc_html__('Disable', 'doobert-chatbot'); ?></option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style=" padding-left: 25px;">
                        <input type="button" class="button button-primary doobert_petcolove_setting_save" value="<?php esc_attr_e('Save', 'doobert-chatbot'); ?>">
                    </td>
                </tr>
            </table>
        </div>
        <h2><?php echo esc_html__('Adopt a Pet Settings', 'doobert-chatbot'); ?></h2>
        <div class="sub_div_premium">
            <table class="form-table">
                <tr>
                    <td colspan="2" style="padding-left: 25px;">
                        <?php echo esc_html__('When you enable Adopt a Pet, your chatbot will automatically help customers search for animals in their local shelter.', 'doobert-chatbot'); ?>
                    </td>
                </tr>
                <tr>
                    <th class="th_padding"><?php echo esc_html__('Disable/Enable', 'doobert-chatbot'); ?></th>
                    <td>
                        <select name="adopt_pet" id="adopt_pet_id">
                            <option <?php selected($adopt_pet, '1'); ?> value="1"><?php echo esc_html__('Enable', 'doobert-chatbot'); ?></option>
                            <option <?php selected($adopt_pet, '0'); ?> value="0"><?php echo esc_html__('Disable', 'doobert-chatbot'); ?></option>
                        </select>
                    </td>
                </tr>
                <tr class="enable_petcolove">
                    <th class="th_padding"><?php echo esc_html__('Shelter Zipcode', 'doobert-chatbot'); ?></th>
                    <td><input type="text" name="shelter_zipcode" id="shelter_zipcode" placeholder="<?php echo esc_html__('Shelter Zipcode', 'doobert-chatbot'); ?>" value="<?php echo esc_attr($shelter_zipcode); ?>"></td>
                </tr>
                <tr class="enable_petcolove">
                    <th class="th_padding"><?php echo esc_html__('Shelter Id', 'doobert-chatbot'); ?></th>
                    <td>
                        <select id="shelter_ids" name="shelter_ids[]" multiple="multiple" style="width: 100%">
                            <?php
                            if (!empty($shelter_ids)) {
                                $shelter_ids = explode(',', $shelter_ids);
                                foreach ($shelter_ids as $val) {
                            ?>
                                    <option value="<?php echo esc_attr($val); ?>" selected=""><?php echo esc_attr($val); ?></option>
                            <?php
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr class="enable_petcolove">
                    <th></th>
                    <td>
                        <img id="adopt_image_id" <?php echo empty($adopt_image) ? 'style="display:none"' : ''; ?> src="<?php echo esc_attr($adopt_image); ?>" width="120">
                    </td>
                </tr>
                <tr class="enable_petcolove">
                    <th class="th_padding"><?php echo esc_html__('Organization Logo', 'doobert-chatbot'); ?></th>
                    <td>
                        <label for="upload_image">
                            <input id="adopt_image" type="hidden" size="36" name="adopt_image" value="<?php echo esc_attr($adopt_image); ?>" />
                            <input id="upload_image_button" class="button" type="button" value="Upload Image" />

                        </label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="padding-left: 25px;">
                        <input type="button" class="button button-primary doobert_adopt_setting_save" value="<?php esc_attr_e('Save', 'doobert-chatbot'); ?>">
                    </td>
                </tr>
            </table>
        </div>
        <h3><?php echo esc_html__('Multiple Languages', 'doobert-chatbot'); ?></h3>
        <div class="sub_div_premium div_left_padding">

            <p style="max-width: 800px;"><?php echo esc_html__('Your chatbot will default to the English language. If you’d like to offer another language to your users, please select it from the dropdown and
click the ‘Add’ button. When done reviewing the details, click the ‘Confirm & Update’ button. Once updated, your users will now see a flag in
the upper right corner of the chatbot which can be clicked on to choose which language they’d like to converse in.
', 'doobert-chatbot'); ?></p>

            <p style="max-width: 800px;"><?php echo esc_html__('If you need to offer more languages to your users beyond English and your first chosen language, you can purchase more', 'doobert-chatbot'); ?> <a href="<?php echo esc_url($pageurl); ?>"><?php echo esc_html__('here.', 'doobert-chatbot'); ?></a></p>

            <?php
            if (!empty($existing_language)) {
            ?>
                <h3><?php echo esc_html__('Existing Languages', 'doobert-chatbot'); ?></h3>
                <?php
                foreach ($existing_language as $lang) {
                    $id = isset($lang->id) ? $lang->id : '';
                    $lang_code = isset($lang->language) ? $lang->language : '';
                    $translations = isset($lang->translations) ? $lang->translations : '';
                    $already_saved_languages[$lang_code] = $lang_code;
                    if (!empty($translations)) {
                        $translations = unserialize($translations);
                    } else {
                        $translations = array();
                    }
                    $status = isset($lang->status) ? $lang->status : '';
                    $title_translation = isset($translations['title_translation']) ? sanitize_text_field($translations['title_translation']) : '';
                    $placeholder_translation = isset($translations['placeholder_translation']) ? sanitize_text_field($translations['placeholder_translation']) : '';
                    $default_message_translation = isset($translations['default_message_translation']) ? sanitize_text_field($translations['default_message_translation']) : '';
                    $default_options_translation = isset($translations['default_options_translation']) ? sanitize_text_field($translations['default_options_translation']) : '';
                    $exisiting_languages_count = $exisiting_languages_count + 1;
                ?>
                    <div class="add_language_div update_language">
                        <div class="add_language_inner">
                            <div class="add_lan_title">
                                <?php echo esc_html__('Selected Language', 'doobert-chatbot'); ?>
                            </div>
                            <div class="select_lang">
                                <select name="language_select_options" class="language_select_options" disabled="">

                                    <?php
                                    foreach ($allowed_languages as $key => $name) {
                                        $text = isset($name->name) ? sanitize_text_field($name->name) : '';
                                    ?>
                                        <option <?php selected($key, $lang_code); ?> value="<?php echo esc_attr($key); ?>"><?php echo esc_html($text); ?></option>
                                    <?php
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="select_lang_btn">
                                <div class="change_add_button button button-primary"><?php echo esc_html__('Edit', 'doobert-chatbot'); ?></div>
                            </div>
                        </div>
                        <div class="details_enter_div hide_it">
                            <p><?php echo esc_html__('Details entered below will only display to the user after they’ve selected this language.', 'doobert-chatbot'); ?></p>
                            <table class="form-table">
                                <tr>
                                    <th><?php echo esc_html__('Status', 'doobert-chatbot'); ?></th>
                                    <td>
                                        <select class="language_status">
                                            <option <?php selected('enable', $status); ?> value="enable"><?php echo esc_html__('Enable', 'doobert-chatbot'); ?></option>
                                            <option <?php selected('disable', $status); ?> value="disable"><?php echo esc_html__('Disable', 'doobert-chatbot'); ?></option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th><?php echo esc_html__('Header Title', 'doobert-chatbot'); ?></th>
                                    <td>
                                        <input type="text" name="title_translation" class="title_translation" value="<?php echo esc_attr($title_translation); ?>" />
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php echo esc_html__('Send Message Input Placeholder', 'doobert-chatbot'); ?></th>
                                    <td>
                                        <input type="text" name="placeholder_translation" class="placeholder_translation" value="<?php echo esc_attr($placeholder_translation); ?>" />
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php echo esc_html__('Default Quick Question Prompt ', 'doobert-chatbot'); ?></th>
                                    <td>
                                        <textarea name="default_message_translation" class="default_message_translation" cols="35" rows="4"><?php echo esc_attr($default_message_translation); ?></textarea>
                                    </td>
                                </tr>

                                <tr>
                                    <th><?php echo esc_html('Default Options to Show  (separate them by ; operator)'); ?></th>
                                    <td>
                                        <textarea name="default_options_translation" class="default_options_translation" cols="35" rows="4"><?php echo esc_attr($default_options_translation); ?></textarea>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <div class="close_btn_lang button button-primary"><?php echo esc_html__('Cancel', 'doobert-chatbot'); ?></div>
                                    </td>

                                    <td style="text-align: right;">
                                        <div class="button button-primary edit_translation" data-id="<?php echo esc_attr($id); ?>"><?php echo esc_html__('Save Translation', 'doobert-chatbot'); ?></div>
                                    </td>
                                </tr>

                            </table>
                        </div>
                    </div>
            <?php
                }
            }
            ?>
            <hr><!-- comment -->
            <h3><?php echo esc_html__('Add New Language', 'doobert-chatbot'); ?></h3>
            <div class="add_language_div add_language_div_section">
                <div class="add_language_inner">
                    <div class="add_lan_title">
                        <?php echo esc_html__('Select Additional Language', 'doobert-chatbot'); ?>
                    </div>
                    <div class="select_lang">

                        <select name="language_select_options" class="language_select_options language_select_options_new">
                            <option value=""><?php echo esc_html__('Select Language', 'doobert-chatbot'); ?></option>
                            <?php
                            foreach ($allowed_languages as $key => $lang) {
                                $disabled = '';
                                if (!empty($already_saved_languages) && in_array($key, $already_saved_languages)) {
                                    $disabled = 'disabled=""';
                                }
                                //                            if ($total_allowed_languages == 1 && $key != 'spanish') {
                                //                                $disabled = 'disabled=""';
                                //                            }
                                if ($key != '' && $exisiting_languages_count == $total_allowed_languages) {
                                    $disabled = 'disabled=""';
                                }
                            ?>
                                <option <?php echo esc_attr($disabled); ?> value="<?php echo esc_attr($key); ?>"><?php echo esc_html($lang->name); ?></option>
                            <?php
                            }
                            ?>
                        </select>

                    </div>
                    <div class="select_lang_btn">
                        <div class="lang_add_button button button-primary"><?php echo esc_html__('Add', 'doobert-chatbot'); ?></div>
                    </div>
                </div>
                <div class="details_enter_div hide_it">
                    <p><?php echo esc_html__('Details entered below will only display to the user after they’ve selected this language.', 'doobert-chatbot'); ?></p>
                    <table class="form-table">
                        <tr>
                            <th><?php echo esc_html__('Header Title', 'doobert-chatbot'); ?></th>
                            <td>
                                <input type="text" name="title_translation" class="title_translation" />
                            </td>
                        </tr>

                        <tr>
                            <th><?php echo esc_html__('Send Message Input Placeholder', 'doobert-chatbot'); ?></th>
                            <td>
                                <input type="text" name="placeholder_translation" class="placeholder_translation" />
                            </td>
                        </tr>

                        <tr>
                            <th><?php echo esc_html__('Default Quick Question Prompt ', 'doobert-chatbot'); ?></th>
                            <td>
                                <textarea name="default_message_translation" class="default_message_translation" cols="35" rows="4"></textarea>
                            </td>
                        </tr>

                        <tr>
                            <th><?php echo esc_html('Default Options to Show  (separate them by ; operator)'); ?></th>
                            <td>
                                <textarea name="default_options_translation" class="default_options_translation" cols="35" rows="4"></textarea>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <div class="close_btn_lang button button-primary"><?php echo esc_html__('Cancel', 'doobert-chatbot'); ?></div>
                            </td>

                            <td style="text-align: right;">
                                <div class="button button-primary save_new_translation"><?php echo esc_html__('Save Translation', 'doobert-chatbot'); ?></div>
                            </td>
                        </tr>

                    </table>
                </div>
            </div>

            <br><br>
        </div>

        <?php

        $args = [
            'post_type'      => 'attachment',
            'post_mime_type' => 'application/pdf',
            'posts_per_page' => -1,
            'post_status'    => 'inherit',
            'meta_query'     => [
                [
                    'key'     => '_doobert_filename_pdf',
                    'compare' => 'EXISTS'
                ]
            ]
        ];

        $pdfs = get_posts($args);

        ?>
        <h3><?php echo esc_html__('Upload Offline Content', 'doobert-chatbot'); ?></h3>
        <div class="sub_div_premium div_left_padding">


            <div class="pdf_upload_section">
                <p style="max-width: 800px;"><?php echo esc_html__('Use this functionality if you’re looking to upload content that might not be on your current website, but would be helpful to your chatbot users.
For example, let’s pretend you’ve uploaded your organization’s foster manual below. Now, when a user asks a question via the chatbot, the
system will comb through your website, as well as the uploaded manual for answers. If there is relative information found within the file, the
chatbot will respond to the user with that information, as well as a hyperlink for the file.
', 'doobert-chatbot'); ?></p>

                <br>
                <div class="select_language_pdf">
                    <label><?php echo esc_html__('Choose Language', 'doobert-chatbot'); ?></label>
                    <br>
                    <select id="language_for_pdf" name="language_for_pdf" multiple="">
                        <?php
                        if (!empty($existing_language)) {
                            foreach ($existing_language as $lang) {
                                $code = $lang->language;
                                $name = $allowed_languages[$code]->name;
                        ?>
                                <option value="<?php echo esc_attr($code); ?>"><?php echo esc_attr($name); ?></option>
                            <?php
                            }
                        } else {
                            $code = 'english';
                            $name = 'Do not translate';
                            ?>
                            <option value="<?php echo esc_attr($code); ?>"><?php echo esc_attr($name); ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </div>
                <br><!-- comment -->
                <?php $disabled = '';
                if (count($pdfs) > 9) {
                    $disabled = 'disabled';
                } ?>
                <div class="upload_files_btn_section">
                    <input type="file" id="doobert_pdf_upload" accept="application/pdf" style="display:none;" />
                    <div class="button button-primary upload_files_btn <?php echo $disabled; ?>"><?php echo esc_html__('Add Files', 'doobert-chatbot'); ?></div>
                    <div>
                        <div class="in_headings"><?php echo esc_html__('Accepted Files: PDF', 'doobert-chatbot'); ?></div>
                        <div class="in_headings"><?php echo esc_html__('Max Size Per File: 20MB', 'doobert-chatbot'); ?></div>
                    </div>
                </div>
                <div class="doobert_selected_file"></div>
                <div class="doobert_selected_file_error"></div>


                <?php
                echo '<table style="width:400px;" id="pdf-list" border="1" cellpadding="5" cellspacing="0">';
                echo '<thead>';
                echo '<tr>';
                echo '<th style="width: 85%;">File</th>';
                echo '<th style="width: 15%;">Action</th>';
                echo '</tr>';
                echo '</thead>';
                echo '<tbody>';
                $iii = 1;
                if ($pdfs) {
                    foreach ($pdfs as $pdf) {
                        $url = wp_get_attachment_url($pdf->ID);
                        echo '<tr id="pdf-' . esc_attr($pdf->ID) . '">';
                        echo '<td><a href="' . esc_url($url) . '" download>' . esc_html($pdf->post_title) . '</a></td>'; // Title & link
                        echo '<td><button class="delete-pdf" data-id="' . esc_attr($pdf->ID) . '">Delete</button></td>'; // Delete button
                        echo '</tr>';
                    }
                } else {
                    echo '<tr><td colspan="2" style="text-align:center;">No PDFs found.</td></tr>';
                }
                echo '</tbody></table>';
                ?>
                <br>
            </div>
        </div>
    </div>

    <?php
    $posts = get_posts(array(
        'post_type' => array('post', 'page'),
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC',
    ));
    $exlude_posts = get_option('_doobert_excluded_posts_from_api');
    ?>
    <h3><?php echo esc_html__('Other Configurations', 'doobert-chatbot'); ?></h3>
    <div class="sub_div_premium div_left_padding">

        <table class="form-table">
            <tr>
                <th><?php echo esc_html__('Exclude certain pages of your website from chatbot response.', 'doobert-chatbot'); ?></th>
                <td>
                    <select name="pages_to_exclude[]" id="pages_to_exclude" multiple="">
                        <?php
                        foreach ($posts as $post) {
                            $selected = "";
                            if (!empty($exlude_posts) && is_array($exlude_posts) && in_array($post->ID, $exlude_posts)) {
                                $selected = 'selected=""';
                            }
                        ?>
                            <option <?php echo esc_attr($selected); ?> value="<?php echo esc_attr($post->ID); ?>"><?php echo esc_html($post->post_title); ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="button button-primary pages_to_exclude_save"><?php echo esc_html__('Save Settings', 'doobert-chatbot'); ?></div>
                </td>
            </tr>
        </table>
    </div>

    <!--     <div class="wrap">
        <h3><?php // echo esc_html__('Monthly Audit Log', 'doobert-chatbot'); 
            ?></h3>
        <p><?php // echo esc_html__('Opt in to receive a monthly email report containing a detailed audit log of the questions asked and', 'doobert-chatbot'); 
            ?><br>
            <?php // echo esc_html__('responses provided by our chatbot. This report will help you keep track of interactions, ensuring', 'doobert-chatbot'); 
            ?><br>
            <?php // echo esc_html__('transparency and improving overall service quality.', 'doobert-chatbot'); 
            ?><br>
        </p>
        <h4><?php // echo esc_html__('Email Address', 'doobert-chatbot'); 
            ?></h4>
        <input type="email" id="doobert_monthly_audit_email" value="<?php // echo esc_attr($saved_email); 
                                                                    ?>"/><br>
        <div class="button button-primary doobert_monthly_audit_email_save"><?php // echo esc_html__('Save', 'doobert-chatbot'); 
                                                                            ?></div>
    </div> -->

<?php
    wp_enqueue_script('admin_doobert_language_js', DOOBERT_CHAPI_URL . 'assets/admin/doobert-admin-language.js', array('jquery'), '3.0', true);
    wp_localize_script('admin_doobert_language_js', 'obj', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'admin_url' => admin_url(),
        'security' => wp_create_nonce("verify_doobert_translaton_nonce"),
        'total_allowed' => $total_allowed_languages,
        'total_avaible' => $exisiting_languages_count,
    ));
}
