<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta name="renderer" content="webkit">
    <meta name="renderer" content="ie-comp">
    <meta name="renderer" content="ie-stand">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layui在线调试</title>
    <link rel="stylesheet" href="{{ asset('frame/layui/css/layui.css') }}" media="all">
    <style>
        body {
            margin: 10px;
        }

        .demo {
            text-align: center;
            line-height: 40px;
        }

        .demo p {
            border: 1px solid #000000;
            padding: 5px 5px;
        }

        .demo input {
            width: 90%;
            height: 30px;
        }

        .tit {
            font-size: 18px;
            /*font-weight: bold;*/
        }

        .odds p {
            font-size: 0.6rem;
        }

        .menu {
            position: fixed;
            left: 0px;
            top: 20px;
            height: 500px;
            overflow: auto;
        }

        .top {
            height: 40px;
            line-height: 40px;
            padding: 0px 5px;
            font-size: 22px;
            position: fixed;
            right: 0px;
            top: 50%;
            background: #e2e2e2;
        }

        .game {
            text-align: center;
            margin-bottom: 15px;
        }

        .oneKey .layui-form-item .layui-input-inline {
            width: 100px;
        }

        .menu {
            width: 140px;
        }

        .text {
            width: 88%;
        }
    </style>
</head>
<body>


<div class="layui-row">
    <div class="layui-col-sm1 layui-col-md1 menu">
        <!-- tree -->
        <ul id="tree" class="tree-table-tree-box"></ul>
    </div>
    <div class="layui-col-sm11 layui-col-md11 text" style="float: right;">
        <h1 class="game">{{$data['name']}}</h1>
        <input type="hidden" name="game" id="game" value="{{$data['game']}}">
        <div class="oneKey"></div>
        <div class="demo"></div>
        <p style="margin-top: 20px">
            <button class="layui-btn layui-btn-normal save" gamename="lol">保存</button>
        </p>
    </div>

    <a href="javaScript:;" class="top">TOP</a>
</div>
<script type="text/javascript" src="/frame/layui/layui.js"></script>
<script src="<?php echo e(URL::asset('admin/js/jquery-1.11.3.js')); ?>" charset="utf-8"></script>
<script>
    layui.config({
        version: '1511795639295' //为了更新 js 缓存，可忽略
    });

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    // 返回顶部
    $(document).on('click', '.top', function () {
        $('body,html').animate({scrollTop: '0px'}, 'show');
    });

    function odds(data) {
        $.ajax({
            type: 'get',
            url: '/admin/egame/getSettings',
            data: data,
            dataType: 'json',
            success: function (json) {


                var str = '';
                var html = '';
                for (var i in json.data) {
                    if (i === 'parent') {
                        var Onekey = json.data[i];


                        html += '<div class=" ' + i + '" gametype="' + i + '">'
                        for (var t = 0; t < Onekey.length; t++) {
                            html += '<div class="layui-form-item odds" name="' + Onekey[t].name + '">';


                            var allType = Onekey[t].settings;

                            html += '<label class="layui-form-label">' + json.data.lang[Onekey[t].name] + '</label>';
                            for (var r in allType) {

                                if (r === 'multiple') {
                                    html += '<div class="layui-inline ' + r + '" settings=' + r + '>';
                                    for (var d in allType[r]) {
                                        html += '<label class="layui-form-label">' + json.data.lang[d] + '</label>';
                                        html += '<div class="layui-input-inline">';
                                        html += '<input type="text" name="' + d + '" value="' + allType[r][d] + '" class="layui-input">';
                                        html += '</div>';
                                    }
                                    html += '</div>';
                                } else {
                                    html += '<div class="layui-inline ' + r + '" settings=' + r + '>';
                                    html += '<label class="layui-form-label">' + json.data.lang[r] + '</label>';
                                    html += '<div class="layui-input-inline">';
                                    html += '<input type="text" name="' + r + '" value="' + allType[r] + '" class="layui-input">';
                                    html += '</div>';
                                    html += '</div>';
                                }
                            }
                            html += '<div class="layui-inline">';
                            html += '<button class="layui-btn  voluation" gameDataType=' + Onekey[t].name + '>一键修改</button>';
                            html += ' </div>';
                            html += ' </div>';
                        }
                        html += '</div>';

                    } else if (i === 'lang') {

                    } else {
                        var data = json.data[i];
                        str += '<fieldset class="layui-elem-field layui-field-title " style="margin-top: 30px;">';
                        str += '<legend>' + json.data.lang[i] + '</legend>';
                        str += '</fieldset>';

                        str += '<ul class="layui-row layui-col-space5 ' + i + '" gametype="' + i + '">';


                        for (var n = 0; n < data.length; n++) {

                            var current = data[n];

                            str += '<li class="layui-col-md2">';
                            str += '<p class="tit layui-bg-gray">' + json.data.lang[current.name] + '</p>';
                            str += '<div class="odds ' + current.name + '" name="' + current.name + '">';
                            for (var x in current.settings) {
                                var judge = current.settings;
                                if (x == 'cost') {
                                    str += '<div class="layui-row ' + x + '" settings=' + x + '>';
                                    for (var h = 0; h < judge.cost.length; h++) {
                                        var section = judge.cost[h];
                                        str += '<p class="layui-col-xs6">' + section.max + '-' + section.min + '</p>';
                                        str += '<p class="layui-col-xs6">';
                                        str += '<input min="' + section.min + '" max="' + section.max + '" name="' + current.name + '"  value="' + section.multiple + '"/>';
                                        str += '</p>';
                                    }
                                    str += '</div>';
                                } else if (x == 'multiple') {
                                    str += '<div class="layui-row ' + x + '" settings=' + x + '>';
                                    for (var l in judge.multiple) {
                                        str += '<p class="layui-col-xs6">' + json.data.lang[l] + '</p>';
                                        str += '<p class="layui-col-xs6">';
                                        str += '<input name="' + l + '"  value="' + judge.multiple[l] + '"/>'
                                        str += '</p>';
                                    }
                                    str += '</div>';
                                } else {
                                    str += '<div class="layui-row ' + x + '"settings=' + x + '>';
                                    str += '<p class="layui-col-xs6">' + json.data.lang[x] + '</p>';
                                    str += '<p class="layui-col-xs6">';
                                    str += '<input name="' + x + '" value="' + judge[x] + '"/>';
                                    str += '</p>';
                                    str += '</div>';
                                }
                            }

                            str += '</div>';

                            str += '</li>';
                        }

                        str += '</ul>';
                    }

                }

                $('.demo').html(str);
                $('.oneKey').html(html);

            },
            error: function () {
            }
        });
    }
    var game=$('#game').val();
    var  obj={game: 'lol'};
    if(game==''){
        obj={game: 'lol'};
    }else{
        var str='{"game": "'+game+'"}';
        obj=JSON.parse(str);
    }
    console.log(obj);
    odds(obj);

    layui.use(['tree', 'layer'], function () {
//        var laydate = layui.laydate //日期
//            , laypage = layui.laypage //分页
        var layer = layui.layer //弹层
//            , table = layui.table //表格

        var menu = [];
        $.ajax({
            type: 'get',
            url: '/admin/egame/GameList',
            data: {},
            dataType: 'json',
            async: false,
            success: function (json) {
                menu = json;
            }
        });

        layui.tree({
            elem: '#tree' //传入元素选择器
            , click: function (item) { //点击节点回调
                //layer.msg('当前节名称：' + item.id);

                // 加载中...
                var loadIndex = layer.load(2, {shade: true});

                odds({game: item.gameCode, id: item.id});
                $('.game').text(item.name);
                $('.save').attr('gamename', item.gameCode)
                // 关闭加载
                layer.close(loadIndex);


            }
            , nodes: menu.data
        });

    });


    $(function () {
        var height = document.documentElement.clientHeight;
        $('.menu').css({height: (height - 30)});
        //一键修改
        $(document).on('click', '.voluation', function () {

            layer.prompt({
                title: '二级密码',
                formType: 1,
            },function(value, index, elem){
                //alert(value); //得到value

                var obj = {};
                var n = $("*[gametype]");
                var game = $('.save').attr('gamename');
                $(n).each(function () {
                    var gameType = $(this).attr('gametype');
                    obj[gameType] = []

                    var oneData = $(this).find('.odds');

                    $(oneData).each(function () {
                        var data = {};
                        var name = $(this).attr('name');
                        data.name = name;

                        var backs = $(this).find('.back').find('input[name=back]').val();
                        data.settings = {};

                        var n = {}
                        if (backs !== undefined) {
                            n.back = backs;
                        }
                        //                n.multiple = {}
                        var c = {}
                        $(this).find('.multiple').find('input').each(function () {
                            var na = $(this).attr('name');
                            var val = $(this).val();
                            c[na] = val;
                        });

                        var cost = [];
                        $(this).find('.cost').find('input').each(function () {
                            var min = $(this).attr('min');
                            var max = $(this).attr('max');
                            var val = $(this).val();

                            var f = {
                                min: min,
                                max: max,
                                multiple: val
                            }
                            cost.push(f)
                        });
                        n.multiple = c;
                        data.settings = n;

                        if (cost.length !== 0) {
                            data.settings['cost'] = cost;
                        } else {

                        }

                        obj[gameType].push(data)
                    });

                });

                $.ajax({
                    type: 'POST',
                    url: '/admin/egame/setSettings?game=' + game,
                    data: {password:value,data: obj},
                    dataType: 'json',
                    success: function (json) {
                        if (json.status === 1) {
                            layer.open({
                                title: '系统提示',
                                type: 0,
                                id: 'layerDemo',
                                btnAlign: 'c',//按钮居中
                                content: '<div style="padding: 20px 100px;font-size: 22px;">' + json.msg + '</div>'
                            });
                        } else {
                            layer.msg(json.msg)
                        }
                    }
                });


                layer.close(index);
            });

        })

        $(document).on('click', '.save', function () {

            layer.prompt({
                title: '二级密码',
                formType: 1,
            },function(value, index, elem){
                //alert(value); //得到value

                var obj = {};
                var n = $("*[gametype]");
                var game = $('.save').attr('gamename');
                $(n).each(function () {
                    var gameType = $(this).attr('gametype');
                    obj[gameType] = []

                    var oneData = $(this).find('.odds');

                    $(oneData).each(function () {
                        var data = {};
                        var name = $(this).attr('name');
                        data.name = name;

                        var backs = $(this).find('.back').find('input[name=back]').val();
                        data.settings = {};

                        var n = {}
                        if (backs !== undefined) {
                            n.back = backs;
                        }
                        //                n.multiple = {}
                        var c = {}
                        $(this).find('.multiple').find('input').each(function () {
                            var na = $(this).attr('name');
                            var val = $(this).val();
                            c[na] = val;
                        });

                        var cost = [];
                        $(this).find('.cost').find('input').each(function () {
                            var min = $(this).attr('min');
                            var max = $(this).attr('max');
                            var val = $(this).val();

                            var f = {
                                min: min,
                                max: max,
                                multiple: val
                            }
                            cost.push(f)
                        });
                        n.multiple = c;
                        data.settings = n;

                        if (cost.length !== 0) {
                            data.settings['cost'] = cost;
                        } else {

                        }

                        obj[gameType].push(data)
                    });

                });

                $.ajax({
                    type: 'POST',
                    url: '/admin/egame/setSettingsOne?game=' + game,
                    data: {password:value,data: obj},
                    dataType: 'json',
                    success: function (json) {
                        if (json.status === 1) {
                            layer.open({
                                title: '系统提示',
                                type: 0,
                                id: 'layerDemo',
                                btnAlign: 'c',//按钮居中
                                content: '<div style="padding: 20px 100px;font-size: 22px;">' + json.msg + '</div>'
                            });
                        } else {
                            layer.msg(json.msg)
                        }
                    }
                });


                layer.close(index);
            });

        })

    });
</script>
</body>
</html>
