<!DOCTYPE html>
<html>

<head lang='zh'>
    <meta charset='utf-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="renderer" content="webkit" />
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0" />
    <title>Luckysheet</title>
    <link rel='stylesheet' href='/static/js/luckysheet/plugins/css/pluginsCss.css' />
    <link rel='stylesheet' href='/static/js/luckysheet/plugins/plugins.css' />
    <link rel='stylesheet' href='/static/js/luckysheet/css/luckysheet.css' />
    <link rel='stylesheet' href='/static/js/luckysheet/assets/iconfont/iconfont.css' />
    <link charset="utf-8" rel="stylesheet" href="/static/css/layui.css?v=1.0.0">
    <script>
        var _PageHeight = document.documentElement.clientHeight,
            _PageWidth = document.documentElement.clientWidth;
        var _LoadingTop = _PageHeight > 61 ? (_PageHeight - 61) / 2 : 0,
            _LoadingLeft = _PageWidth > 215 ? (_PageWidth - 215) / 2 : 0;
        var _LoadingHtml = '<div id="loadingDiv" style="position:absolute;left:0;width:100%;height:' + _PageHeight + 'px;top:0;background:#f3f8ff;opacity:1;filter:alpha(opacity=80);z-index:10000;"><div style="position: absolute; cursor1: wait; left: ' + _LoadingLeft + 'px; top:' + _LoadingTop + 'px; width: auto; height: 57px; line-height: 57px; padding-left: 50px; padding-right: 5px; color: #696969; font-family:\'Microsoft YaHei\';"><img style="width:200px;height:200px" src="../images/loading.gif"></div></div>';
        document.write(_LoadingHtml);
        document.onreadystatechange = completeLoading;
        function completeLoading() {
            if (document.readyState == "complete") {
                var loadingMask = document.getElementById('loadingDiv');
                loadingMask.parentNode.removeChild(loadingMask);
            }
        }
    </script>
    <script src="/static/js/exportExcel.js"></script>
    <script src="/static/js/layui.js?v=1.0.0"></script>
    <script src="/static/js/exceljs.js"></script>
    <script src="/static/js/fileSave.js"></script>
    <script src="/static/js/luckysheet/luckSheetExcel.js"></script>
    <script src="/static/js/luckysheet/plugins/js/plugin.js"></script>
    <script src="/static/js/luckysheet/luckysheet.umd.js"></script>
    <script src="/static/js/api.config.js"></script>
    <script src="/static/js/util.js?v=1.0.0"></script>
    <script src="/static/js/url.js?v=1.0.0"></script>
    <script src="/static/js/fullScreen.js"></script>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    html,
    body {
        height: 100%;
    }

    .excel-style .layui-form-label {
        text-align: left !important;
        padding-left: 0 !important;
    }

    .excel-style .layui-inline {
        margin: 1% 0;
        height: 5%;
    }

    #noExcel {
        width: 100%;
        height: 90%;
        display: none;
        justify-content: center;
        align-items: center;
    }
</style>

<body>
    <div style="height:97%;width: 100%;" class="excel-style">
        <div class="layui-inline" id="monthSelect">
            <label class="layui-form-label">请选择年份</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="year" placeholder="请选择年份">
            </div>
        </div>
        <div id="luckysheet" style="width:100%;height:93%;display: none;">
        </div>
        <div id="noExcel">无部门预算信息</div>
    </div>
    <script>
        //监听window是否全屏，并进行相应的操作,支持esc键退出
        window.onresize = function () {
            var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen ||
                document.msFullscreenElement || document.fullscreenElement
            );//!document.webkitIsFullScreen都为true。因此用!!
            if (isFull == false) {
                $("#exitFullScreen").css("display", "none");
                $("#fullScreen").css("display", "");
            } else {
                $("#exitFullScreen").css("display", "");
                $("#fullScreen").css("display", "none");
            }
        }
    </script>
    <script>
        var date = new Date();
        var lockFlag; // 回复状态
        var excelData; // excel数据源
        var flag = false // 是否可编辑状态
        var id; // 任务id
        var userId; // 用户ID
        var nowDate = date.getFullYear(); // 当前时间
        $(function () {
            userId = JSON.parse(window.top.sessionStorage.userInfo).loginNo
            getData(nowDate)
        })
        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;
            var htmlStr = ''
            laydate.render({
                elem: '#year'
                , value: nowDate
                , type: 'year', done: function (value, date, endDate) {
                    nowDate = value
                    getData(value)
                }
            });
        })

        function getData(data) {
            window.luckysheet.destroy();
            util.ajaxJson("数据加载中", ctx + '/market/preTask/load', {
                "funcCode": '1000',
                "sendYear": data,
                "sts": '0'
            }, function (res) {
                if (res.result == 0) {
                    if (res.body[0].config.authority) {
                        res.body[0].config.authority.sheet = 0
                    }
                    excelData = res.body
                    id = res.body[0].taskId
                    lockFlag = res.body[0].isRec
                    $('#luckysheet').show()
                    $('#noExcel').hide()
                    initExcel(res.body)
                } else {
                    $('#noExcel').css({ display: 'flex' })
                    $('#luckysheet').hide()
                    window.top.$vm.$message({
                        message: res.desc,
                        type: 'info'
                    })
                }
            });
        }
        function initExcel(data) {
            try {
                luckysheet.create({
                    container: 'luckysheet',
                    data: data,
                    showsheetbar: false, // 底部sheet显示/隐藏
                    showstatisticBar: false, // 底部计数
                    showinfobar: true, // 是否显示顶部信息栏
                    allowUpdate: false,
                    showtoolbar: (lockFlag == '1' || lockFlag == '3' ) ? false : true, //是否显示工具栏
                    allowEdit: flag, // 是否允许前台编辑
                    forceCalculation: true, // 强制刷新公式
                    column: 12,
                    title: data[0].fileName,
                    row: 10,
                    lang: 'zh',
                    showstatisticBarConfig: {
                        count: false, // 计数栏
                        view: false, // 打印视图
                        zoom: false, // 缩放
                    },
                    functionButton: '<button id="updates" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">开启编辑</button> <button id="saves" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;">回复</button><button id="save" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">保存</button><button id="exports" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">导出</button><button id="goBack" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">返回</button><button id="fullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">全屏显示</button><button id="exitFullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;display:none">退出全屏</button>',
                    cellRightClickConfig: {
                        copy: true, // 复制
                        copyAs: false, // 复制为
                        paste: true, // 粘贴
                        insertRow: true, // 插入行
                        insertColumn: true, // 插入列
                        deleteRow: true, // 删除选中行
                        deleteColumn: true, // 删除选中列
                        deleteCell: false, // 删除单元格
                        hideRow: false, // 隐藏选中行和显示选中行
                        hideColumn: true, // 隐藏选中列和显示选中列
                        rowHeight: false, // 行高
                        columnWidth: false, // 列宽
                        clear: false, // 清除内容
                        matrix: false, // 矩阵操作选区
                        sort: false, // 排序选区
                        filter: false, // 筛选选区
                        chart: false, // 图表生成
                        image: false, // 插入图片
                        link: false, // 插入链接
                        data: false, // 数据验证
                        cellFormat: false, // 设置单元格格式
                        mComplete: false,
                        mCetup: false,
                        mRevoke: false
                    },
                    hook: {
						cellRenderBefore: function (r, c){
							var numlength = data[0].data[0].length-1;
							if(c.c == numlength){
								return false
							}
						},
                        cellMousedown: function (a, b, c) {
                            if (lockFlag == '1' || lockFlag == '3' ) {
                                // window.top.$vm.$message({
                                //     message: '只能查看',
                                //     type: 'info'
                                // })
                            } else {
                                if (flag == false) {
                                    window.top.$vm.$message({
                                        message: '请先点击开启编辑按钮',
                                        type: 'info'
                                    })
                                } else {
                                    if (b.c == 0 && luckysheet.getSheet().powerFlag == '0') {
                                        window.top.$vm.$message({
                                            message: '当前列为模板预留列不可填写',
                                            type: 'info'
                                        })
                                        return false
                                    }
                                }
                            }
                        },//绑定鼠标事件
                        cellUpdateBefore: function (r, c) {
                            // c坐标 r行坐标
                            if (c == luckysheet.getSheet().data[r].length - 1 && luckysheet.getSheet().powerFlag == '0') {
                                return false
                            }
                            if (luckysheet.getSheet().readOnly != '') {
                                if (luckysheet.getSheet().data[r][luckysheet.getSheet().data[r].length - 1] != null && luckysheet.getSheet().data[r][luckysheet.getSheet().data[r].length - 1].v !== userId) {
                                    window.top.$vm.$message({
                                        message: '您无权修改当前行',
                                        type: 'info'
                                    })
                                    return false
                                }
                            } else {
                                if (luckysheet.getSheet().data[r][luckysheet.getSheet().data[r].length - 1] != null) {
                                    window.top.$vm.$message({
                                        message: '您无权修改当前行',
                                        type: 'info'
                                    })
                                    return false
                                }
                            }
                        },
                    },
                })
            } catch (err) {
                console.log(err)
            }
            if (lockFlag == '1' || lockFlag == '3' ) {
                $('#saves').hide()
                $('#save').hide()
                $('#updates').hide()
                $('#exports').show()
            } else {
                if (flag) {
                    $('#saves').show()
                    $('#save').show()
                    $('#updates').hide()
                } else {
                    $('#saves').hide()
                    $('#save').hide()
                    $('#updates').show()
                }

            }
            initHandel()
        }
        function initHandel() {
            $("#fullScreen").on("click", function () {
                fullScreen();
            })
            //退出全屏
            $("#exitFullScreen").on("click", function () {
                exitFullscreen();
            })
            $('#goBack').off("click").click(function () {
                window.top.$vm.$router.push('/leader')
            })
            $('#exports').click(function () {
                exportExcel(luckysheet.getluckysheetfile(), luckysheet.getWorkbookName())
            })
            $('#updates').off("click").click(function () {
                util.ajaxJson("", ctx + '/market/preTask/lock', {
                    taskId: id
                }, function (data) {
                    if (data.result == 1) {
                        window.top.$vm.$message({
                            message: data.desc,
                            type: 'info'
                        })
                    } else {
                        $('#saves').show()
                        $('#save').show()
                        $('#updates').hide()
                        flag = true
                        getData(nowDate)
                    }
                });
            })
            $('#save').off('click').click(function () {
                var sheetName = luckysheet.getSheet().name
                var data = luckysheet.getSheet(sheetName)
                data.fileName = luckysheet.getWorkbookName()
                $('#saves').hide()
                $('#updates').show()
                data.id = id
                util.ajaxJson("正在加载中", ctx + '/market/preTask/saveInfo', {
                    data: data
                }, function (data) {
                    flag = false
                    getData(nowDate)
                });
            })
            // 保存
            $('#saves').off("click").click(function () {
                //  当前工作表名
                var sheetName = luckysheet.getSheet().name
                var data = luckysheet.getSheet(sheetName)
                data.fileName = luckysheet.getWorkbookName()
                $('#saves').hide()
                $('#updates').show()
                data.id = id
                util.ajaxJson("正在加载中", ctx + '/market/preTask/save', {
                    data: data
                }, function (data) {
                    if (data.result == 0) {
                        flag = false
                        window.top.$vm.$router.push('/leader')
                    } else {
                        window.top.$vm.$message({
                            message: data.desc,
                            type: 'info'
                        })
                    }
                });
            })
        }
    </script>
</body>

</html>
