<!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="/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/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>
    <div style="background-color: white;">
        <div class="layui-inline" id="monthSelect">
            <label class="layui-form-label">模板类型</label>
            <div class="layui-input-inline">
                <form class="layui-form mc-work-form">
                    <select name="city" id="tempType" lay-filter="tempType">
                        <option value="0">年度预算</option>
                        <option value="1">季度预算</option>
                        <option value="2">月度预算</option>
                        <option value="3">采购预算</option>
                    </select>
                </form>
            </div>
        </div>
        <div class="layui-inline" id="monthSelect">
            <label class="layui-form-label">控制权限</label>
            <div class="layui-input-inline">
                <form class="layui-form mc-work-form">
                    <select name="city" lay-filter="powerFlag" id="powerFlag">
                        <option value="0">开启</option>
                        <option value="1">关闭</option>
                    </select>
                </form>
            </div>
        </div>
        <div class="layui-inline" id="monthSelect">
            <label class="layui-form-label">表头位置</label>
            <div class="layui-input-inline">
                <form class="layui-form mc-work-form">
                    <input type="text" name="actionUrl" id="excelPosition" ag-verify="required" autocomplete="off"
                        placeholder="表头位置" class="layui-input">
                </form>
            </div>
        </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 lockFlag; // 模板状态--see查看add新增updates修改
        var id = '' //  模板id 查看/修改load时候用
        var powerFlag = '0'; // 权限控制 0 开启 1关闭
        var excelId = '';
        var tempType = '0'; // 模板类型 0-年度预算  1-季度预算  2-月度预算  3-部门预算采购项目管理
        var flag = false // 控制excel是否可以编辑
        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;
            // 下拉监听事件
            form.on('select(powerFlag)', function (data) {
                powerFlag = data.value
            })
            // 下拉监听事件
            form.on('select(tempType)', function (data) {
                tempType = data.value
            })
            // 获取数据
            function getData() {
                // 先销毁excel重新加载
                if (window.luckysheet) {
                    window.luckysheet.destroy();
                }
                util.ajaxJson("数据加载中", ctx + '/market/pre/load', {
                    "id": id ? id : '',
                    "opMonth": "",
                    "sts": '0',
                }, function (res) {
                    if (res.result == 0) {
                        // 查看和修改时 下拉框赋值
                        if (id) {
                            powerFlag = res.body[0].powerFlag
                            tempType = res.body[0].tempType
                            $('#powerFlag').val(res.body[0].powerFlag)
                            $('#excelPosition').val(res.body[0].colHeaderNum)
                            $('#tempType').val(res.body[0].tempType)
                            form.render();
                        }
                        excelId = res.body[0].id
                        initExcel(res.body)
                    }
                });
            }
            // 获取url参数
            function getQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.top.location.href.split('?')[1].match(reg);
                if (r != null) return unescape(r[2]); return null;
            }
            // 表格初始化
            function initExcel(data) {
                if (lockFlag == 'add' || lockFlag == 'update') { // 新增模板过来直接编辑
                    flag = true
                }
                try {
                    luckysheet.create({
                        container: 'luckysheet',
                        data: data,
                        showsheetbar: false, // 底部sheet显示/隐藏
                        showstatisticBar: false, // 底部计数
                        showinfobar: true, // 顶部信息 文件名称 按钮
                        allowUpdate: false, // 是否允许操作表格后的后台更新 在线文档websocket时为true
                        allowEdit: flag, // 是否允许前台编辑
                        column: 12,
                        title: data[0].fileName, // 文件名称
                        row: 10,
                        lang: 'zh',
                        showstatisticBar: false, // 底部计数
                        showstatisticBarConfig: {
                            count: false, // 计数栏
                            view: false, // 打印视图
                            zoom: false, // 缩放
                        },
                        functionButton: '<input id="imports" type="file" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;"></input><button id="saves" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;">保存</button><button id="exports" class="btn btn-primary btn-danger" 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: false, // 隐藏选中列和显示选中列
                            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
                        }
                    })
                } catch (err) {
                    console.log(err)
                }
                if (lockFlag == 'see') { // 查看预算跳转过来
                    $('#saves').hide()
                    $('#imports').hide()
                }
                initHandel()
            }
            // 绑定按钮事件
            function initHandel() {
                $("#fullScreen").on("click", function () {
                    fullScreen();
                })
                //退出全屏
                $("#exitFullScreen").on("click", function () {
                    exitFullscreen();
                })
                // 返回
                $('#goBack').off('click').click(function () {
                    window.top.$vm.$router.push('/budgetList')
                })
                // 保存
                $('#saves').off("click").click(function () {
                    if (!$('#excelPosition').val() && !id) {
                        window.top.$vm.$message({
                            message: '请输入表头位置',
                            type: 'info'
                        })
                        return;
                    }
                    //  当前工作表名
                    var sheetName = luckysheet.getSheet().name
                    var data = luckysheet.getSheet(sheetName)
                    data.id = id ? id : excelId
                    data.fileName = luckysheet.getWorkbookName()
                    data.powerFlag = powerFlag
                    data.powerFlagDev = powerFlag == '0' ? '开' : '关'
                    data.tempType = tempType
                    data.colHeaderNum = $('#excelPosition').val()
                    data.tempTypeDesc = tempType == "0"
                        ? "年度预算"
                        : tempType == "1"
                            ? "季度预算"
                            : tempType == "2"
                                ? "月度预算"
                                : tempType == "3"
                                    ? "采购预算"
                                    : ""
                    util.ajaxJson("正在加载中", ctx + '/market/pre/save', {
                        data: data
                    }, function (data) {
                        if (data.result == 1) {
                            window.top.$vm.$message({
                                message: data.desc,
                                type: 'info'
                            })
                        } else {
                            flag = false
                            if (lockFlag == 'update') {
                                console.log(this)
                                getData()
                            } else if (lockFlag == 'recall') {
                                window.top.$vm.$router.push('/recallAdmin')
                            } else {
                                window.top.$vm.$router.push('/budgetList?isshow=1')
                            }
                        }
                    });
                })
                // 导出
                $('#exports').click(function () {
                    exportExcel(luckysheet.getluckysheetfile(), 'excel')
                })
                // 导入
                $("#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) {
                            if (exportJson.sheets == null || exportJson.sheets.length == 0) {
                                window.top.$vm.$message({
                                    message: '无法读取excel文件的内容，当前不支持xls文件',
                                    type: 'info'
                                })
                                return;
                            }
                            // 销毁原来的excel
                            window.luckysheet.destroy();
                            // 重新加载
                            initExcel([exportJson.sheets[0]])
                            // 绑定按钮事件
                            initHandel()
                        }
                    );
                });
            }
            lockFlag = getQueryString('status').replace(/'/g, '')
            id = getQueryString('id') ? getQueryString('id').replace(/'/g, '') : ''
            getData()
        })

    </script>
</body>

</html>