<?php
/*
 * @author: CloudZA(云之安) <2922619853@qq.com>
 * @hitokoto: 一场秋雨一场凉，秋心酌满泪为霜。
 * Copyright (c) 2022 by CloudZA, All Rights Reserved.
 */

include_once 'common.php';
if ( !isset($islogin)) header("Location: /");//非法访问
$title = '开关配置';
include_once 'partials/head.php';
$row = Db::table('app_web')
         ->find();

?>
<div id="pjax-container">
<!--导航-->
<div class="bg-body-light">
  <div class="content content-full">
    <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2">
      <div class="flex-grow-1">
        <h1 class="h3 fw-bold mb-2">
          网站信息
        </h1>
      </div>
      <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
        <ol class="breadcrumb breadcrumb-alt">
          <li class="breadcrumb-item">
            <a class="link-fx" href="javascript:void(0)">网站设置</a>
          </li>
          <li class="breadcrumb-item" aria-current="page">
            <?php echo $title ?>
          </li>
        </ol>
      </nav>
    </div>
  </div>
</div>
<!--导航结束-->
<!--内容-->
<div class="content animated fadeIn">
    <div class="block block-rounded">
        <div class="block-header block-header-default">
            <h3 class="block-title"> <?php echo $title ?> </h3>
        </div>
        <div class="block-content">
            <form action="" method="POST" onsubmit="return false;">
                <div class="form-floating mb-2">
                    <textarea class="form-control" id="site_notice" name="site_notice" style="height: 80px" placeholder="留空则不显示公告"><?php echo $row['site_notice']; ?></textarea>
                    <label class="fs-sm text-muted" for="site_notice">留空则不显示公告</label>
                </div>
                <div class="form-group mb-2">
                    <label class="fs-sm text-muted" for="site_notice_status">公告开关</label>
                    <select class="form-select" name="site_notice_status" id="site_notice_status">
                        <option value="0" <?php echo ($row['site_notice_status'] == 0)? 'selected':'' ?>>关闭</option>
                        <option value="1" <?php echo ($row['site_notice_status'] == 1)? 'selected':'' ?>>开启</option>
                    </select>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm text-muted" for="reg_chat_limit">ChatGPT API</label>
                    <select class="form-select" name="openai_api" id="openai_api">
                        <option value="<?php echo $row[ 'openai_api' ] ?>" <?php echo ( $row[ 'openai_api' ] == GPTAPI ) ? 'selected' : '' ?>>自定义源</option>
                        <option value="https://api.openai.com" <?php echo ( $row[ 'openai_api' ] == 'https://api.openai.com' ) ? 'selected' : '' ?>>https://api.openai.com</option>
                        <?php foreach (__API_LIST__ as $value): ?>
                            <option value="<?php echo $value ?>" <?php echo ( $row[ 'openai_api' ] == $value ) ? 'selected' : '' ?>><?php echo $value ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <label class="form-label fs-sm text-muted" for="my_openai_api">自定义ChatGPT API</label>
                <div class="input-group mb-2">
                    <input type="text" name="my_openai_api" id="my_openai_api" value="<?php echo $row[ 'openai_api' ] ?>" class="form-control" placeholder="自定义ChatGPT API（https://开头，结尾不需要/）" required />
                    <button type="button" onclick="speed()" class="btn btn-alt-primary">接口测速</button>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm text-muted" for="reg_chat_limit">注册额度</label>
                    <input type="text" name="reg_chat_limit" id="reg_chat_limit" value="<?php echo $row[ 'reg_chat_limit' ] ?>" class="form-control" placeholder="用户注册额度，填0则不送额度" required />
                </div>
                <label class="form-label fs-sm text-muted" for="bad_words">违禁词内容<small>(每个用英文逗号分开),留空则不检测</small></label>
                <div class="form-floating mb-2">
                    <textarea class="form-control" id="bad_words" name="bad_words" style="height: 80px" placeholder="违禁词内容"><?php echo $row[ 'bad_words' ]; ?></textarea>
                    <label class="fs-sm text-muted" for="bad_words">违禁词内容</label>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm" for="allow_registration">是否允许注册</label>
                    <select class="form-select" id="allow_registration">
                        <option value="0" <?php echo ( $row[ 'allow_registration' ] == 0 ) ? 'selected' : '' ?>>关闭</option>
                        <option value="1" <?php echo ( $row[ 'allow_registration' ] == 1 ) ? 'selected' : '' ?>>开启</option>
                    </select>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm" for="enable_email_verification">是否启用邮箱验证</label>
                    <select class="form-select" id="enable_email_verification">
                        <option value="0" <?php echo ( $row[ 'enable_email_verification' ] == 0 ) ? 'selected' : '' ?>>关闭</option>
                        <option value="1" <?php echo ( $row[ 'enable_email_verification' ] == 1 ) ? 'selected' : '' ?>>开启</option>
                    </select>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm" for="enable_membership_activation_code">是否启用卡密开通会员</label>
                    <select class="form-select" id="enable_membership_activation_code">
                        <option value="0" <?php echo ( $row[ 'enable_membership_activation_code' ] == 0 ) ? 'selected' : '' ?>>关闭</option>
                        <option value="1" <?php echo ( $row[ 'enable_membership_activation_code' ] == 1 ) ? 'selected' : '' ?>>开启</option>
                    </select>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm" for="enable_online_membership">是否启用在线开通会员</label>
                    <select class="form-select" id="enable_online_membership">
                        <option value="0"<?php echo ( $row[ 'enable_online_membership' ] == 0 ) ? 'selected' : '' ?>>关闭</option>
                        <option value="1"<?php echo ( $row[ 'enable_online_membership' ] == 1 ) ? 'selected' : '' ?>>开启</option>
                    </select>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm" for="enable_local_key_input">是否启用填写本地Key</label>
                    <select class="form-select" id="enable_local_key_input">
                        <option value="0"<?php echo ( $row[ 'enable_local_key_input' ] == 0 ) ? 'selected' : '' ?>>关闭</option>
                        <option value="1"<?php echo ( $row[ 'enable_local_key_input' ] == 1 ) ? 'selected' : '' ?>>开启</option>
                    </select>
                </div>
                <div class="form-group mb-2">
                    <label class="form-label fs-sm" for="system_maintenance">是否开启系统维护</label>
                    <select class="form-select" id="system_maintenance">
                        <option value="0"<?php echo ( $row[ 'system_maintenance' ] == 0 ) ? 'selected' : '' ?>>关闭</option>
                        <option value="1"<?php echo ( $row[ 'system_maintenance' ] == 1 ) ? 'selected' : '' ?>>开启</option>
                    </select>
                </div>
                <div class="row mb-2">
                    <div class="col-xl-12 mt-2">
                        <input type="submit" name="submit" id="submit" value="保存" class="btn btn-sm btn-alt-primary form-control" />
                    </div>
                </div>
            </form>
        </div>
    </div>
    <!--内容结束-->
    <script>
    $('#submit').click(() => {
        app.ajax('ajax.php?act=membership', {
            reg_chat_limit                   : app.getval('#reg_chat_limit'),
            allow_registration               : app.getval('#allow_registration'),
            enable_email_verification        : app.getval('#enable_email_verification'),
            enable_membership_activation_code: app.getval('#enable_membership_activation_code'),
            enable_online_membership         : app.getval('#enable_online_membership'),
            enable_local_key_input           : app.getval('#enable_local_key_input'),
            bad_words                        : app.getval('#bad_words'),
            openai_api                       : app.getval('#my_openai_api'),
            system_maintenance               : app.getval('#system_maintenance'),
            site_notice:app.getval('#site_notice'),
            site_notice_status:app.getval('#site_notice_status'),
        }, res => {
            if (res.code === 200) {
                app.notify(res.msg, 'success')
                setTimeout(() => {
                    app.pjax('membership.php');
                }, 1000);
            }
            else {
                app.notify(res.msg, 'warning')
            }
        })
    });

    $(document).ready(() => {
        const openaiApiSelect = document.getElementById('openai_api');
        const aaaInput = document.getElementById('my_openai_api');

        if (openaiApiSelect && aaaInput) { // 判断元素是否存在
            openaiApiSelect.addEventListener('change', () => {
                aaaInput.value = openaiApiSelect.value;
            });
        }
    });


    function speed ()
    {
        app.ajax('ajax.php?act=speed', {
            url: app.getval('#my_openai_api'),
        }, res => {
            if (res.code === 200) {
                app.btn(`当前API速度为：${res.data.speed}`, '我知道了')
            }
            else {
                app.btn(res.msg, '我知道了')
            }
        })
    }
</script>
</div>
    <?php include_once 'partials/foot.php'; ?>
