<!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/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%;
	}
</style>

<body>
	<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 style="display: flex;height: 93%;width: 100%;" id="excelBox">
		<div id="luckysheet" style="width:50%;height:100%">
		</div>
		<div style="width:50%;height:100%">
			<iframe src="./text.html" id="complete" height="100%" width="100%" frameborder="0"></iframe>
		</div>
	</div>
	<script>
		var date = new Date();
		var month = date.getMonth()
		if (month >= 1 && month <= 9) {
			month = "0" + (month + 1);
		}
		var nowDate = date.getFullYear() + '-' + month
		$(function () {
			getData(nowDate)
		})
		layui.use(['laydate', 'form'], function () {
			var laydate = layui.laydate;
			var form = layui.form;
			var htmlStr = ''
			laydate.render({
				elem: '#year'
				, value: nowDate
				, type: 'month', done: function (value, date, endDate) {
					$('#complete')[0].contentWindow.postMessage({
						module: "aaa",
						month: value
					}, 'http://10.230.26.15:8000');
					nowDate = value
					getData(value)
				}
			});
		})
		var flag = false
		var excelData;
		function getData(nowDate) {
			window.luckysheet.destroy();
			util.ajaxJson("", ctx + '/market/keywork/load', {
				"id": '',
				"opMonth": nowDate,
				"sts": '0'
			}, function (res) {
				if (res.result == 0) {
					if (res.body[0].config.authority) {
						res.body[0].config.authority = {}
					}
					initExcel(res.body)
					excelData = res.body

				}
			});
		}
		var datas = [
			{
				"position": {
					"column": [
						2,
						4
					],
					"row": [
						1,
						6
					]
				},
				"id": null,
				"name": "11",
				"color": "",
				"status": "1",
				"order": "0",
				"opMonth": null,
				"data": [
					[
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null
					],
					[
						null,
						null,
						{
							"m": null,
							"ct": null,
							"v": null,
							"bl": null,
							"fs": null,
							"ff": null,
							"bg": "#00ff00",
							"fc": null,
							"un": null,
							"it": null,
							"ht": null
						},
						null,
						{
							"m": null,
							"ct": null,
							"v": null,
							"bl": null,
							"fs": null,
							"ff": null,
							"bg": "#00ff00",
							"fc": null,
							"un": null,
							"it": null,
							"ht": null
						},
						null,
						null,
						null,
						null,
						null,
						null,
						null
					],
					[
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null
					],
					[
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null
					],
					[
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null,
						null
					]
				],
				"config": {
					"rowlen": null,
					"customHeight": null,
					"merge": null,
					"authority": {
						selectLockedCells: 1, //选定锁定单元格
						selectunLockedCells: 0, //选定解除锁定的单元格
						formatCells: 0, //设置单元格格式
						formatColumns: 0, //设置列格式
						formatRows: 0, //设置行格式
						insertColumns: 0, //插入列
						insertRows: 0, //插入行
						insertHyperlinks: 0, //插入超链接
						deleteColumns: 0, //删除列
						deleteRows: 0, //删除行
						sort: 0, //排序
						filter: 0, //使用自动筛选
						usePivotTablereports: 0, //使用数据透视表和报表
						editObjects: 0, //编辑对象
						editScenarios: 0, //编辑方案
						hintText: "当前工作表正在编辑中",
						sheet: 1
					}
				}
			}
		]
		function initExcel(data) {
			try {
				luckysheet.create({
					container: 'luckysheet',
					mComplete: function () {
						//  选取范围坐标
						var position = luckysheet.getRange()[0]
						//  当前表所有数据
						for (var i = position.row[0]; i <= position.row[1]; i++) {
							for (var j = position.column[0]; j <= position.column[1]; j++) {
								if (JSON.stringify(luckysheet.getSheet().data[i][j]) != '{}' && JSON.stringify(luckysheet.getSheet().data[i][j]) != 'null') {
									console.log(luckysheet.getSheet().data[i][j])
									luckysheet.setCellValue(i, j, { bg: "#00ff00" })
									luckysheet.setCellValue(i, j, { bg: "#00ff00" })
									excelData[0].data[i][j].complete = '1'
									excelData[0].data[i][j].revoke = '2'
								}
							}
						}

					},
					mSetup: function () {
						//  选取范围坐标
						var position = luckysheet.getRange()[0]
						//  当前表所有数据
						for (var i = position.row[0]; i <= position.row[1]; i++) {
							for (var j = position.column[0]; j <= position.column[1]; j++) {
								excelData[0].data[i][j].setup = '1'
							}
						}

					},
					mRevoke: function () {
						//  选取范围坐标
						var position = luckysheet.getRange()[0]
						//  当前表所有数据
						for (var i = position.row[0]; i <= position.row[1]; i++) {
							for (var j = position.column[0]; j <= position.column[1]; j++) {
								if (JSON.stringify(luckysheet.getSheet().data[i][j]) != '{}' && luckysheet.getSheet().data[i][j].m) {
									luckysheet.setCellValue(i, j, { bg: "" })
									luckysheet.setCellValue(i, j, { bg: "" })
									excelData[0].data[i][j].bg = ''
									excelData[0].data[i][j].revoke = '1'
								}
							}
						}

					},
					data: data,
					title: data[0].fileName || '默认模板',
					showsheetbar: false, // 底部sheet显示/隐藏
					showstatisticBar: false, // 底部计数
					myFolderUrl: false,
					showinfobar: true,
					allowUpdate: false,
					allowEdit: flag, // 是否允许前台编辑
					column: 12,
					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;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: true,
						mSetup: true,
						mRevoke: true
					},
					hook: {
						cellMousedown: function (a, b, c) {
							if (flag == false) {
								window.top.$vm.$message({
									message: '请先点击开启编辑按钮',
									type: 'info'
								})
							}
						},//绑定鼠标事件
					},
				})
			} catch (err) {
				console.log(err)
			}
			if (flag) {
				$('#saves').show()
				$('#updates').hide()
			} else {
				$('#saves').hide()
				$('#updates').show()
			}
			$('#updates').off('click').click(function () {
				util.ajaxJson("", ctx + '/market/keywork/lock', {
					id: luckysheet.getSheet().id
				}, function (data) {
					if (data.result == 1) {
						window.top.$vm.$message({
							message: data.desc,
							type: 'info'
						})
					} else {
						flag = true
						getData(1)
						$('#saves').show()
						$('#updates').hide()
					}

				});
			})
			// 保存
			$('#saves').off('click').click(function () {
				//  当前工作表名
				var sheetName = luckysheet.getSheet().name
				var data = luckysheet.getSheet(sheetName)
				$('#saves').hide()
				$('#updates').show()
				flag = false
				data.fileName = luckysheet.getWorkbookName()
				util.ajaxJson("正在加载中", ctx + '/market/keywork/save', {
					data: data
				}, function (data) {
					if (data.result == 0) {
						$('#complete')[0].contentWindow.postMessage({
							module: "aaa",
							month: nowDate
						}, 'http://114.215.71.182:8080');
					}
					getData(1)
				});
			})
		}
	</script>
</body>

</html>