<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title text="审批流程"></title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
    <link rel="stylesheet" href="../../../admin/css/cool/main.css" />
</head>
<body class="pear-container">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">流程编号:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="flowNumber" placeholder="请输入关键字搜索" class="layui-input">
                    </div>
                    <label class="layui-form-label">流程名称:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="flowName" placeholder="请输入关键字搜索" class="layui-input">
                    </div>
                    <button class="pear-btn pear-btn-md-0 pear-btn-primary" lay-submit lay-filter="query">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md-0">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="table-box">
            <table id="table" lay-filter="table"></table>
        </div>
    </div>
</body>
<script type="text/html" id="toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md-permission"
            permission="approval:flow:add" lay-event="add">
        <i class="pear-icon pear-icon-add"></i> 新增
    </button>
</script>
<script type="text/html" id="bar">
    <a class="cool-line-btn cool-bg-blue" lay-event="edit">编辑</a>
</script>
<script type="text/html" id="enableTpl">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch"
           lay-text="启用|禁用" lay-filter="switch" {{ (d.state ? 'checked' : '') }} />
</script>
<script type="text/html" id="noticeWayTpl">
    {{#if (d.noticeWay == 1) { }}
        系统通知
    {{# } else if(d.noticeWay == 2){ }}
        短信通知
    {{# } }}
</script>
<script type="text/html" id="createTimeTpl">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
</script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['request', 'table', 'form', 'popup'], function() {
        var request = layui.request,
            table = layui.table,
            form = layui.form,
            popup = layui.popup;

        var MODULE_PATH = "/approval/flow/v1/";

        request.tableRender({
            url: MODULE_PATH,
            cols: [
                [
                    {type: 'checkbox'},
                    {title: '流程编号', field: 'flowNumber', align: 'center', templet: linkDetails},
                    {title: '流程名称', field: 'flowName', align: 'center'},
                    {title: '流程状态', field: 'state', align: 'center', templet: '#enableTpl'},
                    {title: '通知方式', field: 'noticeWay', align: 'center', templet: '#noticeWayTpl'},
                    {title: '创建人', field: 'createName', align: 'center'},
                    {title: '创建时间', field: 'createTime', align: 'center', templet: '#createTimeTpl'},
                    {title: '操作', toolbar: '#bar', align: 'center', width: 130}
                ]
            ]
        });

        function linkDetails(d) {
            return '<a href="javascript:void(0)" style="color:#0064c8" lay-event="detail" class="layui-blue">'+ d.flowNumber +'</a>';
        }

        table.on('tool(table)', function (obj) {
            if (obj.event === 'edit') {
                edit(obj);
            }
            else if (obj.event === 'detail') {
                detail(obj);
            }
        });

        table.on('toolbar(table)', function (obj) {
            if (obj.event === 'add') {
                add();
            }
        });

        form.on('submit(query)', function (data) {
            refresh(data.field);
            return false;
        });

        form.on('switch(switch)', function(obj) {
            var params = {
                id: obj.value,
                state: obj.elem.checked === true ? 1: 0
            };
            request.http({
                type: 'post',
                url: MODULE_PATH + 'switch',
                data: params,
                loading: true,
                success: function (result) {
                    refresh();
                    if (result.success) {
                        popup.success(result.msg);
                    } else {
                        popup.failure(result.msg);
                    }
                }
            });
        });

        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['500px', '430px'],
                content: ['add.html', 'no']
            });
        };

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '编辑',
                shade: 0.1,
                area: ['500px', '430px'],
                content: ['add.html?id=' + obj.data.id, 'no']
            });
        };

        window.detail = function (obj) {
            var index =layer.open({
                type: 2,
                title: '编辑',
                shade: 0.1,
                area: ['500px', '430px'],
                content: [ 'view.html?id='+ obj.data.id, 'no']
            });
            layer.full(index);
        };

        window.refresh = function(param) {
            table.reload('table', {where: param});
        };
    })
</script>
</html>
