<?php
defined('ABSPATH') or die('No script kiddies please!!');

$ciws_main_settings = get_option('ciws_main_settings');

$selected_floating_templates = (!empty($ciws_floating_settings['layout']['floating_template'])) ? esc_attr($ciws_floating_settings['layout']['floating_template']) : 'ciws-floating-template-1';
$selected_floating_box_templates = (!empty($ciws_floating_settings['layout']['floating_box_template'])) ? esc_attr($ciws_floating_settings['layout']['floating_box_template']) : 'ciws-box-template-1';
$button_head = (!empty($ciws_floating_settings['layout']['button_heading'])) ? esc_attr($ciws_floating_settings['layout']['button_heading']) : 'Need Help? Chat with us';
$floating_title_text = (!empty($ciws_floating_settings['layout']['floating_title_text'])) ? esc_attr($ciws_floating_settings['layout']['floating_title_text']) : '';
$floating_sub_title_text = (!empty($ciws_floating_settings['layout']['floating_sub_title_text'])) ? esc_attr($ciws_floating_settings['layout']['floating_sub_title_text']) : '';
$floating_reply_info_text = (!empty($ciws_floating_settings['layout']['floating_reply_info_text'])) ? esc_attr($ciws_floating_settings['layout']['floating_reply_info_text']) : '';
$floating_position = (!empty($ciws_floating_settings['layout']['floating_position'])) ? esc_attr($ciws_floating_settings['layout']['floating_position']) : 'ciws-floating-right';
$customize = (!empty($ciws_floating_settings['layout']['customize_status'])) ? esc_attr($ciws_floating_settings['layout']['customize_status']) : '';
$floating_button_fill_color = (!empty($ciws_floating_settings['layout']['button_fill_color'])) ? esc_attr($ciws_floating_settings['layout']['button_fill_color']) : '';
$floating_button_title_color = (!empty($ciws_floating_settings['layout']['button_title_color'])) ? esc_attr($ciws_floating_settings['layout']['button_title_color']) : 'ciws-floating-right';

$hide_desktop = (!empty($ciws_main_settings['display']['desktop'])) ? esc_attr('ciws-desktop-hide') : '';
$hide_mobile = (!empty($ciws_main_settings['display']['mobile'])) ? esc_attr('ciws-mob-hide') : '';
$url_open_tab = (!empty($ciws_main_settings['url']['open_in_new_tab'])) ? 1 : 0;
$url_for_desktop = (!empty($ciws_main_settings['url']['url_for_desktop'])) ? $ciws_main_settings['url']['url_for_desktop'] : 'web';
$url_for_mobile = (!empty($ciws_main_settings['url']['url_for_mobile'])) ? $ciws_main_settings['url']['url_for_mobile'] : 'api';
$default_text = (!empty($account_details['account']['default_msg'])) ? $account_details['account']['default_msg'] : '';
$account_number = !empty($account_details['account']['account_number']) ? $account_details['account']['account_number'] : '';
$icon_slug = !empty($ciws_floating_settings['layout']['icon_img']) 
    ? esc_attr($ciws_floating_settings['layout']['icon_img']) 
    : 'ciws-whatsapp';

// Construct the image URL
$icon_url = esc_url(CIWS_IMG_DIR . '/' . $icon_slug . '.svg');

$box_status = (!empty($ciws_floating_settings['layout']['box_status'])) ? $ciws_floating_settings['layout']['box_status'] : 'none';

include('ciws-url-link.php');
$selected_accounts = (!empty($ciws_floating_settings['select_account']['ciws_floating_account_ids'])) ? $ciws_floating_settings['select_account']['ciws_floating_account_ids'] : '';

?>
<div
    class="ciws-floating-button-wrapper <?php echo esc_attr($floating_position); ?> <?php echo esc_attr($selected_floating_templates); ?> <?php echo esc_attr($hide_desktop); ?> <?php echo esc_attr($hide_mobile); ?>">
    <div class="ciws-floating-button">

        <?php
        if ($box_status == 'floating' || $box_status == 'greeting') {

            $acc_ids = explode(",", $selected_accounts);


        ?>

            <div class="ciws-floating-box-wrap">
                <div class="ciws-floating-template-check <?php echo esc_attr($selected_floating_box_templates); ?>">
                    <?php
                    if ($customize == 1) { ?>
                        <div class="ciws-box-header-wrap"
                            style="background:<?php echo esc_attr($floating_button_fill_color); ?>;">
                        <?php } else { ?>
                            <div class="ciws-box-header-wrap">
                            <?php } ?>
                            <div class="ciws-header-img-wrap">
                                <div class="ciws-heading-img">
                                    <img src="<?php echo esc_url(CIWS_IMG_DIR . '/whats-app.png'); ?>"
                                        class="ciws-box-header-logo-white">
                                    <img src="<?php echo esc_url(CIWS_IMG_DIR . '/whatsapp-green.png'); ?>"
                                        class="ciws-box-header-logo-green">
                                </div>
                            </div>
                            <div class="ciws-heading-desc">
                                <div class="ciws-box-heading">
                                    <?php
                                    if ($customize == 1) { ?>
                                        <h1 class="ciws-floating-box-text"
                                            style="color: <?php echo esc_attr($floating_button_title_color); ?>">
                                            <?php echo esc_html($floating_title_text); ?>
                                        </h1>
                                    <?php } else { ?>
                                        <h1 class="ciws-floating-box-text"><?php echo esc_html($floating_title_text); ?></h1>
                                    <?php } ?>

                                </div>
                                <div class="ciws-box-sub-heading">
                                    <?php
                                    if ($customize == 1) { ?>
                                        <h3 class="ciws-floating-box-sub-text"
                                            style="color: <?php echo esc_attr($floating_button_title_color); ?>">
                                            <?php echo esc_html($floating_sub_title_text); ?>
                                        </h3>
                                    <?php } else { ?>
                                        <h3 class="ciws-floating-box-sub-text">
                                            <?php echo esc_html($floating_sub_title_text); ?>
                                        </h3>
                                    <?php } ?>

                                </div>
                            </div>
                            <div class="ciws-close-btn"><img data-logo=""
                                    src="<?php echo esc_url(CIWS_IMG_DIR . '/ciws-close.svg'); ?>" class="ciws-close-svg" />
                            </div>
                            </div>
                            <div class="ciws-box-body-wrap">
                                <div class="ciws-reply-txt"><span><?php echo esc_html($floating_reply_info_text); ?><span>
                                </div>
                                <?php
                                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));

                                    $acc_details = (!empty($account_row->ciws_account_details)) ? $account_row->ciws_account_details : '';

                                    $ciws_account_details = maybe_unserialize($acc_details);

                                    if (!empty($ciws_account_details)) {
                                        $account_profile = !empty($ciws_account_details['account']['profile_photo']) ? $ciws_account_details['account']['profile_photo'] : '';
                                        $each_account_number = !empty($ciws_account_details['account']['account_number']) ? $ciws_account_details['account']['account_number'] : [];


                                        $default_text = !empty($ciws_account_details['account']['default_msg']) ? $ciws_account_details['account']['default_msg'] : '';
                                        $offline_text = !empty($ciws_account_details['account']['ofline_text']) ? $ciws_account_details['account']['ofline_text'] : 'I will be back soon!';
                                        $online_text = !empty($ciws_account_details['account']['online_text']) ? $ciws_account_details['account']['online_text'] : 'Online';
                                        $account_online = !empty($ciws_account_details['account']['online']) ? 1 : 0; // Always online option


                                        // Generate WhatsApp URLs
                                        $wh_url = '';
                                        $account_number = str_replace('+', '', $each_account_number);
                                        if ($url_for_desktop == 'web') {
                                            // WhatsApp Web/Desktop URL
                                            $wh_url = "https://web.whatsapp.com/send?phone=" . $account_number;

                                            if (!empty($default_text)) {
                                                $wh_url .= "&text=" . $default_text;
                                            }
                                        } elseif ($url_for_desktop == 'api' && !empty($account_number)) {
                                            // WhatsApp Moble URL

                                            $wh_url = "https://api.whatsapp.com/send?phone=" . $account_number;
                                            if (!empty($default_text)) {
                                                $wh_url .= "&text=" . $default_text;
                                            }
                                            $wh_url .= "&type=phone_number&app_absent=0";
                                        }

                                        $mob_wh_url = '';
                                        if ($url_for_mobile == 'protocol') {
                                            // WhatsApp Web/Desktop URL
                                            $mob_wh_url = "https://web.whatsapp.com/send?phone=" . $account_number;

                                            if (!empty($default_text)) {
                                                $mob_wh_url .= "&text=" . $default_text;
                                            }
                                        } elseif ($url_for_mobile == 'api' && !empty($account_number)) {
                                            // WhatsApp Moble URL
                                            $mob_wh_url = "https://api.whatsapp.com/send?phone=" . $account_number;
                                            if (!empty($default_text)) {
                                                $mob_wh_url .= "&text=" . $default_text;
                                            }
                                            $mob_wh_url .= "&type=phone_number&app_absent=0";
                                        }

                                        // If unavailable, set URLs to '#'
                                        if (!$account_online) {
                                            $wh_url = '#';
                                            $mob_wh_url = '#';
                                        }

                                ?>
                                        <div class="ciws-contact-list-wrap">
                                            <a href="<?php echo esc_attr($wh_url); ?>"
                                                target="<?php echo esc_attr(($account_online && $url_open_tab == 1) ? '_blank' : ''); ?>"
                                                class="ciws-box-desktop">
                                                <div class="ciws-contact-list">
                                                    <div class="ciws-contact-img">
                                                        <?php
                                                        if (!empty($account_profile)) { ?>
                                                            <img src="<?php echo esc_url($account_profile); ?>" alt="ciws-account-image"
                                                                class="ciws-img-avt">
                                                        <?php } else { ?>
                                                            <img src="<?php echo esc_url(CIWS_IMG_DIR . '/whats-app.png'); ?>"
                                                                class="ciws-box-header-logo-white">
                                                        <?php } ?>
                                                        <div
                                                            class="ciws-online-status <?php echo esc_attr($account_online ? 'ciws-online' : 'ciws-offline'); ?>">
                                                        </div>
                                                    </div>
                                                    <div class="ciws-contact-discription">
                                                        <div class="ciws-contact-name">
                                                            <?php echo esc_html($account_row->account_name); ?>
                                                        </div>
                                                        <div class="ciws-contact-deg">
                                                            <?php echo (!empty($ciws_account_details['account']['title'])) ? esc_html($ciws_account_details['account']['title']) : ''; ?>
                                                        </div>
                                                        <div class="ciws-online-desc">
                                                            <?php echo $account_online ? esc_html($online_text) : esc_html($offline_text); ?>
                                                        </div>
                                                    </div>
                                                    <div
                                                        class="ciws-online-status <?php echo esc_attr($account_online ? 'ciws-online' : 'ciws-offline'); ?>">
                                                    </div>
                                                </div>
                                            </a>
                                            <a href="<?php echo esc_attr($mob_wh_url); ?>"
                                                target="<?php echo esc_attr(($account_online && $url_open_tab == 1) ? '_blank' : ''); ?>"
                                                class="ciws-box-mobile">
                                                <div class="ciws-contact-list">
                                                    <div class="ciws-contact-img">

                                                        <?php
                                                        if (!empty($account_profile)) { ?>
                                                            <img src="<?php echo esc_url($account_profile); ?>" alt="<?php echo esc_attr($account_row->account_name); ?>"
                                                                class="ciws-img-avt">
                                                        <?php } else { ?>
                                                            <img src="<?php echo esc_url(CIWS_IMG_DIR . '/whats-app.png'); ?>"
                                                                class="ciws-box-header-logo-white">
                                                        <?php } ?>
                                                    </div>
                                                    <div class="ciws-contact-discription">
                                                        <div class="ciws-contact-name">
                                                            <?php echo esc_html($account_row->account_name); ?>
                                                        </div>
                                                        <div class="ciws-contact-deg">
                                                            <?php echo (!empty($ciws_account_details['account']['title'])) ? esc_html($ciws_account_details['account']['title']) : ''; ?>
                                                        </div>
                                                        <div class="ciws-online-desc">
                                                            <?php echo $account_online ? esc_html($online_text) : esc_html($offline_text); ?>
                                                        </div>
                                                    </div>
                                                    <div
                                                        class="ciws-online-status <?php echo esc_attr($account_online ? 'ciws-online' : 'ciws-offline'); ?>">
                                                    </div>
                                                </div>
                                            </a>
                                        </div>
                                <?php
                                    }
                                }
                                ?>



                            </div>
                        </div>
                </div>
                <a class="ciws-floating-btn-content-wrap ciws-floating-box">
                    <?php
                    if ($customize == 1) { ?>
                        <div class="ciws-floating-icon-wrap ciws-floating-icon-attr ciws-btn-fill-color"
                            style="background-color: <?php echo esc_attr($floating_button_fill_color); ?>">
                        <?php } else { ?>
                            <div class="ciws-floating-icon-wrap ciws-floating-icon-attr ciws-btn-fill-color">
                            <?php } ?>
                           <img src="<?php echo $icon_url; ?>" alt="chat-in-website">
                            </div>
                            <?php
                            if ($customize == 1) { ?>
                                <div class="ciws-floating-btn-info-wrap ciws-floating-bg-attr ciws-btn-fill-color"
                                    style="background-color: <?php echo esc_attr($floating_button_fill_color); ?>">
                                <?php } else { ?>
                                    <div class="ciws-floating-btn-info-wrap ciws-floating-bg-attr ciws-btn-fill-color">
                                    <?php } ?>
                                    <?php
                                    if ($customize == 1) { ?>
                                        <div class="ciws-floating-btn-title ciws-floating-btn-info-attr"
                                            style="color:<?php echo esc_attr($floating_button_title_color); ?>">
                                            <?php echo esc_html($button_head); ?>
                                        </div>
                                        <?php } else {
                                        if (!empty($button_head)) {
                                        ?>
                                            <div class="ciws-floating-btn-title ciws-floating-btn-info-attr">
                                                <?php echo esc_html($button_head); ?>
                                            </div>
                                    <?php }
                                    } ?>
                                    </div>
                </a>
            </div>
        <?php } else { ?>
            <a href="<?php echo esc_url($wh_url); ?>" target="<?php echo esc_attr(($url_open_tab == 1) ? '_blank' : ''); ?>"
                class="ciws-floating-btn-content-wrap ciws-floating-computer-url ">
                <div class="ciws-floating-icon-wrap ciws-floating-icon-attr ciws-btn-fill-color"
                    style="background-color: <?php echo esc_attr($floating_button_fill_color); ?>">
                    <img src="<?php echo $icon_url; ?>" alt="chat-in-website">
                </div>
                <div class="ciws-floating-btn-info-wrap ciws-floating-bg-attr ciws-btn-fill-color"
                    style="background-color: <?php echo esc_attr($floating_button_fill_color); ?>">
                    <div class="ciws-floating-btn-title ciws-floating-btn-info-attr"
                        style="color:<?php echo esc_attr($floating_button_title_color); ?>">
                        <?php echo esc_html($button_head); ?>
                    </div>
                </div>

            </a>
            <a href="<?php echo esc_url($mob_wh_url); ?>"
                target="<?php echo esc_attr(($url_open_tab == 1) ? '_blank' : ''); ?>"
                class="ciws-floating-btn-content-wrap ciws-floating-mobile-url">
                <div class="ciws-floating-icon-wrap ciws-floating-icon-attr ciws-btn-fill-color"
                    style="background-color: <?php echo esc_attr($floating_button_fill_color); ?>">
                    <img src="<?php echo $icon_url; ?>" alt="chat-in-website">
                </div>
                <div class="ciws-floating-btn-info-wrap ciws-floating-bg-attr ciws-btn-fill-color"
                    style="background-color: <?php echo esc_attr($floating_button_fill_color); ?>">
                    <div class="ciws-floating-btn-title ciws-floating-btn-info-attr"
                        style="color:<?php echo esc_attr($floating_button_title_color); ?>">
                        <?php echo esc_html($button_head); ?>
                    </div>
                </div>
            </a>
        <?php } ?>




    </div>
    <?php
    if (!empty($floating_button_fill_color)) {
        wp_register_style('ciws-frontend-custom', false);
        wp_enqueue_style('ciws-frontend-custom');
    }
    include(CIWS_PATH . '/includes/cores/ciws-customize.php');
