{extend name="main"}
{block name="body"}
<div class="bg-white p-3">
	<div class="p-3 bg-white mb-3 hover-box-shadow rounded-lg">
		<div class="flex flex-center">
			<div class="table-title text-secondary flex-1"></div>
			<el-link type="primary" :underline='false' :loading="true" @click="updateSystemVersion()">
			<el-badge is-dot :hidden="!SystemInfo.update">{{SystemInfo.update?'立即更新':'检查更新'}}</el-badge>
			</el-link>
		</div>
		<table class="table table-borderless table-sm">
			<tr>
				<td width="100" class="text-right text-secondary small">当前版本</td>
				<td class="text-primary small">{{SystemInfo.version_name}}</td>
			</tr>
			<tr v-if="SystemInfo.update">
				<td width="100" class="text-right text-secondary small">新版本</td>
				<td class="text-primary small">{{SystemInfo.new_version_name}}
				<a :href="SystemInfo.website+'/packages/version/'+SystemInfo.new_version_code" target="_blank" class="ml-2">查看</a>
				</td>
			</tr>
			<tr>
				<td class="text-right text-secondary small">授权版本</td>
				<td class="text-primary small">{{SystemInfo.authorization_type}}</td>
			</tr>
			<tr v-if="SystemInfo.new_authorization_type">
				<td class="text-right text-secondary small">新授权版本</td>
				<td class="text-primary small">{{SystemInfo.new_authorization_type}}</td>
			</tr>
			<tr>
				<td class="text-right text-secondary small">授权对象</td>
				<td class="text-primary small">{{SystemInfo.authorization_object}}</td>
			</tr>
			<tr>
				<td class="text-right text-secondary small">授权IP</td>
				<td class="text-primary small">{{SystemInfo.authorization_ip}}</td>
			</tr>
			<tr>
				<td class="text-right text-secondary small">上次检查时间</td>
				<td class="text-secondary small">{{SystemInfo.time}}</td>
			</tr>
		</table>
	</div>
	<div class="table-responsive">
		<el-table
	      	:data="tableData"
    		stripe
    		v-loading="search.ajax"
    		style="width: 100%;">
	      <el-table-column
	        prop="filename"
	        label="版本"
	        width="120">
	      </el-table-column>
		  <el-table-column type="expand" width="20">
			  <template slot-scope="scope">
			  	<div v-html="conGetHtml(scope.row.log)"></div>
			  </template>
		  </el-table-column>
	      <el-table-column
	        prop="path"
	        label="路径">
	      </el-table-column>
	      <el-table-column
	        label="备份"
	        width="80">
		      <template slot-scope="scope">
		        <el-button type="success"size="minism" v-if="scope.row.backup"> 有 </el-button>
		        <el-button type="info"size="minism" v-else> 无 </el-button>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="安装包"
	        width="80">
		      <template slot-scope="scope">
		        <el-button type="success"size="minism" v-if="scope.row.step"> 有 </el-button>
		        <el-button type="info"size="minism" v-else> 无 </el-button>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="时间线"
	        width="180">
		      <template slot-scope="scope">
		        <el-popover trigger="hover" placement="top">
		          <p>创建时间: {{ scope.row.create_time }}</p>
		          <p>更新时间: {{ scope.row.update_time }}</p>
		          <div slot="reference" class="name-wrapper">
		            <el-tag size="small" type="info">{{ scope.row.create_time }}</el-tag>
		          </div>
		        </el-popover>
		      </template>
	      </el-table-column>
	      <el-table-column
	        label="/"
	        width="180">
		      <template slot-scope="scope">
		        <el-button
		            type="primary"
		            size="minism"
		            @click="rollBackup(scope.row);"
		            v-if="scope.row.backup&&SystemInfo.version_code===scope.row.filename">
		            回滚
		        </el-button>
		        <el-button
		            type="success"
		            size="minism"
		            @click="installPackage(scope.row);"
		            v-if="scope.row.step&&SystemInfo.version_code<scope.row.filename">
		            安装
		        </el-button>
		        <el-button
		            type="info"
		            size="minism"
		            v-if="SystemInfo.version_code===scope.row.filename">
		            已安装
		        </el-button>
		        <el-button
		            type="danger"
		            size="minism"
		            @click="delInstallPackages(scope.row)">
		            删除
		        </el-button>
		      </template>
	      </el-table-column>
	    </el-table>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
	{empty name="SystemInfo"}
	var SystemInfo={};
	{else/}
	var SystemInfo={$SystemInfo|raw};
	{/empty}
</script>
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			SystemInfo:SystemInfo,
			search:{
				ajax:false
			},
			tableData:[],
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
			this.initHtml();
		},
	    methods: {
	    	conGetHtml:function(str){
		      if (str) {
		        return str.replace(/\n/g, '<br/>');
		      }else{
		        return '';
		      }
		    },
			delInstallPackages(data) {
	      		var _this=this;
		        this.$confirm('是否确定删除【'+data.filename+'】版本的安装包?', '提示', {
		          confirmButtonText: '删除',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		    		const loading=XYBase.openloading();
		    		$.getJSON('/packages/delInstallPackage/'+data.filename,function(ret){
		    			loading.close();
		    			if (ret) {
		    				if (ret.code === 0) {
		    					data.step=false;
		    				}else XYBase.msg(ret.msg);
		    			}else XYBase.error(XYBase.config.system_network_error);
		    		});
		        }).catch(() => {
					XYBase.msg('已取消');
		        });
			},
			
			rollBackup(data) {
	      		var _this=this;
		        this.$confirm('是否确定回滚备份版本?</br><span class="text-danger">ps:请勿跨版本回滚，会出现不可预料的错误</span>', '提示', {
		        	dangerouslyUseHTMLString:true,
		          confirmButtonText: '回滚',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		    		const loading=XYBase.openloading();
		    		$.getJSON('/packages/rollBackup/'+data.filename,function(ret){
		    			loading.close();
		    			if (ret) {
		    				if (ret.code === 0) {
		    					window.location.reload();
		    				}else XYBase.msg(ret.msg);
		    			}else XYBase.error(XYBase.config.system_network_error);
		    		});
		        }).catch(() => {
					XYBase.msg('已取消');
		        });
			},
			installPackage(data) {
	      		var _this=this;
		        this.$confirm('是否确定安装【'+data.filename+'】版本的安装包?</br><span class="text-danger">ps:如若跨版本安装，请确定下载的是升级包，并非补丁包</span>', '提示', {
		          confirmButtonText: '安装',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		    		const loading=XYBase.openloading();
		    		$.getJSON('/packages/installPackage/'+data.filename,function(ret){
		    			loading.close();
		    			if (ret) {
		    				if (ret.code === 0) {
		    					window.location.reload();
		    				}else XYBase.msg(ret.msg);
		    			}else XYBase.error(XYBase.config.system_network_error);
		    		});
		        }).catch(() => {
					XYBase.msg('已取消');
		        });
			},
	      	initHtml() {
	      		var _this=this;
	      		this.search.ajax=true;
				$.ajax({
					url:'',
					type:'get',
					dataType:'json',
					error:function(err){
	      				_this.search.ajax=false;
						XYBase.error(XYBase.config.system_network_error);
					},
					success:function(ret){
	      				_this.search.ajax=false;
						if (ret) {
							if (ret.code === 0) {
								_this.tableData=ret.data;
							}else{
								XYBase.msg(ret.msg);
							}
						}else{
							XYBase.error(XYBase.config.system_network_error);
						}
					}
				});
			},
			updateSystemVersion(){
				if(this.SystemInfo.update){
			        this.$confirm('是否立即更新？', '提示', {
			          title:'更新提示',
			          confirmButtonText: '立即更新',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(() => {
						XYBase.updateSystemVersion();
			        }).catch(() => {
						XYBase.msg('已取消');
			        });
				}else{
					this.inspectSystemVersion();
				}
			},
			inspectSystemVersion(){
				var _this=this;
				XYBase.inspectSystemVersion(this,function(ret) {
					if(ret){
						if(ret.code===600){
							_this.SystemInfo=ret.data;
							_this.showUpdateNotify();
						}else if(ret.code===0){
							XYBase.success(ret.msg)
						}else{
							XYBase.msg(ret.msg)
						}
					}else{
						XYBase.error(XYBase.config.system_network_error);
					}
				});
			},
			showUpdateNotify(){
				this.$notify({
		          title: '更新提示',
		          message: '有新版是否更新？点击立即更新',
		          position: 'bottom-right',
		          iconClass:'el-icon-upload',
		          showClose:false,
		          duration:3000,
		          onClick:()=>{
		          	this.updateSystemVersion();
		          }
		        });
			}
	    }
	});
</script>
{/block}