<template>
	<div class="container" v-loading="loading">
		<fullscreen :fullscreen.sync="fullscreen" class="container-box">
			<div class="titbox">
				<h2 class="font-ui">科室绩效回复</h2>
				<div>
					<i class="el-icon-refresh" @click="iconCli(1)"></i>
					<i class="el-icon-full-screen" @click="iconCli(2)"></i>
				</div>
			</div>
			<div style="height: 100%">
				<div style="overflow: none">
					<el-date-picker v-model="monthValue" @change="checkMonth" value-format="yyyy-MM" type="month"
						placeholder="选择月">
					</el-date-picker>
					<el-select v-model="value" @change="checkDecpt" clearable placeholder="请选择部门">
						<el-option v-for="item in options" :key="item.receiveName" :label="item.receiveDeptName"
							:value="item.receiveDeptCode">
						</el-option>
					</el-select>
					<div style="float: right">
						<el-button v-if="dataBody.task.sts == '0' && dataBody.rows.length > 0" size="small"
							@click="submit" type="primary">回复</el-button>
						<el-button v-if="dataBody.task.sts == '0' && dataBody.rows.length > 0" size="small"
							@click="save" type="primary">保存</el-button>
						<el-button v-if="dataBody.rows.length > 0" size="small" type="primary" @click="exportExcel">导出
						</el-button>
						<el-button size="small" @click="goBack" type="primary">返回</el-button>
					</div>
					<!-- <el-upload
            v-if="dataBody.task.sts == '0'"
            :disabled="value == '' ? true : false"
            style="float: right"
            class="upload-demo"
            action="http://192.168.2.170:9600/spfm/market/kpitemps/addTemp"
            :before-remove="beforeRemove"
            :before-upload="beforeUploadForm"
            :http-request="fileChange"
            :show-file-list="false"
            :file-list="fileList"
          >
            <el-button
              v-if="dataBody.task.sts == '0'"
              size="small"
              @click="isDisabled"
              type="primary"
              >点击上传</el-button
            >
          </el-upload> -->
				</div>

				<div v-if="dataBody.rows.length > 0" id="tabbox">
					<div style="width: 30%; margin: 15px auto">
						<el-input readonly v-model="dataBody.table.tabName"></el-input>
					</div>
					<table class="dataBody-box" border-collapse="collapse" border="1">
						<tbody>
							<tr v-for="(item, index) in dataBody.rows" :key="index">
								<td @contextmenu="showMenu(index, zindex)" class="cell" v-for="(row, zindex) in item"
									:key="zindex" :rowspan="row.rowSpan" :colspan="row.colSpan"
									v-show="row.displayFlag == '0'">
									<div v-show="false">
										{{ (dataBody.rows[index][zindex].colNum = zindex) }}
										{{ (dataBody.rows[index][zindex].rowNum = index) }}
									</div>
									<el-input :disabled="dataBody.task.sts != '0'" class="table-input"
										v-model="row.cellVal"></el-input>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="center-style" v-else>请选择需要查看的绩效</div>
			</div>
			<vue-context-menu :contextMenuData="contextMenuData" @deleteHang="deleteHang" @addHangUp="addHangUp">
			</vue-context-menu>
		</fullscreen>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				disabled: true,
				options: [],
				value: "",
				monthValue: "",
				cellWidth: {
					width: "auto"
				},
				isMove: false,
				autoStyle: {},
				xindex: "",
				yindex: "",
				dataBody: {
					rows: [],
					table: {
						opName: "",
						opNo: "",
						opTime: "",
						opTimeFrom: "",
						opTimeTo: "",
						tabCode: "",
						tabCodePks: "",
						tabName: "",
					},
					task: {
						sts: 1,
					},
				},
				contextMenuData: {
					menuName: "demo",
					//菜单显示的位置
					axis: {
						x: null,
						y: null,
					},
					//菜单选项
					menulists: [{
							fnHandler: "deleteHang", //绑定事件
							btnName: "删除行", //菜单名称
						},
						{
							fnHandler: "addHangUp", //绑定事件
							btnName: "插入行", //菜单名称
						},
					],
				},
				hangIndex: "",
				lieIndex: "",
				fullscreen: false,
				loading: false,
				fileList: [],
			};
		},
		methods: {
			isDisabled() {
				if (this.value == "") {
					this.$message({
						message: "请先选择部门!",
						type: "warning",
					});
					return false;
				}
			},
			// 回复
			submit() {
				this.loading = true;
				this.$http({
					url: "/market/kpidepts/saveInfo",
					method: "post",
					headers: {
						"Content-Type": "application/json",
					},
					data: this.dataBody,
				}).then((res) => {
					if (res.data.result == 0) {
						this.$message({
							message: "回复成功",
							type: "success",
						});
						this.getInitData(this.value);
					}
					this.loading = false;
				});
			},
			beforeUploadForm(file) {
				// 验证文件类型
				var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
				const extension = testmsg === "xlsx";
				if (!extension) {
					this.$message({
						message: "上传文件只能是excel格式!",
						duration: 1000,
						showClose: true,
						type: "warning",
					});
					return false;
				}
			},
			// 提交文件
			fileChange(param, type) {
				let formData = new FormData();
				formData.append("file", param.file);
				formData.append("tabCode", "");
				this.loading = true;
				this.$http({
						url: "/market/kpitemps/addTemp",
						method: "post",
						headers: {
							"Content-Type": "application/json",
						},
						data: formData,
					})
					.then((res) => {
						if (res.data.result == 0) {
							this.closeMenu = true;
							this.$message({
								message: "上传成功",
								type: "success",
							});
							this.dataBody = res.data.body;
							this.$forceUpdate();
						}
						this.loading = false;
					})
					.catch((err) => {
						this.loading = false;
					});
			},
			beforeRemove(file, fileList) {
				// return this.$confirm(`确定移除 ${file.name}？`);
			},
			exportExcel() {
				this.$http({
					url: "/market/kpidepts/downloadKpi",
					method: "get",
					headers: {
						"Content-Type": "application/json",
					},
					responseType: "blob",
					params: {
						tabInstId: this.dataBody.tabInstId
					},
				}).then((response) => {
					if (window.navigator && window.navigator.msSaveOrOpenBlob) {
						let blob = new Blob([response.data], {
							type: "application/vnd.ms-excel",
						});
						window.navigator.msSaveOrOpenBlob(
							blob,
							this.dataBody.table.tabName + ".xlsx"
						);
					} else {
						/* 火狐谷歌的文件下载方式 */
						var blob = new Blob([response.data]);
						var downloadElement = document.createElement("a");
						var href = window.URL.createObjectURL(blob);
						downloadElement.href = href;
						downloadElement.download = this.dataBody.table.tabName + ".xlsx";
						document.body.appendChild(downloadElement);
						downloadElement.click();
						document.body.removeChild(downloadElement);
						window.URL.revokeObjectURL(href);
					}
				});
			},
			imports() {},
			checkDecpt(val) {
				if (val == "") {
					this.dataBody.rows = [];
				} else {
					this.getInitData(val);
				}
			},
			checkMonth(val) {
				this.$http({
					url: "/market/kpidepts/loadReceiver",
					method: "post",
					headers: {
						"Content-Type": "application/json",
					},
					data: {
						kpiMonth: val
					},
				}).then((res) => {
					if (res.data.result == 0) {
						this.options = res.data.body;
						if (res.data.body.length > 0) {
							this.value = res.data.body[0].receiveDeptCode;
							this.getInitData(res.data.body[0].receiveDeptCode);
						} else {
							this.value = "";
							this.dataBody.rows = [];
						}
					} else {
						this.value = "";
						this.dataBody.rows = [];
					}
				});
			},
			goBack() {
				this.$router.push("/leader");
			},
			//功能栏
			iconCli(v) {
				if (v === 1) {
					this.getList(this.params, this.pageSize);
				}
				if (v === 2) {
					this.fullscreen = !this.fullscreen;
				}
			},
			showMenu(index, zindex) {
				this.hangIndex = index;
				this.lieIndex = zindex;
				event.preventDefault();
				var x = event.clientX;
				var y = event.clientY;
				// Get the current location
				this.contextMenuData.axis = {
					x,
					y,
				};
			},
			// 保存
			save() {
				this.loading = true;
				this.$http({
					url: "/market/kpidepts/save",
					method: "post",
					headers: {
						"Content-Type": "application/json",
					},
					data: this.dataBody,
				}).then((res) => {
					if (res.data.result == 0) {
						this.loading = false;
						this.$message({
							message: "操作成功",
							type: "success",
						});
					}
				});
			},
			// 向上增加行
			addHangUp() {
				let itemArr = [];
				for (let i = 0; i < this.hangIndex; i++) {
					this.dataBody.rows[i].map((item) => {
						if (
							this.hangIndex <= parseInt(item.rowSpan) + i - 1 &&
							parseInt(item.rowSpan) > 1
						) {
							item.rowSpan = parseInt(item.rowSpan) + 1;
						}
					});
				}
				itemArr = JSON.parse(JSON.stringify(this.dataBody.rows[this.hangIndex]));
				itemArr.map((item) => {
					item.rowSpan = "1";
					item.cellVal = "";
				});
				this.dataBody.rows.splice(this.hangIndex, 0, itemArr);
			},
			// 删除行
			deleteHang() {
				if (
					parseInt(this.dataBody.rows[this.hangIndex][this.lieIndex].rowSpan) > 1
				) {
					this.$message({
						message: "当前行有合并项不能删除",
						type: "wanrning",
					});
					return;
				}
				let num = 0;
				this.dataBody.rows[this.hangIndex].map((item, index) => {
					if (parseInt(item.rowSpan) > 1) {
						this.dataBody.rows[parseInt(this.hangIndex) + 1][index].displayFlag =
							"0";
						this.dataBody.rows[parseInt(this.hangIndex) + 1][index].rowSpan =
							parseInt(item.rowSpan) - 1;
					} else if (item.displayFlag == "1") {
						for (let i = this.hangIndex; i >= 0; i--) {
							num = 0;
							this.dataBody.rows[i].map((item, zindex) => {
								if (item.displayFlag == "0") {
									num++;
								}
								if (
									parseInt(item.rowSpan) > 1 &&
									zindex == index &&
									this.hangIndex <= parseInt(item.rowSpan) + i - 1
								) {
									item.rowSpan = parseInt(item.rowSpan) - 1;
								}
							});
							if (num == this.dataBody.rows[i].length) {
								return;
							}
						}
					}
				});
				this.dataBody.rows.splice(this.hangIndex, 1);
			},
			getInitData(val) {
				this.loading = true;
				this.$http({
					url: "/market/kpidepts/load",
					method: "post",
					headers: {
						"Content-Type": "application/json",
					},
					data: {
						kpiMonth: this.monthValue,
						receiveDeptCode: val,
						tempType: "0",
						subType: "a",
					},
				}).then((res) => {
					if (res.data.result == 0) {
						this.dataBody = res.data.body;
					} else {
						this.$message({
							message: "无绩效数据",
							type: "success",
						});
						this.dataBody.rows = [];
					}
					this.loading = false;
				});
			},
		},
		mounted() {},
	};
</script>
<style>
	#tabbox .el-input.is-disabled .el-input__inner {
		color: black;
		background-color: white;
	}

	.btn-wrapper-simple {
		height: auto !important;
	}
</style>
<style lang="scss" scoped>
	#tabbox {
		height: 75%;
		overflow-y: auto;
	}

	.dataBody-box {
		border-collapse: collapse;
		margin-top: 20px;

		.cell {
			position: relative;
		}
	}

	.fcous {
		border-color: red;
	}

	.center-style {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}

	.auto-dom {
		width: 3px;
		opacity: 0;
		position: absolute;
		right: 0;
		top: 0;
		z-index: 1000;
		cursor: pointer;
		height: 100%;
	}
</style>>
<style scoped lang="scss">
	.btn-default {
		display: inline;
		margin-left: 10px;
	}

	.titbox {
		margin-bottom: 20px;

		div {
			float: right;

			i {
				font-size: 22px;
				margin-left: 20px;
				cursor: pointer;
			}
		}
	}

	.tabbox {
		margin-top: 15px;
	}

	.pageBox {
		text-align: right;
		margin-top: 10px;
	}
</style>
