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


<script>
    /* *************************** 自定义模块 ***************************** */
    var configJob = {
        dialog:null,
        grid:null,
        init:function(){
            var me = this;

            $('.m-retrieve-area').show();

            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/datacollect/getJob',
                columns: [
                    {display:'任务名称',name:'jobName', width: '20%'},
                    {display:'适配方案',name:'schemeAndVersion', width: '20%'},
                    {display:'数据集',name:'jobDataset', width: '20%'},
                    {display:'执行计划',name:'jobPlan',width: '20%'},
                    {display:'操作', name: 'operator', width: '20%', render: function (row) {
                        var html = '<div class="m-inline-buttons" style="width:80px;">';
//                        if(row.valid == '1'){
//                            html += '<a href="javascript:void(0)" onclick="javascript:configJob.valid(\''+row.id+'\',\'0\')">失效</a>';
//                        }else{
//                            html += '<a href="javascript:void(0)" onclick="javascript:configJob.valid(\''+row.id+'\',\'1\')">开启</a>';
//                        }
                        html += '<a class="m-btn-edit" onclick="javascript:configJob.editor(\''+row.id+'\')"></a>';
                        html += '<a class="m-btn-delete" onclick="configJob.delete(\''+row.id+'\')"></a>';

                        return html;
                    }}
                ],
                onDblClickRow : function (row){
                    me.editor(row.id);
                }
            });

            me.bindEvents();
        },
        //绑定按钮事件
        bindEvents: function () {
            var me = this;
            $('#btnAdd').click(function () {
                me.editor();
            });
            $('#btnExecute').click(function(){
                //获取选择行
                debugger
                var row = me.grid.getSelectedRow();
                if(row!=null &&row.id!=undefined&&row.id.length>0)
                {
                    $.ligerDialog.success("已交由后台运行！");
                    var jobId = row.id;
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/datacollect/executeJob",
                        dataType : "json",
                        data:{jobId:jobId},
                        cache:false,
                        success :function(data){
                            if(!data.successFlg) {console.log(data);
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
                else{
                    $.ligerDialog.warn('请选择要执行任务！');
                }

            });
        },
        //编辑弹窗
        editor:function(id){
            var me = this;
            var title = "新增任务";
            var params = null;
            if(id!=undefined&&id!=null)
            {
                title = "编辑任务";
                params = {jobId:id};
            }

            me.dialog = $.ligerDialog.open({
                height: 556,
                width: 700,
                title: title,
                url: '${contextRoot}/datacollect/editorJob',
                //load: true,
                urlParms: params
            });
        },
        //生效失效
        valid:function(id,valid){
            var message = "确定要失效该任务吗？";

            if(valid == "1")
            {
                message = "确定要开启该任务吗？";
            }

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/datacollect/validJob",
                        dataType : "json",
                        data:{jobId:id,valid:valid},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                configJob.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });

        },
        //删除任务
        delete:function(id)
        {

            $.ligerDialog.confirm("确定要删除该任务吗？", function (confirm) {
                if (confirm)
                {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/datacollect/deleteJob",
                        dataType : "json",
                        data:{jobId:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                configJob.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });

        },
        //弹窗返回消息
        callbackDialog:function(message){
            $.ligerDialog.success(message);
            this.grid.reload();
            this.dialog.close();
        }
    }

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


