@extends('admin.layouts.app')

@section('title', '微信机器人配置')

@section('content')
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="p-6 border-b border-gray-100 flex justify-between items-center bg-gray-50">
            <div>
                <h2 class="text-lg font-bold text-gray-800">微信机器人全站配置</h2>
                <p class="text-xs text-gray-500 mt-1">保存后如涉及 Kafka 参数修改，需要重启后端消息消费守护进程。</p>
            </div>
        </div>

        <div class="p-6">
            <form action="{{ route('admin.wechat_bot.update') }}" method="POST" enctype="multipart/form-data">
                @csrf
                @method('PUT')

                <!-- 1. Kafka 及 API 基础配置 -->
                <div class="mb-8">
                    <h3 class="text-md font-bold text-gray-800 border-b pb-2 mb-4">核心参数 (Kafka / API)</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">Kafka Brokers</label>
                            <input type="text" name="kafka_brokers" value="{{ old('kafka_brokers', $config['kafka']['brokers'] ?? '') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="如 10.10.11.18:9092">
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">Kafka Topic</label>
                            <input type="text" name="kafka_topic" value="{{ old('kafka_topic', $config['kafka']['topic'] ?? '') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="监听的主题">
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">Kafka Group ID</label>
                            <input type="text" name="kafka_group_id" value="{{ old('kafka_group_id', $config['kafka']['group_id'] ?? 'chatroom_wechat_bot') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="消费组ID">
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">机器人微信号 (对外展示)</label>
                            <input type="text" name="kafka_bot_wxid" value="{{ old('kafka_bot_wxid', $config['kafka']['bot_wxid'] ?? '') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="供用户添加好友用的微信号">
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">机器人二维码 (对外展示图)</label>
                            <input type="file" name="qrcode_image" accept="image/*" class="w-full border-gray-300 rounded-md shadow-sm p-2 bg-gray-50 border">
                            @if(!empty($config['api']['qrcode_image']))
                                <div class="mt-2">
                                    <img src="{{ Storage::url($config['api']['qrcode_image']) }}" alt="QR Code" class="h-20 w-auto rounded border border-gray-200">
                                </div>
                            @endif
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">被调用方机器人服务基础API URL</label>
                            <input type="text" name="api_base_url" value="{{ old('api_base_url', $config['api']['base_url'] ?? '') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="如 http://10.10.11.14:8848">
                        </div>
                        <div>
                            <label class="block text-sm font-bold text-gray-700 mb-1">机器人 Key (必需)</label>
                            <input type="text" name="api_bot_key" value="{{ old('api_bot_key', $config['api']['bot_key'] ?? '') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="机器人的对接 Key">
                        </div>
                    </div>
                </div>

                <!-- 2. 全局推送时间设置 -->
                <div class="mb-8">
                    <h3 class="text-md font-bold text-gray-800 border-b pb-2 mb-4">全局推送时间设置 (免打扰)</h3>
                    <div class="mb-4">
                        <p class="text-sm text-gray-600 mb-4">设置系统允许进行微信通知（包含群聊与私聊）的时间段，留空则全天24小时允许推送。适用于避免夜间打扰。</p>
                        <div class="flex items-center space-x-4 max-w-lg">
                            <div class="flex-1">
                                <label class="block text-sm font-bold text-gray-700 mb-1">允许推送开始时间</label>
                                <input type="time" name="global_start_time" value="{{ old('global_start_time', $config['global_notify']['start_time'] ?? '08:00') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border">
                            </div>
                            <span class="text-gray-500 font-bold mt-6">至</span>
                            <div class="flex-1">
                                <label class="block text-sm font-bold text-gray-700 mb-1">允许推送结束时间</label>
                                <input type="time" name="global_end_time" value="{{ old('global_end_time', $config['global_notify']['end_time'] ?? '22:00') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border">
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 3. 群通知设置 -->
                <div class="mb-8">
                    <h3 class="text-md font-bold text-gray-800 border-b pb-2 mb-4">群聊通知设置</h3>
                    <div class="mb-6 max-w-lg">
                        <label class="block text-sm font-bold text-gray-700 mb-1">目标微信群 Wxid</label>
                        <input type="text" name="group_target_wxid" value="{{ old('group_target_wxid', $config['group_notify']['target_wxid'] ?? '') }}" class="w-full border-gray-300 rounded-md shadow-sm p-2.5 bg-gray-50 border" placeholder="群的wxid标识">
                        <p class="text-xs mt-1 text-gray-500">以下开关针对此群发送通知</p>
                    </div>

                    <div class="space-y-4">
                        <label class="flex items-center cursor-pointer">
                            <input type="hidden" name="toggle_admin_online" value="0">
                            <input type="checkbox" name="toggle_admin_online" value="1" class="form-checkbox h-5 w-5 text-indigo-600 rounded border-gray-300 focus:ring-indigo-500" {{ !empty($config['group_notify']['toggle_admin_online']) ? 'checked' : '' }}>
                            <span class="ml-2 text-sm text-gray-700 font-bold">管理员上线通知</span>
                        </label>
                        <label class="flex items-center cursor-pointer">
                            <input type="hidden" name="toggle_baccarat_result" value="0">
                            <input type="checkbox" name="toggle_baccarat_result" value="1" class="form-checkbox h-5 w-5 text-indigo-600 rounded border-gray-300 focus:ring-indigo-500" {{ !empty($config['group_notify']['toggle_baccarat_result']) ? 'checked' : '' }}>
                            <span class="ml-2 text-sm text-gray-700 font-bold">百家乐开奖通知</span>
                        </label>
                        <label class="flex items-center cursor-pointer">
                            <input type="hidden" name="toggle_lottery_result" value="0">
                            <input type="checkbox" name="toggle_lottery_result" value="1" class="form-checkbox h-5 w-5 text-indigo-600 rounded border-gray-300 focus:ring-indigo-500" {{ !empty($config['group_notify']['toggle_lottery_result']) ? 'checked' : '' }}>
                            <span class="ml-2 text-sm text-gray-700 font-bold">彩票开奖通知</span>
                        </label>
                    </div>
                </div>

                <!-- 3. 点对点私聊通知设置 -->
                <div class="mb-8">
                    <h3 class="text-md font-bold text-gray-800 border-b pb-2 mb-4">一对一私聊通知设置 (目标为用户微信)</h3>
                    <div class="space-y-4">
                        <label class="flex items-center cursor-pointer">
                            <input type="hidden" name="toggle_friend_online" value="0">
                            <input type="checkbox" name="toggle_friend_online" value="1" class="form-checkbox h-5 w-5 text-indigo-600 rounded border-gray-300 focus:ring-indigo-500" {{ !empty($config['personal_notify']['toggle_friend_online']) ? 'checked' : '' }}>
                            <span class="ml-2 text-sm text-gray-700 font-bold">好友上线 (带有30分钟冷却机制判断)</span>
                        </label>
                        <label class="flex items-center cursor-pointer">
                            <input type="hidden" name="toggle_spouse_online" value="0">
                            <input type="checkbox" name="toggle_spouse_online" value="1" class="form-checkbox h-5 w-5 text-indigo-600 rounded border-gray-300 focus:ring-indigo-500" {{ !empty($config['personal_notify']['toggle_spouse_online']) ? 'checked' : '' }}>
                            <span class="ml-2 text-sm text-gray-700 font-bold">夫妻上线 (带有30分钟冷却机制判断)</span>
                        </label>
                        <label class="flex items-center cursor-pointer">
                            <input type="hidden" name="toggle_level_change" value="0">
                            <input type="checkbox" name="toggle_level_change" value="1" class="form-checkbox h-5 w-5 text-indigo-600 rounded border-gray-300 focus:ring-indigo-500" {{ !empty($config['personal_notify']['toggle_level_change']) ? 'checked' : '' }}>
                            <span class="ml-2 text-sm text-gray-700 font-bold">等级变动</span>
                        </label>
                    </div>
                </div>

                <div class="mt-8 pt-6 border-t flex space-x-3">
                    <button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-md font-bold hover:bg-indigo-700 shadow-sm transition">保存配置</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 发送群内公告/消息 独立模块 -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mt-6">
        <div class="p-6 border-b border-gray-100 flex justify-between items-center bg-gray-50">
            <div>
                <h2 class="text-lg font-bold text-gray-800">发送群内公告 (即时通知)</h2>
                <p class="text-xs text-gray-500 mt-1">发出的内容将通过微信机器人直接推送到上方配置的目标微信群。</p>
            </div>
        </div>

        <div class="p-6">
            @if($errors->has('announcement_content'))
                <div class="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">
                    ❌ {{ $errors->first('announcement_content') }}
                </div>
            @endif

            <form action="{{ route('admin.wechat_bot.send_announcement') }}" method="POST">
                @csrf
                <div class="mb-4">
                    <label class="block text-sm font-bold text-gray-700 mb-2">公告内容</label>
                    <textarea name="announcement_content" rows="4" class="w-full border-gray-300 rounded-md shadow-sm p-3 bg-gray-50 border focus:ring-indigo-500 focus:border-indigo-500" placeholder="请输入要发送到微信群内的公告/通知内容...">{{ old('announcement_content') }}</textarea>
                </div>
                
                <div class="flex">
                    <button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-md font-bold hover:bg-indigo-700 shadow-sm transition flex items-center">
                        <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path></svg>
                        立即发送公告
                    </button>
                </div>
            </form>
        </div>
    </div>
@endsection
