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

    /* *************************** 模块初始化 ***************************** */
    var flow = {
        grid: null,
        $searchStatusName: $('#status'),//转台文本匡对象
        $searchBtn: $('.l-trigger-search'),//搜索按钮
        dialog: null,
        init: function () {
            this.bindEvents();
            this.initForm();
        },
        initForm: function () {
            var me = this;
            $('.m-retrieve-area').show();
            $("#txtName").ligerSearch({
                onClick:function(value){
                    me.reloadGrid();
                }});

            var flowValid = liger.get("statusName").value;
            if(flowValid ==undefined || flowValid ==null || flowValid.length<=0){
                liger.get("statusName").selectValue("");
            }

            me.grid = $("#div_grid").ligerGrid({
                url: '${contextRoot}/flow/getFlowList',
                parms: {
                    name: $('#txtName').val(),
                    valid:flowValid
                },
                columns: [
                    {display: '流程名称', id: 'id', name: 'name', width: '20%'},
                    {display: '文件类型', id: 'fileType', name: 'fileType', width: '20%'},
                    {display: '状态', name: 'valid', width: '20%',align: 'center', render: function (rowdata, rowindex, value) {
                        if(rowdata.valid==1 ){
                            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>有效  </span></div>';
                        }else if(rowdata.valid==0){
                            return ' <div style="222;margin-top: 10px;"><span>无效 </span></div>';
                        }
                    }},
                    {
                        display: '操作', name: 'operator', width: '40%', render: function (row) {

                        var html = '<div class="m-inline-buttons" style="width:350px;vertical-align:middle;">';
                        html += "<a class=\"m-btn\" style=\"padding-right:10px\" onclick=\"flow.showFlow('"+row.id+"')\">查看流程图</a>";
                        html += "<a class=\"m-btn-edit\" onclick=\"flow.editorDialog('"+row.id+"')\"></a>";
                        html += "<a class=\"m-btn-delete\" onclick=\"flow.delete('"+row.id+"','"+row.chart+"')\"></a>";
                        html += '</div>';
                        return html;
                    }
                    }
                ],
                onDblClickRow: function (row) {
                    me.editorDialog(row.id);
                }
            });

        },
	    showFlow:function (id) {
		    indexPage.openChildPage("",'${contextRoot}/process/initial?flowId='+id);
	    },
        bindEvents: function () {
            var me = this;
            var flag = false;
            $('#div_new_record').click(function () {
                me.editorDialog();
            });

            $("#statusName").ligerComboBox({data : [{"value":"全部","code":""},{"value":"有效","code":"1"},{"value":"无效","code":"0"}],
                cancelable:false,
                onSelected: function (value)
                {
                    if (flag) {
                        me.reloadGrid();
                    } else {
                        flag = true;
                    }
                },
                onSuccess:function(data){
            }});

            $(".l-text").css("display","inline-block");
            $(".l-text-wrapper").css("display","inline-block");


            //查看流程图
//            $('#assignmentDataGrid').on('click','.flowChart-btn',function () {
//                var imgurl=$(this).data("flowimg");
//                var ImgObj = new Image(); //判断图片是否存在
//                if(!Util.isStrEmpty(imgurl)){
//                    ImgObj.src = imgurl;
//                }else{
//                    jJKZL.Tips.success("当前暂无流程图！");
//                    return false;
//                }
//                //没有图片，则返回-1
//                $("#div_flow_chart").attr("src","../../.."+$(this).data("flowimg"));
//                jJKZL.popUp({popUpId: "flowChartCornPup", title: "查看流程图", width: "800px", ok: function () {
//                }});
//            });


        },
        delete:function(id,chart){
            var message = "流程删除后无法恢复，是否确认删除？";

            jQuery.ligerDialog.confirm(message, function (confirm) {
                if (confirm)
                {
                    _deleteFileAfterUpdate(chart);

                    $.ajax({ //ajax处理
                        type: "POST",
                        url : "${contextRoot}/flow/deleteFlow",
                        dataType : "json",
                        data:{id:id},
                        cache:false,
                        success :function(data){
                            if(data.successFlg) {
                                $.ligerDialog.success(data.message);
                                flow.grid.reload();
                            }
                            else{
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error :function(data){
                            $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                        }
                    });
                }
            });
        },
        //刷新列表数据
        reloadGrid: function () {
            this.grid.set({
                parms: {name: $('#txtName').val(),valid:$('#statusName_val').val()}
            });
            this.grid.reload();
        },
        //编辑弹窗
        editorDialog: function (id) {
            var me = this;
            var title = "新增流程";
            var params = null;
            if (id != undefined && id != null) {
                title = "编辑流程";
                params = {"id": id};
            }

            me.dialog = $.ligerDialog.open({
                height: 800,
                width: 800,
                title: title,
                url: '${contextRoot}/flow/editorFlow',
                //load: true,
                urlParms: params
            });
        },
        showImage: function (chart) {
            var me = this;
            var title = "流程图";
            var url = chart;

            $("#divedit").show();
            $("#divImg").attr("src",url);

            me.dialog = $.ligerDialog.open({
                height: 500,
                width: 500,
                target:$("#divedit"),
                title: title
            });
        },
    anthorize: function (id) {

        },
        //弹窗返回
        dialogSuccess: function (message) {
            $.ligerDialog.success(message);
            flow.reloadGrid();
            flow.dialog.close();
        }


    };

    //删除旧文件
    function _deleteFileAfterUpdate(fileId){
        if(null == fileId || fileId.length == 0){
            return;
        }
        var url = "${dfs}/" + fileId;
        $.ajax({
            url: url,
            type: 'DELETE',
            async: false,
            success: function(){
                console.log("旧文件删除成功");
            }
        });
    }

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