<?php
defined('ABSPATH') or die('No script kiddies please!!');
if (!class_exists('CIWS_CRUD')) {
    class CIWS_CRUD
    {
        function __construct()
        {
            /**
             * CIWP ACCOUNT CRUD
             */
            add_action('admin_post_ciws_create_account_action', array($this, 'ciws_account_create'));
            add_action('admin_post_ciws_edit_account_action', array($this, 'ciws_account_edit'));
            add_action('admin_post_ciws_delete_account_action', array($this, 'delete_account_action'));

            /**
             * CIWP Floating Icon CRUD
             */
            add_action('admin_post_ciws_floating_icon_action', array($this, 'ciws_create_floating_icon'));
            add_action('admin_post_ciws_main_action', array($this, 'ciws_create_main_settings'));
        }
        function ciws_account_create()
        {
            // Check if nonce is set and valid before processing data
            if (
                isset($_POST['ciws_settings_nonce_field']) &&
                wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ciws_settings_nonce_field'])), 'ciws_settings_nonce') &&
                current_user_can('manage_options')
            ) {
                // Sanitize account_name
                $account_name = !empty($_POST['account_name']) ? sanitize_text_field(wp_unslash($_POST['account_name'])) : '';
                $account_photo = (isset($_POST['ciws_settings']['account']['profile_photo'])) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['profile_photo'])) : '';
                // Sanitize account array
                $account_number = isset($_POST['ciws_settings']['account']['account_number']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['account_number'])) : '';
                $default_msg = isset($_POST['ciws_settings']['account']['default_msg']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['default_msg'])) : '';
                $online = isset($_POST['ciws_settings']['account']['online']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['online'])) : '';

                // Sanitize shortcode settings
                $button_heading = isset($_POST['ciws_settings']['shortcode']['button_heading']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['button_heading'])) : '';
                $shortcode_template = isset($_POST['ciws_settings']['shortcode']['shortcode_template']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['shortcode_template'])) : '';
                $shortcode_customize_status = isset($_POST['ciws_settings']['shortcode']['shortcode_customize_status']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['shortcode_customize_status'])) : '';
                $button_fill_color = isset($_POST['ciws_settings']['shortcode']['button_fill_color']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['button_fill_color'])) : '';
                $button_title_color = isset($_POST['ciws_settings']['shortcode']['button_title_color']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['button_title_color'])) : '';

                // Create sanitized data array
                $account_sanitized_data = array(
                    'account' => array(
                        'account_number' => $account_number,
                        'profile_photo' => $account_photo,
                        'default_msg' => $default_msg,
                        'online' => $online,
                    ),
                    'shortcode' => array(
                        'button_heading' => $button_heading,
                        'shortcode_template' => $shortcode_template,
                        'shortcode_customize_status' => $shortcode_customize_status,
                        'button_fill_color' => $button_fill_color,
                        'button_title_color' => $button_title_color,
                    ),
                );

                // Prepare database table and insert sanitized data
                global $wpdb;
                $account_table = CIWS_TABLE;

                $check = $wpdb->insert(
                    $account_table,
                    [
                        'account_name' => $account_name,
                        'ciws_account_details' => maybe_serialize($account_sanitized_data)
                    ],
                    [
                        '%s', // account_name
                        '%s' //ciws_account_details
                    ]
                );


                // Handle success or failure
                if ($check) {
                    $lastid = $wpdb->insert_id;
                    wp_safe_redirect(admin_url('admin.php?page=chat-in-website&message=5'));
                    exit;
                } else {
                    wp_die(
                        esc_html__('Something went wrong while saving the account!', 'chat-in-website'),
                        esc_html__('Error', 'chat-in-website')
                    );
                }
            } else {
                wp_die(
                    esc_html__('Invalid request or session expired. Please try again.', 'chat-in-website'),
                    esc_html__('Permission Denied', 'chat-in-website')
                );
            }
        }


        function delete_account_action()
        {
            if (isset($_GET['account_id']) && !empty($_GET['_wpnonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'])), 'ciwo_account_delete_nonce') && current_user_can('manage_options')) {
                $account_id = !empty($_GET['account_id']) ? absint($_GET['account_id']) : 0;
                global $wpdb;
                $check = $wpdb->delete(CIWS_TABLE, array('account_id' => $account_id));
                if ($check) {
                    wp_redirect(admin_url('admin.php?page=chat-in-website&message=3'));
                } else {
                    die('Something went wrong!!');
                }
            } else {
                die('Something went wrong!!');
            }
        }

        function ciws_create_floating_icon()
        {
            if (!empty($_POST['ciws_floating_icon_settings_nonce_field']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ciws_floating_icon_settings_nonce_field'])), 'ciws_floating_icon_settings_nonce') && current_user_can('manage_options')) {
                //sanitize account array 
                
                $ciws_floating_account_id = (isset($_POST['ciws_floating_settings']['select_account']['ciws_floating_account_id'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['select_account']['ciws_floating_account_id'])) : '';
                $ciws_floating_account_ids = (isset($_POST['ciws_floating_settings']['select_account']['ciws_floating_account_ids'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['select_account']['ciws_floating_account_ids'])) : '';
                $button_heading = (isset($_POST['ciws_floating_settings']['layout']['button_heading'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['button_heading'])) : '';
                $floating_template = (isset($_POST['ciws_floating_settings']['layout']['floating_template'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_template'])) : '';
                $customize_status = (isset($_POST['ciws_floating_settings']['layout']['customize_status'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['customize_status'])) : '';
                $button_fill_color = (isset($_POST['ciws_floating_settings']['layout']['button_fill_color'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['button_fill_color'])) : '';
                $button_title_color = (isset($_POST['ciws_floating_settings']['layout']['button_title_color'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['button_title_color'])) : '';
                $box_status = (isset($_POST['ciws_floating_settings']['layout']['box_status'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['box_status'])) : 'none';
                $floating_title_text = (isset($_POST['ciws_floating_settings']['layout']['floating_title_text'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_title_text'])) : '';
                $floating_sub_title_text = (isset($_POST['ciws_floating_settings']['layout']['floating_sub_title_text'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_sub_title_text'])) : '';
                $floating_reply_info_text = (isset($_POST['ciws_floating_settings']['layout']['floating_reply_info_text'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_reply_info_text'])) : '';
                $floating_template = (isset($_POST['ciws_floating_settings']['layout']['floating_template'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_template'])) : 'ciws-floating-template-1';
                $floating_box_template = (isset($_POST['ciws_floating_settings']['layout']['floating_box_template'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_box_template'])) : 'ciws-floating-box-template-1';
                $floating_position = (isset($_POST['ciws_floating_settings']['shortcode']['floating_position'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['shortcode']['floating_position'])) : '';
               
                $greeting_header_text = wp_kses_post(wp_unslash((!empty($_POST['ciws_floating_settings']['layout']['greeting_header_text'])) ? $_POST['ciws_floating_settings']['layout']['greeting_header_text'] : ''));
                $greeting_button_text = (isset($_POST['ciws_floating_settings']['layout']['greeting_button_text'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['greeting_button_text'])) : '';
                $greeting_message_text = wp_kses_post(wp_unslash((!empty($_POST['ciws_floating_settings']['layout']['greeting_message_text'])) ? $_POST['ciws_floating_settings']['layout']['greeting_message_text'] : ''));
                $greeting_footer_text = wp_kses_post(wp_unslash((!empty($_POST['ciws_floating_settings']['layout']['greeting_footer_text'])) ? $_POST['ciws_floating_settings']['layout']['greeting_footer_text'] : ''));
                $floating_position = (isset($_POST['ciws_floating_settings']['layout']['floating_position'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['floating_position'])) : '';
                $icon_img = (isset($_POST['ciws_floating_settings']['layout']['icon_img'])) ? sanitize_text_field(wp_unslash($_POST['ciws_floating_settings']['layout']['icon_img'])) : 'ciws-whatsapp';
                $floating_settings_sanitized_data = array(
                    'select_account' => array(
                        'ciws_floating_account_id' => $ciws_floating_account_id,
                        'ciws_floating_account_ids' => $ciws_floating_account_ids,
                        'box_status' => $box_status,
                    ),

                    'layout' => array(
                        'icon_img' => $icon_img,
                        'button_heading' => $button_heading,
                        'floating_template' => $floating_template,
                        'customize_status' => $customize_status,
                        'button_fill_color' => $button_fill_color,
                        'button_title_color' => $button_title_color,
                        'floating_position' => $floating_position,
                        'box_status' => $box_status,
                        'floating_title_text' => $floating_title_text,
                        'floating_sub_title_text' => $floating_sub_title_text,
                        'floating_reply_info_text' => $floating_reply_info_text,
                        'floating_template' => $floating_template,
                        'floating_box_template' => $floating_box_template,
                        'greeting_header_text' => $greeting_header_text,
                        'greeting_button_text' => $greeting_button_text,
                        'greeting_message_text' => $greeting_message_text,
                        'greeting_box_template' => $greeting_box_template,
                        'greeting_footer_text' => $greeting_footer_text,
                         
                    ),
                   


                );

                // Update the option
                update_option('ciws_floating_settings', $floating_settings_sanitized_data);

                // Redirect to the admin page with a success message
                wp_redirect(admin_url('admin.php?page=ciws-floating-icon&message=1'));
                exit;
            } else {
                // Redirect to the admin page with an error message
                wp_redirect(admin_url('admin.php?page=ciws-floating-icon&message=2'));
                exit;
            }
        }



        function ciws_create_main_settings()
        {

            if (
                isset($_POST['ciws_main_settings_nonce_field']) &&
                wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ciws_main_settings_nonce_field'])), 'ciws_main_settings_nonce')
                && current_user_can('manage_options')
            ) {
                // Sanitize input data
         
                $open_in_new_tab = isset($_POST['ciws_main_settings']['url']['open_in_new_tab']) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['url']['open_in_new_tab'])) : '';
                $url_for_desktop = isset($_POST['ciws_main_settings']['url']['url_for_desktop']) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['url']['url_for_desktop'])) : '';
                $url_for_mobile = isset($_POST['ciws_main_settings']['url']['url_for_mobile']) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['url']['url_for_mobile'])) : '';
                $button_status = isset($_POST['ciws_main_settings']['woocommerce']['button_status']) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['woocommerce']['button_status'])) : '';
                $button_position = isset($_POST['ciws_main_settings']['woocommerce']['button_position']) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['woocommerce']['button_position'])) : '';
                $woo_button_account = isset($_POST['ciws_main_settings']['woocommerce']['woo_button_account']) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['woocommerce']['woo_button_account'])) : '';
                $home_page = (isset($_POST['ciws_main_settings']['display']['home_page'])) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['display']['home_page'])) : '';
                $archive_page = (isset($_POST['ciws_main_settings']['display']['archive_page'])) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['display']['archive_page'])) : '';
                $desktop = (isset($_POST['ciws_main_settings']['display']['desktop'])) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['display']['desktop'])) : '';
                $mobile = (isset($_POST['ciws_main_settings']['display']['mobile'])) ? sanitize_text_field(wp_unslash($_POST['ciws_main_settings']['display']['mobile'])) : '';
                // Create sanitized data array
                $main_settings_sanitized_data = array(
                    'url' => array(
                        'open_in_new_tab' => $open_in_new_tab,
                        'url_for_desktop' => $url_for_desktop,
                        'url_for_mobile' => $url_for_mobile,
                    ),
                    'display' => array(
                        'home_page' => $home_page,
                        'archive_page' => $archive_page,
                        'desktop' => $desktop,
                        'mobile' => $mobile,
                    ),
                    'woocommerce' => array(
                        'button_status' => $button_status,
                        'button_position' => $button_position,
                        'woo_button_account' => $woo_button_account,
                    ),
                );

                // Save settings
                update_option('ciws_main_settings', $main_settings_sanitized_data);

                // Redirect after saving
                wp_safe_redirect(admin_url('admin.php?page=ciws-settings&message=1'));
                exit;
            } else {
                // Redirect if nonce verification fails
                wp_safe_redirect(admin_url('admin.php?page=ciws-settings&message=2'));
                exit;
            }
        }


        function ciws_account_edit()
        {

            if (!empty($_POST['account_id']) && isset($_POST['ciws_edit_settings_nonce_field']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ciws_edit_settings_nonce_field'])), 'ciws_edit_settings_nonce') && current_user_can('manage_options')) {
             
                // Sanitize text inputs
                
                $account_name = isset($_POST['account_name']) ? sanitize_text_field(wp_unslash($_POST['account_name'])) : '';
                $account_photo = (isset($_POST['ciws_settings']['account']['profile_photo'])) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['profile_photo'])) : '';
                // Sanitize account array
                $account_number = isset($_POST['ciws_settings']['account']['account_number']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['account_number'])) : '';
                $title = isset($_POST['ciws_settings']['account']['title']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['title'])) : '';
                $default_msg = isset($_POST['ciws_settings']['account']['default_msg']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['account']['default_msg'])) : '';
                $online = isset($_POST['ciws_settings']['account']['online']) ? boolval($_POST['ciws_settings']['account']['online']) : false;

                // Sanitize shortcode fields
                $button_heading = isset($_POST['ciws_settings']['shortcode']['button_heading']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['button_heading'])) : '';
                $shortcode_template = isset($_POST['ciws_settings']['shortcode']['shortcode_template']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['shortcode_template'])) : '';
                $shortcode_customize_status = isset($_POST['ciws_settings']['shortcode']['shortcode_customize_status']) ? sanitize_text_field(wp_unslash($_POST['ciws_settings']['shortcode']['shortcode_customize_status'])) : '';
                $button_fill_color = isset($_POST['ciws_settings']['shortcode']['button_fill_color']) ? sanitize_hex_color(wp_unslash($_POST['ciws_settings']['shortcode']['button_fill_color'])) : '';
                $button_title_color = isset($_POST['ciws_settings']['shortcode']['button_title_color']) ? sanitize_hex_color(wp_unslash($_POST['ciws_settings']['shortcode']['button_title_color'])) : '';

                // Prepare sanitized array
                $edit_account_sanitized_data = array(
                    'account' => array(
                        'account_number' => $account_number,
                        'profile_photo' => $account_photo,
                        'title' => $title,
                        'default_msg' => $default_msg,
                        'online' => $online,
                    ),
                    'shortcode' => array(
                        'shortcode_template' => $shortcode_template,
                        'button_heading' => $button_heading,
                        'shortcode_customize_status' => $shortcode_customize_status,
                        'button_fill_color' => $button_fill_color,
                        'button_title_color' => $button_title_color,
                    )
                );

                $account_id = intval($_POST['account_id']);
                $account_table = CIWS_TABLE;
                global $wpdb;
                $check = $wpdb->update(
                    $account_table,
                    array(

                        'account_name' => $account_name,
                        'ciws_account_details' => maybe_serialize($edit_account_sanitized_data),

                    ),
                    array('account_id' => $account_id),
                    array(

                        '%s',
                        '%s',

                    ),
                    array('%d')
                );

                
                wp_safe_redirect(admin_url('admin.php?page=chat-in-website&account_id=' . $account_id . '&action=edit_account&message=2'));
               
            } else {
                die('Something went wrong!!');
            }
        }
    }

    new CIWS_CRUD();
}
