@extends('base.base')
@section('base')
    <!-- 内容区域 -->
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="row">
                <div class="col-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">添加数字游戏映射</h4>

                            <form class="forms-sample" id="form">
                                <div class="form-group">
                                    <label for="number">*用户ID最后一位 (0-9)</label>
                                    <select class="form-control required" name="number" id="number">
                                        <option value="">请选择数字</option>
                                        @for($i = 0; $i <= 9; $i++)
                                            <option value="{{ $i }}" {{ in_array($i, $usedNumbers) ? 'disabled' : '' }}>
                                                {{ $i }}{{ in_array($i, $usedNumbers) ? ' (已使用)' : '' }}
                                            </option>
                                        @endfor
                                    </select>
                                    <small class="form-text text-muted">选择0-9之间的数字，已使用的数字将不可选</small>
                                </div>

                                <div class="form-group">
                                    <label for="game_id">*选择游戏</label>
                                    <select class="form-control required" name="game_id" id="game_id">
                                        <option value="">请选择游戏</option>
                                        @foreach($games as $game)
                                            <option value="{{ $game->id }}" data-brand="{{ $game->brand }}" data-title="{{ $game->title }}">
                                                {{ $game->brand }}-{{ $game->title }} (ID: {{ $game->id }})
                                            </option>
                                        @endforeach
                                    </select>
                                    <small class="form-text text-muted">从游戏列表中选择要映射的游戏</small>
                                </div>

                                <button type="button" onclick="commit()" class="btn btn-sm btn-gradient-primary btn-icon-text">
                                    <i class="mdi mdi-file-check btn-icon-prepend"></i>
                                    提交
                                </button>
                                <button type="button" onclick="cancel()" class="btn btn-sm btn-gradient-warning btn-icon-text">
                                    <i class="mdi mdi-reload btn-icon-prepend"></i>
                                    取消
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        function commit() {
            if (!checkForm()) {
                return false;
            }

            var data = $("#form").serializeObject();
            var number = data.number;
            var game_id = data.game_id;

            if (!number || number === '') {
                layer.msg('请选择数字');
                return false;
            }

            if (!game_id || game_id === '') {
                layer.msg('请选择游戏');
                return false;
            }

            myRequest("/admin/game-number-mapping/add", "post", {number, game_id}, function(res) {
                if (res.code == '200') {
                    layer.msg(res.msg);
                    setTimeout(function() {
                        parent.location.reload();
                    }, 1500);
                } else {
                    layer.msg(res.msg);
                }
            });
        }

        function cancel() {
            parent.layer.closeAll();
        }
    </script>
@endsection
