@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">{{ __('auto.请填写管理员信息') }}</h4>

                            <form class="forms-sample" id="form">
                                <div class="form-group">
                                    <label>{{ __('auto.头像上传') }}</label>
                                    <input type="file" class="file-upload-default img-file" data-path="avatar">
                                    <input type="hidden" name="avatar" class="image-path">
                                    <div class="input-group col-xs-12">
                                        <input type="text" class="form-control file-upload-info" disabled="">
                                        <span class="input-group-append">
                                            <button class="file-upload-browse btn btn-gradient-primary" onclick="upload($(this))" type="button">{{ __('auto.上传') }}</button>
                                        </span>
                                    </div>
                                    <div class="img-yl">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="nickname">*昵称</label>
                                    <input type="text"  class="form-control required" name="nickname" placeholder="{{ __('auto.请输入管理员昵称') }}">
                                </div>

                                <div class="form-group">
                                    <label for="account">*账号</label>
                                    <input type="text"  class="form-control required" name="account" placeholder="{{ __('auto.请输入账号') }}">
                                </div>

                                <div class="form-group">
                                    <label for="password">*密码</label>
                                    <input type="password" id="password"  class="form-control required" name="password" placeholder="{{ __('auto.请输入密码') }}">
                                </div>

                                <div class="form-group">
                                    <label for="password">*确认密码</label>
                                    <input type="password" id="password_verify"  class="form-control required" name="password_verify" placeholder="{{ __('auto.请再次输入密码') }}">
                                </div>

                                <div class="form-group">
                                    <label for="type">*账号类型</label>
                                    <select class="form-control form-control-lg" name="type">
                                        @foreach($types as $key=>$type)
                                            <option value="{{$key}}">{{$type}}</option>
                                        @endforeach
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="role">*角色</label>

                                    <select id="roles-selector" class="form-control form-control-lg" multiple="multiple">
                                        @foreach($roles as $role)
                                            <option value="{{$role->id}}">{{$role->name}}</option>
                                        @endforeach
                                    </select>

                                </div>
                                <div class="form-group">
                                    <label for="locale">*语言</label>
                                    <select id="locale" name="locale" class="form-control form-control-lg"  >
                                        <option value="zh_CN" selected>{{ __('auto.中文') }}</option>
                                        <option value="en_US" >English</option>
                                    </select>

                                </div>
                                <div class="form-group">
                                    <label for="channel">*渠道编号</label>

                                    @foreach($channels as $channel)
                                        <label for="channel" style="display: block"><input value="{{$channel}}" id="channel" name="channel"  class="form-control" type="checkbox">{{$channel}}</label>
                                    @endforeach

                                </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>
                                    {{ __('auto.提交') }}
                                </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>
                                    {{ __('auto.取消') }}
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>


        function commit(){
            if($("#password").val() != $("#password_verify").val()){
                layer.msg('{{ __('auto.两次密码输入不一致') }}', function(){});
            }
            if(!checkForm()){
                return false;
            }
            var data = $("#form").serializeObject();
            data.roles = []
            var rolesSelector = document.querySelector('select#roles-selector')
            for(opt of rolesSelector) {
                if(opt.selected) {
                    data.roles.push(opt.value)
                }
            }
            data.channel = []
            var channelChecks = $('input#channel')
            for (opt of channelChecks) {
                if (opt.checked) {
                    data.channel.push(opt.value)
                }
            }
            myRequest("/admin/administrator/add","post",data,function(res){
                if(res.code == '200'){
                    layer.msg(res.msg)
                    setTimeout(function(){
                        parent.location.reload();
                    },1500)
                }else{
                    layer.msg(res.msg)
                }
            });
        }
        function cancel() {
            parent.location.reload();
        }
    </script>
@endsection
