@extends('vip.layouts')
@section('content')

    <form class="layui-form" action="{{ url()->full() }}" id="submitForm" name="submitForm" method="post"
          eventType=eventAjaxForm>
        {!! csrf_field() !!}
        <div class="layui-form-item">
            <label class="layui-form-label">房间名称</label>
            <div class="layui-input-block">
                <input type="text" name="room_name" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value="{{isset($data['room_name'])?$data['room_name']:''}}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间类型</label>
            <div>
                <div class="layui-input-inline" style="width: 20%">
                    <select name="room_type" id="room_type" lay-filter="room_type" autocomplete="off" class="layui-input" click="changeRoomType()"
                            eventType=event-query>
                        @if(isset($room_types)&&is_array($room_types))
                            @foreach($room_types as $k=>$t)
                                <option value="{{$k}}"
                                        @if(isset($data['room_type']) && $data['room_type']==$k)selected @endif>{{$t}}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">默认普通房间</div>

            </div>
        </div>
        <div class="layui-form-item" id="room_agent"  @if(isset($data['room_type'])&&$data['room_type']!=2) style="display: none" @endif>
            <label class="layui-form-label">归属代理</label>
            <div class="layui-input-inline" style="width: 20%">
                <input type="text" name="agentname" id="agentname"
                       class="layui-input"
                       value="{{isset($data['agentname'])?$data['agentname']:''}}">
            </div>
            <div class="layui-btn layui-btn-normal" onclick="addManger(1) ">验证</div>
            <div class="layui-form-mid layui-word-aux">填写用户账号</div>
        </div>
        <div class="layui-form-item" id="room_agent_users"  @if(isset($data['room_type'])&&$data['room_type']!=2) style="display: none" @endif> 
            <label class="layui-form-label">代理白名单用户</label>
            <div class="layui-input-block">
                <textarea name="agent_white_users" id="agent_white_users" class="layui-textarea"
                >{{isset($data['agent_white_users'])?$data['agent_white_users']:''}}</textarea>
            </div>

            <label class="layui-form-label">
                <p style="color: green;width: 270px;">注：多个代理白名单用户用<span style="color: red;">||</span>分割
                </p>
            </label>
        </div>

        <div class="layui-form-item" id="room_gamecode"  @if(isset($data['room_type'])&&$data['room_type']!=3) style="display: none" @endif>
            <label class="layui-form-label">游戏代码</label>
            <div class="layui-input-inline" style="width: 20%">
                <input type="text" name="gamecode" id="agentname"
                       class="layui-input"
                       value="{{isset($data['gamecode'])?$data['gamecode']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">例如：xy28；可为空</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">进入等级</label>
            <div>
                <div class="layui-input-inline" style="width: 20%">
                    <select name="limit_vip" id="fr_type" lay-verify="" autocomplete="off" class="layui-input"
                            eventType=event-query>
                        <option value="">请选择等级</option>
                        @if(isset($vip)&&is_array($vip))
                            @foreach($vip as $v)
                                <option value="{{$v['grade']}}"
                                        @if(isset($data['limit_vip'])&&$data['limit_vip']==$v['grade'])selected @endif>{{$v['name']}}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">及以上用户</div>

            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发言等级</label>
            <div>
                <div class="layui-input-inline" style="width: 20%">
                    <select name="speaker_grade" id="fr_type" lay-verify="" autocomplete="off" class="layui-input"
                            eventType=event-query>
                        <option value="">请选择等级</option>
                        @if(isset($vip)&&is_array($vip))
                            @foreach($vip as $v)
                                <option value="{{$v['grade']}}"
                                        @if(isset($data['speaker_grade'])&&$data['speaker_grade']==$v['grade'])selected @endif>{{$v['name']}}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">及以上用户</div>

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">红包等级</label>
            <div class="layui-input-inline" style="width: 20%">
                <select name="redbag_grade" id="fr_type" lay-verify="" autocomplete="off" class="layui-input"
                        eventType=event-query>
                    <option value="">请选择等级</option>
                    @if(isset($vip)&&is_array($vip))
                        @foreach($vip as $v)
                            <option value="{{$v['grade']}}"
                                    @if(isset($data['redbag_grade'])&&$data['redbag_grade']==$v['grade'])selected @endif>{{$v['name']}}</option>
                        @endforeach
                    @endif
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">及以下用户，每日最多可领取</div>
            <div class="layui-input-inline" style="width: 20%">
                <input type="number" name="redbag_limit" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value="{{isset($data['redbag_limit'])?$data['redbag_limit']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">次</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">红包限制</label>
            <div class="layui-form-mid layui-word-aux">最大金额</div>
            <div class="layui-input-inline" style="width: 20%">
                <input type="number" name="redbag_max_money" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value="{{isset($data['redbag_max_money'])?$data['redbag_max_money']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">元，最大个数</div>
            <div class="layui-input-inline" style="width: 20%">
                <input type="number" name="redbag_max_no" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value="{{isset($data['redbag_max_no'])?$data['redbag_max_no']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">个</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">自定义红包祝福语</label>
            <div class="layui-input-block">
                <input type="radio" name="edit_redbag_wish" value="1" title="开启"
                       {{(!isset($data['edit_redbag_wish']) || $data['edit_redbag_wish']===1)?'checked':''}} onclick="changRedEdit(1);">
                <input type="radio" name="edit_redbag_wish" value="0" title="关闭"  
                       {{(!isset($data['edit_redbag_wish']) || $data['edit_redbag_wish']===0)?'checked':''}} onclick="changRedEdit(0);">
            </div>
        </div>
        <div class="layui-form-item" id="room_red_wish" @if(isset($data['edit_redbag_wish']) && $data['edit_redbag_wish']==1) style="display: none" @endif>
            <label class="layui-form-label">红包祝福语列表</label>
            <div class="layui-input-block">
                <textarea name="redbag_wish_list" id="redbag_wish_list" class="layui-textarea"
                >{{isset($data['redbag_wish_list'])?$data['redbag_wish_list']:''}}</textarea>
            </div>

            <label class="layui-form-label">
                <p style="color: green;width: 270px;">注：多个红包祝福语用<span style="color: red;">||</span>分割
                </p>
            </label>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">打赏等级</label>
            <div>
                <div class="layui-input-inline" style="width: 20%">
                    <select name="gift_level" id="fr_type" lay-verify="" autocomplete="off" class="layui-input"
                            eventType=event-query>
                        <option value="">请选择等级</option>
                        @if(isset($vip)&&is_array($vip))
                            @foreach($vip as $v)
                                <option value="{{$v['grade']}}"
                                        @if(isset($data['gift_level'])&&$data['gift_level']==$v['grade'])selected @endif>{{$v['name']}}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">及以上用户</div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">打赏限制</label>
            <div class="layui-form-mid layui-word-aux">最大金额不大于30天充值总额的</div>
            <div class="layui-input-inline" style="width: 10%">
                <input type="text" name="gift_per_month" required lay-verify="required" class="layui-input"
                       value="{{isset($data['gift_per_month'])?$data['gift_per_month']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">%，单日最大可打赏</div>
            <div class="layui-input-inline" style="width: 10%">
                <input type="text" name="gift_per_day_num" required lay-verify="required" class="layui-input"
                       value="{{isset($data['gift_per_day_num'])?$data['gift_per_day_num']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">个，单次最大可打赏金额为</div>
            <div class="layui-input-inline" style="width: 10%">
                <input type="text" name="gift_max_money" required lay-verify="required" class="layui-input"
                       value="{{isset($data['gift_max_money'])?$data['gift_max_money']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">元</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否禁言</label>
            <div class="layui-input-block">
                <input type="radio" name="forbid_speak" value="1" title="开启"
                       {{(!isset($data['forbid_speak']) || $data['forbid_speak']===1)?'checked':''}}>
                <input type="radio" name="forbid_speak" value="0" title="关闭"  
                       {{(!isset($data['forbid_speak']) || $data['forbid_speak']===0)?'checked':''}}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">房间开启</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="开启"
                       {{(!isset($data['status']) || $data['status']===1)?'checked':''}}
                       >
                <input type="radio" name="status" value="0" title="关闭" 
                       {{(!isset($data['status']) || $data['status']===0)?'checked':''}}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">游客登录</label>
            <div class="layui-input-block">
                <input type="radio" name="guest_allowed" value="1"
                       title="启用" {{isset($data) && $data['guest_allowed']===1?'checked':''}} />
                <input type="radio" name="guest_allowed" value="0"
                       title="禁用" {{(!isset($data['guest_allowed']) || $data['guest_allowed']===0)?'checked':''}}>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">虚拟人数</label>
            <div class="layui-input-inline" style="width: 20%">
                <input type="number" name="false_num" required lay-verify="required" autocomplete="off"
                       class="layui-input"
                       value="{{isset($data['false_num'])?$data['false_num']:''}}">
            </div>
            <div class="layui-form-mid layui-word-aux">人</div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">优先级</label>
            <div class="layui-input-block">
                <input type="text" name="sort" class="layui-input"
                       value="{{isset($data['sort'])?$data['sort']:''}}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" class="layui-input"
                       value="{{isset($datas->title)?$datas->title:''}}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">公告内容</label>
            <div class="layui-input-block">
                <textarea name="content" id="content" class="layui-textarea"
                    >{{isset($datas->content)?$datas->content:''}}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">房间管理员</label>
            <div class="layui-input-block">
                <div class="layui-input-inline" style="width: 20%">
                    <input type="text" name="mangername" id="mangername"
                           class="layui-input"
                           value="{{isset($data['room_mangers_id'])?$data['room_mangers_id']:''}}">
                </div>
                <input type="hidden" name='room_mangers_id' id='mangersinfo'
                       value="{{isset($data['room_mangers_id'])?$data['room_mangers_id']:''}}">

                <!-- <div class="layui-form-mid layui-word-aux">填写用户账号，点击管理员删除</div>
                <div class="layui-btn layui-btn-normal" onclick="addManger() ">添加</div> -->
                <!--                <label class="layui-form-label">
                                    <p style="color: green;width: 570px;">注：添加房间管理员用<span
                                                style="color: red;">&nbsp;||&nbsp;</span>分割,<span
                                                style="color: red;">如:qwer1234||超级客服</span>
                                    </p>
                                </label>-->
            </div>


            <div class="layui-input-block mangers" style="margin-top: 5px">
                @if(isset($data['mangerinfo'])&& !empty($data['mangerinfo']))
                    @foreach($data['mangerinfo'] as $v)
                        <div class="layui-btn layui-btn-primary " id="m_{{$v}}"><span>{{$v}}</span><i
                                    class="layui-icon"></i></div>
                    @endforeach
                @endif
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">昵称列表</label>
                <div class="layui-input-block">
                    <textarea name="nickname_list" id="nickname_list" class="layui-textarea"
                    >{{isset($data['nickname_list'])?$data['nickname_list']:''}}</textarea>
                </div>

                <label class="layui-form-label">
                    <p style="color: green;width: 270px;">注：添加昵称是用<span style="color: red;">||</span>分割
                    </p>
                </label>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">禁用IP</label>
                <div class="layui-input-block">
                    <textarea name="disable_ip_list" id="disable_ip_list" class="layui-textarea"
                    >{{isset($data['disable_ip_list'])?$data['disable_ip_list']:''}}</textarea>
                </div>

                <label class="layui-form-label">
                    <p style="color: green;width: 270px;">注：多个IP用<span style="color: red;">||</span>分割
                    </p>
                </label>
            </div>

        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                @if(checkRriv('/admin/Notice/view'))
                    <button class="layui-btn" lay-submit lay-filter="submitForm">立即提交</button>@endif
                <button type="reset" class="layui-btn layui-btn-normal">重置</button>
            </div>
        </div>
    </form>
    <script type="text/javascript" src={{ asset("baidu/third-party/jquery.min.js") }}></script>
    <script type="text/javascript" charset="utf-8" src={{ asset("baidu/umeditor.config.js") }}></script>
    <script type="text/javascript" charset="utf-8" src={{ asset("baidu/umeditor.min.js") }}></script>
    <script type="text/javascript" src={{ asset("baidu/lang/zh-cn/zh-cn.js") }}></script>
    <script>
        $(document).ready(function () {
            $(".mangers ").delegate('.layui-btn', 'click', function () {
                let id = ($(this).attr('id').replace('m_', ''))
                if (delManger(id)) {
                    layer.msg('删除成功')
                    $(this).remove()
                } else {
                    layer.msg('删除失败')
                }

            })
            var room_type = $("#room_type").val();
            changeRoomType(room_type);
        });


        function addManger(m = 0) {
            let agentname = $('#agentname').val()
            console.log('m', m)
            let name = m == 0 ? $('#mangername').val() : agentname
            let mangersinfo = $('#mangersinfo').val()

            $.get('/admin/ChatRoom/checkManger?username=' + name, function (data) {
                if (data > 0) {
                    if (mangersinfo.length > 0 && (mangersinfo.split(',').indexOf(name) > -1)) {
                        m == 0 ? layer.msg('已添加') : layer.msg('验证成功');
                        return false
                    }
                    let info = mangersinfo.length > 0 ? mangersinfo + ',' + name : name;
                    console.log('name', info)
                    $('#mangersinfo').val(info)
                    layer.msg('添加成功')
                    $('.mangers').append(' <div class="layui-btn layui-btn-primary " id="m_' + name + '"><span>' + name + '</span><i class="layui-icon"></i></div>')
                } else {
                    m == 0 ? layer.msg('添加失败') : layer.msg('验证失败')
                }
            })

        }

        function changeRoomType(type) {
            if(type == 2){//代理房间
                $("#room_agent").css('display','block');
                $("#room_agent_users").css('display','block');
                $("#room_gamecode").css('display','none');
            }else if(type == 3){//游戏房间
                $("#room_agent").css('display','none');
                $("#room_agent_users").css('display','none');
                $("#room_gamecode").css('display','block');
            }else{
                $("#room_agent").css('display','none');
                $("#room_agent_users").css('display','none');
                $("#room_gamecode").css('display','none');
            }
            
        }
        function changRedEdit(type){
            if(type == 1){//开启自定义红包祝福语
                $("#room_red_wish").css('display','none');
            }else{
                $("#room_red_wish").css('display','block');
            }
        }

        function delManger(m) {
            let mangersinfo = $('#mangersinfo').val()
            let mangersarr = []
            if (mangersinfo.length > 0) {
                mangersarr = mangersinfo.split(',')
                let index = mangersarr.indexOf(m)
                if (index > -1) {
                    mangersarr.splice(index, 1);
                }
            }
            let info = mangersarr.length > 0 ? mangersarr.join(',') : ''
            $('#mangersinfo').val(info)
            return 1
        }

        //Demo
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;
            //图片上传
            layedit.set({
                uploadImage: {
                    url: '/admin/notice/upImg' //接口url
                    , type: 'post' //默认post
                }
            });
            //var um = UM.getEditor('myEditor');
            var um = UM.getEditor('myEditor', {
                initialFrameWidth: 1000,
                initialFrameHeight: 400
            });
            /*var editIndex = layedit.build('LAY_demo_editor');*/
            form.verify({
                title: function (value) {
                    if (value.length < 4) {
                        return '标题至少得4个字符啊';
                    }
                }
                , pass: [/(.+){6,12}$/, '密码必须6到12位']
                , content: function (value) {
                    layedit.sync(editIndex);
                }
            });
            form.on('select(testSelect)', function (data) {
                console.log(data.value);
                if (data.value == 20) {
                    $('.single').show();
                    // $('#labels').hide();
                } else {
                    $('.single').hide();
                    // $('#labels').show();
                }
            });
            form.on('select(room_type)', function (data) {
                changeRoomType(data.value)
            });
            form.on('select(room_type)', function (data) {
                changeRoomType(data.value)
            });


            //监听提交
            form.on('submit(submitForm)', function (data) {
                return true;
            });


        });

        //创建一个编辑器
        function cbFormSuccess(data) {
            layer.msg(data.msg);
            setTimeout(function () {
              //  location.href = document.referrer
            }, 500)
        }
    </script>
@endsection
