<template>
	<lay-container fluid="true" class="content-box">
		<lay-table v-if="dataList.length>0" :columns="dataColumn" :data-source="dataList">
			<template v-slot:operator="{ data }">
				<lay-button size="xs" type="primary" @click="_getTeamworkView(data,1)">
					查看
				</lay-button>
				<lay-button size="xs" type="primary" @click="_getTeamworkView(data,2)">
					导出
				</lay-button>
			</template>
		</lay-table>
		<lay-form class="dataInfo">
			<div>
				<lay-button v-if="dataInfoObj.FCanEdit!=2" type="danger" @click="submitClick()">确认</lay-button>
				<lay-button v-if="dataInfoObj.FCanEdit!=2" @click="cancelClick">取消</lay-button>
				<p class="note">备注：所有视图填写完成，请确认流程完成，流程完成后不允许修改</p>
			</div>
		</lay-form>
	</lay-container>
	<lay-layer area="90%" v-model="showEditBox" title="视图查看">
		<ShowView v-if="showEditBox" :viewList="viewList" :viewHeight="viewHeight"></ShowView>
	</lay-layer>
	<OperateLog v-if="dataInfoObj.FTeamID" ref="OperateLog" :dataInfoObj="dataInfoObj"></OperateLog>
</template>

<style scoped>
	.content-box {
		margin: 30px 10px;
		display: block;
		position: relative;
		clear: both;
		float: none;
		min-height: 400px;
	}

	.dataInfo {
		margin-top: 40px;
		text-align: center;
	}

	.note {
		color: #999;
		font-size: 12px;
		margin: 10px auto;
	}
</style>
<script>
	import {
		ref
	} from 'vue';
	import {
		getTeamworkView
	} from "/src/api/api/teamwork";
	import {
		getBasicRoleList
	} from "/src/api/api/user";
	import {
		sureTeamWork
	} from "/src/api/api/task";
	import {
		getDataModel
	} from "/src/api/api/common";
	import ShowView from '../../Team/components/ShowView.vue';

	export default {
		components: {
			ShowView
		},
		setup() {
			const dataList = ref([]);
			let dataColumn = [{
				title: "视图",
				key: "name",
			},
				{
					title: "操作",
					key: "operator",
					width: "148px",
					customSlot: "operator",
					align: "center"
				}
			];
			const teamInfoObj = ref({});
			//总共有几种视图类型
			const viewTypeCount = ref(5);
			const showEditBox = ref(false);
			const viewList = ref([]);
			const viewHeight = ref(500);
			return {
				dataColumn,
				dataList,
				viewTypeCount,
				showEditBox,
				viewList,
				viewHeight
			}
		},
		props: {
			dataInfoObj: {
				type: Object,
				default: () => {},
			}
		},
		mounted() {
			this._getViewList();
			console.log(this.dataInfoObj);
		},
		methods: {
			//获取视图
			async _getViewList() {
				this.teamInfoObj = await getDataModel({
					FID: this.dataInfoObj.FTeamID
				}, "Teamwork");
				let result = await getBasicRoleList({
					FRoleType: 39,
					FRoleSet: "true"
				}) || [];
				let dataList = [];
				let hasType = this.teamInfoObj.FViewType;
				result.forEach((item) => {
					let viewData = dataList.find(s => s.name == item.FName) || null;
					if (viewData == null) {
						if (hasType.indexOf(item.FCode) != -1) {
							viewData = {
								name: item.FName,
								seeCode: "",
								outCode: ""
							};
							if (item.FRemark.indexOf("查看") != -1) viewData.seeCode = item.FCode;
							if (item.FRemark.indexOf("导出") != -1) viewData.outCode = item.FCode;
							dataList.push(viewData);
						}
					} else {
						if (item.FRemark.indexOf("查看") != -1) viewData.seeCode = item.FCode;
						if (item.FRemark.indexOf("导出") != -1) viewData.outCode = item.FCode;
					}
				});
				this.dataList = dataList;
			},
			async _getTeamworkView(data, type) {
				let viewType = parseInt(type == 1 ? data.seeCode : data.outCode);
				if (type == 2) viewType = viewType - this.viewTypeCount;
				let postData = {
					FTeamID: this.dataInfoObj.FTeamID,
					FViewType: viewType,
					HalfId: this.dataInfoObj.FMaterialHalfIDs,
					FType: type
				};
				postData.FFinish = this.dataInfoObj.FCanEdit == 2 ? 1 : 0;
				let idx = layer.load(2);
				let result = await getTeamworkView(postData);
				layer.close(idx);
				if (type == 1) {
					let list = [];
					let count = 0;
					for (let key in result) {
						list.push(result[key]);
						count += result[key].length;
					};
					if (count == 0) {
						layer.msg("请稍等，视图信息还在编辑中", {
							time: 2000,
							icon: 3
						});
					} else {
						let height = document.documentElement.clientHeight || document.body.clientHeight || 1000;
						this.viewList = list;
						this.viewHeight = Math.ceil(height * 0.6);
						this.showEditBox = true;
					}
				} else {
					window.open(result);
				}
			},
			submitClick() {
				let message = "流程完成之后视图不允许修改，您确认完成吗？";
				let $this = this;
				layer.confirm(message, {
					title: "提示",
					btn: [{
						text: '确定',
						callback: function(id) {
							$this._submitData(id);
						}
					}, {
						text: '取消',
						callback: function(id) {
							layer.close(id);
						}
					}]
				});
			},
			async _submitData(layId) {
				layer.close(layId);
				let result = await sureTeamWork(this.dataInfoObj.FTeamID);
				if (result > 0) {
					layer.msg('操作成功', {
						icon: 1,
						time: 3000
					});
					this.cancelClick(true);
				} else {
					layer.msg('还有事项未完成', {
						icon: 3,
						time: 3000
					});
				}
			},
			cancelClick(isRefresh) {
				isRefresh = isRefresh == undefined ? false : isRefresh;
				this.$emit('cancelClick', isRefresh);
			}
		}
	}
</script>
