<?php 

global $ST_OPTION_st_proqyz__open_ai_endpoint, $ST_OPTION_st_proqyz__open_ai_modal, $ST_OPTION_st_proqyz__open_ai_enable, $ST_OPTION_st_proqyz__open_ai_apikey;

if( isset($_POST['proqyz__open-ai-save-changes']) && !empty($_POST['proqyz__open-ai-save-changes']) ) {
    if(isset($_POST[$ST_OPTION_st_proqyz__open_ai_endpoint]) && !empty($_POST[$ST_OPTION_st_proqyz__open_ai_endpoint])){
        update_option($ST_OPTION_st_proqyz__open_ai_endpoint, $_POST[$ST_OPTION_st_proqyz__open_ai_endpoint]);
    } else {
        update_option($ST_OPTION_st_proqyz__open_ai_modal, '');
    }

    if(isset($_POST[$ST_OPTION_st_proqyz__open_ai_modal]) && !empty($_POST[$ST_OPTION_st_proqyz__open_ai_modal])){
        update_option($ST_OPTION_st_proqyz__open_ai_modal, $_POST[$ST_OPTION_st_proqyz__open_ai_modal]);
    } else {
        update_option($ST_OPTION_st_proqyz__open_ai_modal, 'gpt-4o-mini');
    }

    if(isset($_POST[$ST_OPTION_st_proqyz__open_ai_enable])){
        if((int) $_POST[$ST_OPTION_st_proqyz__open_ai_enable] == 1){
            update_option($ST_OPTION_st_proqyz__open_ai_enable, (int) $_POST[$ST_OPTION_st_proqyz__open_ai_enable]);
        } else {
            update_option($ST_OPTION_st_proqyz__open_ai_enable, 0);
        }
        
    } else {
        update_option($ST_OPTION_st_proqyz__open_ai_enable, 0);
    }

    if(isset($_POST[$ST_OPTION_st_proqyz__open_ai_apikey])) {
        update_option($ST_OPTION_st_proqyz__open_ai_apikey, $_POST[$ST_OPTION_st_proqyz__open_ai_apikey]);
    } else {
        update_option($ST_OPTION_st_proqyz__open_ai_apikey, '');
    }

}

$ST_OPTION_st_proqyz__open_ai_endpoint__option_value    = get_option($ST_OPTION_st_proqyz__open_ai_endpoint, 'https://api.openai.com/v1/chat/completions');
$ST_OPTION_st_proqyz__open_ai_modal__option_value       = get_option($ST_OPTION_st_proqyz__open_ai_modal, 'gpt-4o-mini');
$ST_OPTION_st_proqyz__open_ai_enable__option_value      = get_option($ST_OPTION_st_proqyz__open_ai_enable, 0);
$ST_OPTION_st_proqyz__open_ai_apikey__option_value      = get_option($ST_OPTION_st_proqyz__open_ai_apikey, '');

?>
<form method="POST">
    <table class="form-table">
        <tbody>
            <tr class="">
                <th scope="row" class="titledesc">
                    <label for="<?php echo $ST_OPTION_st_proqyz__open_ai_endpoint; ?>">
                        API endpoint 
                    </label>
                </th>
                <td class="forminp forminp-text">
                    <input 
                        name="<?php echo $ST_OPTION_st_proqyz__open_ai_endpoint; ?>" 
                        id="<?php echo $ST_OPTION_st_proqyz__open_ai_endpoint; ?>" 
                        type="text" 
                        value="<?php echo $ST_OPTION_st_proqyz__open_ai_endpoint__option_value; ?>" 
                        class="" 
                        placeholder="OpenAI endpoint" 
                    /> 
                    <p class="description ">default: https://api.openai.com/v1/completions</p>	
                </td>
            </tr>
            <tr class="">
                <th scope="row" class="titledesc">
                    <label for="<?php echo $ST_OPTION_st_proqyz__open_ai_modal; ?>">
                        Model
                    </label>
                </th>
                <td class="forminp forminp-text">
                    <select 
                        name="<?php echo $ST_OPTION_st_proqyz__open_ai_modal; ?>" 
                        id="<?php echo $ST_OPTION_st_proqyz__open_ai_modal; ?>" 
                        class=""
                        value="<?php echo $ST_OPTION_st_proqyz__open_ai_modal__option_value; ?>"
                    > 
                        <option value="gpt-4o-mini" <?php echo ($ST_OPTION_st_proqyz__open_ai_modal__option_value == 'gpt-4o-mini')? 'selected' : ''; ?>> GPT 4o Mini</option>
                        <option value="gpt-4o" <?php echo ($ST_OPTION_st_proqyz__open_ai_modal__option_value == 'gpt-4o')? 'selected' : ''; ?>> GPT 4o</option>
                        <option value="gpt-4" <?php echo ($ST_OPTION_st_proqyz__open_ai_modal__option_value == 'gpt-4')? 'selected' : ''; ?>> GPT 4</option>
                        
                    </select>
                    <p class="description ">default: GPT 4o</p>							
                </td>
            </tr>
            <tr class="">
                <th scope="row" class="titledesc">
                    <label for="<?php echo $ST_OPTION_st_proqyz__open_ai_apikey; ?>">
                        API Key
                    </label>
                </th>
                <td class="forminp forminp-text">
                    <input 
                        name="<?php echo $ST_OPTION_st_proqyz__open_ai_apikey; ?>" 
                        id="<?php echo $ST_OPTION_st_proqyz__open_ai_apikey; ?>" 
                        type="password" 
                        value="<?php echo $ST_OPTION_st_proqyz__open_ai_apikey__option_value; ?>" 
                        class="" 
                        placeholder="OpenAI Api key" 
                    /> 						
                </td>
            </tr>
            <tr class="">
                <th scope="row" class="titledesc">Enable</th>
                <td class="forminp forminp-checkbox ">
                    <fieldset>
                        <label for="<?php echo $ST_OPTION_st_proqyz__open_ai_enable; ?>">
                            <input 
                                name="<?php echo $ST_OPTION_st_proqyz__open_ai_enable; ?>" 
                                id="<?php echo $ST_OPTION_st_proqyz__open_ai_enable; ?>" 
                                type="checkbox" 
                                class=""
                                value="1"
                                <?php echo ((int) $ST_OPTION_st_proqyz__open_ai_enable__option_value == 1)? 'checked' : ''; ?>
                            /> 
                            Enable OpenAI API request	
                        </label> 
                        <p class="description "></p>																
                    </fieldset>
                </td>
            </tr>
            
        </tbody>
    </table>

    <p class="submit">
        <input type="hidden" name="proqyz__open-ai-save-changes" value="1" />
        <button name="save" class="proqyz__open-ai-save-btn components-button is-primary" type="submit" value="Save changes">Save changes</button>
    </p>
</form>