<?php
$title='卡密管理';
include __DIR__ . '/head.php';
if($conf['kami_open'] != 1)exit("<script>alert('卡密功能已关闭');location.href='./set.php';</script>");
?>
<div class="space-y-4">
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="px-4 py-3 bg-gray-50 border-b border-gray-100">
            <h3 class="font-medium text-gray-800 flex items-center">
                <i class="fas fa-key text-indigo-500 mr-1.5"></i>
                卡密管理
            </h3>
        </div>
        <div class="p-4">
            <form onsubmit="searchOrder();return false;" class="flex flex-wrap gap-2 items-end">
                <div class="flex-1 min-w-[180px]">
                    <label class="block text-sm font-medium text-gray-700 mb-1">搜索卡密</label>
                    <input type="text" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="kw" placeholder="输入卡密信息">
                </div>
                <div class="w-full sm:w-32">
                    <label class="block text-sm font-medium text-gray-700 mb-1">状态</label>
                    <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="type">
                        <option value="-1">全部</option>
                        <option value="0">未使用</option>
                        <option value="1">已使用</option>
                    </select>
                </div>
                <div class="flex flex-wrap gap-2 w-full sm:w-auto">
                    <button type="submit" class="px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium transition-colors text-sm">
                        <i class="fas fa-search mr-1"></i>搜索
                    </button>
                    <button type="button" onclick="openGenerateModal()" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg font-medium transition-colors text-sm">
                        <i class="fas fa-plus mr-1"></i>生成
                    </button>
                    <button type="button" onclick="daochu(0)" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-colors text-sm">
                        <i class="fas fa-download mr-1"></i>导出
                    </button>
                    <button type="button" onclick="if(confirm('是否清除所有卡密，未使用的退回余额')){qk(0)}" class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg font-medium transition-colors text-sm">
                        <i class="fas fa-trash mr-1"></i>清空
                    </button>
                    <button type="button" onclick="qk(1)" class="px-4 py-2 bg-orange-600 hover:bg-orange-700 text-white rounded-lg font-medium transition-colors text-sm">
                        <i class="fas fa-trash-alt mr-1"></i>清空已使用
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full min-w-[800px]">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">卡密</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">版本</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">商品名称</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">状态</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">金额</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">使用QQ</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">使用时间</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">添加时间</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 whitespace-nowrap">操作</th>
                    </tr>
                </thead>
                <tbody id="list" class="divide-y divide-gray-100"></tbody>
            </table>
        </div>
        <div class="px-4 py-3 flex items-center justify-between border-t border-gray-100">
            <div class="text-sm text-gray-500">
                共 <span id="total" class="font-medium text-indigo-600">0</span> 条卡密
            </div>
            <div class="flex items-center gap-2">
                <button onclick="prevPage()" id="prevPage" disabled class="w-8 h-8 flex items-center justify-center bg-gray-100 hover:bg-gray-200 text-gray-600 rounded transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
                    <i class="fas fa-chevron-left text-sm"></i>
                </button>
                <span class="text-sm text-gray-500">第 <span id="currentPage">1</span> / <span id="totalPages">1</span> 页</span>
                <button onclick="nextPage()" id="nextPage" disabled class="w-8 h-8 flex items-center justify-center bg-gray-100 hover:bg-gray-200 text-gray-600 rounded transition-colors disabled:opacity-50 disabled:cursor-not-allowed">
                    <i class="fas fa-chevron-right text-sm"></i>
                </button>
                <select id="page" class="px-3 py-1.5 bg-gray-50 border border-gray-200 rounded-lg text-sm">
                    <option value="1">1</option>
                </select>
            </div>
        </div>
    </div>

    <div class="text-center">
        <a href="javascript:history.back(-1)" class="text-sm text-gray-500 hover:text-gray-700 transition-colors">
            <i class="fas fa-arrow-left mr-1"></i>返回上一层
        </a>
    </div>
</div>

<div id="generateModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeGenerateModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md max-h-[85vh] flex flex-col">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 class="text-lg font-semibold text-gray-800">卡密生成</h3>
                <button onclick="closeGenerateModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div class="px-5 py-4 overflow-y-auto">
                <form id="km_form" class="space-y-3">
                    <?php 
                    $qzone_enabled = $QZONE_ENABLED;
                    $cloud_enabled = $CLOUD_ENABLED;
                    $dg_enabled = $DG_ENABLED;
                    $version_name_cloud = $conf['version_name_cloud'] ?? '云端版';
                    $version_name_dg = $conf['version_name_dg'] ?? '代挂版';
                    $version_name_qzone = $conf['version_name_qzone'] ?? '秒赞版';
                    $version_name_cloud_all = $conf['version_name_cloud_all'] ?? (preg_replace('/版$/u', '', $version_name_cloud) . '·秒赞');
                    $version_name_dg_all = $conf['version_name_dg_all'] ?? (preg_replace('/版$/u', '', $version_name_dg) . '·秒赞');
                    $version_name_tri_all = $conf['version_name_tri_all'] ?? '三版通用';
                    $has_cloud_all_goods = $DB->getColumn("SELECT COUNT(*) FROM pre_goods WHERE status='1' AND (version='cloud_all' OR version='all')");
                    $has_dg_all_goods = $DB->getColumn("SELECT COUNT(*) FROM pre_goods WHERE status='1' AND version='dg_all'");
                    $has_tri_all_goods = $DB->getColumn("SELECT COUNT(*) FROM pre_goods WHERE status='1' AND version='tri_all'");
                    $has_qzone_goods = $DB->getColumn("SELECT COUNT(*) FROM pre_goods WHERE status='1' AND (version='qzone' OR version='')");
                    $has_cloud_goods = $DB->getColumn("SELECT COUNT(*) FROM pre_goods WHERE status='1' AND version='cloud'");
                    $has_dg_goods = $DB->getColumn("SELECT COUNT(*) FROM pre_goods WHERE status='1' AND version='dg'");

                    $show_qzone = $qzone_enabled && $has_qzone_goods > 0;
                    $show_cloud = $cloud_enabled && $has_cloud_goods > 0;
                    $show_dg = $dg_enabled && $has_dg_goods > 0;
                    $show_cloud_all = $has_cloud_all_goods > 0 && $qzone_enabled && $cloud_enabled;
                    $show_dg_all = $has_dg_all_goods > 0 && $qzone_enabled && $dg_enabled;
                    $show_tri_all = $has_tri_all_goods > 0 && $qzone_enabled && $cloud_enabled && $dg_enabled;
                    $show_version_select = $show_qzone || $show_cloud || $show_dg || $show_cloud_all || $show_dg_all || $show_tri_all;

                    if($show_version_select): 
                    ?>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">产品版本</label>
                        <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="version" id="km-version" onchange="loadGoodsByVersion()">
                            <?php if($show_cloud): ?>
                            <option value="cloud"><?php echo htmlspecialchars($version_name_cloud); ?></option>
                            <?php endif; ?>
                            <?php if($show_dg): ?>
                            <option value="dg"><?php echo htmlspecialchars($version_name_dg); ?></option>
                            <?php endif; ?>
                            <?php if($show_qzone): ?>
                            <option value="qzone"><?php echo htmlspecialchars($version_name_qzone); ?></option>
                            <?php endif; ?>
                            <?php if($show_cloud_all): ?>
                            <option value="cloud_all"><?php echo htmlspecialchars($version_name_cloud_all); ?></option>
                            <?php endif; ?>
                            <?php if($show_dg_all): ?>
                            <option value="dg_all"><?php echo htmlspecialchars($version_name_dg_all); ?></option>
                            <?php endif; ?>
                            <?php if($show_tri_all): ?>
                            <option value="tri_all"><?php echo htmlspecialchars($version_name_tri_all); ?></option>
                            <?php endif; ?>
                        </select>
                    </div>
                    <?php elseif($show_qzone): ?>
                    <input type="hidden" name="version" id="km-version" value="qzone">
                    <?php elseif($show_cloud): ?>
                    <input type="hidden" name="version" id="km-version" value="cloud">
                    <?php elseif($show_dg): ?>
                    <input type="hidden" name="version" id="km-version" value="dg">
                    <?php else: ?>
                    <div class="text-center py-8 text-gray-500">暂无可生成的商品，请联系管理员</div>
                    <?php endif; ?>
                    <div id="kami-cid-container">
                        <label class="block text-sm font-medium text-gray-700 mb-1">商品分类</label>
                        <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="cid" id="cid"></select>
                    </div>
                    <div id="kami-goods-container">
                        <label class="block text-sm font-medium text-gray-700 mb-1">商品列表</label>
                        <select class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="goods" id="goods"></select>
                        <div class="text-xs text-gray-500 mt-2">限购商品不会出现在卡密生成列表中</div>
                    </div>
                    <div id="kami-time-container" style="display:none;">
                        <label class="block text-sm font-medium text-gray-700 mb-1">卡密时长</label>
                        <div class="flex gap-2">
                            <input type="number" class="flex-1 px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="time_num" value="30" min="1" placeholder="时长数值">
                            <select class="w-20 px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="time">
                                <option value="1">天</option>
                                <option value="0">月</option>
                                <option value="3">年</option>
                            </select>
                        </div>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">生成数量</label>
                        <input type="number" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="num" value="1" min="1" max="9999" oninput="num_money()">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">卡密前缀</label>
                        <input type="text" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm" name="kmqz" placeholder="可留空、仅支持数英">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">商品价钱</label>
                        <input type="text" class="w-full px-3 py-2 bg-gray-100 border border-gray-200 rounded-lg text-sm" id="need" disabled>
                    </div>
                </form>
                <div class="mt-3 text-sm text-red-600">
                    <?php echo $conf['kami_tc'] == 1 ? '注：当下级用户卡密被激活时您才会得到提成' : '注：卡密激活是没有提成的'; ?>
                </div>
            </div>
            <div class="px-5 py-3 border-t border-gray-100 flex gap-2 justify-end">
                <button id="addKamiBtn" onclick="add_kami()" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg font-medium transition-colors text-sm">确定</button>
                <button onclick="closeGenerateModal()" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg font-medium transition-colors text-sm">取消</button>
            </div>
        </div>
    </div>
</div>

<div id="resultModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeResultModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-md max-h-[85vh] flex flex-col">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 class="text-lg font-semibold text-gray-800">生成成功</h3>
                <button onclick="closeResultModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div class="px-5 py-4 overflow-y-auto">
                <textarea id="txt_km" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg text-sm" rows="10" readonly></textarea>
            </div>
            <div class="px-5 py-3 border-t border-gray-100 flex gap-2 justify-end">
                <button onclick="copyKami()" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-colors text-sm">复制全部</button>
                <button onclick="closeResultModal()" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg font-medium transition-colors text-sm">关闭</button>
            </div>
        </div>
    </div>
</div>

<div id="messageModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeMessageModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-sm">
            <div class="p-6 text-center">
                <div id="messageIcon" class="mb-3"></div>
                <p id="messageText" class="text-gray-700 mb-4"></p>
                <button onclick="closeMessageModal()" class="px-6 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg font-medium transition-colors text-sm">确定</button>
            </div>
        </div>
    </div>
</div>

<div id="confirmModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-sm">
            <div class="p-6 text-center">
                <div class="mb-3"><i class="fas fa-exclamation-triangle text-yellow-500 text-4xl"></i></div>
                <p id="confirmText" class="text-gray-700 mb-4"></p>
                <div class="flex gap-2 justify-center">
                    <button id="confirmYesBtn" class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg font-medium transition-colors text-sm">删除</button>
                    <button onclick="closeConfirmModal()" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg font-medium transition-colors text-sm">取消</button>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="exportModal" class="fixed inset-0 z-50 hidden">
    <div class="fixed inset-0 bg-black/50 backdrop-blur-sm" onclick="closeExportModal()"></div>
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-2xl shadow-2xl w-full max-w-sm">
            <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100 bg-gray-50/50 rounded-t-2xl">
                <h3 class="text-lg font-semibold text-gray-800">导出卡密</h3>
                <button onclick="closeExportModal()" class="w-8 h-8 flex items-center justify-center rounded-full hover:bg-gray-200 text-gray-400 hover:text-gray-600 transition-colors"><i class="fas fa-times"></i></button>
            </div>
            <div class="p-5">
                <div class="mb-3">
                    <label class="block text-sm font-medium text-gray-700 mb-1">导出类型</label>
                    <select id="exportType" class="w-full px-3 py-2 bg-gray-50 border border-gray-200 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 text-sm">
                        <option value="0">全部</option>
                        <option value="1">未使用</option>
                        <option value="2">已使用</option>
                        <option value="3">当前类型_未使用</option>
                        <option value="4">当前类型_已使用</option>
                    </select>
                </div>
                <p class="text-xs text-red-600">当前类型：是只该【类型】的所有卡密</p>
            </div>
            <div class="px-5 py-3 border-t border-gray-100 flex gap-2 justify-end">
                <button id="exportYesBtn" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-colors text-sm">导出</button>
                <button onclick="closeExportModal()" class="px-4 py-2 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg font-medium transition-colors text-sm">取消</button>
            </div>
        </div>
    </div>
</div>

<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/clipboard.min.js"></script>
<script src="../assets/js/FileSaver.min.js"></script>
<script>
function showLoading() { document.getElementById('loadingOverlay').classList.remove('hidden'); }
function hideLoading() { document.getElementById('loadingOverlay').classList.add('hidden'); }
function prevPage() { if (page > 1) get_list(page - 1, pagesize); }
function nextPage() { var totalPage = parseInt($('#totalPages').text()) || 1; if (page < totalPage) get_list(page + 1, pagesize); }
</script>
<script src="./assets/js/kami.js?ver=13<?php echo VERSION ?>"></script>
