@extends('base.base')
@section('base')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">推广奖金审核</h3>
                    <div class="card-tools">
                        <form action="{{ route('admin.extension_new.verify') }}" method="get" class="form-inline">
                            <div class="input-group input-group-sm">
                                <input type="text" name="NickName" class="form-control" placeholder="昵称" value="{{ request('NickName') }}">
                                <input type="text" name="GameID" class="form-control" placeholder="游戏ID" value="{{ request('GameID') }}">
                                <input type="text" name="SpreaderID" class="form-control" placeholder="推广者ID" value="{{ request('SpreaderID') }}">
                                <input type="text" name="start_time" class="form-control" placeholder="开始时间" value="{{ request('start_time') }}" id="start_time">
                                <input type="text" name="end_time" class="form-control" placeholder="结束时间" value="{{ request('end_time') }}" id="end_time">
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-default">搜索</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="card-body table-responsive p-0">
                    <table class="table table-hover text-nowrap">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户昵称</th>
                                <th>游戏ID</th>
                                <th>推广者</th>
                                <th>推广者等级</th>
                                <th>注册时间</th>
                                <th>充值金额</th>
                                <th>佣金比例</th>
                                <th>佣金金额</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($list as $item)
                            <tr>
                                <td>{{ $item->id }}</td>
                                <td>{{ $item->NickName }}</td>
                                <td>{{ $item->GameID }}</td>
                                <td>{{ $item->spreader->NickName ?? '' }}</td>
                                <td>{{ $item->spreader_level }}</td>
                                <td>{{ $item->RegisterDate }}</td>
                                <td>{{ number_format($item->amount, 2) }}</td>
                                <td>{{ $item->commission_rate }}%</td>
                                <td>{{ number_format($item->commission_amount, 2) }}</td>
                                <td>
                                    @if($item->status == 0)
                                        <span class="badge badge-warning">待审核</span>
                                    @elseif($item->status == 1)
                                        <span class="badge badge-success">已通过</span>
                                    @else
                                        <span class="badge badge-danger">已拒绝</span>
                                    @endif
                                </td>
                                <td>
                                    @if($item->status == 0)
                                        <button type="button" class="btn btn-sm btn-success" onclick="verifyRecord({{ $item->id }}, 1)">通过</button>
                                        <button type="button" class="btn btn-sm btn-danger" onclick="verifyRecord({{ $item->id }}, 2)">拒绝</button>
                                    @endif
                                    <button type="button" class="btn btn-sm btn-info" onclick="showRemarks({{ $item->id }})">备注</button>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="card-footer clearfix">
                    {{ $list->appends(request()->query())->links() }}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 备注模态框 -->
<div class="modal fade" id="remarksModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">添加备注</h5>
                <button type="button" class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <textarea id="remarks" class="form-control" rows="3"></textarea>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="saveRemarks()">保存</button>
            </div>
        </div>
    </div>
</div>

@endsection

@section('scripts')
<script>
    $(function() {
        $('#start_time, #end_time').datetimepicker({
            format: 'YYYY-MM-DD HH:mm:ss'
        });
    });

    function verifyRecord(id, status) {
        if (!confirm('确定要' + (status == 1 ? '通过' : '拒绝') + '该记录吗？')) {
            return;
        }

        $.post('/admin/extension_new/verify_update/' + id, {
            status: status,
            _token: '{{ csrf_token() }}'
        }, function(res) {
            if (res.code === 0) {
                alert('操作成功');
                location.reload();
            } else {
                alert(res.msg || '操作失败');
            }
        });
    }

    var currentRecordId = 0;

    function showRemarks(id) {
        currentRecordId = id;
        $('#remarksModal').modal('show');
    }

    function saveRemarks() {
        if (!currentRecordId) return;

        $.post('/admin/extension_new/verify_remarks/' + currentRecordId, {
            remarks: $('#remarks').val(),
            _token: '{{ csrf_token() }}'
        }, function(res) {
            if (res.code === 0) {
                alert('保存成功');
                $('#remarksModal').modal('hide');
                location.reload();
            } else {
                alert(res.msg || '保存失败');
            }
        });
    }
</script>
@endsection
