{template "header.html"}
<div class="note note-danger">
    <p>升级程序之前，请务必备份全站数据</p>
</div>

<div class="right-card-box">
<form class="form-horizontal" role="form" id="myform">
    {dr_form_hidden()}
    <div class="table-scrollable">
        <table class="table table-striped table-bordered table-hover table-checkable dataTable">
            <thead>
            <tr class="heading">
                <th width="80"> 类型</th>
                <th width="250"> 程序名称</th>
                <th width="110"> 更新时间 </th>
                <th width="100"> 版本 </th>
                <th width="110" style="text-align: center"> 备份 </th>
                <th> </th>
            </tr>
            </thead>
            <tbody>
            {php $i=1;}
            {loop $list $dir $t}
            <tr class="odd gradeX">
                <td>{$t.tname}</td>
                <td>{$t.name}{if $t.type == 'app'} / {$dir}{/if}</td>
                <td> {$t.updatetime} </td>
                <td>{if !$is_oem}<a href="javascript:dr_show_log('{$t.id}', '{$t.version}');">{$t.version}</a>{else}{$t.version}{/if}</td>
                <td align="center">
					{if $t.backup}
                    <a href="javascript:dr_tips(1, '备份目录：{$t.backup}', -1);" class="label label-success"> 已备份 </a>
                    {else}
                    <span class="label label-danger"> 未备份 </span>
                    {/if}
                </td>
                <td>
                    <label style="display: none" id="dr_update_{$dir}">
					<button type="button" onclick="dr_update_cms('{dr_url('cloud/todo_update', ['id'=>$t.id, 'dir'=>$dir])}', '{dr_lang('升级前请做好系统备份，你确定要升级吗？')}', 1)" class="btn red btn-xs"> <i class="fa fa-cloud-upload"></i> {dr_lang('在线升级')}</button>
					{if $dir == 'phpcmf' && !$is_oem}
					<a href="https://www.xunruicms.com/member.php?action=down&cid={$cmf_version.id}&is_update=v{$t.version}" target="_blank" class="btn green btn-xs"> <i class="fa fa-cloud-download"></i> {dr_lang('离线下载')}</a>
					{/if}
					</label>
                    <label class="dr_check_version" id="dr_row_{$dir}"></label>
                </td>
            </tr>
            {php $i++;}
            {/loop}
            </tbody>
        </table>
    </div>
    <div class="row fc-list-footer table-checkable ">
        <div class="col-md-12">
            {if dr_is_app('beifen')}
            <label><button type="button" onclick="dr_beifen_cms('{dr_url('beifen/home/add')}', '{dr_lang('你确定要备份全站文件吗？')}', 1)" class="btn green btn-sm"> <i class="fa fa-copy"></i> {dr_lang('备份全站')}</button></label>
            {/if}
            <label><a href="javascript:dr_help('610');" class="btn blue btn-sm"> <i class="fa fa-book"></i> {dr_lang('离线升级程序')}</a></label>
        </div>
    </div>

</form>
</div>
<script type="text/javascript">

    $(function() {
        {loop $list $dir $t}
        {if $t.id}
        $("#dr_row_{$dir}").html("<img style='height:17px' src='{THEME_PATH}assets/images/loading-0.gif'>");
        $("#dr_update_{$dir}").hide();
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "{dr_url('cloud/check_version', ['id'=>$t.id, 'version' => $t.version])}",
            success: function(json) {
                if (json.code) {
                    $("#dr_row_{$dir}").html(json.msg);
                    $("#dr_update_{$dir}").show();
                } else {
                    $("#dr_row_{$dir}").html("<font color='red'>"+json.msg+"</font>");
                }
            },
            error: function(HttpRequest, ajaxOptions, thrownError) {
                $("#dr_row_{$dir}").html("<font color='red'>网络异常，请稍后再试</font>");
            }
        });
        {/if}
        {/loop}
    });

    // ajax 批量操作确认
    function dr_update_cms(url, msg, remove) {
        layer.confirm(
                msg,
                {
                    icon: 3,
                    shade: 0,
                    title: lang['ts'],
                    btn: [lang['ok'], lang['esc']]
                }, function(index){
                    layer.close(index);


                    var login_url = '{dr_url("cloud/login")}';
                    layer.open({
                        type: 2,
                        title: '登录官方云账号',
                        fix:true,
                        scrollbar: false,
                        shadeClose: true,
                        shade: 0,
                        area: ['500px', '50%'],
                        btn: [lang['ok'], lang['esc']],
                        yes: function(index, layero){
                            var body = layer.getChildFrame('body', index);
                            $(body).find('.form-group').removeClass('has-error');
                            // 延迟加载
                            var loading = layer.load(2, {
                                shade: [0.3,'#fff'], //0.1透明度的白色背景
                                time: 100000000
                            });
                            $.ajax({type: "POST",dataType:"json", url: login_url, data: $(body).find('#myform').serialize(),
                                success: function(json) {
                                    layer.close(loading);
                                    if (json.code == 1) {
                                        layer.close(index);
                                        var yz_url = url+'&'+$('#myform').serialize()+'&ls='+json.data;
                                        // 验证成功
                                        layer.open({
                                            type: 2,
                                            title: '升级程序',
                                            scrollbar: false,
                                            resize: true,
                                            maxmin: true, //开启最大化最小化按钮
                                            shade: 0,
                                            area: ['80%', '80%'],
                                            success: function(layero, index){
                                                // 主要用于后台权限验证
                                                var body = layer.getChildFrame('body', index);
                                                var json = $(body).html();
                                                if (json.indexOf('"code":0') > 0 && json.length < 150){
                                                    var obj = JSON.parse(json);
                                                    layer.closeAll(index);
                                                    dr_tips(0, obj.msg);
                                                }
                                            },
                                            content: yz_url
                                        });
                                    } else {
                                        $(body).find('#dr_row_'+json.data.field).addClass('has-error');
                                        dr_tips(0, json.msg);
                                    }
                                    return false;
                                },
                                error: function(HttpRequest, ajaxOptions, thrownError) {
                                    dr_ajax_alert_error(HttpRequest, ajaxOptions, thrownError)
                                }
                            });
                            return false;
                        },
                        content: login_url+'&is_ajax=1'
                    });



                });
    }

    function dr_beifen_cms(url, msg, remove) {
        layer.confirm(
                msg,
                {
                    icon: 3,
                    shade: 0,
                    title: lang['ts'],
                    btn: [lang['ok'], lang['esc']]
                }, function(index){
                    layer.close(index);
                    layer.open({
                        type: 2,
                        title: '备份程序',
                        scrollbar: false,
                        resize: true,
                        maxmin: true, //开启最大化最小化按钮
                        shade: 0,
                        area: ['80%', '80%'],
                        success: function(layero, index){
                            // 主要用于后台权限验证
                            var body = layer.getChildFrame('body', index);
                            var json = $(body).html();
                            if (json.indexOf('"code":0') > 0 && json.length < 150){
                                var obj = JSON.parse(json);
                                layer.closeAll(index);
                                dr_tips(0, obj.msg);
                            }
                        },
                        content: url
                    });
                });
    }
    
    function dr_show_log(id, v) {
        layer.open({
            type: 2,
            title: '版本日志',
            scrollbar: false,
            resize: true,
            maxmin: true, //开启最大化最小化按钮
            shade: 0,
            area: ['80%', '80%'],
            content: '{dr_url("cloud/log_show")}&id='+id+'&version='+v,
            //content: 'http://www.phpcmf.net/version.php?id='+id+'&version='+v,
        });
    }

</script>


{template "footer.html"}