<?php
$title='接口分类';
include __DIR__ . '/head.php';

$biz = isset($_GET['biz']) ? daddslashes($_GET['biz']) : 'all';
if(!in_array($biz, ['all','cloud','dg'], true)) $biz = 'all';

$is=$DB->getColumn("select count(*) from information_schema.columns where table_name = 'pre_loginconfig' and column_name = 'cid'");
if($is==0){
	$DB->exec("ALTER TABLE `pre_loginconfig` ADD COLUMN `cid` varchar(255) DEFAULT NULL COMMENT '协议分类' AFTER `id`");
	$DB->exec("ALTER TABLE `pre_loginconfig` ADD COLUMN `cids` int(11) NOT NULL DEFAULT 0 COMMENT '分类id' AFTER `cid`");
}
?>


<div class="p-4 md:p-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">
            <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-3">
                <div>
                    <h2 class="text-base md:text-lg font-semibold text-gray-800">分类管理</h2>
                    <p class="text-xs md:text-sm text-gray-500 mt-1">按业务（云端/代挂）分别管理接口分类与体验天数</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <select class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent" onchange="location.href='login_class.php?biz='+this.value">
                        <option value="all" <?php echo $biz === 'all' ? 'selected' : '' ?>>全部</option>
                        <option value="cloud" <?php echo $biz === 'cloud' ? 'selected' : '' ?>>云端</option>
                        <option value="dg" <?php echo $biz === 'dg' ? 'selected' : '' ?>>代挂</option>
                    </select>
                    <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm inline-flex items-center" data-bs-toggle="modal" data-bs-target="#tmp_new" style="<?php echo $conf['is_new_time']==''?'':' display: none;'?>">
                    <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm inline-flex items-center" data-bs-toggle="modal" data-bs-target="#tmp_new" style="<?php echo ($conf['is_new_time']=='' && $biz !== 'all')?'':' display: none;'?>">
                    </button>
                </div>
            </div>
        </div>
        <div class="p-4 md:p-6">
            <input type="hidden" id="isgetdata" value="1">
            <input type="hidden" id="biz" value="<?php echo htmlspecialchars($biz)?>">
            <div class="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg">
            <div class="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg">
                <p class="text-sm text-red-600">
                    <i class="fas fa-info-circle mr-2"></i>体验天数：填写0则是收费，其他值则是免费体验的天数。每个分类都能单独设置体验天数<br>
                    想长期免费？骚操作如下：1、在【接口列表】把【接口密码】设置成0。然后把这个接口绑定到某个分类下即可
                </p>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full min-w-[600px]">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap min-w-[90px]">排序</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">版本</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">名称</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">体验天数</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">状态</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">操作</th>
                        </tr>
                    </thead>
                    <tbody id="list" class="divide-y divide-gray-200">
                    </tbody>
                </table>
            </div>
            <div class="p-3 md:p-4 border-t border-gray-200 flex flex-col md:flex-row md:items-center md:justify-between gap-3">
                <span class="text-xs md:text-sm text-gray-600">共 <span id="total" class="font-semibold">0</span> 条分类</span>
                <div class="flex items-center gap-2">
                    <span class="text-xs md:text-sm text-gray-600">第</span>
                    <select id="page" class="px-3 py-1.5 border border-gray-300 rounded-lg text-xs md:text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="1">1</option>
                    </select>
                    <span class="text-xs md:text-sm text-gray-600">页</span>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 排序操作弹窗 -->
<div class="modal fade" id="sort_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" id="sort_content">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="confirm_sort()">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 删除分类确认弹窗 -->
<div class="modal fade" id="del_class_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 450px;">
        <div class="modal-content">
            <div class="modal-header bg-red-50">
                <h5 class="modal-title text-red-600"><i class="fas fa-exclamation-triangle mr-2"></i>删除确认</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-4">
                <p id="del_class_message" class="text-gray-700"></p>
                <p class="text-red-600 text-sm mt-2"><i class="fas fa-info-circle mr-1"></i>确定后将删除该分类，并清除这些QQ的该分类到期时间（其他分类不受影响）</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white font-medium rounded-lg transition text-sm" onclick="confirm_del_class()">确定删除</button>
                <button type="button" class="px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition text-sm" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!-- 版本转移弹窗 -->
<div class="modal fade" id="biz_transfer_modal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered" style="max-width: 520px;">
        <div class="modal-content">
            <div class="modal-header bg-blue-50">
                <h5 class="modal-title text-blue-700"><i class="fas fa-exchange-alt mr-2"></i>版本转移</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-4 space-y-3">
                <input type="hidden" id="biz_transfer_src_biz" value="">
                <input type="hidden" id="biz_transfer_src_cid" value="">
                <div class="text-sm text-gray-700">
                    <div class="mb-1">当前分类：<span id="biz_transfer_src_text" class="font-semibold text-gray-900"></span></div>
                    <div class="text-xs text-gray-500">将该分类在对应版本下的接口/商品/QQ时长转移到目标版本（分类ID不变）</div>
                </div>
                <div class="input-group">
                    <span class="input-group-text">目标版本</span>
                    <select class="form-select" id="biz_transfer_dst_biz">
                        <option value="cloud">云端</option>
                        <option value="dg">代挂</option>
                    </select>
                </div>
                <p class="text-xs text-red-600"><i class="fas fa-info-circle mr-1"></i>转移只处理所选分类：接口节点/商品会转到目标版本；QQ时长仅转移该分类到期时间（不影响其他分类时长）</p>
            </div>
            <div class="modal-footer">
                <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="confirmBizTransfer()">开始转移</button>
                <button type="button" class="px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition text-sm" data-bs-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="tmp_new" 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">

                <div class="input-group mb-3">
                    <span class="input-group-text">原有分类</span>
                    <select class="form-select" id="tmp_apiid">
                        <option value="0">默认</option> 
                        <option value="1">独享</option>
                        <option value="2">独享+默认</option>                          
                    </select>
                </div>
                <h2>转</h2>
                <div class="input-group mb-3">
                    <span class="input-group-text">接口分类</span>
                    <select class="form-select" id="tmp_cids">
                    <?php
                        // 只展示可见分类，避免隐藏分类继续出现在转移下拉
                        if($biz === 'all'){
                            $rs=$DB->query("SELECT * FROM `pre_login_class` WHERE active='0' AND (biz='cloud' OR biz='dg') ORDER BY sort ASC");
                        }else{
                            $rs=$DB->query("SELECT * FROM `pre_login_class` WHERE active='0' AND biz='{$biz}' ORDER BY sort ASC");
                        }
                        while($res = $rs->fetch()){
                            echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
                        }
                    ?>
                    </select>
                </div>
                <font color="red">请在【接口管理-接口分类】里设置好分类后再操作</font>
                <div class="d-grid gap-2 mt-3 mb-3">
                   <button type="submit" class="btn btn-outline-primary" onclick="time_change();">转移时长</button>
                </div>

                <p><font color="red">
                    示：代挂时长计费方式改为 接口分类 模式<br><br>
                    接口分类 可以关联多个 框架接口。用户购买后 可以登录 该分类下的所有框架接口
                </font></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>


        <script src="../assets/js/jquery.cookie.min.js"></script>
        <script src="./assets/js/login_class.js?ver=a21<?php echo VERSION ?>"></script>
<?php include 'foot.php'; ?>
<script>
	function time_change() {
	    var apiid = $('#tmp_apiid').val();
	    var cids = $('#tmp_cids').val();
	    showLoading();
	    $.ajax({
	        type : 'POST',
	        url : 'ajax.php?act=time_change',
	        data : {apiid:apiid,cids:cids},
	        dataType : 'json',
	        success : function(data) {
	        	hideLoading();
	        	showToast(data.msg, 'success');
	        },
	        error:function(data){
	        	hideLoading();
	            showToast('服务器错误', 'error');
	        }
	    });
	}
</script>
