<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="utf-8"%>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>

<script src="${contextRoot}/develop/lib/plugin/switchery/switchery.js"></script>
<script src="${contextRoot}/develop/lib/plugin/echarts/echarts-all.js"></script>
<script>
    /* *************************** 自定义模块 ***************************** */

    var trackJob = {
        $form:$("#div_form"),
        grid:null,
        chart:null,
        dialog:null,
        repeat:null,
        switchery:null,
        init:function(){
            var me = this;

            //控件初始化
            $("#selJob").ligerComboBox({
                dict:true,
                dictName:"RS_JOB_CONFIG",
                onSelected:function(value,text)
                {
                    me.search(value);
                },
                onSuccess:function(data)
                {
                    if(data!=null&&data.length>0)
                    {
                        this.selectValue(data[0].code);
                    }
                }
            });
            me.initSwitcher();
            me.chart=echarts.init(document.getElementById("echarts-bar-chart"));

            //列表控件
            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/datacollect/getJobLog',
                selectRowButtonOnly:true,
                delayLoad:true,//初始化不加载
                columns: [
                    {display:'采集开始时间',name:'jobStartTime', width: '10%'},
                    {display:'采集结束时间',name:'jobEndTime', width: '10%'},
                    {display:'采集数据集数',name:'jobDatasetCount', width: '10%'},
                    {display:'成功数据集数',name:'jobDatasetSuccess', width: '10%'},
                    {display:'数据总数',name:'count', width: '10%'},
                    {display:'成功数据',name:'success', width: '10%'},
                    {display:'补采数据',name:'repeatNum', width: '10%'},
                    {
                        display:'操作',name:'id',width: '12%',render:function(row){
                            return '<a href="javascript:void(0)" onclick="javascript:trackJob.repeat()">补采</a>';
                        }
                    },
                    {display:'信息',name:'jobContent', width: '18%'}
                ],
                onSuccess:function(data)
                {
                    try {
                        var list = data.detailModelList;
                        if(list!=null&&list.length>0)
                        {
                            me.reload(list);
                        }
                        return true;
                    }
                    catch(e)
                    {
                        return false;
                    }
                },
                onDblClickRow:function(data)
                {
                    me.reloadRow(data.id);
                }
            });

            //任务补采

            $('#btnRepeatJob').click(function () {
                parent.indexPage.loadPage("${contextRoot}/datacollect/repeatDatacollect", {
                    jobId: $("#selJob").ligerComboBox("getValue")
                });
            });
            //任务编辑
            $('#btnEditJob').click(function(){
                   var title = "编辑任务";
                   var params = {jobId:$("#selJob").ligerComboBox("getValue")};
                   me.dialog=$.ligerDialog.open({
                    height: 556,
                    width: 700,
                    title: title,
                    url: '${contextRoot}/datacollect/editorJob',
                    //load: true,
                    urlParms: params
                });
            });

        },
        initSwitcher:function(){
            var me = this;
            //开关控件
            var changeCheckbox = document.querySelector('#jobStatusSwitch');
            me.switchery = new Switchery(changeCheckbox, { disabled: true,size: 'large' });
            changeCheckbox.onchange = function() {
                //debugger
                var a = me.switchery;
                var jobId = $("#selJob").ligerComboBox("getValue");

                if(!changeCheckbox.checked)
                {
                    me.valid(jobId,"0");
                }
                else{
                    me.valid(jobId,"1");
                }
            };
        },
        repeat:function() {
                parent.indexPage.openMenuByText("任务补采", {jobId: $("#selJob").ligerComboBox("getValue")}, false);
        },
        callbackDialog:function(message){
            $.ligerDialog.success(message);
            this.dialog.close();
        },
        //获取任务相关数据
        search:function(jobId){
            //debugger
            var me = this;
            me.clearTop();
            me.initSwitcher();
            if(jobId!="" && jobId.length>0)
            {
                //获取运行状态
                $.ajax({
                    type: "POST",
                    url : "${contextRoot}/datacollect/getJobInfo",
                    dataType : "json",
                    data:{jobId:jobId},
                    cache:false,
                    success :function(data){
                        if(data.successFlg) {
                            me.switchery.enable();
                            me.setJobStatus(data.data);
                        }
                        else{
                            $.ligerDialog.error(data.message);
                        }
                    },
                    error :function(data){
                        $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                    }
                });

                me.grid.set({
                    parms: {jobId:jobId}
                });
                me.grid.reload();
            }
        },
        //任务状态
        setJobStatus:function(status)
        {
            var me = this;
            if(status=="1")
            {
                $('#jobStatusSwitch').attr("checked",true);
                me.switchery.element.checked=true;
                me.switchery.setPosition();
                $("#jobStatus").html('<span class="green job_run">正在运行中...</span>');
            }
            else{

                $('#jobStatusSwitch').removeAttr("checked");
                me.switchery.element.checked=false;
                me.switchery.setPosition();
                $("#jobStatus").html('<span class="red job_stop">运行暂停</span>');
            }
        },
        //生效失效
        valid:function(id,valid){
            if(id==null||id.length==0) return false;
            setTimeout(function () { return; }, 1000);

            $.ajax({
                type: "POST",
                url : "${contextRoot}/datacollect/validJob",
                dataType : "json",
                data:{jobId:id,valid:valid},
                cache:false,
                success :function(data){
                    if(data.successFlg) {
                        if(valid=="1")
                        {
                            $("#jobStatus").html('<span class="green job_run">正在运行中...</span>');
                        }
                        else{
                            $("#jobStatus").html('<span class="red job_stop">运行暂停</span>');
                        }
                    }
                    else{
                        $.ligerDialog.error(data.message);
                    }
                },
                error :function(data){
                    $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                }
            });


        },
        //上部数据情况
        clearTop:function(){
            var me = this;
            var parent = $("#jobStatusSwitch").parent();
            $("#jobStatusSwitch").next().remove();
            $("#jobStatusSwitch").remove();
            $('<input type="checkbox" class="js-switch" id="jobStatusSwitch" checked />').appendTo(parent);
            $("#divCount").html("-");
            $("#divSuccess").html("-");
            $("#divPercent").html("-");
            $("#echarts-bar-chart").hide();
            $("#echarts-blank").show();
        },
        setTop:function(count,success,percent){
            $("#divCount").html(count);
            $("#divSuccess").html(success);
            $("#divPercent").html(percent +"%");

            $("#echarts-bar-chart").show();
            $("#echarts-blank").hide();
        },
        //刷新页面数据
        reload:function(data)
        {
            var me =this;
            if(data!=null && data.length>0)
            {
                var count = 0
                var success =0;
                var x = [];
                var y1=[];
                var y2=[];
                for(var i=0;i<data.length;i++)
                {
                    count += data[i].count;
                    success += data[i].success;
                    x.push(data[i].jobStartTime);
                    y1.push(data[i].count);
                    y2.push(data[i].success);
                }
                me.setTop(count,success,count?(success/count *100).toFixed(2):0);

                me.chart.setOption({
                    title: {text:"采集情况"},
                    tooltip:{trigger:"axis"},
                    legend:{data:["数据总数","成功数据"]},
                    grid:{x:30,x2:40,y2:24},
                    calculable:!0,
                    xAxis:[{type:"category",data:x}],
                    yAxis:[{type:"value"}],
                    series:[{name:"数据总数",type:"bar",data:y1,markLine:{data:[{type:"average",name:"平均值"}]}},
                        {name:"成功数据",type:"bar",data:y2,markLine:{data:[{type:"average",name:"平均值"}]}}]},true);
                window.onresize=me.chart.resize;
            }
            else{
                me.clearTop();
            }
        },
        //根据某行获取刷新
        reloadRow:function(id)
        {
            var me = this;
            if(id!="" && id.length>0)
            {
                //获取某次采集情况数据
                $.ajax({
                    type: "POST",
                    url : "${contextRoot}/datacollect/getJobLogDataset",
                    dataType : "json",
                    data:{logId:id},
                    cache:false,
                    success :function(re){
                        if(re.successFlg) {
                            var data = re.detailModelList;
                            if(data!=null && data.length>0)
                            {
                                var count = 0
                                var success =0;
                                var x = [];
                                var y1=[];
                                var y2=[];
                                for(var i=0;i<data.length;i++)
                                {
                                    count += data[i].y1;
                                    success += data[i].y2;
                                    x.push(data[i].x);
                                    y1.push(data[i].y1);
                                    y2.push(data[i].y2);
                                }
                                me.setTop(count,success,count?(success/count *100).toFixed(2):0);

                                me.chart.setOption({title:{text:"采集情况"},
                                    tooltip:{trigger:"axis"},
                                    legend:{data:["数据总数","成功数据"]},
                                    grid:{x:30,x2:40,y2:24},
                                    calculable:!0,
                                    xAxis:[{type:"category",data:x}],
                                    yAxis:[{type:"value"}],
                                    series:[{name:"数据总数",type:"bar",data:y1,markLine:{data:[{type:"average",name:"平均值"}]}},
                                        {name:"成功数据",type:"bar",data:y2,markLine:{data:[{type:"average",name:"平均值"}]}}]},true);
                                window.onresize=me.chart.resize;
                            }
                            else{
                                me.clearTop();
                            }
                        }
                        else{
                            $.ligerDialog.error(re.message);
                        }
                    },
                    error :function(data){
                        $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                    }
                });

            }

        }
    }

    $(function () {
        trackJob.init();
    });
</script>


