<!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 class="layui-input-inline">
        <form class="layui-form mc-work-form">
            <label> 地市可编辑列：从 </label><input type="text" id="editFrom" style="width: 100px"
                                            onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')"></input> 到 <input
            type="text" id="editTo" style="width: 100px"
            onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')"></input>
        </form>
    </div>
    <label style="color: red;float: right;padding-right: 10px">提示：请在第三列填写地市</label>
</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 lockFlag = 'see';
    util.ajaxJson("", ctx + "/market/cadvLedger/getRoleByCode", {}, function (res) {
        console.log("html" + res.result);
        if (res.result == 1) {
            lockFlag = 'add';//省级
        } else {
            lockFlag = 'updata';//管理员
        }
    });
    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 = '';
    $(function () {
        userId = JSON.parse(window.top.sessionStorage.userInfo).loginNo
    })
    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];
            toolBar = '';
            var url = '/market/cKeyChannelCoopExcel/getExcelInfo';
            util.ajaxJson("数据加载中", ctx + url, {
                "id": excelId ? excelId : '',
            }, function (res) {
                if (res.result == 0) {
                    // 查看和修改时 下拉框赋值
                    excelInfo = res.body.excelInfo;
                    excelLevel = res.body.excelLevel;
                    isAdmin = res.body.isExcelAdmin;
                    $("#editFrom").val(excelInfo.editConStart);
                    $("#editTo").val(excelInfo.editConEnd);
                    console.log(type);
                    if (type == '2') {
                        if (isAdmin) {
                            toolBar += '<input id="imports" type="file" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;"></input>';
                        }
                        toolBar += '<button id="saves" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;">保存</button>';
                    }
                    if (isAdmin) {
                        toolBar += '<button id="exports" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">导出</button>';
                    }
                    toolBar += '<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>';
                    var eJson = res.body.excelJson;
                    var cellData = luckysheet.transToCellData(eJson[0].data);
                    eJson[0].celldata = cellData;
                    initExcel(res.body.excelJson);
                }
            });
            initExcel()
        }

        // 表格初始化
        function initExcel(data) {
            if (type == '1') {
                flag = false
            } else {
                flag = true
            }
            if (isAdmin) {
                adminFlag = true;
            }
            try {
                luckysheet.create({
                    container: 'luckysheet',
                    data: data,
                    showsheetbar: false, // 底部sheet显示/隐藏
                    showsheetbarConfig: {
                        add: false, //新增sheet
                        menu: false, //sheet管理菜单
                        sheet: false //sheet页显示
                    },
                    showstatisticBar: false, // 底部计数
                    showtoolbar: false,
                    showinfobar: true, // 顶部信息 文件名称 按钮
                    allowUpdate: false, // 是否允许操作表格后的后台更新 在线文档websocket时为true
                    allowEdit: flag, // 是否允许前台编辑
                    title: excelInfo.name, // 文件名称
                    lang: 'zh',
                    enableAddRow: adminFlag,
                    showstatisticBar: false, // 底部计数
                    showstatisticBarConfig: {
                        count: false, // 计数栏
                        view: false, // 打印视图
                        zoom: false, // 缩放
                    },
                    functionButton: toolBar,
                    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) {
                            // if (b.c == luckysheet.getSheet().data[b.r].length - 1) {
                            //     return false
                            // }
                        },//绑定鼠标事件
                        cellUpdateBefore: function (r, c) {
                            console.log('执行事件');
                            // c坐标 r行坐标
                            // if(lockFlag == 'updata'){
                            // 	return true
                            // }
                            //      if (c == luckysheet.getSheet().data[r].length - 1) {
                            // if(luckysheet.getSheet().data[r][c] != null ){
                            // 	return false
                            // }
                            //      }
                            // var editConStart = excelInfo.editConStart;
                            // var editConEnd = excelInfo.editConEnd;
                            // //console.log(r + '---' + c)
                            // if ($.trim(editConStart) != '' && $.trim(editConEnd) != '' && isAdmin == false) {
                            //     editConStart = editConStart - 1;
                            //     editConEnd = editConEnd - 1;
                            //     // console.log(editConStart + '---' + editConEnd);
                            //     // console.log(c < editConStart);
                            //     // console.log(c > editConEnd);
                            //     // console.log(c == 3);
                            //     if (c < editConStart || c > editConEnd || c == 2) {
                            //         window.top.$vm.$message({
                            //             message: '您无权修改当前列',
                            //             type: 'info'
                            //         });
                            //         return false;
                            //     }
                            // }
                            if (r == 0 && isAdmin == false) {
                                window.top.$vm.$message({
                                    message: '您无权修改当前行',
                                    type: 'info'
                                });
                                return false;
                            }
                        },
                        cellMousedownBefore: function (a, p, s) {
                            if (flag == false) {
                                window.top.$vm.$message({
                                    message: '查看模式无法编辑',
                                    type: 'info'
                                });
                                return false;
                            } else {
                                //r行，c列
                                var r = p.r;
                                var c = p.c;
                                // var editConStart = excelInfo.editConStart;
                                // var editConEnd = excelInfo.editConEnd;
                                // //console.log(r + '---' + c)
                                // if ($.trim(editConStart) != '' && $.trim(editConEnd) != '' && isAdmin == false) {
                                //     editConStart = editConStart - 1;
                                //     editConEnd = editConEnd - 1;
                                //     // console.log(editConStart + '---' + editConEnd);
                                //     // console.log(c < editConStart);
                                //     // console.log(c > editConEnd);
                                //     // console.log(c == 3);
                                //     if (c < editConStart || c > editConEnd || c == 2) {
                                //         window.top.$vm.$message({
                                //             message: '您无权修改当前列',
                                //             type: 'info'
                                //         });
                                //         return false;
                                //     }
                                // }
                                if (r == 0 && isAdmin == false) {
                                    window.top.$vm.$message({
                                        message: '您无权修改当前行',
                                        type: 'info'
                                    });
                                    return false;
                                }
                            }
                        },
                        rangePasteBefore: function (range, data) {
                            //禁止选区粘贴
                            return false;
                        },
                    },
                })
            } catch (err) {
                console.log(err)
            }
            if (isAdmin == false) {
                $('#monthSelect').hide()
            } else {
                $('#monthSelect').show()
            }
            initHandel()
        }

        // 绑定按钮事件
        function initHandel() {
            $("#fullScreen").on("click", function () {
                fullScreen();
            })
            //退出全屏
            $("#exitFullScreen").on("click", function () {
                exitFullscreen();
            })
            // 保存
            $('#saves').off("click").click(function () {
                //  当前工作表名
                var r = window.top.location.href.split('?')[1];
                var excelId = r.split('&')[1].split('=')[1];
                var sheetName = luckysheet.getSheet().name;
                var data = luckysheet.getSheet(sheetName);
                var urlsave = '/market/cKeyChannelCoopExcel/saveExcel';
                var editFrom = $("#editFrom").val();
                var editTo = $("#editTo").val();
                // if (editTo < editFrom) {
                //     window.top.$vm.$message({
                //         message: '可编辑列的起始列不能大于结束列',
                //         type: 'error'
                //     });
                //     return;
                // }
                if (isAdmin && ($.trim(editFrom) == '' || $.trim(editTo) == '')) {
                    window.top.$vm.$message({
                        message: '可编辑列不可为空',
                        type: 'error'
                    });
                    return;
                }
                util.ajaxJson("正在加载中", ctx + urlsave, {
                    data: data,
                    id: excelId,
                    editConStart: editFrom,
                    editConEnd: editTo
                }, function (data) {
                    if (data.result == 1) {
                        window.top.$vm.$message({
                            message: data.desc,
                            type: 'error'
                        })
                    } else {
                        console.log(data);
                        getData()
                    }
                });
            })
            // 导出
            $('#exports').click(function () {
                exportExcel(luckysheet.getluckysheetfile(), excelInfo.name)
            })
            // 导入
            $("#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])
                        // 绑定按钮事件
                        initHandel()
                    }
                );
            });
        }

        getData()
    })

</script>
</body>

</html>
