<?php
$act=isset($_GET['act'])?$_GET['act']:null;
if($act == 'mail'){
	include(dirname(__DIR__) . '/includes/common.php');
	@header('Content-Type: application/json; charset=UTF-8');
	$ret = send_mail($_POST['mail_recv'], '测试邮件', '我是测试邮件<br><br>检查是否配置成功<br>'.$date);
	if($ret['code'] == 1){
		$msg = '发送成功';
	}else{
		$msg = str_replace(array("\r\n", "\r", "\n"), "", $ret['msg']);
	}
	exit('{"code":1,"msg":"'.$msg.'"}');
}elseif($act == 'sms'){
	include(dirname(__DIR__) . '/includes/common.php');
	if($_SESSION['send_sms_time'] > time()){
		exit('{"code":-1,"msg":"1分钟内不可以重复发送短信，不然会被短信平台拉黑的。"}');
	}
	$ret = send_sms($_POST['mail_recv'], '这是测试短信，方便查看是否对接成功....验证码：123456');
	if($ret['code'] == 1){
		$_SESSION['send_sms_time'] = strtotime('+1minute');
		$msg = '发送成功';
	}else{
		$msg = '发送失败<br>'.$ret['msg'];
	}
	exit('{"code":1,"msg":"'.$msg.'"}');
}else{
	$title='邮件发信配置';
	include __DIR__ . '/head.php';
	$default_end_notify_tpl_html = '<h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">尊贵的用户，您好！</h2><p style="padding-left:2em;margin:0">账号：[qq][换行]通道：<font color="red">[接口分类名称]</font>[换行]到期剩余：<font color="red">[剩余天数]天</font>[换行]请及时续费，避免服务中断。</p>';
	$end_notify_tpl_value = trim((string)($conf['qq_end_notify_nr'] ?? ''));
	if($end_notify_tpl_value === ''){
		$end_notify_tpl_value = $default_end_notify_tpl_html;
	}
}

?>
<div class="p-4 md:p-6 space-y-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">邮件提醒配置</h2>
        </div>
        <div class="p-4 md:p-6">
            <form onsubmit="saveSetting(this);return false;" method="post" class="space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">账号到期提醒</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_end" default="<?php echo $conf['notice_end']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启中</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">QQ掉线提醒</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_qqzt" default="<?php echo $conf['notice_qqzt']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启中</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">接口异常提醒_自己</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_frame" default="<?php echo $conf['notice_frame']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启_邮箱</option>
                            <option value="2">开启_短信</option>
                            <option value="3">开启_微信</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">用户提现提醒_自己</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_tixian" default="<?php echo $conf['notice_tixian']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启_邮箱</option>
                            <option value="2">开启_短信</option>
                            <option value="3">开启_微信</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">充值余额提示_用户</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_recharge" default="<?php echo $conf['notice_recharge']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启_邮箱</option>
                            <option value="2">开启_短信</option>
                            <option value="3">开启_微信</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">充值余额提示_模板</label>
                    <div class="md:col-span-3">
                        <textarea class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_recharge_tpl" rows="4"><?php echo htmlspecialchars($conf['notice_recharge_tpl'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
                        <p class="mt-1 text-xs text-gray-500">可用变量：[站点名称] [用户账号] [用户ID] [产品版本] [金额] [余额] [说明]</p>
                        <div class="mt-2 flex flex-wrap gap-2">
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('recharge')">重置模板（邮箱/微信）</button>
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('recharge2')">重置模板（短信）</button>
                        </div>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">消费余额提示_用户</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_consume" default="<?php echo $conf['notice_consume']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启_邮箱</option>
                            <option value="2">开启_短信</option>
                            <option value="3">开启_微信</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">消费余额提示_模板</label>
                    <div class="md:col-span-3">
                        <textarea class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_consume_tpl" rows="4"><?php echo htmlspecialchars($conf['notice_consume_tpl'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
                        <p class="mt-1 text-xs text-gray-500">可用变量：[站点名称] [用户账号] [用户ID] [产品版本] [金额] [余额] [商品] [QQ] [说明]</p>
                        <div class="mt-2 flex flex-wrap gap-2">
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('consume')">重置模板（邮箱/微信）</button>
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('consume2')">重置模板（短信）</button>
                        </div>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">在线购买提示_用户</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_online_buy" default="<?php echo $conf['notice_online_buy']?>">
                            <option value="0">关闭</option>
                            <option value="1">开启_邮箱</option>
                            <option value="2">开启_短信</option>
                            <option value="3">开启_微信</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">在线购买提示_模板</label>
                    <div class="md:col-span-3">
                        <textarea class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_online_buy_tpl" rows="4"><?php echo htmlspecialchars($conf['notice_online_buy_tpl'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
                        <p class="mt-1 text-xs text-gray-500">可用变量：[站点名称] [用户账号] [用户ID] [产品版本] [商品] [金额] [支付方式] [QQ]</p>
                        <div class="mt-2 flex flex-wrap gap-2">
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('onlinebuy')">重置模板（邮箱/微信）</button>
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('onlinebuy2')">重置模板（短信）</button>
                        </div>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">接口异常提醒_次数</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_frame_count" value="<?php echo $conf['notice_frame_count']?>" placeholder="填写数值，留空则提醒5次">
                        <span class="text-sm text-gray-600">次</span>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">接口异常提醒_频率</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_frame_rate" value="<?php echo $conf['notice_frame_rate']?>" placeholder="每多少次异常通知一次">
                        <span class="text-sm text-gray-600">次/通知</span>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">接口异常提醒_间隔</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_frame_time" value="<?php echo $conf['notice_frame_time']?>" placeholder="填写数值，留空则5分钟每次">
                        <span class="text-sm text-gray-600">分</span>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">账号到期前提醒</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notify_end_day" value="<?php echo ($conf['notify_end_day']==''?5:$conf['notify_end_day'])?>" placeholder="填写数值，留空则提醒5天">
                        <span class="text-sm text-gray-600">天</span>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">账号到期前提醒_间隔</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="end_time" value="<?php echo ($conf['end_time']==''?8:$conf['end_time'])?>" placeholder="填写数值，留空则提醒6时">
                        <span class="text-sm text-gray-600">时</span>
                    </div>
                    <p class="md:col-start-2 md:col-span-3 text-xs text-gray-500 -mt-2">同一 QQ、同一接口分类在间隔内只提醒一次。</p>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">账号到期前提醒_内容</label>
                    <div class="md:col-span-3">
                        <textarea class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="qq_end_notify_nr" rows="4"><?php echo htmlspecialchars($end_notify_tpl_value ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
                        <p class="mt-1 text-xs text-gray-500">可用变量：[qq] [接口分类名称] [剩余天数] [换行]</p>
                        <div class="mt-2 flex flex-wrap gap-2">
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('end')">重置模板（邮箱/微信）</button>
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('end2')">重置模板（短信）</button>
                        </div>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">QQ掉线提醒_次数</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_qqzt_cuont" value="<?php echo $conf['notice_qqzt_cuont']?>" placeholder="填写数值，留空则3次">
                        <span class="text-sm text-gray-600">次</span>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">QQ掉线提醒_间隔</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_qqzt_time" value="<?php echo $conf['notice_qqzt_time']?>" placeholder="填写数值，留空则30分钟">
                        <span class="text-sm text-gray-600">分</span>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">QQ掉线提醒_内容</label>
                    <div class="md:col-span-3">
                        <textarea class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="notice_qqzt_nr" rows="4"><?php echo htmlspecialchars($conf['notice_qqzt_nr'] ?? '', ENT_QUOTES, 'UTF-8'); ?></textarea>
                        <div class="mt-2 flex flex-wrap gap-2">
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('zt')">重置邮箱和微信内容</button>
                            <button type="button" class="px-3 py-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg text-xs" onclick="resetting('zt2')">重置短信内容</button>
                        </div>
                    </div>
                </div>
                <div class="pt-4 border-t border-gray-200">
                    <button type="submit" class="w-full md:w-auto px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                        保存
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="p-4 md:p-6 border-b border-gray-200">
                <h2 class="text-base md:text-lg font-semibold text-gray-800">邮件发信配置</h2>
            </div>
            <div class="p-4 md:p-6">
                <form onsubmit="saveSetting(this);return false;" method="post" class="space-y-6">
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                        <label class="text-xs md:text-sm font-medium text-gray-700">发信模式</label>
                        <div class="md:col-span-3">
                            <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_cloud" default="<?php echo $conf['mail_cloud']?>">
                                <option value="0">SMTP发信</option>
                                <option value="1">搜狐Sendcloud</option>
                                <option value="2">阿里云邮件推送</option>
                            </select>
                        </div>
                    </div>
                    <div id="frame_set1" style="display: inherit;">
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">SMTP服务器</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_smtp" value="<?php echo $conf['mail_smtp']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">SMTP端口</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_port" value="<?php echo $conf['mail_port']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">邮箱账号</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_name" value="<?php echo $conf['mail_name']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">邮箱密码</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_pwd" value="<?php echo $conf['mail_pwd']?>">
                            </div>
                        </div>
                    </div>
                    <div id="frame_set2" style="display: none;">
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">API_USER</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_apiuser" value="<?php echo $conf['mail_apiuser']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">API_KEY</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_apikey" value="<?php echo $conf['mail_apikey']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">发信邮箱</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_name2" value="<?php echo $conf['mail_name2']?>">
                            </div>
                        </div>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                        <label class="text-xs md:text-sm font-medium text-gray-700">收信邮箱</label>
                        <div class="md:col-span-3">
                            <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="mail_recv" value="<?php echo $conf['mail_recv']?>" placeholder="填写自己能收到邮件的邮箱">
                            <p class="mt-1 text-xs text-gray-500">用于通知框架掉线/其他通知</p>
                            <button type="button" class="mt-2 px-4 py-2 bg-gray-800 hover:bg-gray-900 text-white font-medium rounded-lg transition text-sm" onclick="test_mail('mail')">发送一封测试邮件</button>
                        </div>
                    </div>
                    <div class="pt-4 border-t border-gray-200">
                        <button type="submit" class="w-full md:w-auto px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                            保存
                        </button>
                    </div>
                    <div class="p-4 bg-gray-50 border border-gray-200 rounded-lg">
                        <p class="text-xs text-gray-600 leading-relaxed">
                            此功能为用户下单时给自己发邮件提醒以及发卡商品发送给用户的邮件。<br>
                            使用普通模式发信时，建议使用QQ邮箱，SMTP服务器smtp.qq.com，端口465或587，密码不是QQ密码也不是邮箱独立密码，是QQ邮箱设置界面生成的
                            <a href="https://service.mail.qq.com/cgi-bin/help?subtype=1&&no=1001256&&id=28" target="_blank" rel="noreferrer" class="text-blue-600 hover:underline">授权码</a>。<br>
                            阿里云邮件推送：<a href="https://www.aliyun.com/product/directmail" target="_blank" rel="noreferrer" class="text-blue-600 hover:underline">点此进入</a>｜<a href="https://usercenter.console.aliyun.com/#/manage/ak" target="_blank" rel="noreferrer" class="text-blue-600 hover:underline">获取AK/SK</a>
                        </p>
                    </div>
                </form>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="p-4 md:p-6 border-b border-gray-200">
                <h2 class="text-base md:text-lg font-semibold text-gray-800">手机短信配置</h2>
            </div>
            <div class="p-4 md:p-6">
                <form onsubmit="saveSetting(this);return false;" method="post" class="space-y-6">
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                        <label class="text-xs md:text-sm font-medium text-gray-700">短信平台</label>
                        <div class="md:col-span-3">
                            <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="sms_type" default="<?php echo $conf['sms_type']?>">
                                <option value="0" url="http://sms.yfyidc.cn" urls="<?php echo $conf['sms_url']?>">易发云聚合短信</option>
                                <option value="1" url="http://api.smsbao.com" urls="<?php echo $conf['sms_url']?>">短信宝</option>
                                <option value="2" url="http://api.freesms.chat" urls="<?php echo $conf['sms_url']?>">自由云</option>
                            </select>
                        </div>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                        <label class="text-xs md:text-sm font-medium text-gray-700">平台网址</label>
                        <div class="md:col-span-3">
                            <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="sms_url" value="<?php echo $conf['sms_url']?>" placeholder="必须填写协议头如：http(s)://qq.com">
                            <input type="hidden" name="ding_sms_url" value="<?php echo $conf['ding_sms_url']?>">
                            <div class="mt-2 flex items-center gap-2">
                                <input type="checkbox" id="_ding_sms_url"<?php if($conf['ding_sms_url']==1)echo ' checked'?> class="w-4 h-4 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
                                <label for="_ding_sms_url" class="text-sm text-gray-700">自定义平台网址</label>
                            </div>
                        </div>
                    </div>
                    <div id="frame_set1" style="display: inherit;">
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">用户名/keyID</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="sms_appid" value="<?php echo $conf['sms_appid']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                            <label class="text-xs md:text-sm font-medium text-gray-700">秘钥/key</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="sms_appkey" value="<?php echo $conf['sms_appkey']?>">
                            </div>
                        </div>
                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                            <label class="text-xs md:text-sm font-medium text-gray-700">签名</label>
                            <div class="md:col-span-3">
                                <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="sms_name" value="<?php echo $conf['sms_name']?>" placeholder="如填写：云端登录，名称必须通过审核的">
                                <p class="mt-1 text-xs text-red-600">如：【云端登录】您的验证码是1234，填写：云端登录<br>名称必须通过审核的</p>
                            </div>
                        </div>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                        <label class="text-xs md:text-sm font-medium text-gray-700">手机号码</label>
                        <div class="md:col-span-3">
                            <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="sms_phone" value="<?php echo $conf['sms_phone']?>" placeholder="填写自己能收到短信的手机号码">
                            <p class="mt-1 text-xs text-gray-500">用于给自己发送短信通知</p>
                            <button type="button" class="mt-2 px-4 py-2 bg-gray-800 hover:bg-gray-900 text-white font-medium rounded-lg transition text-sm" onclick="test_mail('sms')">发送一条测试短信</button>
                        </div>
                    </div>
                    <div class="pt-4 border-t border-gray-200">
                        <button type="submit" class="w-full md:w-auto px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                            保存
                        </button>
                    </div>
                    <div class="p-4 bg-gray-50 border border-gray-200 rounded-lg">
                        <p class="text-xs text-gray-600 leading-relaxed">
                            易发云聚合短信 <a href="http://sms.yfyidc.cn/user/register?code=p7uJ" target="_blank" class="text-blue-600 hover:underline">点此进入</a><br>
                            短信宝 <a href="http://www.smsbao.com/reg?r=HS6W" target="_blank" class="text-blue-600 hover:underline">点此进入</a><br>
                            自由云 <a href="https://www.freesms.chat" target="_blank" class="text-blue-600 hover:underline">点此进入</a>
                        </p>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">微信消息设置</h2>
        </div>
        <div class="p-4 md:p-6">
            <form onsubmit="saveSetting(this);return false;" method="post" class="space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">微信消息接口</label>
                    <div class="md:col-span-3">
                        <select class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="wechat_api" default="<?php echo $conf['wechat_api']?>">
                            <option value="0">WxPusher</option>
                        </select>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">appToken</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="wechat_apptoken" value="<?php echo $conf['wechat_apptoken']?>">
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">用户UID</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="text" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="wechat_appuid" value="<?php echo $conf['wechat_appuid']?>" placeholder="请点击_右侧获取">
                        <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" onclick="get_appuid()">获取</button>
                    </div>
                </div>
                <div class="pt-4 border-t border-gray-200">
                    <button type="submit" class="w-full md:w-auto px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                        保存
                    </button>
                </div>
                <div class="p-4 bg-gray-50 border border-gray-200 rounded-lg">
                    <p class="text-xs text-gray-600">
                        WxPusher <a href="https://wxpusher.zjiecode.com/admin/" target="_blank" class="text-blue-600 hover:underline">点此进入</a>
                    </p>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 扫码关注弹窗 -->
<div class="modal fade" id="qr_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">请扫码关注</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body text-center">
                <img id="qr_img" src="" style="width:300px;height:300px;">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<?php include 'foot.php'; ?>


<script type="text/javascript">
	var t;
	$("select[name='sms_type']").change(function(){
		sms_ischange();
	});
	$("#_ding_sms_url").change(function(){
		if($('#_ding_sms_url').is(':checked')){
			$("input[name='ding_sms_url']").val('1');
		}else{
			$("input[name='ding_sms_url']").val('0');
		}
	});
	function resetting(type){
		if(type == 'end'){
			$("textarea[name='qq_end_notify_nr']").val('<h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">尊贵的用户，您好！</h2><p style="padding-left:2em;margin:0">账号：[qq][换行]通道：<font color=\"red\">[接口分类名称]</font>[换行]到期剩余：<font color=\"red\">[剩余天数]天</font>[换行]请及时续费，避免服务中断。</p>');
		}else if(type == 'end2'){
			$("textarea[name='qq_end_notify_nr']").val('尊贵的用户，您好！账号：[qq] [换行]通道：[接口分类名称] [换行]到期剩余：[剩余天数]天 [换行]请及时续费，避免服务中断。');
		}else if(type == 'zt'){
			$("textarea[name='notice_qqzt_nr']").val('<h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">尊贵的用户，您好！</h2><p style="padding-left:2em;margin:0">检测到您的账号：[qq]，掉线了！请重新登录。</p>');
		}else if(type == 'zt2'){
			$("textarea[name='notice_qqzt_nr']").val('尊贵的用户，您好！检测到您的账号：[qq]，掉线了！请重新登录。');
		}else if(type == 'recharge'){
			$("textarea[name='notice_recharge_tpl']").val('<h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">尊贵的用户，您好！</h2><p style="padding-left:2em;margin:0">【[站点名称]】充值余额提醒<br>用户：[用户账号](ID:[用户ID])<br>产品版本：[产品版本]<br>金额：[金额]元<br>变动后余额：[余额]元<br>说明：[说明]</p>');
		}else if(type == 'recharge2'){
			$("textarea[name='notice_recharge_tpl']").val('【[站点名称]】充值余额提醒 用户：[用户账号](ID:[用户ID]) 产品版本：[产品版本] 金额：[金额]元 余额：[余额]元 说明：[说明]');
		}else if(type == 'consume'){
			$("textarea[name='notice_consume_tpl']").val('<h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">尊贵的用户，您好！</h2><p style="padding-left:2em;margin:0">【[站点名称]】消费余额提醒<br>用户：[用户账号](ID:[用户ID])<br>产品版本：[产品版本]<br>购买商品：[商品]<br>购买QQ：[QQ]<br>金额：[金额]元<br>变动后余额：[余额]元<br>说明：[说明]</p>');
		}else if(type == 'consume2'){
			$("textarea[name='notice_consume_tpl']").val('【[站点名称]】消费余额提醒 用户：[用户账号](ID:[用户ID]) 产品版本：[产品版本] 购买商品：[商品] 购买QQ：[QQ] 金额：[金额]元 余额：[余额]元 说明：[说明]');
		}else if(type == 'onlinebuy'){
			$("textarea[name='notice_online_buy_tpl']").val('<h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">尊贵的用户，您好！</h2><p style="padding-left:2em;margin:0">【[站点名称]】在线购买提醒<br>用户：[用户账号](ID:[用户ID])<br>产品版本：[产品版本]<br>购买商品：[商品]<br>购买QQ：[QQ]<br>金额：[金额]元<br>支付方式：[支付方式]</p>');
		}else if(type == 'onlinebuy2'){
			$("textarea[name='notice_online_buy_tpl']").val('【[站点名称]】在线购买提醒 用户：[用户账号](ID:[用户ID]) 产品版本：[产品版本] 购买商品：[商品] 购买QQ：[QQ] 金额：[金额]元 支付方式：[支付方式]');
		}
	}
	function sms_ischange() {
		let url = $("select[name='sms_type']").find("option:selected").attr("url");
		if(!$('#_ding_sms_url').is(':checked')){
			$("input[name='sms_url']").val(url);
		}
	}
	function get_appuid(){
		let wechat_apptoken = $("input[name='wechat_apptoken']").val();
		if(wechat_apptoken == ''){
			showToast('appToken 不能为空', 'error');return false;
		}
		clearInterval(t);
		showLoading();
		$.ajax({
			type : 'POST',
			url : 'ajax.php?act=get_WxPusher_qr',
			data : {wechat_apptoken:wechat_apptoken},
			dataType : 'json',
			success : function(data) {
				hideLoading();
				if(data.code != 1000){
					showToast(data.msg, 'error');
				}else{
					$('#qr_img').attr('src', data.data.url);
					$('#qr_modal').modal('show');
					t = setInterval(function(){
						$.ajax({
							type : 'POST',
							url : 'ajax.php?act=get_WxPusher_uid',
							data : {wechat_apptoken:wechat_apptoken},
							dataType : 'json',
							success : function(data) {
								if(data.code == 1000){
									clearInterval(t);
									$('#qr_modal').modal('hide');
									$("input[name='wechat_appuid']").val(data.data);
									showToast('获取成功', 'success');
								}
							}
						});
					}, 2000);
				}
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}
	function test_mail(type){
		if(type == 'mail'){
			var mail_recv = $("input[name='mail_recv']").val();
			if(mail_recv == ''){
				showToast('收信邮箱不能为空', 'error');return false;
			}
			showLoading();
			$.ajax({
				type : 'POST',
				url : '?act=mail',
				data : {mail_recv:mail_recv},
				dataType : 'json',
				success : function(data) {
					hideLoading();
					showToast(data.msg, 'success');
				},
				error:function(data){
					hideLoading();
					showToast('服务器错误', 'error');
					return false;
				}
			});
		}else if(type == 'sms'){
			var sms_phone = $("input[name='sms_phone']").val();
			if(sms_phone == ''){
				showToast('手机号码不能为空', 'error');return false;
			}
			showLoading();
			$.ajax({
				type : 'POST',
				url : '?act=sms',
				data : {mail_recv:sms_phone},
				dataType : 'json',
				success : function(data) {
					hideLoading();
					showToast(data.msg, 'success');
				},
				error:function(data){
					hideLoading();
					showToast('服务器错误', 'error');
					return false;
				}
			});
		}
	}
	function saveSetting(obj){
		showLoading();
		$.ajax({
			type : 'POST',
			url : 'ajax.php?act=set',
			data : $(obj).serialize(),
			dataType : 'json',
			success : function(data) {
				hideLoading();
				showToast(data.msg, 'success');
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}
	var items = $("select[default]");
	for (i = 0; i < items.length; i++) {
		$(items[i]).val($(items[i]).attr("default")||0);
	}
</script>
