<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>岗位管理</title>
    <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, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息(<b>收起-展开</b>)</legend>
            <div id="search-div" style="margin: 5px 10px -10px -20px">
                <form class="layui-form" action="" lay-filter="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-inline">
                                <input type="text" id="name" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" id="code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="status" id="status" lay-filter="status">
                                    <option value="">所有</option>
                                    <option th:each="item:${bugproject}" th:text="${item.value}" th:value="${item.key}"/>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn"><i
                                    class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-reset-btn"><i class="layui-icon layui-icon-refresh"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>新增
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">
                    <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="view">
                    <i class="layui-icon layui-icon-eye" style="font-size: 10px;"></i>查看
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">
                    <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
                </button>
                <button class="layui-btn layui-btn-sm layui-icon-add-btn" lay-event="progress">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>进度录入
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">
                        <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
                    </a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">
                        <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
                    </a>
                </script>

<!--        <script type="text/html" id="statusTpl">
            {{#
            var str = '';
            if (d.status == '0') {
            str = '<span class="layui-badge layui-bg-blue">正常</span>';
            } else {
            str = '<span class="layui-badge layui-bg-orange">停用</span>'
            }
            return str;
            }}
        </script>-->
    </div>
</div>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script th:inline="javascript" type="text/javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    var updateflag = 0;
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            table = layui.table;

        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'bug/mylist',
            method: 'post',
            where: {sortName: 'post_sort', sortOrder: 'asc'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: "radio", fixed: 'left',title: '😊'},
                    {type: 'numbers', fixed: 'left',title: '序号'},
                    {field: 'projectName', width: '8%', title: '项目名称',  align: 'center'},
                    {field: 'code',width: '11%',title:'项目编号',align: 'center'},
                    {field: 'dictValue', width: '8%', title: '项目状态', align: 'center'},

                    {field: 'leaderNames', width: '12%', title: '项目组负责人'},
                    {width: '8%', title: '项目周期',align: 'center',templet: function (row) {
                        let qs = new Date(row.beginTime).getTime();
                        let wc = new  Date(row.comeTime).getTime();
                        let date = wc-qs;
                        if (date != null){
                            return date/(24*60*60*1000)+'天';
                        }
                        return '时间异常';
                        }},
                    {field: 'beginTime', width: '10%', title: '开始时间', templet: '#statusTpl', sort: true, align: 'center'},
                    {field: 'comeTime', width: '21%', title: '项目结束时间'},
                    {field: 'createUserName', width: '10%', title: '创建人'},
                    {field: 'projectCreateTime', width: '15%', title: '创建时间', align: 'center'},
                    {field: 'updateUserName',width: '10%',title: '更新人',align: 'center'},
                    {field: 'projectUpdateTime',width: '15%',title: '更新时间',align: 'center'},
                    {title: '操作', width: '12%', toolbar: '#currentTableBar', align: 'center', fixed: 'right'}
                ]
            ],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'grid',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.rows //解析数据列表
                };
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1 // 页码从1开始
                },
                where: form.val("search-form")
            });
            return false;
        });

        // 监听重置操作
        form.on('submit(data-reset-btn)', function (data) {
            form.val("search-form", {
                "name": '',
                "code": '',
                "status": ''
            });
            // 执行搜索重载
            currTable.reload({
                page: {
                    curr: 1
                },
                where: form.val("search-form")
            });
            return false;
        });

        // 监听排序事件
        table.on('sort(currentTableFilter)', function (obj) {
            currTable.reload({
                initSort: obj,
                where: {
                    sortName: toLine(obj.field), //排序字段
                    sortOrder: toLine(obj.type) //排序方式
                }
            });
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '新增项目信息',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    shadeClose: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'bugger/add',
                    end: function () {
                        // 重载表格
                        if (updateflag == 1)
                            currTable.reload();
                        updateflag = 0;
                        layer.close(index);
                    }
                });
            } else if (obj.event == 'edit') {

                var dataPost = table.cache["currentTableId"];
                if (dataPost.length == 0)
                    return;
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0)
                    layer.msg("请选择要编辑的岗位信息！", {icon:2});
                else if (data.length > 1)
                    layer.msg("请选择一条记录进行编辑！", {icon:2});
                else {
                    var index = layer.open({
                        title: '修改项目信息',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: [layerwidth + 'px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'bugger/update/' + data[0].id,
                        end: function () {
                            // 重载表格
                            if (updateflag == 1)
                                currTable.reload();
                            updateflag = 0;
                            layer.close(index);
                        }
                    });
                }
            } else if (obj.event == 'view') {
                var dataPost = table.cache["currentTableId"];
                if (dataPost.length == 0)
                    return;

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0)
                    Message.warning("请选择要查看的岗位信息！", 1500);
                else if (data.length > 1)
                    Message.warning("请选择一条记录进行查看！", 1500);
                else {
                    var index = layer.open({
                        title: '查看岗位信息',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: [layerwidth + 'px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'bugger/view/' + data[0].id,
                        end: function () {
                            layer.close(index);
                        }
                    });
                }
            } else if (obj.event === 'delete') {
                var dataPost = table.cache["currentTableId"];
                if (dataPost.length == 0)
                    return;

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    layer.msg("请选择要删除的项目！", {icon:2});
                    return;
                }else if (data[0].dictValue != '执行中...'){

                    layer.msg('项目状态不等于执行中，不允许删除！',{icon:2},function () {
                        return;
                    });
                }else {
                    layer.confirm('确定删除所选的项目吗？', function (index) {
                        layer.close(index);
                        AjaxUtil.post({
                            url: AjaxUtil.ctx + "bug/de/"+data[0].id,
                            contentType: "application/json",
                            success: function (res) {
                                if (res.code === 0) {
                                    layer.msg(res.message,{icon:1}, function () {
                                        currTable.reload();
                                    });
                                } else {
                                    layer.msg(res.message, {icon:2});
                                }
                            },
                            error: function (error) {
                            }
                        });
                    });
                }
            }else if(obj.event === 'progress'){
                var checkStatus = table.checkStatus('currentTableId')
                    ,data = checkStatus.data;
                if (data == null || data.length === 0){
                    layer.msg('请选择要录入的项目！',{icon:2});
                    return;
                }else if (data.length > 1){
                    layer.msg('请选择一条数据进行补充！',{icon:2});
                    return;
                    
                }else {
                    var index = layer.open({
                        title: '进度录入',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: [layerwidth + 'px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'bugger/init/' + data[0].id,
                        end: function () {
                            layer.close(index);
                        }
                    });
                }
            }
        });


        // 监听行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data);
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改项目信息',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    shadeClose: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'bugger/update/' + data.id,
                    end: function () {
                        // 重载表格
                        if (updateflag == 1)
                            currTable.reload();
                        updateflag = 0;
                        layer.close(index);
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该项目吗？', function (index) {
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "bug/de/" + data.id,
                        success: function (res) {
                            if (res.code === 0) {
                                layer.msg(res.message,{icon:1}, function () {
                                    currTable.reload();
                                });
                            } else {
                                layer.msg(res.message, {icon:2});
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
            }
        });

        //双击
        table.on('rowDouble(currentTableFilter)', function (obj) {
            data = obj.data;
            var index = layer.open({
                title: '查看岗位信息',
                type: 2,
                shade: 0.3,
                maxmin: true,
                shadeClose: true,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'bugger/view/' + data.id,
                end: function () {
                    layer.close(index);
                }
            });
        });
    });

    function setUpdateFlag() {
        updateflag = 1;
    }
</script>
</body>
</html>