<?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();
if ($_GET[ 'mod' ] == 'mailtest') {
    $email     = $row['send_collect_email'];
    $sendEmail = send($email , '邮箱验证码 - ' . WEB_NAME , '这是一封测试邮件');
    if ($sendEmail) {
        infoMessage('发信成功' , '1');
    } else {
        infoMessage('发信失败' , '4');
    }
}

?>
<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"> 发信配置 </h3>
    </div>
     <div class="block-content">
        <form action="" method="POST" onsubmit="return false;">
            <div class="form-group mb-2">
                <label class="form-label fs-sm" for="host">SMTP服务器</label>
                <input type="text" name="host" id="host" value="<?php echo $row[ 'send_smtp_host' ] ?>" class="form-control" placeholder="smtp.exmail.qq.com" required />
            </div>
            <div class="form-group mb-2">
                <label class="form-label fs-sm" for="port">SMTP端口</label>
                <input type="text" name="port" id="port" value="<?php echo $row[ 'send_smtp_port' ] ?>" class="form-control" placeholder="465" required />
            </div>
            <div class="form-group mb-2">
                <label class="form-label fs-sm" for="from_name">发信名称</label>
                <input type="text" name="from_name" id="from_name" value="<?php echo $row[ 'send_from_name' ] ?>" class="form-control" required placeholder="云之安" />
            </div>
            <div class="form-group mb-2">
                <label class="form-label fs-sm" for="username">邮箱账号</label>
                <input type="text" name="username" id="username" value="<?php echo $row[ 'send_username' ] ?>" class="form-control" required placeholder="2922619853@qq.com" />
            </div>
             <div class="form-group mb-2">
                <label class="form-label fs-sm" for="password">邮箱密码</label>
                <input type="text" name="password" id="password" value="<?php echo $row[ 'send_password' ] ?>" class="form-control" required placeholder="********" />
            </div>
            <div class="form-group mb-2">
                <label class="form-label fs-sm" for="inbox">收信邮箱<small class="text-muted">（用于测试）</small></label>
                <input type="text" name="inbox" id="inbox" value="<?php echo $row[ 'send_collect_email' ] ?>" class="form-control" required placeholder="admin@osuu.cc" />
            </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>
            <div class="mb-2">
                <span class="text-muted fs-sm">
                    <?php if ($row[ 'send_from_name' ]) : ?>
                        [
                        <a href="send.php?mod=mailtest">给<?php echo $row[ 'send_collect_email' ] ?: $row[ 'send_from_name' ] ?>发一封测试邮件</a>]
                    <?php endif; ?>
                </span>
            </div>
        </form>
    </div>
</div>
    <!--内容结束-->
    <script>
    $('#submit').click(() => {
        app.ajax('ajax.php?act=send', {
            hosts    : app.getval('#host'),
            ports    : app.getval('#port'),
            from_name: app.getval('#from_name'),
            username : app.getval('#username'),
            password : app.getval('#password'),
            inbox    : app.getval('#inbox')
        }, res => {
            if (res.code === 200) {
                app.notify(res.msg, 'success')
                setTimeout(() => {
                    app.pjax('send.php');
                }, 1000);
            }
            else {
                app.notify(res.msg, 'warning')
            }
        })
    });
</script>
</div>
    <?php include_once 'partials/foot.php'; ?>
