<?php
$title='提现管理';
include __DIR__ . '/head.php';
?>

<div class="p-4 md:p-6">
    <!-- 提现配置 -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 mb-4 md:mb-6 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="max-w-2xl space-y-4">
                <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">
                        <select class="w-full px-3 md:px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="tixian">
                            <option value="0"<?php echo $conf['tixian']==0?' selected':''?>>关闭</option>
                            <option value="1"<?php echo $conf['tixian']==1?' selected':''?>>开启_余额</option>
                            <option value="2"<?php echo $conf['tixian']==2?' selected':''?>>开启_提成</option>
                            <option value="3"<?php echo $conf['tixian']==3?' selected':''?>>开启_余额+提成</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">提现余额比例</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-3 md:px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm" name="tixian_rate" value="<?php echo $conf['tixian_rate']?>">
                        <p class="mt-1 text-xs text-gray-500">例如：90表示扣除10%手续费</p>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-sm font-medium text-gray-700">提现最低余额</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent" name="tixian_min" value="<?php echo $conf['tixian_min']?>">
                    </div>
                </div>
                
                <div class="flex items-center gap-4 pt-4">
                    <button type="submit" class="w-full md:w-auto px-4 md:px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                        <i class="fas fa-save mr-2"></i>保存配置
                    </button>
                </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-3 md:p-6">
            <div class="overflow-x-auto">
                <table class="w-full min-w-[700px]">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">ID</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase whitespace-nowrap">用户ID</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>
                            <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="tixian-list" class="divide-y divide-gray-200">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 处理提现模态框 -->
<div class="fixed inset-0 bg-black bg-opacity-50 z-50 flex items-center justify-center hidden" id="processModal">
    <div class="bg-white rounded-lg shadow-xl max-w-md w-full mx-4">
        <div class="p-4 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800">处理提现申请</h3>
        </div>
        <div class="p-6">
            <div class="mb-4">
                <p class="text-sm text-gray-600 mb-2">用户信息：<span id="modalUser" class="font-medium"></span></p>
                <p class="text-sm text-gray-600 mb-2">提现金额：<span id="modalAmount" class="font-medium text-red-600"></span></p>
                <p class="text-sm text-gray-600 mb-2">收款方式：<span id="modalPayType" class="font-medium"></span></p>
            </div>
            <div class="mb-4 text-center">
                <p class="text-sm text-gray-600 mb-2">收款码：</p>
                <div id="modalQrcode" class="inline-block border border-gray-200 p-2 bg-white">
                    <!-- 收款码图片将在这里显示 -->
                </div>
            </div>
        </div>
        <div class="p-4 border-t border-gray-200 flex justify-end gap-3">
            <button onclick="closeModal()" class="px-4 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 text-sm">取消</button>
            <button onclick="confirmPayment()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm">确认支付</button>
        </div>
    </div>
</div>

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

<script>
var pagesize = 30;
var page = 1;
var currentTixianId = 0;

function get_tixian_list(page) {
    page = page || 1;
    
    // 显示加载状态
    showLoading();
    
    $.ajax({
        type: 'GET',
        url: 'ajax.php?act=get_tixian',
        data: {
            pagesize: pagesize,
            page: page
        },
        dataType: 'json',
        success: function(data) {
            // 隐藏加载状态
            hideLoading();
            
            if (data && data.data) {
                var html = '';
                $.each(data.data, function(index, row) {
                    var statusClass = '';
                    var statusText = '';
                    var status = parseInt(row.status);
                    switch(status) {
                        case 1:
                            statusClass = 'bg-green-100 text-green-700';
                            statusText = '已到账';
                            break;
                        case 2:
                            statusClass = 'bg-red-100 text-red-700';
                            statusText = '已退回';
                            break;
                        default:
                            statusClass = 'bg-blue-100 text-blue-700';
                            statusText = '未结算';
                            break;
                    }
                    
                    html += '<tr class="hover:bg-gray-50">';
                    html += '<td class="px-4 py-3 text-sm text-gray-500 whitespace-nowrap">' + row.id + '</td>';
                    html += '<td class="px-4 py-3 text-sm font-medium text-gray-800 whitespace-nowrap">' + row.zid + '</td>';
                    html += '<td class="px-4 py-3 text-sm whitespace-nowrap">' + row.user + '</td>';
                    html += '<td class="px-4 py-3 text-sm text-gray-600 whitespace-nowrap">' + row.money + '</td>';
                    html += '<td class="px-4 py-3 text-sm text-gray-600 whitespace-nowrap">' + row.realmoney + '</td>';
                    html += '<td class="px-4 py-3 whitespace-nowrap"><span class="px-2 py-1 text-xs ' + statusClass + ' rounded">' + statusText + '</span></td>';
                    html += '<td class="px-4 py-3 text-sm text-gray-600 whitespace-nowrap">' + row.addtime + '</td>';
                    html += '<td class="px-4 py-3 text-sm text-gray-600 whitespace-nowrap">' + (row.endtime || '-') + '</td>';
                    html += '<td class="px-4 py-3 whitespace-nowrap">';
                    html += '<div class="flex items-center gap-2 whitespace-nowrap">';
                    if (row.status != 1 && row.status != 2) {
                        html += '<button onclick="process(' + row.id + ')" class="px-3 py-1 text-xs bg-blue-600 text-white rounded hover:bg-blue-700 whitespace-nowrap">处理</button>';
                        html += '<button onclick="reject(' + row.id + ')" class="px-3 py-1 text-xs bg-red-600 text-white rounded hover:bg-red-700 whitespace-nowrap">拒绝</button>';
                    } else {
                        html += '<span class="text-xs text-gray-400 whitespace-nowrap">已处理</span>';
                    }
                    html += '</div>';
                    html += '</td>';
                    html += '</tr>';
                });
                $('#tixian-list').html(html);
            }
        },
        error: function() {
            // 隐藏加载状态
            hideLoading();
            showToast('服务器错误', 'error');
        }
    });
}

function approve(id) {
    if (confirm('确定要通过这个提现申请吗？')) {
        showLoading();
        $.ajax({
            type: 'POST',
            url: 'ajax.php?act=tixian_ok',
            data: {id: id},
            dataType: 'json',
            success: function(data) {
                hideLoading();
                showToast(data.msg, data.code == 1 ? 'success' : 'error');
                if(data.code == 1) {
                    get_tixian_list(page);
                }
            }
        });
    }
}

function process(id) {
    currentTixianId = id;
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=get_tixian_detail',
        data: {id: id},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            if(data.code == 1 && data.data) {
                var tixian = data.data;
                $('#modalUser').text(tixian.user || '');
                $('#modalAmount').text((tixian.money || '0') + ' 元');
                $('#modalPayType').text(getPayTypeName(tixian.pay_type) || '');
                
                // 显示收款码
                if(tixian.pay_img) {
                    $('#modalQrcode').html('<img src="../' + tixian.pay_img + '" alt="收款码" class="max-w-full h-auto max-h-48">');
                } else {
                    $('#modalQrcode').html('<p class="text-sm text-gray-500">暂无收款码</p>');
                }
                
                // 显示模态框
                $('#processModal').removeClass('hidden');
            } else {
                showToast(data.msg || '获取提现详情失败', 'error');
            }
        },
        error: function() {
            hideLoading();
            showToast('服务器错误', 'error');
        }
    });
}

function getPayTypeName(type) {
    switch(type) {
        case 0: case '0': case 'alipay': return '支付宝';
        case 1: case '1': case 'wechat': return '微信';
        case 2: case '2': case 'qq': return 'QQ';
        case 'bank': return '银行卡';
        default: return type;
    }
}

function closeModal() {
    $('#processModal').addClass('hidden');
    currentTixianId = 0;
}

function confirmPayment() {
    if(currentTixianId == 0) return;
    
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=tixian_ok',
        data: {id: currentTixianId},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showToast(data.msg, data.code == 1 ? 'success' : 'error');
            if(data.code == 1) {
                closeModal();
                get_tixian_list(page);
            }
        },
        error: function() {
            hideLoading();
            showToast('服务器错误', 'error');
        }
    });
}

function reject(id) {
    var text = prompt('拒绝原因：');
    if(!text) return;
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=tixian_ret',
        data: {id: id, ret: text},
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showToast(data.msg, data.code == 1 ? 'success' : 'error');
            if(data.code == 1) {
                get_tixian_list(page);
            }
        }
    });
}

function saveSetting(form) {
    showLoading();
    $.ajax({
        type: 'POST',
        url: 'ajax.php?act=gonggao',
        data: $(form).serialize(),
        dataType: 'json',
        success: function(data) {
            hideLoading();
            showToast(data.msg, 'success');
        }
    });
    return false;
}

$(document).ready(function() {
    get_tixian_list(1);
});
</script>
