<!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">
    <style>
        body {
            overflow-y: scroll;
        }
        /* 禁止刷新后出现横向滚动条 */
    </style>
</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 0px">
                <form class="layui-form" action="" lay-filter="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px">报告名称</label>
                            <div class="layui-input-inline" style="width: 220px">
                                <input type="text" id="MC" name="MC" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px; margin-left: -10px">审批状态</label>
                            <div class="layui-input-inline" style="width: 150px">
                                <select name="SPZT" id="SPZT" lay-filter="status">
                                    <option value="">全部</option>
                                    <option th:each="item:${approveList}" th:text="${item.name}"
                                            th:value="${item.name}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px; margin-left: -10px">所属部门</label>
                            <div class="layui-input-inline" style="width: 150px">
                               <!-- <input type="text" id="SSBMNAME" name="SSBMNAME" class="layui-input"
                                       autocomplete="off"/>-->
                                <!--                                <input type="text" id="parentName" name="parentName"/>-->
                                <!--                                <input type="hidden" id="parentId" name="parentId"/>-->
                                <!--                                <input type="hidden" id="ancestors" name="ancestors"/>-->
                                <select name="SSBMNAME" id="SSBMNAME" lay-filter="status">
                                    <option value="">全部</option>
                                    <option th:each="item:${dept}" th:text="${item.deptName}"
                                            th:value="${item.deptName}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 45px; margin-left: -10px">已审批</label>
                            <div class="layui-input-inline" style="width: 80px">
                                <select name="YSP" id="YSP" lay-filter="status">
                                    <option value="">全部</option>
                                    <option th:each="item:${approvedList}" th:text="${item.name}"
                                            th:value="${item.code}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 60px; margin-left: -10px">起始日期</label>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="SQRQStart" id="SQRQStart" placeholder="开始日期"
                                       class="layui-input" readonly>
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="SQRQEnd" id="SQRQEnd" placeholder="结束日期" class="layui-input"
                                       readonly>
                            </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" th:if="${addRight}">
                    <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" th:if="${editRight}">
                    <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="browse">
                    <i class="layui-icon layui-icon-file" style="font-size: 10px;"></i>查看
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete" th:if="${delRight}">
                    <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
                </button>
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="tjApprove">
                    <i class="layui-icon layui-icon-ok" style="font-size: 10px;"></i>提交审批
                </button>

                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="tjrevokeApprove">
                    <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="spOption">
                        <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="spList">
                    <i class="layui-icon layui-icon-list" style="font-size: 10px;"></i>审批列表
                </button>
                <button class="layui-btn layui-btn-sm" lay-event="downloadWord">
                    <i class="layui-icon layui-icon-export" style="font-size: 10px;"></i>导出
                </button>
                <!--                <button class="layui-btn layui-btn-sm layui-inline" style="width:86px;height:32px;" lay-event="LAYTABLE_EXPORT">-->
                <!--                    <i class="layui-icon layui-icon-export" style="font-size: 10px;color: white"></i><span style="font-size: 10px;color: white">导出列表</span>-->
                <!--                    <ul class="layui-table-tool-panel" style="display: none;"><li data-type="csv" style="color: black">导出到 Csv 文件</li><li data-type="xls" style="color: black">导出到 Excel 文件</li></ul>-->
                <!--                </button>-->
            </div>
        </script>

        <input type="hidden" id="userId" name="userId" class="layui-input" th:value="${userId}">
        <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" th:if="${editRight}">
                <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" th:if="${delRight}">
                <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
            </a>
        </script>
    </div>
</div>
<script>
    // 获取所有需要去除空格的输入框
    const inputs = [
        document.getElementById('MC'),
        document.getElementById('SSBMNAME')

    ];
    // 为每个输入框添加事件监听
    inputs.forEach(input => {
        input.addEventListener('input', function() {
            this.value = this.value.replace(/\s+/g, ''); // 去除空格
        });
    });
</script>
<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:src="@{/lib/xm-select-v1.2.2/xm-select.js}" charset="utf-8"></script>
<script th:inline="javascript" type="text/javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    var updateflag = 0;
    var xmApproval = {};
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            table = layui.table,
            laydate = layui.laydate;
        var userId = $("#userId").val();
        //日期
        laydate.render({
            elem: '#SQRQStart'
        });
        laydate.render({
            elem: '#SQRQEnd'
        });

        var currTable = table.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'reportinfo/list_myproject',
            method: 'post',
            // where: {sortName:'cjrq', sortOrder:'asc'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [
                    {type: "checkbox", fixed: 'left'},
                    {type: 'numbers', align: 'center',title: '序号'},
                    {field: 'mc', width: '25%', title: '报告名称'},
                    {field: 'spzt', width: '10%', title: '审批状态', sort: true},
                    {field: 'qs', width: '7%', title: '起始', align: 'center'},
                    {field: 'wc', width: '7%', title: '完成', align: 'center'},
                    {field: 'sqdwName', width: '15%', title: '申请单位'},
                    {field: 'ssbmName', width: '12%', title: '所属部门'},
                    {field: 'cjrq', width: '8%', title: '创建日期', align: 'center'},
                    {field: 'cjrName', width: '6%', title: '创建人', align: 'center'},
                    {
                        field: 'ysp', width: '5%', title: '已审批', align: 'center', templet: function (d) {
                            if (1 == d.ysp) {
                                return '<span class="layui-badge layui-bg-blue">是</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-orange">否</span>';
                            }
                        }
                    },
                    // {field: 'sprName', width: '6%', title: '审批人', align: 'center'},
                    // {field: 'sprq', width: '8%', title: '审批时间', align: 'center'},
                    // {title: '操作', width: '10%', toolbar: '#currentTableBar', align: 'center', fixed: 'right'},
                    {field: 'id', width: '0', title: 'id', hide: true}
                ]
            ],
            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", {
                "MC": '',
                "SPZT": '',
                'SSBMNAME': '',
                'SQRQStart': '',
                'SQRQEnd': '',
                'YSP': ''
            });
            // 执行搜索重载
            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) //排序方式
                }
            });
        });
        var cancelIndex;
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title: '新建项目可研报告',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1090px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'reportinfo/forAdd',
                    end: function (index) {
                        // 重载表格
                        if (updateflag == 1)
                            currTable.reload();
                        updateflag = 0;
                    }
                });
                // layer.full(index);
            } else if (obj.event === 'edit') {
                var projects = table.cache["currentTableId"];
                if (projects.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 if (data[0].spzt == "审批结束") {
                    Message.warning("该可研报告已审批结束，不能修改！", 1500);
                } else if (data[0].spzt != null && data[0].spzt.indexOf("未提交") < 0) {
                    Message.warning("该可研报告审批中，不能修改！", 2000);
                } else {
                    xmApproval = {
                        "xmid": data[0].id,
                        "xmmc": data[0].mc
                    }
                    layer.open({
                        title: '编辑可研报告',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['1090px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'reportinfo/forUpdate/' + data[0].id,
                        end: function (index) {
                            // 重载表格
                            if (updateflag == 1)
                                currTable.reload();
                            updateflag = 0;
                        }
                    });
                }
            } else if (obj.event === 'delete') {
                var projects = table.cache["currentTableId"];
                if (projects.length == 0)
                    return;

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要删除的可研报告！", 1000);
                    return;
                } else {
                    for (let item of data) {
                        if (item.spzt != "未提交") {
                            if (item.spzt == "审批结束")
                                Message.error("存在已审批结束的可研报告，不能直接删除！", 2000);
                            else if (data[0].spzt != null)
                                Message.error("存在审批中的可研报告，不能直接删除！", 2000);
                            return;
                        }
                    }
                }

                layer.confirm('确定批量删除所选择可研报告信息吗？', function (index) {
                    var ids = [];
                    for (var i = 0, len = data.length; i < len; i++) {
                        ids.push(data[i].id);
                    }
                    layer.close(index);
                    AjaxUtil.post({
                        url: AjaxUtil.ctx + "reportinfo/batchDelete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (res) {
                            if (res.code === 0) {
                                Message.success(1500, res.message, function () {
                                    currTable.reload();
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        error: function (error) {
                        }
                    });
                });
            } else if (obj.event === 'browse') {  // 监听查看操作
                var projects = table.cache["currentTableId"];
                if (projects.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 {
                    xmApproval = {
                        "xmid": data[0].id,
                        "xmmc": data[0].mc
                    }
                    layer.open({
                        title: '查看项目可研报告',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: ['1090px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'reportinfo/forView/' + data[0].id,
                        end: function (index) {
                        }
                    });
                    // layer.full(index);
                }
            } else if (obj.event === 'tjApprove') {
                var projects = table.cache["currentTableId"];
                if (projects.length == 0)
                    return;

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要提交审批的可研报告！", 2000);
                } else if (data.length > 1) {
                    Message.warning("请选择一条记录进行提交！", 1500);
                } else if (data[0].spzt == "审批结束") {
                    Message.warning("该可研报告已审批结束！", 1500);
                } else if (data[0].spzt != null && data[0].spzt.indexOf("未提交") < 0) {
                    Message.warning("该可研报告正在审批中……", 1500);
                } else {
                    xmApproval = {
                        "xmid": data[0].id,
                        "xm": data[0].mc,
                        "zt": data[0].spzt,
                        "cjr": data[0].cjr
                    };
                    if (data[0].spzt == null) {
                        xmApproval.zt = "未提交";
                        data[0].spzt = "未提交";
                    }

                    var content = AjaxUtil.ctx + 'reportinfo/forApproveAdd';
                    if (data[0].spzt != "未提交")
                        // content = AjaxUtil.ctx + 'reportinfo/forApproveUpdate/' + data[0].id + '?spzt=' + data[0].spzt;
                        content = AjaxUtil.ctx + 'reportinfo/forApproveUpdate/' + data[0].id + '/' + data[0].ysje + '?spzt=' + data[0].spzt;

                    layer.open({
                        title: '提交审批',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: ['1025px', layerheight + 'px'],
                        content: content,
                        end: function (index) {
                            if (updateflag == 1)
                                currTable.reload();
                            updateflag = 0;
                        }
                    });
                }
            } else if (obj.event === 'spOption') {
                var projects = table.cache["currentTableId"];
                if (projects.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 if (data[0].spzt == null || data[0].spzt.indexOf("未提交") >= 0) {
                    Message.warning("该可研报告尚未提交审批！", 1500);
                } else if (data[0].spzt == "审批结束") {
                    Message.warning("该可研报告已审批结束！", 1500);
                } else {
                    xmApproval = {
                        "xmid": data[0].id,
                        "xm": data[0].mc,
                        "ysje": data[0].ysje
                    };

                    layer.open({
                        title: '审批意见',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: ['1025px', layerheight + 'px'],
                        // content: AjaxUtil.ctx + 'reportinfo/forApproveUpdate/' + data[0].id  + '?spzt=' + data[0].spzt,
                        content: AjaxUtil.ctx + 'reportinfo/forApproveUpdate/' + data[0].id + '/' + data[0].ysje + '?spzt=' + data[0].spzt,
                        end: function (index) {
                            if (updateflag == 1)
                                currTable.reload();
                            updateflag = 0;
                            layer.close(index);
                        }
                    });
                }
            } else if (obj.event === 'spList') {
                var projects = table.cache["currentTableId"];
                if (projects.length == 0)
                    return;

                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data == null || data.length === 0) {
                    Message.warning("请选择要查看审批列表的可研报告！", 2000);
                } else if (data.length > 1) {
                    Message.warning("请选择一条记录进行查看！", 1500);
                } else if (data[0].spzt == null || data[0].spzt == "未提交") {
                    Message.warning("该可研报告尚未提交审批！", 1500);
                } else {
                    xmApproval = {
                        "xmid": data[0].id,
                        "xm": data[0].mc
                    };
                    var index = layer.open({
                        title: '审批列表',
                        type: 2,
                        shade: 0.3,
                        maxmin: true,
                        shadeClose: true,
                        area: ['1300px', layerheight + 'px'],
                        content: AjaxUtil.ctx + 'reportinfo/forApproveList',
                        end: function () {
                            // layer.close(index);
                        }
                    });
                    // layer.full(index);
                }
            } else if (obj.event === 'downloadWord') {  // 监听查看操作
                var projects = table.cache["currentTableId"];
                if (projects.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 {
                    layer.confirm('是否确定导出可研报告——' + data[0].mc + '？', {
                        btn: ['导出', '取消']
                    }, function (index, layero) {
                        window.location.href = AjaxUtil.ctx + "reportinfo/exportWord/" + data[0].id;
                        layer.close(index);

                    }, function (index) {

                    });
                }
            } else if (obj.event === 'tjrevokeApprove') {
                var projects = table.cache["currentTableId"];
                if (projects.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 if (data[0].spzt == "审批结束") {
                    Message.warning("该可研报告已审批结束！", 1500);
                }
                    // else if (data[0].cjr != userId) {
                // Message.error("您不是该可研报告的创建人！", 1500);}
                else {
                    xmApproval = {
                        "xmid": data[0].id,
                        "xm": data[0].zwmc,
                        "zt": "未提交"
                    };
                    if (data[0].spzt == null) {
                        xmApproval.zt = "未提交";
                        data[0].spzt = "未提交";
                    }
                    var project = {
                        "id": xmApproval.xmid,
                        "spzt": '未提交'
                    }
                    layer.confirm('确定撤销项目任务书吗？', function (index) {
                        layer.close(index);
                        AjaxUtil.post({
                            url: AjaxUtil.ctx + "reportinfo/TJrevokeApprove",
                            contentType: "application/json",
                            data: JSON.stringify(project),
                            success: function (res) {
                                if (res.code === 0) {
                                    Message.success(1500, res.message, function () {
                                        // 重载表格
                                        currTable.reload();
                                    });
                                } else {
                                    Message.error(res.message, 1000);
                                }
                            },
                            error: function (error) {
                            }
                        });
                    });
                }
            }

        });

        //双击
        table.on('rowDouble(currentTableFilter)', function (obj) {
            var data = obj.data;
            xmApproval = {
                "xmid": data.id,
                "xmmc": data.mc
            }
            layer.open({
                title: '查看项目可研报告',
                type: 2,
                shade: 0.3,
                maxmin: true,
                shadeClose: true,
                area: ['1090px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'reportinfo/forView/' + data.id,
                end: function (index) {
                }
            });
        });
    });

    function setUpdateFlag() {
        updateflag = 1;
    }

    function getXmApproval() {
        return xmApproval;
    }
</script>
</body>
</html>