@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">--}}
                            <form class="form-horizontal" 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" value="{{$admin->avatar}}">
                                    <div class="input-group col-xs-12">
                                        <input type="text" class="form-control file-upload-info" disabled=""
                                               value="{{$admin->avatar}}">
                                        <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" style="display: block;">
                                        <img src="{{$admin->avatar}}">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="nickname">*昵称</label>
                                    <input type="text" class="form-control required" name="nickname"
                                           value="{{$admin->nickname}}">
                                </div>

                                <div class="form-group">
                                    <label for="account">*账号</label>
                                    <input type="text" class="form-control required" name="account"
                                           value="{{$admin->account}}">
                                </div>

                                <div class="form-group">
                                    <label for="password">*密码</label>
                                    <input type="password" id="password" class="form-control" name="password"
                                           value="{{$admin->clear_password}}">
                                </div>

                                <div class="form-group">
                                    <label for="password">*确认密码</label>
                                    <input type="password" id="password_verify" class="form-control"
                                           name="password_verify" value="{{$admin->clear_password}}">
                                </div>

                                <div class="form-group">
                                    <label for="">*请添加彩金额度：</label>

                                    <div class="input-group col-xs-12">
                                       <span class="input-group-append">
                                            <button class="btn " type="button">{{ __('auto.当前余额：') }}{{$admin->lottery_amount}}</button>
                                        </span>
                                        <input type="text" class="form-control" name="lottery_amount" value="">

                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="">*请添加彩金单控送金额度：</label>
                                    <div class="input-group col-xs-12">
                                       <span class="input-group-append">
                                            <button class="btn " type="button">{{ __('auto.当前余额：') }}{{$admin->recharge_amount}}</button>
                                        </span>
                                        <input type="text" class="form-control" name="recharge_amount" value="">

                                    </div>
                                </div>

                                <input type="hidden" name="type" value="{{$admin->type}}">
                                <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}}"
                                                    @if(in_array($role->id, $s_role_id_arr)) selected @endif>{{$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">*渠道编号<button type="button" onclick="checkAllChannel()"class="btn btn-sm btn-gradient-primary btn-icon-text">All</button> </label>

                                    @foreach($channels as $channel)
                                        <input value="{{$channel}}" name="channel[]"  @if(in_array($channel, $admin->channel)) checked @endif id="channel" type="checkbox"><span>{{$channel}}</span>

                                    @endforeach

                                </div>
                                <div class="form-group">
                                    <label>Google Authenticator</label>
                                    <div class="input-group col-xs-12" style="margin-bottom: 10px;">
                                        <input type="text" id="ga_secret" class="form-control" name="ga_secret" value="{{ $ga['secret'] }}" readonly>
                                        <span class="input-group-append">
                                            <button class="btn btn-gradient-info" type="button" onclick="resetGaSecret({{ $admin->id }})">重置GA密钥</button>
                                        </span>
                                    </div>
                                    <div style="margin-bottom: 10px;">
                                        <label><input type="checkbox" id="ga_enabled" name="ga_enabled" value="1" @if($ga['enabled'] == 1) checked @endif> 启用GA登录校验</label>
                                    </div>
                                    <div>
                                        <img id="ga_qr_img" src="{{ $ga['qr_code_url'] }}" alt="ga-qr" style="max-width: 200px; border: 1px solid #eee; padding: 4px; @if(empty($ga['qr_code_url'])) display:none; @endif">
                                    </div>
                                </div>
                                <button type="button" onclick="commit({{$admin->id}})"
                                        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>
        let allchecked=false;
        function checkAllChannel(){
            allchecked=!allchecked
            var rolesSelector = $('input#channel')
            for (opt of rolesSelector) {
                opt.checked=allchecked;
            }
        }
        function commit(id) {
            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 rolesSelector = $('input#channel')
            for (opt of rolesSelector) {
                if (opt.checked) {
                    data.channel.push(opt.value)
                }
            }
            data.ga_enabled = $('#ga_enabled').is(':checked') ? 1 : 0
            data.ga_secret = $('#ga_secret').val()
            myRequest("/admin/administrator/update/" + id, "post", data, function (res) {
                if (res.code == '200') {
                    layer.msg(res.msg)
                    setTimeout(function () {
                        parent.location.reload();
                    }, 1500)
                } else {
                    layer.msg(res.msg)
                }
            });
        }

        function resetGaSecret(id) {
            myRequest("/admin/administrator/ga/reset/" + id, "post", {}, function (res) {
                if (res.code == '200') {
                    layer.msg(res.msg)
                    var payload = res.data || {}
                    $('#ga_secret').val(payload.secret || '')
                    if (payload.qr_code_url) {
                        $('#ga_qr_img').attr('src', payload.qr_code_url).show()
                    }
                    $('#ga_enabled').prop('checked', false)
                } else {
                    layer.msg(res.msg)
                }
            });
        }

        function cancel() {
            parent.location.reload();
        }
    </script>
@endsection
