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

<div class="wrap ciws-wrap">
    <div class="ciws-header-relative">
        <div class="ciws-header">
            <h1 class="ciws-floatLeft">
                <img src="<?php echo esc_url(CIWS_IMG_DIR . '/ciws-bknd-logo.png'); ?>" class="ciws-plugin-logo">
            </h1>
            <div class="aftm-header-btn-wrap">
                <a class="ciws-preview-btn ciws-btn-secondary ciws-save-btn"
                    href="<?php echo esc_url(admin_url('admin.php?page=ciws-add-new-account')); ?>">Add New Account</a>

            </div>
        </div>
    </div>
    <!-- End of header section -->

    <h2 class="nav-tab-wrapper ciws-menu-list wp-clearfix">
        <?php esc_html_e('Account Lists', 'chat-in-website'); ?>
    </h2>
    <?php
    if (!empty($_GET['message']) && $_GET['message'] == 5) {
        ?>
        <div class="notice notice-info is-dismissible inline">
            <p>
                <?php esc_html_e('Account Created Successfully.', 'chat-in-website'); ?>
            </p>
        </div>
        <?php
    }
    ?>

    <div class="ciws-settings-wrap ciws-menu-list-wrap">
        <div class="ciws-settings-section">
            <table class="wp-list-table widefat fixed striped table-view-list posts">
                <thead>
                    <th>
                        <?php esc_html_e('Account Name', 'chat-in-website'); ?>
                    </th>
                    <th>
                        <?php esc_html_e('Shortcode', 'chat-in-website'); ?>
                    </th>
                    <th>
                        <?php esc_html_e('Action', 'chat-in-website'); ?>
                    </th>
                </thead>
                <tbody>
                    <?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);
                            $account_id = $account_row->account_id;

                            $ac_id = (!empty($account_id)) ? $account_id : '';
                            $ciws_shortcode = '[ciws id=' . $ac_id . ']';
                    ?>
                    <tr>
                        <td>
                            <a
                                href="<?php echo esc_url(admin_url('admin.php?page=chat-in-website&account_id=' . $account_row->account_id . '&action=edit_account')); ?>">
                                <?php echo esc_html($account_row->account_name); ?>
                            </a>

                        </td>
                        <td>
                            <div class="fpsm-shortcode-display">
                                <span class="ciws-shortcode-preview"><?php echo esc_html($ciws_shortcode); ?></span>
                                <span class="ciws-clipboard-copy"><i class="fas fa-clipboard-list"></i></span>
                            </div>
                        </td>

                        <td>

                            <a class="ciws-account-list-icon ciws-list-edit"
                                href="<?php echo esc_url(admin_url('admin.php?page=chat-in-website&account_id=' . $account_row->account_id . '&action=edit_account')); ?>">
                                <span class="dashicons dashicons-edit ciws-edit-be"><span
                                        class="ciws-backend-edit"></span></span></a>
                            <?php $account_delete_nonce = wp_create_nonce('ciwo_account_delete_nonce'); ?>
                            <a class="ciws-account-list-icon ciws-list-delete"
                                href="<?php echo esc_url(admin_url('admin-post.php?action=ciws_delete_account_action&account_id=' . $account_row->account_id . '&_wpnonce=' . $account_delete_nonce)); ?>"
                                onclick="return confirm('Are you sure you want to delete this account?');"><span
                                    class="dashicons dashicons-trash ciws-trash-be"><span class="ciws-backend-trash">
                                    </span></span></a>

                    </tr>
                    <?php }
                    } else { ?>
                    <tr>
                        <td colspan="3">
                            <?php echo esc_html_e("No accounts added yet", 'chat-in-website'); ?>
                        </td>


                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

    </div>