@include('common.header')

<div class="page-body">
    @include('common.meun');
    <div class="content">
    @include('common.tab')
    <!--  useful  -->
        <div class="row animated col-lg-12" style="padding-left: 10px">
            <div class="panel col-lg-12">
                <div class="panel-content">
                    <div class="text-md mt-md bg-scale-0 mb-sm" style="height: 30px;">apk列表
                        <button class="add-item btn btn-o btn-primary btn-xs pull-right" style="height: 30px;" onclick="return addFunc();"> 新增apk包
                    </div>
                    <div class="table-responsive">
                        <form action="{{ URL::action('ApkController@getApkList',[]) }}" method="GET">
                            <div style="height: 50px;">
                                <div class="col-lg-9"></div>
                                <div class="col-lg-3 mt-md mb-md">
                                    <div class="col-lg-1 pull-right"></div>
                                    <div class="col-lg-10">
                                        <input  placeholder="游戏名称" value="{{ @$NAME }}" class="form-control" name="name" id="name"/>
                                    </div>
                                    <div class="col-lg-2 pull-right">
                                        <button type="submit" class="fa fa-search" style="height: 32px;width: 32px;border-radius:5px;border: none" id="submit"/>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table id="basic-table2" class="data-table table table-striped nowrap table-hover" cellspacing="0" width="100%">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>游戏名称</th>
                                <th>版本</th>
                                <th>包地址</th>
                                <th>包大小</th>
                                <th>包原名</th>
                                <th>备注</th>
                                <th>状态</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($LISTS as $list)
                                <tr>
                                    <td>{{ $list->id }}</td>
                                    <td>{{ $list->name }}</td>
                                    <td>{{ $list->version }}</td>
                                    <td>{{ $list->local_url }}</td>
                                    <td>{{ $list->size }}MB</td>
                                    <td>{{ $list->origin_name }}</td>
                                    <td>{{ $list->remark }}</td>
                                    <td>
                                        @if($list->status == 1)
                                            在用
                                        @else
                                            禁用
                                        @endif
                                    </td>
                                    <td>{{ date('Y-m-d',strtotime($list->created_at)) }}</td>
                                    <td>
                                        @if($list->status == 1)
                                            <button type="button" class="btn btn-default" style="color: red;" onclick="return changeStatus('{{ $list->id }}',0,'{{ $list->name }}')">停用</button>
                                        @else
                                            <button type="button" class="btn btn-default" style="color: green;" onclick="return changeStatus('{{ $list->id }}',1,'{{ $list->name }}')">启用</button>
                                        @endif
                                            <button type="button" class="btn btn-default" onclick="return editFunc('{{ $list->id }}','{{ $list->name }}','{{ $list->version }}','{{ $list->remark }}')">编辑</button>
                                            <button type="button" class="btn btn-default" onclick="return deleteApk('{{ $list->id }}','{{ $list->name }}')">删除</button>

                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        @include('apk.modal')
                        @include('common.message')
                        <div class="dataTables_info" id="basic-table2_info" role="status" aria-live="polite"> 共 {{ @$LISTS->total() }} 条记录</div>
                        <div class="libaoTurnPage" style="float:right;">
                            {{ @$LISTS->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<input type="hidden" id="id" value=""/>
<input type="hidden" id="status" value="0"/>
<input type="hidden" id="_token" value="{{csrf_token()}}"/>

<script src="/html/vendor/jquery/jquery-1.12.3.min.js"></script>
<script src="/html/javascripts/Jquery.form.js"></script>
<script src="/html/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/html/vendor/nano-scroller/nano-scroller.js"></script>
<script src="/html/javascripts/template-script.min.js"></script>
<script src="/html/javascripts/template-init.min.js"></script>
<script src="/html/javascripts/examples/charts/left-sidevar.js"></script>
</body>
</html>
<script type="text/javascript">

    function editFunc(id=null,name=null,version=null,remark=null){
        $('#up_id').val(id);
        $('#up_game_name').val(name);
        $('#up_version').val(version);
        $('#up_remark').val(remark);
        $('#GMUP').modal('show');
    }

    function showRequestUp(){
        var version = $('#up_version').val();
        var name = $('#up_game_name').val();

        if(version == '' || version == null || version == undefined) return verfyField('error_info','*请填写版本号');
        if(name == '' || name == null || name == undefined) return verfyField('error_info','*请填写游戏名称');
        $('#btn_confirm').attr('disabled',true);
        $('#btn_close').attr('disabled',true);
    }

    function checkConfirmUp(){
        var options = {
            beforeSubmit: showRequestUp,
            success:      showResponse,
        }
        $('#form-apk-up').ajaxForm(options).submit();
    }

    function deleteApk(id,name){
        $('#id').val(id);
        $('#message_info').html('确认删除游戏【'+name+'】apk包吗?');
        $('#error-modal').modal('show')
    }

    function deleted(){
        var id = $('#id').val();
        var token = $("input[name='_token']").val();
        $('#delete_role').attr("disabled",true);
        $('#cancel_delete').attr("disabled",true);
        $.ajax({
            type: 'POST',
            url: '{{ URl::action('ApkController@getApkDel',[]) }}',
            data: {_token:token,id:id},
            success:function(data){
                $('#show_error_info').html(data.msg);
                $('#show_success_info').html(data.msg);

                $('#delete_role').attr("disabled",false);
                $('#cir_create_shut').attr("disabled",false);

                $('#error-modal').modal('hide')
                if(data.code == 200){
                    $('#message-success-modal').modal('show')
                }else{
                    $("#message-error-modal").modal('show');
                }
            }
        });
    }

    function checkConfirm() {
        var options = {
            beforeSubmit: showRequest,
            success:      showResponse,
        }
        $('#form-apk').ajaxForm(options).submit();
    }

    function showRequest(){
        var version = $('#version').val();
        var name = $('#game_name').val();

        if(version == '' || version == null || version == undefined) return verfyField('error_info','*请填写版本号');
        if(name == '' || name == null || name == undefined) return verfyField('error_info','*请填写游戏名称');
        $('#btn_confirm').attr('disabled',true);
        $('#btn_close').attr('disabled',true);
    }

    function showResponse(data){
        $('#show_error_info').html(data.msg);
        $('#show_success_info').html(data.msg);

        $('#btn_confirm').attr('disabled',false);
        $('#btn_close').attr('disabled',false);

        if(data.code == 200){
            $('#GM').modal('hide');
            $('#message-success-modal').modal('show');
        }else{
            $('#message-error-modal').modal('show');
        }
    }
    /**
     * 新增
     */
    function addFunc(){

        $('#game_name').val('');
        $('#version').val('');
        $('#remark').val('');
        $('#file').val('');
        $('#savedpath').val('');
        $('#result').append('');
        $('#GM').modal('show');
    }

    function changeStatus(id,status,name){
        $('#id').val(id);
        $('#status').val(status);
        var option = '确认将游戏【'+name+'】apk包停用吗?';
        if(status == 1) option = '确认将游戏【'+name+'】apk包启用吗?';
        $('#change_info').html(option);
        $('#change-status').modal('show')
    }

    function save_status(){
        var id = $('#id').val();
        var status = $('#status').val();
        var token = $('#_token').val();

        $('#change_status').attr("disabled",true);
        $('#cancel_change_status').attr("disabled",true);

        $.ajax({
            type: 'POST',
            url: '{{ URL::action('ApkController@getApkSetStatus',[]) }}',
            data: {_token:token,id:id,status:status},
            success:function(data){
                $('#show_error_info').html(data.msg);
                $('#show_success_info').html(data.msg);

                $('#change_status').attr("disabled",false);
                $('#cancel_change_status').attr("disabled",false);

                $('#change-status').modal('hide')
                if(data.code == 200){
                    $('#message-success-modal').modal('show')
                }else{
                    $("#message-error-modal").modal('show');
                }
            }
        });
    }
    $('#message-success-modal').on('hidden.bs.modal',function(){
        window.location.reload();
    });
    /**
     * 关闭弹框
     */
    function checkClose(id){
        $('#'+id).modal('hide');
    }

    function cir_cancel(id){
        $('#'+id).modal('hide')
    }
    /**
     * 验证字段
     */
    function verfyField(id,error_txt){
        $('#'+id).html(error_txt);
        $('#'+id).show();
        return false;
    }

</script>
