<?php
defined('ABSPATH') or die('No script kiddies please!!');
$selected_account = (!empty($ciws_floating_settings['select_account']['ciws_floating_account_id'])) ? $ciws_floating_settings['select_account']['ciws_floating_account_id'] : '';
$selected_accounts = (!empty($ciws_floating_settings['select_account']['ciws_floating_account_ids'])) ? $ciws_floating_settings['select_account']['ciws_floating_account_ids'] : '';
$box_status = (!empty($ciws_floating_settings['layout']['box_status'])) ? $ciws_floating_settings['layout']['box_status'] : 'none';

?>
<div class="ciws-settings-section" data-settings-ref="select-account">
    <div class="ciws-field-wrap">
        <label>
            <?php esc_html_e('Choose Box Type', 'chat-in-website'); ?>
        </label>
        <div class="ciws-field">
            <select name=" ciws_floating_settings[layout][box_status]" class="ciws-radio-toggle">
                <option value="none" <?php echo selected($box_status, 'none'); ?>>
                    <?php esc_html_e('Floating Icon', 'chat-in-website'); ?>
                </option>
                <option value="floating" <?php echo selected($box_status, 'floating'); ?>>
                    <?php esc_html_e('Floating Box', 'chat-in-website'); ?>
                </option>
                <option value="greeting" <?php echo selected($box_status, 'greeting'); ?>>
                    <?php esc_html_e('Greeting Box', 'chat-in-website'); ?>
                </option>

            </select>
        </div>

    </div>

    <div
        class="ciws-field-wrap ciws-single-account <?php echo ($box_status == 'none' || $box_status == 'greeting') ? '' : esc_attr("ciws-field-hide"); ?>">
        <label>
            <?php esc_html_e('Select Account', 'chat-in-website'); ?>
        </label>
        <div class="ciws-field ciws-checkbox-toggle">
            <select name="ciws_floating_settings[select_account][ciws_floating_account_id]"
                class="ciws-template-dropdown">

                <?php
                global $wpdb;

                $account_rows = $wpdb->get_results($wpdb->prepare("select * from %i order by account_id desc", CIWS_TABLE));
                if (!empty($account_rows)) {
                    foreach ($account_rows as $account_row) {
                        $ciws_account_details = maybe_unserialize($account_row->ciws_account_details);
                ?>
                        <option value="<?php echo esc_attr($account_row->account_id); ?>"
                            <?php echo selected($selected_account, $account_row->account_id); ?>>
                            <?php echo esc_html($account_row->account_name); ?>
                        </option>
                    <?php }
                } else { ?>

                    <option value="">
                        <?php echo esc_html_e("No accounts added yet", 'chat-in-website'); ?>
                    </option>
                <?php } ?>
            </select>

        </div>
    </div>
    <div
        class="ciws-field-wrap ciws-mulitple-account <?php echo ($box_status == 'floating') ? '' : esc_attr("ciws-field-hide"); ?>">
        <label>
            <?php esc_html_e('Select Account', 'chat-in-website'); ?>
        </label>
        <div class="ciws-field">
            <input type="text" id="ciws-account-search" placeholder="Search Accounts..." class="ciws-template-dropdown">
            <div id="ciws-account-dropdown" class="ciws-dropdown-list"></div>

            <ul id="ciws-selected-accounts" class="ciws-selected-accounts-list">
                <?php
                $acc_ids = explode(",", $selected_accounts);

                foreach ($acc_ids as $acc_id) {
                    global $wpdb;

                    $account_row = $wpdb->get_row($wpdb->prepare("select * from %i where account_id = %d", CIWS_TABLE, $acc_id));
                    if (!empty($account_row)) {
                ?>
                        <li class="ciws-selected-account-item" data-id="<?php echo esc_attr($account_row->account_id); ?>">
                            <?php echo esc_html($account_row->account_name); ?>
                            <button class="ciws-remove-account"><span class="dashicons dashicons-trash"></span></button>
                        </li>
                <?php }
                }
                ?>
            </ul>
        </div>



        <!-- Hidden input field -->
        <input type="hidden" name="ciws_floating_settings[select_account][ciws_floating_account_ids]"
            id="ciws-selected-accounts-input" value="<?php echo esc_attr($selected_accounts); ?>">



    </div>

</div>