<?php 

global
$ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_plan_id,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_plan_id,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_plan_id,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_plan_id,
$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_instructions;


$input_ai_writing_task1_title               = 'ai-writing-task-1-plan-title';
$input_ai_writing_task1_price               = 'ai-writing-task-1-plan-price';

$input_ai_writing_task2_title               = 'ai-writing-task-2-plan-title';
$input_ai_writing_task2_price               = 'ai-writing-task-2-plan-price';

$input_ai_writing_task1and2_title           = 'ai-writing-task-1-and-2-plan-title';
$input_ai_writing_task1and2_price           = 'ai-writing-task-1-and-2-plan-price';

$input_ai_speaking_title                    = 'ai-speaking-plan-title';
$input_ai_speaking_price                    = 'ai-speaking-plan-price';


$enable_ai_evaluation                       = 0;
# writing evaluation
$writing_ai_evaluation_instructions         = '';

$ai_writing_evalation_plan1                 = null;
$ai_writing_evaluation_plan1_title          = "AI Writing Task 1 Evaluation cost";
$ai_writing_evaluation_plan1_price          = 0;
$ai_writing_evaluation_plan1_enable         = 0;

$ai_writing_evaluation_plan2_title         = "AI Writing Task 2 Evaluation cost";
$ai_writing_evaluation_plan2_price         = 0;
$ai_writing_evaluation_plan2_enable        = 0;

$ai_writing_evaluation_plan1and2_title     = "AI Writing Task 1 + 2 Evaluation cost";
$ai_writing_evaluation_plan1and2_price     = 0;
$ai_writing_evaluation_plan1and2_enable    = 0;

$ai_speaking_evaluation_plan               = null;
$ai_speaking_evaluation_plan_title         = "AI Speaking Evaluation cost";
$ai_speaking_evaluation_plan_price         = 0;
$ai_speaking_evaluation_plan_enable        = 0;


$ai_writing_evalation_task1_plan_id = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_plan_id, false);
# get writing product for task 1
if(!wc_get_product( $ai_writing_evalation_task1_plan_id )){
    $product_args = array(
        'post_title' => $ai_writing_evaluation_plan1_title,
        'post_status' => 'publish',
        'post_type' => 'product',
        'post_excerpt' => '',
        'post_content' => stripslashes(html_entity_decode( 'Auto generated product for AI writing task 1 evaluation enrollment please do not delete or update.', ENT_QUOTES, 'UTF-8' ) ),
        'post_author' => get_current_user_id()
    );

    $ai_writing_evalation_task1_plan_id = wp_insert_post( $product_args );
    if ( ! is_wp_error( $ai_writing_evalation_task1_plan_id ) ) {
        $product = wc_get_product( $ai_writing_evalation_task1_plan_id );
        if( $product ){
            wp_set_object_terms( $ai_writing_evalation_task1_plan_id, 'simple', 'product_type' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_stock_status', 'instock' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, 'total_sales', '0' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_downloadable', 'no' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_virtual', 'yes' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_regular_price', $ai_writing_evaluation_plan1_price );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_featured', 'no' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_price', $ai_writing_evaluation_plan1_price );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_sold_individually', 'yes' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_manage_stock', 'no' );
            update_post_meta( $ai_writing_evalation_task1_plan_id, '_backorders', 'no' );
            if (version_compare(WC_VERSION, '3.0', '>=' ) ) {
                $product->set_reviews_allowed( false );
                $product->set_catalog_visibility( 'hidden' );
                $product->save();
            }  
            update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_plan_id, $ai_writing_evalation_task1_plan_id); 
        }   
    }
}

$ai_writing_evalation_task2_plan_id = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_plan_id, false);
# get writing product for task 2
if(!wc_get_product( $ai_writing_evalation_task2_plan_id )){
    $product_args = array(
        'post_title' => $ai_writing_evaluation_plan2_title,
        'post_status' => 'publish',
        'post_type' => 'product',
        'post_excerpt' => '',
        'post_content' => stripslashes(html_entity_decode( 'Auto generated product for AI writing task 2 evaluation enrollment please do not delete or update.', ENT_QUOTES, 'UTF-8' ) ),
        'post_author' => get_current_user_id()
    );
        
    $ai_writing_evalation_task2_plan_id = wp_insert_post( $product_args );
    if ( ! is_wp_error( $ai_writing_evalation_task2_plan_id ) ) {
        $product = wc_get_product( $ai_writing_evalation_task2_plan_id );
        if( $product ){
            wp_set_object_terms( $ai_writing_evalation_task2_plan_id, 'simple', 'product_type' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_stock_status', 'instock' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, 'total_sales', '0' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_downloadable', 'no' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_virtual', 'yes' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_regular_price', $ai_writing_evaluation_plan2_price );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_featured', 'no' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_price', $ai_writing_evaluation_plan2_price );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_sold_individually', 'yes' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_manage_stock', 'no' );
            update_post_meta( $ai_writing_evalation_task2_plan_id, '_backorders', 'no' );
            update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_plan_id, $ai_writing_evalation_task2_plan_id); 
        }   
    }
}

$ai_writing_evalation_task1and2_plan_id = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_plan_id, false);
# get writing product for task 1 + 2
if(!wc_get_product( $ai_writing_evalation_task1and2_plan_id )){
    $product_args = array(
        'post_title' => $ai_writing_evaluation_plan1and2_title,
        'post_status' => 'publish',
        'post_type' => 'product',
        'post_excerpt' => '',
        'post_content' => stripslashes(html_entity_decode( 'Auto generated product for AI writing task 1 + 2 evaluation enrollment please do not delete or update.', ENT_QUOTES, 'UTF-8' ) ),
        'post_author' => get_current_user_id()
    );
        
    $ai_writing_evalation_task1and2_plan_id = wp_insert_post( $product_args );
    if ( ! is_wp_error( $ai_writing_evalation_task1and2_plan_id ) ) {
        $product = wc_get_product( $ai_writing_evalation_task1and2_plan_id );
        if( $product ){
            wp_set_object_terms( $ai_writing_evalation_task1and2_plan_id, 'simple', 'product_type' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_stock_status', 'instock' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, 'total_sales', '0' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_downloadable', 'no' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_virtual', 'yes' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_regular_price', $ai_writing_evaluation_plan1and2_price );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_featured', 'no' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_price', $ai_writing_evaluation_plan1and2_price );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_sold_individually', 'yes' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_manage_stock', 'no' );
            update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_backorders', 'no' );    
            update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_plan_id, $ai_writing_evalation_task1and2_plan_id); 
        }   
    }
}

$ai_speaking_evaluation_plan_id = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_plan_id, false);
if( ! wc_get_product( $ai_speaking_evaluation_plan_id ) ){
    $product_args = array(
        'post_title'   => $ai_speaking_evaluation_plan_title,
        'post_status'  => 'publish',
        'post_type'    => 'product',
        'post_excerpt' => '',
        'post_content' => stripslashes( html_entity_decode( 'Auto generated product for AI speaking evaluation enrollment please do not delete or update.', ENT_QUOTES, 'UTF-8' ) ),
        'post_author'  => get_current_user_id()
    );

    $ai_speaking_evaluation_plan_id = wp_insert_post( $product_args );
    if ( ! is_wp_error( $ai_speaking_evaluation_plan_id ) ) {
        $product = wc_get_product( $ai_speaking_evaluation_plan_id );
        if( $product ){
            wp_set_object_terms( $ai_speaking_evaluation_plan_id, 'simple', 'product_type' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_stock_status', 'instock' );
            update_post_meta( $ai_speaking_evaluation_plan_id, 'total_sales', '0' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_downloadable', 'no' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_virtual', 'yes' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_regular_price', $ai_speaking_evaluation_plan_price );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_featured', 'no' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_price', $ai_speaking_evaluation_plan_price );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_sold_individually', 'yes' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_manage_stock', 'no' );
            update_post_meta( $ai_speaking_evaluation_plan_id, '_backorders', 'no' );
            if ( version_compare( WC_VERSION, '3.0', '>=' ) ) {
                $product->set_reviews_allowed( false );
                $product->set_catalog_visibility( 'hidden' );
                $product->save();
            }
            update_option( $ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_plan_id, $ai_speaking_evaluation_plan_id );
        }
    }
}


# post handler - CONSOLIDATED HANDLER FOR ALL AI EVALUATION SETTINGS
if(isset($_POST['st-proqyz-ai-evaluation-settings-save']) && $_POST['st-proqyz-ai-evaluation-settings-save'] == 1){
    
    // Verify nonce for security (optional but recommended)
    // check_admin_referer('st-proqyz-ai-evaluation-settings');
    
    // Handle Enable AI Evaluation checkbox
    if(isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation])){
        update_option($ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation, $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation]);
    } else {
        update_option($ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation, 0);
    }

    // Handle AI Writing Evaluation Instructions
    if(isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions])){
        $ai_writing_evaluation_instructions = $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions];
        $ai_writing_evaluation_instructions = stripslashes($ai_writing_evaluation_instructions);
        update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions, $ai_writing_evaluation_instructions);
    }
    
    # ai task 1 enable
    $ai_writing_evaluation_plan1_enable = isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable]) ? $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable] : 0;
    update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable, $ai_writing_evaluation_plan1_enable);

    # ai task 2 enable
    $ai_writing_evaluation_plan2_enable = isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable]) ? $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable] : 0;
    update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable, $ai_writing_evaluation_plan2_enable);

    # ai task 1 + 2 enable
    $ai_writing_evaluation_plan1and2_enable = isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable]) ? $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable] : 0;
    update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable, $ai_writing_evaluation_plan1and2_enable);

    # ai speaking enable
    $ai_speaking_evaluation_plan_enable = isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable]) ? $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable] : 0;
    update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable, $ai_speaking_evaluation_plan_enable);

    if(isset($_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_instructions])){
        $ai_speaking_evaluation_instructions = $_POST[$ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_instructions];
        $ai_speaking_evaluation_instructions = stripslashes($ai_speaking_evaluation_instructions);
        update_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_instructions, $ai_speaking_evaluation_instructions);
    }

    # task 1 title & price - Get current values first
    $product1 = wc_get_product( $ai_writing_evalation_task1_plan_id );
    $ai_writing_evaluation_plan1_title   = $product1 ? $product1->get_title() : '';
    $ai_writing_evaluation_plan1_price   = $product1 ? $product1->get_price() : 0;
    
    if(isset($_POST[$input_ai_writing_task1_title]) && !empty($_POST[$input_ai_writing_task1_title])){
        $ai_writing_evaluation_plan1_title   = $_POST[$input_ai_writing_task1_title];
    }
    if(isset($_POST[$input_ai_writing_task1_price]) && $_POST[$input_ai_writing_task1_price] !== '' && $_POST[$input_ai_writing_task1_price] >= 0){
        $ai_writing_evaluation_plan1_price    = $_POST[$input_ai_writing_task1_price];
    }
    update_post_meta( $ai_writing_evalation_task1_plan_id, '_regular_price', $ai_writing_evaluation_plan1_price );
    update_post_meta( $ai_writing_evalation_task1_plan_id, '_price', $ai_writing_evaluation_plan1_price );
    wp_update_post(['ID' => $ai_writing_evalation_task1_plan_id, 'post_title' => $ai_writing_evaluation_plan1_title]);

    # task 2 title & price - Get current values first
    $product2 = wc_get_product( $ai_writing_evalation_task2_plan_id );
    $ai_writing_evaluation_plan2_title   = $product2 ? $product2->get_title() : '';
    $ai_writing_evaluation_plan2_price   = $product2 ? $product2->get_price() : 0;
    
    if(isset($_POST[$input_ai_writing_task2_title]) && !empty($_POST[$input_ai_writing_task2_title])){
        $ai_writing_evaluation_plan2_title    = $_POST[$input_ai_writing_task2_title];
    }
    if(isset($_POST[$input_ai_writing_task2_price]) && $_POST[$input_ai_writing_task2_price] !== '' && $_POST[$input_ai_writing_task2_price] >= 0){
        $ai_writing_evaluation_plan2_price    = $_POST[$input_ai_writing_task2_price];
    }
    update_post_meta( $ai_writing_evalation_task2_plan_id, '_regular_price', $ai_writing_evaluation_plan2_price );
    update_post_meta( $ai_writing_evalation_task2_plan_id, '_price', $ai_writing_evaluation_plan2_price );
    wp_update_post(['ID' => $ai_writing_evalation_task2_plan_id, 'post_title' => $ai_writing_evaluation_plan2_title]);

    # task 1 + 2 title & price - Get current values first
    $product1and2 = wc_get_product( $ai_writing_evalation_task1and2_plan_id );
    $ai_writing_evaluation_plan1and2_title   = $product1and2 ? $product1and2->get_title() : '';
    $ai_writing_evaluation_plan1and2_price   = $product1and2 ? $product1and2->get_price() : 0;
    
    if(isset($_POST[$input_ai_writing_task1and2_title]) && !empty($_POST[$input_ai_writing_task1and2_title])){
        $ai_writing_evaluation_plan1and2_title    = $_POST[$input_ai_writing_task1and2_title];
    }
    if(isset($_POST[$input_ai_writing_task1and2_price]) && $_POST[$input_ai_writing_task1and2_price] !== '' && $_POST[$input_ai_writing_task1and2_price] >= 0){
        $ai_writing_evaluation_plan1and2_price    = $_POST[$input_ai_writing_task1and2_price];
    }
    update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_regular_price', $ai_writing_evaluation_plan1and2_price );
    update_post_meta( $ai_writing_evalation_task1and2_plan_id, '_price', $ai_writing_evaluation_plan1and2_price );
    wp_update_post(['ID' => $ai_writing_evalation_task1and2_plan_id, 'post_title' => $ai_writing_evaluation_plan1and2_title]);

    # ai speaking title & price
    $ai_speaking_product = wc_get_product( $ai_speaking_evaluation_plan_id );
    $ai_speaking_evaluation_plan_title = $ai_speaking_product ? $ai_speaking_product->get_title() : $ai_speaking_evaluation_plan_title;
    $ai_speaking_evaluation_plan_price = $ai_speaking_product ? $ai_speaking_product->get_price() : $ai_speaking_evaluation_plan_price;

    if(isset($_POST[$input_ai_speaking_title]) && !empty($_POST[$input_ai_speaking_title])){
        $ai_speaking_evaluation_plan_title = $_POST[$input_ai_speaking_title];
    }
    if(isset($_POST[$input_ai_speaking_price]) && $_POST[$input_ai_speaking_price] !== '' && $_POST[$input_ai_speaking_price] >= 0){
        $ai_speaking_evaluation_plan_price = $_POST[$input_ai_speaking_price];
    }

    update_post_meta( $ai_speaking_evaluation_plan_id, '_regular_price', $ai_speaking_evaluation_plan_price );
    update_post_meta( $ai_speaking_evaluation_plan_id, '_price', $ai_speaking_evaluation_plan_price );
    wp_update_post(['ID' => $ai_speaking_evaluation_plan_id, 'post_title' => $ai_speaking_evaluation_plan_title]);
    
    // Redirect to prevent form resubmission (Post/Redirect/Get pattern)
    $redirect_url = add_query_arg(
        array(
            'page' => 'st-proqyz-evaluation',
            'tab' => 'evaluation-cost',
            'subtab' => 'ai-evaluation-cost',
            'updated' => 'true'
        ),
        admin_url('admin.php')
    );
    wp_safe_redirect($redirect_url);
    exit;
}


# final get - [updated meta]
$enable_ai_evaluation                      = get_option($ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation,0);

$ai_writing_evaluation_instructions        = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions);
$ai_writing_evaluation_instructions        = stripslashes($ai_writing_evaluation_instructions);



$product                                    = wc_get_product( $ai_writing_evalation_task1_plan_id );
$ai_writing_evaluation_plan1_title          = $product->get_title();
$ai_writing_evaluation_plan1_price          = $product->get_price();

$product                                    = wc_get_product( $ai_writing_evalation_task2_plan_id );
$ai_writing_evaluation_plan2_title          = $product->get_title();
$ai_writing_evaluation_plan2_price          = $product->get_price();

$product                                    = wc_get_product( $ai_writing_evalation_task1and2_plan_id );
$ai_writing_evaluation_plan1and2_title      = $product->get_title();
$ai_writing_evaluation_plan1and2_price      = $product->get_price();

$ai_speaking_evaluation_instructions        = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_instructions);
$ai_speaking_evaluation_instructions        = stripslashes($ai_speaking_evaluation_instructions);

$ai_speaking_product                        = wc_get_product( $ai_speaking_evaluation_plan_id );
if( $ai_speaking_product ){
    $ai_speaking_evaluation_plan_title      = $ai_speaking_product->get_title();
    $ai_speaking_evaluation_plan_price      = $ai_speaking_product->get_price();
}




$ai_writing_evaluation_plan1_enable        = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable);
$ai_writing_evaluation_plan2_enable        = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable);
$ai_writing_evaluation_plan1and2_enable    = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable);

$ai_speaking_evaluation_plan_enable        = get_option($ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable);




?>
<br class="clear" />

<?php if(isset($_GET['updated']) && $_GET['updated'] == 'true'): ?>
<div class="notice notice-success is-dismissible">
    <p><strong>Settings saved successfully.</strong></p>
</div>
<?php endif; ?>

<p>In this section, you can add or adjust the price you want to charge for your AI writing and speaking evaluation services.</p>

<!-- START: SINGLE UNIFIED FORM -->
<form method="POST">
    
    <!-- Section 1: Enable AI Evaluation -->
    <table class="form-table">
        <tbody>
            <tr class="">
                <th scope="row" class="titledesc">Enable AI Evaluation</th>
                <td class="forminp forminp-checkbox ">
                    <fieldset>
                        <legend class="screen-reader-text">
                            <span>Enable AI Evaluation</span>
                        </legend>
                        <label for="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation; ?>">
                            <input 
                                name="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation; ?>" 
                                id="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_enable_ai_evaluation; ?>" 
                                type="checkbox" 
                                class="" 
                                value="1" 
                                <?php echo ($enable_ai_evaluation == '1')? 'checked' : ''; ?>
                            />	

                        </label> 
															
                    </fieldset>
                </td>
            </tr>

        </tbody>
    </table>


    <!-- Section 2: Writing AI Evaluation -->
    <div class="row">
        <div class="writing__container">
            <div id="poststuff">
                <div id="writing-evalution" class="postbox st-proqyz__evaluation-metabox">
                    <div class="postbox-header">
                        <h2 class="hndle">Writing AI Evaluation</h2>
                        <div class="handle-actions hide-if-no-js">
                            <button type="button" class="handlediv" aria-expanded="true">
                                <span class="toggle-indicator" aria-hidden="true"></span>
                            </button>
                        </div>
                    </div>
                    <div class="inside" style="display: block;">
                        <div id="writing-ai-evaluation-metabox-content" class="metabox__content">

                            <table class="form-table">
                                <tbody>
                                    <?php if(1 == 2) { ?>
                                    <!-- begin::writing instructions -->
                                    <tr class="">
                                        <th scope="row" class="titledesc">Instructions</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Enable AI Evaluation</span>
                                                </legend>
                                                <?php 
                                                    wp_editor($ai_writing_evaluation_instructions,$ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions, array(
                                                        'media_buttons' => true, // Show media upload buttons
                                                        'textarea_name' => $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_evaluation_instructions,
                                                        'textarea_rows' => 5, // Custom height (number of rows)
                                                        'teeny'         => false,
                                                    )); 
                                                ?>
                                                                                    
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <!-- end::writing instructions -->

                                    <tr class="hr-only" style="border: 1px dotted #cbcaca;"></tr> 
                                    <?php } ?>

                                    <!-- begin::writing task 1 -->
                                    <tr class="">
                                        <th scope="row" class="titledesc">AI Writing Task 1</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                
                                                <label for="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable; ?>">
                                                    <input 
                                                        name="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable; ?>" 
                                                        id="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable; ?>" 
                                                        type="checkbox" 
                                                        class="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1_evaluation_enable; ?>" 
                                                        value="1" 
                                                        <?php echo ($ai_writing_evaluation_plan1_enable == 1)? 'checked' : ''; ?>
                                                    />	
                                                    Enable AI Writing Task 1 Evaluation				
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Title</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                
                                                <label for="<?php echo $input_ai_writing_task1_title; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_writing_task1_title; ?>" 
                                                        id="<?php echo $input_ai_writing_task1_title; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo $ai_writing_evaluation_plan1_title; ?>" 
                                                    />
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr> 
                                    <tr class="">
                                        <th scope="row" class="titledesc">Price</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Price</span>
                                                </legend>
                                                <label for="<?php echo $input_ai_writing_task1_price; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_writing_task1_price; ?>" 
                                                        id="<?php echo $input_ai_writing_task1_price; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo $ai_writing_evaluation_plan1_price; ?>" 
                                                    />	

                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr> 
                                    <!-- end::writing task 1 -->

                                    <tr class="hr-only" style="border: 1px dotted #cbcaca;"></tr> 

                                    <!-- begin::writing task 2 -->
                                    <tr class="">
                                        <th scope="row" class="titledesc">
                                            AI Writing Task 2
                                        </th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                
                                                <label for="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable; ?>">
                                                    <input 
                                                        name="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable; ?>" 
                                                        id="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable; ?>" 
                                                        type="checkbox" 
                                                        class="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task2_evaluation_enable; ?>" 
                                                        value="1" 
                                                        <?php echo ($ai_writing_evaluation_plan2_enable == 1)? 'checked' : ''; ?>
                                                    />	
                                                    Enable AI Writing Task 2 Evaluation				
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Title</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Title</span>
                                                </legend>
                                                <label for="<?php echo $input_ai_writing_task2_title; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_writing_task2_title; ?>" 
                                                        id="<?php echo $input_ai_writing_task2_title; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo $ai_writing_evaluation_plan2_title; ?>" 
                                                    />	
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr> 
                                    <tr class="">
                                        <th scope="row" class="titledesc">Price</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Price</span>
                                                </legend>
                                                <label for="<?php echo $input_ai_writing_task2_price; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_writing_task2_price; ?>" 
                                                        id="<?php echo $input_ai_writing_task2_price; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo $ai_writing_evaluation_plan2_price; ?>" 
                                                    />	

                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr> 
                                    <!--end::writing task 2 -->


                                    <tr class="hr-only" style="border: 1px dotted #cbcaca;"></tr> 
                                    <?php if(1 == 2){ ?>                  
                                    <!-- begin::writing task 1 + 2 -->
                                    <tr class="">
                                        <th scope="row" class="titledesc">
                                            AI Writing Task 1 + 2
                                        </th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                
                                                <label for="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable; ?>">
                                                    <input 
                                                        name="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable; ?>" 
                                                        id="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable; ?>" 
                                                        type="checkbox" 
                                                        class="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_writing_task1and2_evaluation_enable; ?>" 
                                                        value="1" 
                                                        <?php echo ($ai_writing_evaluation_plan1and2_enable == 1)? 'checked' : ''; ?>
                                                    />	
                                                    Enable AI Writing Task 1 + 2 Evaluation				
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Title</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Title</span>
                                                </legend>
                                                <label for="<?php echo $input_ai_writing_task1and2_title; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_writing_task1and2_title; ?>" 
                                                        id="<?php echo $input_ai_writing_task1and2_title; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo $ai_writing_evaluation_plan1and2_title; ?>" 
                                                    />	

                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr> 
                                    <tr class="">
                                        <th scope="row" class="titledesc">Price</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Price</span>
                                                </legend>
                                                <label for="<?php echo $input_ai_writing_task1and2_price; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_writing_task1and2_price; ?>" 
                                                        id="<?php echo $input_ai_writing_task1and2_price; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo $ai_writing_evaluation_plan1and2_price; ?>" 
                                                    />	

                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr> 
                                    <!-- end::writing task 1 + 2 -->
                                    <?php } ?>    

                                </tbody>
                            </table>

                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Section 3: Speaking AI Evaluation -->
    <div class="row">
        <div class="writing__container">
            <div id="poststuff">
                <div id="speaking-ai-evalution" class="postbox st-proqyz__evaluation-metabox">
                    <div class="postbox-header">
                        <h2 class="hndle">Speaking AI Evaluation</h2>
                        <div class="handle-actions hide-if-no-js">
                            <button type="button" class="handlediv" aria-expanded="true">
                                <span class="toggle-indicator" aria-hidden="true"></span>
                            </button>
                        </div>
                    </div>
                    <?php if(defined('ST_PROQYZ_SPEAKING_AI_EVALUAION') && ST_PROQYZ_SPEAKING_AI_EVALUAION == 'true'){ ?>
                    <div class="inside" style="display: block;">
                        <div id="speaking-ai-evaluation-metabox-content" class="metabox__content">
                            <table class="form-table">
                                <tbody>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Instructions</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <legend class="screen-reader-text">
                                                    <span>Speaking AI Evaluation Instructions</span>
                                                </legend>
                                                <?php 
                                                    wp_editor(
                                                        $ai_speaking_evaluation_instructions,
                                                        'ai_speaking_evaluation_instructions',
                                                        array(
                                                            'media_buttons' => true,
                                                            'textarea_name' => $ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_instructions,
                                                            'textarea_rows' => 5,
                                                            'teeny'         => false,
                                                        )
                                                    ); 
                                                ?>
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Enable AI Speaking Evaluation</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <label for="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable; ?>">
                                                    <input 
                                                        name="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable; ?>" 
                                                        id="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable; ?>" 
                                                        type="checkbox" 
                                                        class="<?php echo $ST_OPTION_st_proqyz_IELTS_LMS_ai_speaking_evaluation_enable; ?>" 
                                                        value="1" 
                                                        <?php echo ($ai_speaking_evaluation_plan_enable == 1)? 'checked' : ''; ?>
                                                    /> 
                                                    Enable AI Speaking Evaluation
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Title</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <label for="<?php echo $input_ai_speaking_title; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_speaking_title; ?>" 
                                                        id="<?php echo $input_ai_speaking_title; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo esc_attr($ai_speaking_evaluation_plan_title); ?>" 
                                                    /> 
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr>
                                    <tr class="">
                                        <th scope="row" class="titledesc">Price</th>
                                        <td class="forminp forminp-checkbox ">
                                            <fieldset>
                                                <label for="<?php echo $input_ai_speaking_price; ?>">
                                                    <input 
                                                        name="<?php echo $input_ai_speaking_price; ?>" 
                                                        id="<?php echo $input_ai_speaking_price; ?>" 
                                                        type="text" 
                                                        class="regular-text" 
                                                        value="<?php echo esc_attr($ai_speaking_evaluation_plan_price); ?>" 
                                                    /> 
                                                </label>
                                            </fieldset>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <?php } else { ?>
                        <div class="inside" style="display: block;">
                            <div id="speaking-ai-evaluation-metabox-content" class="metabox__content">
                                <p>AI Speaking Evaluation is not enabled (due to its under development).</p>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>

    <!-- SINGLE SAVE BUTTON FOR ALL SECTIONS -->
    <p class="submit">
        <button name="save" class="components-button is-primary button-primary woocommerce-save-button" type="submit" value="Save changes">
            Save changes
        </button>
        <input 
            type="hidden" 
            id="st-proqyz-ai-evaluation-settings-save" 
            name="st-proqyz-ai-evaluation-settings-save" 
            value="1" 
        />		
    </p>
</form>
<!-- END: SINGLE UNIFIED FORM -->
