{{template "inc"}}
<!-- Main Header -->
{{template "header"}}
<!-- Left side column. 权限菜单 -->
{{template "memu"}}

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
	<section class="content-header">
		<h1>
			<small><a class="btn btn-primary opr" opr="new">申请阿里云实例</a></small>
            <small><a class="btn btn-primary" onclick="upInstanceIds()">更新实例状态</a></small>
            <small><a class="btn btn-primary opr" opr="idrange">id划段</a></small>
            <small><a class="btn btn-primary" onclick="deploy()">部署</a></small>
		    <small><a class="btn btn-primary" onclick="rangetask()">执行</a></small>
            <small><a class="btn btn-primary opr" opr="releasetime">释放设置</a></small>
        </h1>
		<ol class="breadcrumb">
		  <li><a href="/admin/distribution"><i class="fa fa-dashboard"></i>分布式抽取</a></li>
		</ol>
    </section>
  <!-- Main content -->
  <section class="content">
      <div class="row">
	      <div class="col-xs-12">
	        <div class="box">
		        <div class="box-body">
		            <table id="dataTable" class="table table-bordered table-hover">
		              <thead>
		              <tr>
                        <th>任务名称</th>
						<th>实例名称</th>
		                <th>实例id</th>
						<th>ip</th>
                        <th>释放时间(UTC)</th>
						<th>实例状态</th>
                        <th>数据量</th>
                        <th>抽取状态</th>
                        <th>抽取进度</th>
						<th>操作</th>
		              </tr>
		              </thead>
		            </table>
		        </div>
	          <!-- /.box-body -->
	        </div>
        <!-- /.box -->
		</div>
	</div>
  </section>
</div>
	
<!-- footer -->
{{template "mask"}}
{{template "dialog"}}
{{template "footer"}}

<script>
menuActive("distribution")
$(function () {
	ttable=$('#dataTable').DataTable({
		"paging"      : false,
		"lengthChange": false,
		"searching"   : true,
		"ordering"    : false,
		"info"        : true,
		"autoWidth"   : false,
		"ajax": {
			"url": "/admin/distribution/ecslist",
			"type": "post",
			"data":{}
		},
		"language": {
            "url": "/res/dist/js/dataTables.chinese.lang"
        },
		"columns": [
            { "data": "TaskName"},
            { "data": "HostName"},
			{ "data": "InstanceId"},
			{ "data": "ip_nw",render:function(val,a,row,pos){
                return val+"("+row.ip_ww+")"
            }},
			{ "data": "AutoReleaseTime"},
            { "data": "Status"},
            { "data": "extask",render:function(val,a,row,pos){
               if(val){
                    return val[2]
                }else{
                    return "-"
                }
            }},
            { "data": "extstatus",render:function(val,a,row,pos){
               if(val){
                    return val
                }else{
                    return "-"
                }
            }},
            { "data": "pagetotal",render:function(val,a,row,pos){
               if(val){
                    if(row.pagecurrent){
                        return row.pagecurrent+"/"+val
                    }else{
                         return "0/"+val
                    }
      
                }else{
                    return "-"
                }
            }},
			{"data":"InstanceId",render:function(val,a,row,pos){
				return '<a class="btn btn-sm btn-warning" onclick="del(\''+row.InstanceId+'\')">释放</a>';
			}}
       	]
	});
	ttable.on('init.dt', function () {
		$(".opr").click(function(){
			var n=$(this).attr("opr")
			var _tit="",htmlObj={},obj,tag=[]
			switch(n){
			case "new":
            case "idrange":
            case "releasetime":
                if(n=="new"){
                    _tit="批量申请实例"
    				tag=[
    					{label:"任务名称",s_label:"s_name",placeholder:"任务名称",must:true},
                        {label:"机器型号",s_label:"s_computer",type:"tpl_list_local",list:[{"s_name":"4核心","_id":"4"},{"s_name":"8核心","_id":"8"}],default:"4"},
    					{label:"是否公网",s_label:"s_flow",type:"tpl_list_local",list:[{"s_name":"否","_id":false},{"s_name":"是","_id":true}],default:false},
    					{label:"机器数量",s_label:"i_num",placeholder:"机器数量",must:true},
                        {label:"时长(h)",s_label:"i_hours",placeholder:"申请时长单位小时",must:true},
    				]
                }else if (n=="idrange"){
                  _tit="id划段"
    				tag=[
    					{label:"表名",s_label:"s_table",placeholder:"信息表名",must:true},
                        {label:"开始日期",s_label:"s_date",placeholder:"2018-01-01",must:true},
                        {label:"截止日期",s_label:"e_date",placeholder:"2019-01-01",must:true},
    				]   
                }else if (n=="releasetime"){
                    _tit="释放设置"
                    tag=[
    					{label:"实例ID",s_label:"instanceid",placeholder:"实例ID",must:true},
                        {label:"顺延时间",s_label:"hours",placeholder:"当前时间起(单位/h)",must:true},
    				] 
                } 
				htmlObj={
					title:_tit,
					tag:tag,
					bts:[
						{label:"执行",class:"btn-primary",
							fun:function(){
								var obj={}
								var bcon=true
								$("#_con").find("input[id!=s_show],textarea").each(function(i,el){
									var val=$(el).val();
									obj[el.id]=$(el).val()
									if(el.id!="_id"&&$(el).attr("must")&&!val){
										bcon=false
										return false
									}
								}) 
								if (bcon){	
                                    showConfirm("确认执行？", function() {							
    									$.post("/admin/distribution/runInstances",obj,function(data){
    										if(data&&data.rep){
    											window.location.href="/admin/distribution"			
    										}else{
    											showTip(data.msg,1000)
    										}
    									},'json')
                                    });
								}else{
									alert("红色标签的表单不能为空！")
								}
							}
						}
					]
				}
			OpenDialog(htmlObj,obj)
			break;
			}
		});
	})
})
function del(_id){
	showConfirm("确定释放实例?", function() {
        $.ajax({
			url:"/admin/distribution/deleteInstance",
			type:"post",
			data:{"InstanceId":_id},
			success:function(r){
				if(r.rep){				
					ttable.ajax.reload();
				}else{
					showTip("释放失败", 1000, function() {});
				}
			}
		})
	});
}
function upInstanceIds(){
    showConfirm("更新实例?", function() {
    	com.maskShow("正在更新实例...");
		$.ajax({
    		url:"/admin/distribution/upstatus",
    		type:"post",
    		success:function(r){
    			if(r.rep){			
    				ttable.ajax.reload();
                    com.maskHide();
    			}
    		}
    	})
    });
}

function deploy(){
    showConfirm("部署应用?", function() {
       com.maskShow("正在部署应用...");
       $.ajax({
    		url:"/admin/distribution/deploy",
    		type:"post",
    		success:function(r){
    			if(r.rep){				
    				ttable.ajax.reload();
                    com.maskHide();
    			}else{
    				showTip("部署失败", 1000, function() {});
    			}
    		}
    	}) 
    })
}

function rangetask(){
    showConfirm("启动抽取?", function() {
        com.maskShow("正在部署应用...");
        $.ajax({
    		url:"/admin/distribution/rangetask",
    		type:"post",
    		success:function(r){
    			if(r.rep){				
    				ttable.ajax.reload();
                    com.maskHide();
    			}else{
    				showTip("启动抽取失败", 1000, function() {});
    			}
    		}
    	})
    }) 
}
</script>
