<!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>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    html,
    body {
        height: 100%;
    }

    .select-box {
        width: 300px;
        padding: 10px;
        height: 5%;
    }
</style>

<body>
    <div style="height:97%;width: 100%;">
        <div class="layui-inline">
            <label class="layui-form-label">年月选择器</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="year" placeholder="yyyy">
            </div>
        </div>
        <div id="luckysheet" style="width:100%;height:95%">
        </div>
    </div>
    <script>
        var lockFlag;
        var roterId = ''
        var id = '';
        var opations = []
        $(function () {
            lockFlag = getQueryString('status').replace(/'/g, '')
            roterId = getQueryString('id')
            if (typeof (JSON.parse(decodeURIComponent(roterId))) == 'number') {
                id = JSON.parse(decodeURIComponent(roterId))
            } else {
                id = JSON.parse(decodeURIComponent(roterId))[0].taskId
            }
            opations = JSON.parse(decodeURIComponent(roterId))
            getData(id)
        })
        layui.use(['laydate', 'form'], function () {
            var laydate = layui.laydate;
            var form = layui.form;
            var htmlStr = ''
            laydate.render({
                elem: '#year'
                , type: 'year', done: function (value, date, endDate) {
                    console.log(value)
                }
            });
        })
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.top.location.href.split('?')[1].match(reg);
            if (r != null) return decodeURI(r[2]); return null;
        }
        var excelId = ""
        var excelData;
        var flag = false
        function getData(id) {
            window.luckysheet.destroy();
            util.ajaxJson("", ctx + '/market/preTask/load', {
                "id": id,
                "opMonth": "",
                "sts": '0'
            }, function (res) {
                if (res.result == 0) {
                    if (res.body[0].config.authority) {
                        res.body[0].config.authority.sheet = 0
                    }
                    excelData = res.body
                    initExcel(res.body)
                    if (excelId == "") {
                        excelId = res.body[0].id
                    }
                }
            });
        }
        function initExcel(data) {
            if (lockFlag == 'add' || lockFlag == 'update') { // 新增模板过来直接编辑
                flag = true
            }
            try {
                luckysheet.create({
                    container: 'luckysheet',
                    data: data,
                    showsheetbar: false, // 底部sheet显示/隐藏
                    showstatisticBar: false, // 底部计数
                    myFolderUrl: false,
                    showinfobar: true,
                    allowUpdate: false,
                    allowEdit: flag, // 是否允许前台编辑
                    column: 12,
                    title: data[0].fileName,
                    row: 10,
                    lang: 'zh',
                    showstatisticBar: false, // 底部计数
                    myFolderUrl: false,
                    showstatisticBarConfig: {
                        count: false, // 计数栏
                        view: false, // 打印视图
                        zoom: false, // 缩放
                    },
                    showinfobar: true,
                    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="goBack" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">返回</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
                    },
                    hook: {
                        cellMousedown: function (a, b, c) {
                            if (lockFlag == 'see') {
                                // 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) {
                            if (c == 0 && luckysheet.getSheet().powerFlag == '0') {
                                return false
                            }
                            if (luckysheet.getSheet().readOnly != '') {
                                var updateList = luckysheet.getSheet().readOnly.split(',')
                                if (updateList.indexOf(luckysheet.getSheet().data[r][0].v) == -1) {
                                    window.top.$vm.$message({
                                        message: '您无权修改当前行',
                                        type: 'info'
                                    })
                                    return false
                                }
                            } else {
                                if (luckysheet.getSheet().data[r][0] != null) {
                                    window.top.$vm.$message({
                                        message: '您无权修改当前行',
                                        type: 'info'
                                    })
                                    return false
                                }
                            }
                        },
                    },
                })
            } catch (err) {
                console.log(err)
            }
            if (lockFlag == 'see') {
                $('#saves').hide()
                $('#save').hide()
                $('#updates').hide()
            } else {
                if (flag) {
                    $('#saves').show()
                    $('#save').show()
                    $('#updates').hide()
                } else {
                    $('#saves').hide()
                    $('#save').hide()
                    $('#updates').show()
                }

            }
            initHandel()
        }
        function initHandel() {
            $('#goBack').off("click").click(function () {
                window.top.$vm.$router.push('/leader')
            })
            $('#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(id)
                    }
                });
            })
            $('#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(id)
                });
            })
            // 保存
            $('#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) {
                    flag = false
                    window.top.$vm.$router.push('/leader')
                });
            })
        }
    </script>
</body>

</html>