<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'} ">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" type="text/css" href="assets/js/vendor/viewer/viewer.min.css">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script src="assets/js/vendor/viewer/viewer.min.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'laydate', 'common'], function() {
        var $ = layui.$;
        var $win = $(window);
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var common = layui.common;
        var resizeTimeout = null;
        var tableUrl = 'api/oa/listpage-procdef';

        // 初始化表格
        function initTable() {
            table.render({
                elem: '#dataTable',
                id: 'dataTable',
                url: top.restAjax.path(tableUrl, []),
                width: admin.screen() > 1 ? '100%' : '',
                height: $win.height() - 50,
                limit: 20,
                limits: [20, 40, 60, 80, 100, 200],
                request: {
                    pageName: 'page',
                    limitName: 'rows'
                },
                cols: [
                    [
                        {field:'rowNum', width:80, title: '序号', fixed: 'left', align:'center', templet: '<span>{{d.LAY_INDEX}}</span>'},
                        {field:'version', width:80, title: '版本',  align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field:'name', width:200, title: '名称',  align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field:'description', width:200, title: '描述',  align:'center',
                            templet: function(row) {
                                var rowData = row[this.field];
                                if(typeof(rowData) === 'undefined' || rowData == null || rowData == '') {
                                    return '-';
                                }
                                return rowData;
                            }
                        },
                        {field: 'diagramResourceName', width: 80, title: '流程图', align:'center',
                            templet: function(row) {
                                if(!row.deploymentId) {
                                    return '-';
                                }
                                var flowChatImg = '<img id="flowChat'+ row.id +'" src="route/activiti/model/get-process-image/'+ row.deploymentId +'" style="width: 30px; height: 30px;"/>'
                                setTimeout(function() {
                                    new Viewer(document.getElementById('flowChat'+ row.id));
                                }, 50);
                                return flowChatImg;
                            }
                        },
                        {field: 'form', width: 100, title: '操作', align:'center', fixed: 'right',
                            templet: function(row) {
                                if(!row.formCode || !row.formVersion) {
                                    return '-';
                                }
                                return '<div class="layui-btn-group">' +
                                    '<button class="layui-btn layui-btn-xs" lay-event="startProcess">发起流程</button>'+
                                    '</div>';
                            }
                        }
                    ]
                ],
                page: true,
                parseData: function(data) {
                    return {
                        'code': 0,
                        'msg': '',
                        'count': data.total,
                        'data': data.rows
                    };
                }
            });
        }
        // 重载表格
        function reloadTable(currentPage) {
            table.reload('dataTable', {
                url: top.restAjax.path(tableUrl, []),
                where: {},
                page: {
                    curr: currentPage
                },
                height: $win.height() - 50,
            });
        }
        initTable();
        // 事件 - 页面变化
        $win.on('resize', function() {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(function() {
                reloadTable();
            }, 500);
        });
        // 事件 - 搜索
        $(document).on('click', '#search', function() {
            reloadTable(1);
        });


        table.on('tool(dataTable)', function(obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'startProcess') {
                top.dialog.open({
                    url: top.restAjax.path('route/oa-form-report/save/definition-id/{processDefinitionId}/code/{formCode}/version/{formVersion}', [data.id, data.formCode, data.formVersion]),
                    title: '发起流程',
                    width: '800px',
                    height: '80%',
                    onClose: function() {}
                });
            }
        });
    });
</script>
</body>
</html>