<!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/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="/marketfront/static/js/luckysheet_old/plugins/js/plugin.js?v=1.0.0"></script>
    <script src="/marketfront/static/js/luckysheet_old/luckysheet.umd.js?v=1.0.0"></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/exportExcel.js"></script>
    <script src="/static/js/fullScreen.js"></script>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    html,
    body {
        height: 100%;
    }

    #monthSelect {
        padding: 10px 0px;
    }
</style>

<body style="background: #fff;">
<div class="layui-inline" id="monthSelect" style="width: 100%">
    <div style="float: right;padding-right: 10px" id="topBtn"></div>
</div>
<div style="height:90%;width: 100%;">
    <div id="luckysheet" style="width:100%;height:100%">
    </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 id = '' //  模板id 查看/修改load时候用
    var powerFlag = '0'; // 权限控制 0 开启 1关闭
    var excelId = '';
    var flag = false; // 控制excel是否可以编辑
    var adminFlag = false;
    var userId; // 用户ID
    var excelInfo = {};
    var isAdmin = '';
    var excelLevel = '';
    var toolBar = '';
    var type = '';
    var emptyExcel = false;
    var showToolBar = true;
    var fullScreen = false;
    $(function () {
        userId = JSON.parse(window.top.sessionStorage.userInfo).loginNo
    })

    //增加完权限之后解除新增按钮的锁定
    function enableUpload() {
        document.getElementById("enableUpload")["disabled"] = false;
        document.getElementById("enableUpload").innerHTML = '新增';
    }
    //锁定新增按钮
    function disableUpload() {
        document.getElementById("enableUpload")["disabled"] = true;
        document.getElementById("enableUpload").innerHTML = '新增(请先设置权限)';
    }
    function enableSave() {
        document.getElementById("enableSave")["disabled"] = false;
        document.getElementById("enableSave").innerHTML = '保存';
    }

    function disableSave() {
        document.getElementById("enableSave")["disabled"] = true;
        document.getElementById("enableSave").innerHTML = '保存(请先设置权限)';
    }

    layui.use(['laydate', 'form'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        form.on('select(powerFlag)', function (data) {
            powerFlag = data.value
        })

        // 获取数据
        function getData() {
            // 先销毁excel重新加载
            if (window.luckysheet) {
                window.luckysheet.destroy();
            }
            //拿参数的
            var r = window.top.location.href.split('?')[1];
            var excelId = r.split('&')[1].split('=')[1];
            type = r.split('&')[0].split('=')[1];
            console.log("id:" + excelId);
            console.log("type:" + type);
            toolBar = '';
            var url = '/market/cIncomeExcelView/getExcelInfo';
            util.ajaxJson("数据加载中", ctx + url,
                {
                    "excelId": excelId ? excelId : '', "type": type
                }, function (res) {
                    if (res.result == 0) {
                        // 查看和修改时 下拉框赋值
                        excelInfo = res.body.excelInfo;
                        excelLevel = res.body.excelLevel;
                        isAdmin = res.body.isExcelAdmin;
                        emptyExcel = res.body.emptyExcel;
                        console.log(type);
                        if (isAdmin) {
                            toolBar += '<span style="color: red">*</span><span style="padding:0px 6px;font-size: 16px;">模板类型</span><input type="text" id="templateType" style="padding:0px 6px;width: 100px;height: 25px">';
                            toolBar += '<button id="share" class="btn btn-primary btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;margin-left:10px;width: 80px">权限设置</button>'
                            toolBar += '<input id="imports" type="file" class="btn btn-primary" style="padding:3px 6px;font-size: 14px;margin-right: 10px;width: 180px"></input>';
                            toolBar += '<button id="fullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;">全屏显示</button>' +
                                '<button id="exitFullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;display:none">退出全屏</button>';
                            //toolBar += '<button id="exports" class="btn btn-primary btn-danger" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">导出</button>';
                            if (type == '1') {
                                toolBar += '<button id="enableUpload" class="btn btn-primary btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;" disabled>新增(请先设置权限)</button>';
                            } else if (type == '2') {
                                window.parent['setAuth'](excelId);
                                toolBar += '<button id="enableSave" class="btn btn-primary btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;">保存</button>';
                            }
                        }
                        if (type == '3' || type == '4') {
                            toolBar =  '<button id="fullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;">全屏显示</button>' +
                                '<button id="exitFullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;display:none">退出全屏</button>';
                            showToolBar = false;
                        }
                        if (type == '5') {
                            toolBar =  '<button id="fullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;">全屏显示</button>' +
                                '<button id="exitFullScreen" class="btn btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;display:none">退出全屏</button>';
                            toolBar += '<button id="toAudit" class="btn btn-primary btn-primary" style="padding:3px 6px;font-size: 16px;margin-right: 10px;">提交</button>';
                        }
                        //toolBar += '<button id="saves" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;">保存</button>';
                        var eJson = res.body.excelJson;
                        var cellData = luckysheet.transToCellData(eJson[0].data);
                        eJson[0].celldata = cellData;
                        $("#topBtn").html(toolBar);
                        initExcel(res.body.excelJson);
                    }
                });
            initExcel()
        }

        // 表格初始化
        function initExcel(data, name) {
            if (type == '1') {
                flag = true
            } else {
                flag = true
            }
            if (isAdmin) {
                adminFlag = true;
            }
            var fileName = excelInfo.name;
            if ($.trim(name) != '') {
                fileName = name;
            }
            try {
                luckysheet.create({
                    container: 'luckysheet',
                    data: data,
                    showsheetbar: false, // 底部sheet显示/隐藏
                    showsheetbarConfig: {
                        add: false, //新增sheet
                        menu: false, //sheet管理菜单
                        sheet: false //sheet页显示
                    },
                    showstatisticBar: false, // 底部计数
                    showtoolbar: showToolBar,
                    myFolderUrl: '',
                    showtoolbarConfig: {
                        undoRedo: showToolBar, //撤销重做，注意撤消重做是两个按钮，由这一个配置决定显示还是隐藏
                        paintFormat: showToolBar, //格式刷
                        currencyFormat: showToolBar, //货币格式
                        percentageFormat: showToolBar, //百分比格式
                        numberDecrease: showToolBar, // '减少小数位数'
                        numberIncrease: showToolBar, // '增加小数位数
                        moreFormats: showToolBar, // '更多格式'
                        font: showToolBar, // '字体'
                        fontSize: showToolBar, // '字号大小'
                        bold: showToolBar, // '粗体 (Ctrl+B)'
                        italic: showToolBar, // '斜体 (Ctrl+I)'
                        strikethrough: showToolBar, // '删除线 (Alt+Shift+5)'
                        underline: showToolBar, // '下划线 (Alt+Shift+6)'
                        textColor: showToolBar, // '文本颜色'
                        fillColor: showToolBar, // '单元格颜色'
                        mergeCell: showToolBar, // '合并单元格'
                        horizontalAlignMode: showToolBar, // '水平对齐方式'
                        verticalAlignMode: showToolBar, // '垂直对齐方式'
                        textWrapMode: showToolBar, // '换行方式'
                        textRotateMode: showToolBar, // '文本旋转方式'
                        image: showToolBar, // '插入图片'
                        link: showToolBar, // '插入链接'
                        chart: showToolBar, // '图表'（图标隐藏，但是如果配置了chart插件，右击仍然可以新建图表）
                        postil: showToolBar, //'批注'
                        pivotTable: showToolBar,  //'数据透视表'
                        frozenMode: showToolBar, // '冻结方式'
                        sortAndFilter: showToolBar, // '排序和筛选'
                        conditionalFormat: showToolBar, // '条件格式'
                        dataVerification: showToolBar, // '数据验证'
                        splitColumn: showToolBar, // '分列'
                        screenshot: showToolBar, // '截图'
                        findAndReplace: showToolBar, // '查找替换'
                        protection: showToolBar, // '工作表保护'
                        print: showToolBar, // '打印'
                        border: false, // '边框'
                        function: false, // '公式'
                    },
                    showinfobar: true, // 顶部信息 文件名称 按钮
                    allowUpdate: false, // 是否允许操作表格后的后台更新 在线文档websocket时为true
                    allowEdit: flag, // 是否允许前台编辑
                    title: fileName, // 文件名称
                    lang: 'zh',
                    enableAddRow: adminFlag,
                    showstatisticBar: false, // 底部计数
                    showstatisticBarConfig: {
                        count: false, // 计数栏
                        view: false, // 打印视图
                        zoom: false, // 缩放
                    },
                    functionButton: '',
                    cellRightClickConfig: {
                        copy: false, // 复制
                        copyAs: false, // 复制为
                        paste: false, // 粘贴
                        insertRow: adminFlag, // 插入行
                        insertColumn: adminFlag, // 插入列
                        deleteRow: adminFlag, // 删除选中行
                        deleteColumn: adminFlag, // 删除选中列
                        deleteCell: false, // 删除单元格
                        hideRow: false, // 隐藏选中行和显示选中行
                        hideColumn: false, // 隐藏选中列和显示选中列
                        rowHeight: true, // 行高
                        columnWidth: true, // 列宽
                        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: {
                        cellMousedown: function (a, b, c) {

                        },
                        cellUpdateBefore: function (r, c) {

                        },
                        cellMousedownBefore: function (a, p, s) {

                        },
                        rangePasteBefore: function (range, data) {

                        },
                    },
                })
            } catch (err) {
                console.log(err)
            }
            if (type == '2') {
                $("#templateType").val(excelInfo.typeName);
            }
            initHandel();
        }

        // 绑定按钮事件
        function initHandel() {
            $("#fullScreen").on("click", function () {
                if (fullScreen){
                    fullScreen = false;
                    $("#fullScreen").html('全屏显示');
                } else {
                    fullScreen = true;
                    $("#fullScreen").html('退出全屏');
                }
                window.parent['fullScreen']('true');
                //fullScreen();
                $('#monthSelect').show()
            })
            //退出全屏
            $("#exitFullScreen").on("click", function () {
                exitFullscreen();
            })
            //分享
            $('#share').off("click").click(function () {
                if (fullScreen){
                    fullScreen = false;
                    $("#fullScreen").html('全屏显示');
                    window.parent['fullScreen']('true');
                }
                window.parent['showAuthDialog']('true');
                if (type == '1') {
                    disableUpload();
                } else if (type == '2') {
                    disableSave();
                }
                //realExitFullScreen();
            });
            //修改
            $('#enableSave').off("click").click(function () {
                var excelId = $(window.parent.document).find("#excelId").attr("value");
                var sheetName = luckysheet.getSheet().name;
                var excelData = luckysheet.getSheet(sheetName);
                var templateType = $("#templateType").val();
                var excelName = luckysheet.toJson().title;
                if ($.trim(templateType) == '') {
                    window.top.$vm.$message({
                        message: '请输入模板类型',
                        type: 'info'
                    })
                    return;
                }
                //调用vue页面方法执行新增/修改
                window.parent['addNewTemplate'](excelName, excelData, templateType);
            });
            //新增
            $('#enableUpload').off("click").click(function () {
                var excelId = $(window.parent.document).find("#excelId").attr("value");
                var sheetName = luckysheet.getSheet().name;
                var excelData = luckysheet.getSheet(sheetName);
                var templateType = $("#templateType").val();
                var excelName = luckysheet.toJson().title;
                if ($.trim(templateType) == '') {
                    window.top.$vm.$message({
                        message: '请输入模板类型',
                        type: 'info'
                    })
                    return;
                }
                //调用vue页面方法执行新增/修改
                window.parent['addNewTemplate'](excelName, excelData, templateType);
            });
            $('#toAudit').off("click").click(function () {
                var excelId = $(window.parent.document).find("#excelId").attr("value");
                var sheetName = luckysheet.getSheet().name;
                var excelData = luckysheet.getSheet(sheetName);
                var excelName = luckysheet.toJson().title;
                //调用vue页面方法执行提审
                window.parent['openAuditWindow'](excelName, excelData);
            });
            // 导出
            $('#exports').click(function () {
                var excelName = luckysheet.toJson().title;
                exportExcel(luckysheet.getluckysheetfile(), excelName)
            })
            // 导入
            $("#imports").change(function (evt) {
                var files = evt.target.files;
                if (files == null || files.length == 0) {
                    window.top.$vm.$message({
                        message: '没有文件导入',
                        type: 'info'
                    })
                    return;
                }
                var name = files[0].name;
                var suffixArr = name.split("."),
                    suffix = suffixArr[suffixArr.length - 1];
                if (suffix != "xlsx") {
                    window.top.$vm.$message({
                        message: '目前只支持xlsx文件的导入',
                        type: 'info'
                    })
                    return;
                }
                // 解析文件
                LuckyExcel.transformExcelToLucky(
                    files[0],
                    function (exportJson, luckysheetfile) {
                        console.log(exportJson);
                        if (exportJson.sheets == null || exportJson.sheets.length == 0) {
                            window.top.$vm.$message({
                                message: '无法读取excel文件的内容，当前不支持xls文件',
                                type: 'info'
                            })
                            return;
                        }
                        // 销毁原来的excel
                        window.luckysheet.destroy();
                        // 重新加载
                        var a = exportJson.sheets[0];
                        a.name = '';
                        initExcel([a], name.replace('.xlsx', ''));
                        // 绑定按钮事件
                        initHandel()
                    }
                );
            });
        }

        getData()
    });

    //退出全屏，而不是切换
    function realExitFullScreen() {
        $('#monthSelect').show()
        //$("#exitFullScreen").css("display", "");
        $("#fullScreen").css("display", "show");
        $('.excel-style').css('width', '100%')
        $('.excel-style').css('height', '97%')
        var isFullscreen = document.fullScreenElement //W3C
            ||
            document.msFullscreenElement //IE
            ||
            document.mozFullScreenElement //火狐
            ||
            document.webkitFullscreenElement //谷歌
            ||
            false;
        if (isFullscreen) {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.msExitFullscreen) {
                document.msExitFullscreen();
            } else if (document.mozCancelFullScreen) {
                document.mozCancelFullScreen();
            } else if (document.webkitCancelFullScreen) {
                document.webkitCancelFullScreen();
            }
        }
    }

</script>
</body>

</html>
