<?php
if (! class_exists('AI_App_Onsite_Admin')) {
    class AI_App_Onsite_Admin
    {
        public function __construct()
        {
            add_action('admin_menu', array($this, 'register_menu'));
        }

        public function register_menu()
        {
            add_menu_page(
                'AIappOnsite', // Page title
                'AIappOnsite', // Menu title
                'manage_options', // Capability required to access the menu
                'ai-app-onsite-menu', // Menu slug
                array($this, 'menu_page'), // Callback function to render the menu page
                'dashicons-lightbulb', // Icon URL or Dashicons class
                2 // Position of the menu item
            );
        }

        public function menu_page()
        {
            ob_start(); ?>

            <!-- Render your plugin menu page here -->
            <div id="loader" style="display: none;">
                <div class="loader"></div>
            </div>
            <div class="wrap ai-app-onsite-wrap">
                <div class=" ai-app-onsite-top-bar d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white box-shadow">
                    <h5 class="my-0 mr-md-auto font-weight-normal logo-bg-fixed"></h5>
                    <?php if (ai_app_onsite_is_premium_disabled()) : ?>
                        <?php 
                        $is_hackathon_user = get_option('is_hackathon_user');
                        $domain_access_allowed = get_option('domain_access_allowed');
                        if ($is_hackathon_user && $domain_access_allowed) { 
                            echo '<h5 class="hackathon-badge badge-neon-gradient" id="gold_gradient">_HACKATHON</h5>';
                        }else {
                            echo '<h5 class="gold-gradient" id="gold_gradient">PREMIUM</h5>';
                        } ?>
                    <?php endif; ?>
                </div>
                <div class="ai-app-onsite-tab-content-box shadow-sm rounded">
                    <!-- Tab navigation -->
                    <h2 class="nav-tab-wrapper">
                        <a href="#" class="nav-tab nav-tab-active plugin-tab" id="plugin-settings-tab" data-form-tab="app-settings" data-tab-id="plugin-settings-tab">
                            <div class="icon">
                                <div class="assets-settings-grey-icon"></div>
                                <div class="assets-settings-blue-icon"></div>
                            </div>
                            App Settings
                        </a>
                        <a href="#" class="nav-tab plugin-tab" id="app-properties-tab"  data-form-tab="app-details"  data-tab-id="app-properties-tab">
                            <div class="icon">
                                <div class="assets-clapperboard-grey-icon"></div>
                                <div class="assets-clapperboard-blue-icon"></div>
                            </div>
                            App Details
                        </a>
                        <a href="#" class="nav-tab plugin-tab" id="model-settings-tab" data-form-tab="model-settings"  data-tab-id="model-settings-tab">
                            <div class="icon">
                                <div class="assets-disc-grey-icon"></div>
                                <div class="assets-disc-blue-icon"></div>
                            </div>
                            Model Settings
                        </a>
                        <a href="#" class="nav-tab plugin-tab" id="field-selector-tab" data-form-tab="field-selector" data-tab-id="field-selector-tab">
                            <div class="icon">
                                <div class="assets-chevron-grey-icon"></div>
                                <div class="assets-chevron-blue-icon"></div>
                            </div>
                            Field Selector
                        </a>
                        <a href="#" class="nav-tab plugin-tab" id="prompt-editor-tab" data-form-tab="prompt-editor" data-tab-id="prompt-editor-tab">
                            <div class="icon">
                                <div class="assets-promt-icon-grey-icon"></div>
                                <div class="assets-promt-icon-blue-icon"></div>
                            </div>
                            Prompt Editor
                        </a>
                        <a href="#" class="nav-tab plugin-tab" id="app-preview-tab" data-form-tab="app-preview" data-tab-id="app-preview-tab">
                            <div class="icon">
                                <div class="assets-preview-icon-grey-icon"></div>
                                <div class="assets-preview-icon-blue-icon"></div>
                            </div>
                            Style & Preview
                        </a>
                    </h2>

                    <!-- Tab content -->
                    <?php
                    $allowed_html = array(
                        'div' => array(
                            'id' => array(),
                            'class' => array(),
                            'role' => array(),
                            'style' => array(),
                            'aria-hidden' => array(),
                            'data-toggle' => array(), // Added for modal functionality
                            'data-target' => array(), // Added for modal functionality
                        ),
                        'form' => array(
                            'enctype' => array(),
                            'class' => array(),
                            'id' => array(),
                        ),
                        'input' => array(
                            'type' => array(),
                            'id' => array(),
                            'name' => array(),
                            'class' => array(),
                            'value' => array(),
                            'checked' => array(),
                            'onchange' => array(),
                            'accept' => array(),
                            'disabled' => array(),
                            'readonly' => array(),
                            'placeholder' => array(),
                            'min' => array(),
                            'max' => array(),
                            'step' => array(),
                        ),
                        'label' => array(
                            'for' => array(),
                            'class' => array(),
                            'data-toggle' => array(),
                            'data-target' => array(),
                            'data-color-field' => array(),
                            'data-color-label' => array(),
                            'data-allow-gradient' => array(),
                            'tabindex' => array(),
                            'role' => array(),
                            'aria-label' => array(),
                        ),
                        'button' => array(
                            'type' => array(),
                            'id' => array(),
                            'role' => array(),
                            'class' => array(),
                            'name' => array(),
                            'onclick' => array(),
                            'disabled' => array(),
                            'data-dismiss' => array(),
                            'data-bs-dismiss' => array(),
                            'data-mode' => array(),
                            'data-type' => array(),
                            'data-id' => array(),
                            'aria-label' => array(),
                            'aria-expanded' => array(),
                        ),
                        'select' => array(
                            'id' => array(),
                            'class' => array(),
                            'multiple' => array(),
                            'data-multi-select-plugin' => array(),
                        ),
                        'option' => array(
                            'value' => array(),
                        ),
                        'span' => array(
                            'class' => array(),
                            'id' => array(),
                        ),
                        'p' => array(
                            'id' => array(),
                            'class' => array(),
                            'data-toggle' => array(),
                            'data-target' => array(),
                            'data-bs-toggle' => array(),
                            'data-bs-target' => array(),
                        ),
                        'ul' => array(
                            'class' => array(),
                        ),
                        'li' => array(
                            'class' => array(),
                        ),
                        'a' => array(
                            'href' => array(),
                            'class' => array(),
                            'onclick' => array(),
                        ),
                        'h5' => array(
                            'id' => array(),
                            'class' => array(),
                        ),
                        'textarea' => array(
                            'id' => array(),
                            'class' => array(),
                            'name' => array(),
                        ),
                        'table' => array(
                            'class' => array(),
                        ),
                        'tr' => array(),
                        'th' => array(
                            'class' => array(),
                        ),
                        'td' => array(
                            'class' => array(),
                        ),
                        'img' => array(
                            'src' => array(),
                            'alt' => array(),
                            'class' => array(),
                            'id' => array(),
                        ),
                        'script' => array(
                            'src' => array(),
                            'type' => array(),
                            'integrity' => array(),
                            'crossorigin' => array(),
                        ),
                        'style' => array(),
                        'div.modal-dialog' => array(),
                        'div.modal-content' => array(),
                        "span.field-icon" => array(),
                        "p.fields-error" => array(),
                        "button.remove-btn" => array(
                            "onclick" => array()
                        ),
                        "input[type='checkbox']" => array(
                            "id" => array(),
                            "onchange" => array()
                        )

                    );
                    ?>
                    <div id="plugin-settings-content" class="tab-content plugin-settings-tab-section">
                        <?php echo wp_kses(AI_App_Onsite_Plugin_Settings::render_plugin_settings(), $allowed_html); ?>
                    </div>

                    <div id="app-properties-content" class="tab-content app-properties-tab-section" style="display: none;">
                        <?php
                        echo wp_kses(AI_App_Onsite_App_Properties::render_app_properties(), $allowed_html);
                        ?>
                    </div>

                    <div id="model-settings-content" class="tab-content model-settings-tab-section" style="display: none;">
                        <?php echo wp_kses(AI_App_Onsite_Model_Settings::render_model_settings(), $allowed_html); ?>
                    </div>

                    <div id="field-selector-content" class="tab-content field-selector-tab-section" style="display: none;">
                        <?php echo wp_kses(AI_App_Onsite_Field_Selector::render_field_selector(), $allowed_html); ?>
                    </div>

                    <div id="prompt-editor-content" class="tab-content prompt-editor-tab-section" style="display: none;">
                        <?php echo wp_kses(AI_App_Onsite_Prompt_Editor::render_prompt_editor(), $allowed_html); ?>
                    </div>

                    <div id="app-preview-content" class="tab-content app-preview-tab-section" style="display: none;">
                        <?php echo wp_kses(AI_App_Onsite_App_Preview::render_app_preview_content(), $allowed_html); ?>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="unsavedChangesModal" tabindex="-1" aria-labelledby="unsavedChangesModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content text-center">
                    <div class="modal-header">
                        <h5 class="modal-title" id="unsavedChangesModalLabel">Unsaved Changes</h5>
                        <button type="button" class="btn-close dismiss-unsaved-modal"></button>
                    </div>
                    <div class="modal-body">
                        You have unsaved changes. Please save them before navigating away or refreshing.
                    </div>
                    <div class="modal-footer justify-content-center">
                        <button type="button" class="btn fs-medium-14 neutral-black-fs-60 radius-6 py-6 px-16 border border-2 bg-white dismiss-unsaved-modal">Discard</button>
                        <button type="button" class="btn btn-purple fs-medium-14 neutral-black-fs-60 radius-6 py-6 px-16 border border-2 bg-purple text-white save-changes-modal" aria-label="Close" data-bs-dismiss="modal">Save Changes</button>
                    </div>
                    </div>
                </div>
            </div>

            <!-- Modal -->
            <div class="modal fade bd-example-modal-sm plugin-setting" id="terms_conditions_modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                        <h5 class="fs-bold-16" id="terms_conditions_modal_label">Terms of Services</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body mb-1">
                        <div id="terms-conditions-modal-pdf">
                            <p class="fs-regular-14 neutral-black-fs-100 mb-3 text-center">Terms of Services</p>
                            <p class="fs-regular-14 neutral-black-fs-90">Please read these terms carefully before using our service.</p>
                            <p class="fs-regular-14 neutral-black-fs-90">AIappOnsite ("the Service") is a website add-on solution that provides site owners the ability to create AI-powered applications on their sites, according to their own objectives and direction, and fully within their own control.</p>
                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">1. User Responsibility</p>
                            <p class="fs-regular-12 neutral-black-fs-70">If you reside in the European Economic Area, Switzerland, the UK, California or some other locations, you may be subject to special conditions and laws that govern the use of AI. These and other locales also have privacy and data protection rules and laws that may also dictate how you can leverage tools like AIappOnsite.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">It is important that you abide by all such applicable laws. Furthermore, you may not use the Service in a way that infringes, misappropriates or violates anyone’s rights. You may also not use the Service in a way that endangers or harms, or intends to do the same, to end users of applications built with the Service.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">2. Using AI Services</p>
                            <p class="fs-regular-12 neutral-black-fs-70">The Service requires that the site owner or their designee provide an API key which the Service will use tointegrate with an AI model provider. All AI functionality of the Service depends on one or more API keys, whichis provided by the various AI model vendors. An account must be opened with one of these vendors in order to get an API key.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">These AI model providers are separate companies and unaffiliated with the Service, and have their own terms and conditions. Read them carefully. The API keys and their use are bound by the terms and conditions of the AI model provider and strictly controlled by the site owner or their designee (not AIappOnsite).</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">3. Limitation of Liability</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Given that the site owner fully controls the use, purpose and output of any application built with it, AIappOnsite bears no responsibility or liability for how it is used, or to any consequences of its use by site owners or their site visitors.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">You agree that you are leveraging third party AI models and services separate from AIappOnsite, and that you have full control over and final responsibility for the prompts that will access the API as well as the end-user experience, and any representations made to them.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">4. Data Collection</p>
                            <p class="fs-regular-12 neutral-black-fs-70">To help improve AIappOnsite we collect some anonymous data. This helps us understand feature usage and contributes to anonymized research, which is also shared with the AIappOnsite community (so you can make your apps more effective too). Customer data is collected for internal and community research only, and never shared with third parties.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">The types of data we collect includes average dwell time, user completion rate, average app reuse, like ratio, etc.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Data we will NEVER collect includes your app users' email, name or any other personally identifiable user data. We also NEVER attach your domain, IP address or other site related information to this data. </p>
                            <p class="fs-regular-12 neutral-black-fs-70">In short, the data we collect is strictly anonymized, summarized, and aggregated into a single data pool which then helps us produce valuable usage reports; this research is then made available to the AIappOnsite community to help our customers optimize their own apps, and build better visitor experiences.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">5. Paid Accounts</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Billing. If you purchase any services, you will provide complete and accurate billing information, including a valid payment method. For paid subscriptions, we will automatically charge your payment method on each agreed-upon periodic renewal until you cancel. You’re responsible for all applicable taxes, and we’ll charge tax when required. If your payment cannot be completed, we may downgrade your account or suspend your access to our services until payment is received.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Cancellation. You can cancel your paid subscription at any time. Previous payments are non-refundable,except where required by law. These Terms do not override any mandatory local laws regarding your cancellation rights.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Changes. We may change our prices from time to time. If we increase our subscription prices, we will give you at least 30 days’ notice and any price increase will take effect on your next renewal so that you can cancel if you do not agree to the price increase</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">5. Termination and Suspension</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Termination. You are free to stop using our Services at any time. We reserve the right to suspend or terminate your access to our Services or delete your account if we determine:
                            <ul class="pl-2">
                            <li class="fs-regular-12 neutral-black-fs-70">- You breached these Terms of Service.</li>
                            <li class="fs-regular-12 neutral-black-fs-70">- We must do so to comply with the law.</li>
                            <li class="fs-regular-12 neutral-black-fs-70">- Your use of our Services could cause risk or harm to AIappOnsite or anyone else.</li>
                            </ul>
                            </p>
                            <p class="fs-regular-12 neutral-black-fs-70">Appeals. If you believe we have suspended or terminated your account in error, you can file an appeal with us by contacting our Support team.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">6. Discontinuation of Services</p>
                            <p class="fs-regular-12 neutral-black-fs-70">We may decide to discontinue our Services, but if we do, we will give you advance notice.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">7. Disclaimer of Warranties</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Our services are provided “as is.” except to the extent prohibited by law, we and our affiliates and licensors make no warranties (express, implied, statutory or otherwise) with respect to the services, and disclaim all warranties including, but not limited to, warranties of merchantability, fitness for a particular purpose,satisfactory quality, non-infringement, and quiet enjoyment, and any warranties arising out of any course of dealing or trade usage. We do not warrant that the services will be uninterrupted, accurate or error free, or that any content will be secure or not lost or altered.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">You accept and agree that any use of the service is at your sole risk.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">8. Limitation of liability</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Neither we nor any of our affiliates or licensors will be liable for any indirect,incidental, special, consequential, or exemplary damages, including damages for loss of profits, goodwill, use, or data or other losses, even if we have been advised of the possibility of such damages. Our aggregate liability under these terms applies only to paid subscriptions, and will not exceed the greater of the amount you paid for the service that gave rise to the claim during the 12 months before the liability arose or one United States Dollar (US$1).The limitations in this section apply only to the maximum extent permitted by applicable law.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Some countries and states do not allow the disclaimer of certain warranties or the limitation of certain damages, so some or all of the terms above may not apply to you, and you may have additional rights. In that case, these Terms only limit our responsibilities to the maximum extent permissible in your country of residence.</p>
                            <p class="fs-regular-12 neutral-black-fs-70">Aiapponsite’s affiliates, suppliers, licensors, and distributors are intended third party beneficiaries of this section.</p>

                            <p class="fs-regular-14 neutral-black-fs-100 mb-1">9. Indemnity</p>
                            <p class="fs-regular-12 neutral-black-fs-70">If you are a business or organization, to the extent permitted by law, you will indemnify and hold harmless us,our affiliates, and our personnel, from and against any costs, losses, liabilities, and expenses (including attorneys’ fees) from third party claims arising out of or relating to your use of the Services and Content or any violation of these Terms.</p>

                            <div class="terms-form-group form-group mb-0" id="terms-form-group">
                            <input type="checkbox" id="terms_conditions" >
                            <label for="terms_conditions" class="fs-regular-14 pl-1 neutral-black-fs-90">Agree to Terms of Service</label>
                            </div>
                        </div>
                        <div class="divider-light mt-2 mb-3" id="divider"></div>
                        <div class="button-wrap" id="terms_conditions_button">
                            <button type="button" id="download_ai_app_terms_of_service" role="button" class="btn fs-medium-14 neutral-black-fs-60 radius-6 py-6 px-16 border border-2 bg-white" name="save-plugin-settings" onclick="ai_app_onsite_download_terms_Of_service()">Download</button>
                            <button type="button" id="accept_ai_app_terms_of_service" class="btn fs-medium-14 neutral-black-fs-60 radius-6 py-6 px-16 border border-2 bg-purple border-purple text-white" name="save-plugin-settings" data-bs-dismiss="modal">Accept</button>
                        </div>
                        </div>
                    </div>
                </div>
            </div>

<?php
            //echo ob_get_clean();
        }
    }
}
