<template>
	<view class="topBtn">
		<u-subsection bg-color="rgb(215,224,233)" button-color="rgb(0,99,208)" active-color="white" :list="list"
			v-model="current" @change="subChange"></u-subsection>
		<view class="btn">
			<u-button size="medium" @click="onClose">关闭</u-button>
		</view>
	</view>
	<view class="handleFile" v-show="current === 0">

		<!-- 左侧视图 -->
		<view style="height: 100%;width: 65%;">
			<leftView></leftView>
		</view>

		<!-- 右侧视图 -->
		<scroll-view scroll-y="true">
			<u-form :model="form" ref="form1">
				<view style="text-align: start;font-size: 34rpx;font-weight: 700;">领导意见:</view>
				<u-form-item prop="suggest" :borderBottom="false"><u-input type="textarea" v-model="form.suggest" border
						height="400" /></u-form-item>
			</u-form>
			<view style="display: flex;">
				<view style="flex: 1;">

				</view>
				<u-button @click="confirm" style="margin-right: 40rpx;height: 70rpx;">保存</u-button>
				<u-button @click="confirm" style="height: 70rpx;">常用语</u-button>
			</view>
			<view style="text-align: start;font-size: 34rpx;font-weight: 700;line-height: 80rpx;">
				操作
			</view>
			<view class="selected">
				<view style="width: 100%;">
					<my-segmented-control :current="czcurrent" :values="fwBtn" @clickItem="czsubChange" />
				</view>
				<!-- 				<view style="padding: 20rpx;background-color: #f2f2f2;" v-show="!show">
					<view style="display: flex;line-height: 80rpx;padding: 10rpx;">
						<span style="color: #2d68ea;">转处室，直属领导：</span>
						<u-button v-for="(item,index) in zcszsdwList" :type="zcszsdwIndex == index ?'primary' :''" class="lybut"
							size="mini" @click="zcszsdwClick(index)">{{item.value}}</u-button>
						<view style="flex: 1;">
							<u-button style="float: right;margin-top: 10rpx;" size="mini" @click="openRyTree">更多</u-button>
							<section-ry-tree-list ref="sectionRyTreeList"></section-ry-tree-list>
						</view>
					</view>
					<view style="background-color: #fff;height: 100rpx;">

					</view>
				</view>
				<view class="" v-show="show">
					已阅后，本件转入已办件！
				</view> -->
				<view style="height: 300rpx;align-items: center;">
					<view style="line-height: 300rpx;font-size: 30rpx">
						{{flowData.CODEFUNCTITLE}}
						<u-button v-show="showCxxzBut" style="display: inline-block;line-height: 70rpx;height: 70rpx;"
							type="primary" @click="openRyTree">重新选择</u-button>
					</view>
					<section-ry-tree-list ref="sectionRyTreeList"></section-ry-tree-list>
				</view>
				<view class="">
					<u-button type="primary" style="height: 70rpx;width: 400rpx;" @click="openRyTree">提交</u-button>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		ref,
		reactive,
		nextTick
	} from "vue"
	import leftView from "./leftView.vue"
	import mySegmentedControl from "../../components/mySegmentedControl/mySegmentedControl.vue"
	import sectionRyTreeList from "./sectionRyTreeList.vue"
	import {
		abaseQueryData,
		queryFormeditSignByCopyrightid,
		flowdirectionQueryData,
		queryDataSendByTarget,
		saveDataSend,
		saveDataOpinion
	} from "../api/informationApi.js"
	const s4 = new SM4Util()

	export default {
		components: {
			leftView,
			mySegmentedControl,
			sectionRyTreeList
		},
		onLoad(e) {
			//this.urlParams = e.url
			this.urlParams = {
				docmode: 12,
				guid: 'ZjNjOWU1Nj',
				directionid: 'Flow_19dq6vo_87f37f8f-955a-44ff-a531-4675deb7063d',
				routeinfoid: '1225074972981907456',
				routeid: '1225084527690305536',
				formeditid: 'FW_WFH_V_07',
				lastrouteinfoid: '1225072376867115008',
				copyrightid: '87f37f8f-955a-44ff-a531-4675deb7063d',
				dotype: '1',
				type: 'todo',
				lastReturnPageno: '1',
				flowid: 'AA08',
				dotype: '1',
				url: 'pages/handleFile/handleFile'
			}
			let data = {
				"ckey": "FW_WFH",
				"id": 0,
				"directionid": this.urlParams.directionid,
				"formeditid": this.urlParams.formeditid,
				"routeid": this.urlParams.routeid,
				"routeinfoid": this.urlParams.routeinfoid
			}
			abaseQueryData(data).then(res => {
				this.queryData = res.data
				console.log(this.queryData)
				this.fwBtn = []
				this.fwInfo = []
				let fwBtn = res.data["FW_WFH.FUNCLIST"]
				for (let i = 0; i < fwBtn.length; i++) {
					if (fwBtn[i].ENDNODETYPE == 2) {
						this.fwBtn.push(fwBtn[i].CNAME)
						this.fwInfo.push(fwBtn[i])
					}
				}
				if (this.fwInfo.length > 0) {
					this.doFlow(this.fwInfo[0])
				}
			})
		},
		data() {
			return {
				queryData: {},
				urlParams: {},
				show: true,
				list: [{
						name: '办理'
					},
					{
						name: '流程图'
					}
				],
				zcszsdwIndex: -1,
				zcszsdwList: [{
						value: "处室正职",
						ry: ["张三", "李四", "王五"]
					},
					{
						value: "直属单位正职",
						ry: ["张三", "王五"]
					}
				],
				current: 0,
				czcurrent: 0,
				form: {
					suggest: ""
				},
				rules: {
					suggest: [{
						required: true,
						message: '请输入意见',
					}]
				},
				fwBtn: [],
				fwInfo: [],
				flowData: {
					SENDERRORS: {}
				},
				forwardUser: [],
				flowDataList: [],
				activeDirection: {}
			};
		},
		computed: {
			showCxxzBut() {
				return this.flowData.ENDNODETYPE != 1 && this.flowData.CODEFUNCBUTTON != 'SENDCOPYEND' && this.flowData
					.CODEFUNCBUTTON !=
					'SENDPERUSAL' && this.flowData.CODEFUNCBUTTON != 'SENDCOMMIT' && this.flowData.SENDERRORS.MESSAGE == ''
			}
		},
		created() {

		},
		mounted() {
			this.$refs.form1.setRules(this.rules)
		},
		methods: {
			openRyTree() {
				this.$refs.sectionRyTreeList.open()
			},
			onClose() {
				uni.reLaunch({
					url: this.urlParams.url
				})
			},

			confirm() {
				this.$refs.form1.validate((valid) => {
					if (valid) {
						saveDataOpinion({
							"cmanid": "0c67561f-5d0d-48fd-a90b-90d69a98fbac", //userid
							"routeinfoid": this.urlParams.routeinfoid,
							"routeid": this.urlParams.routeid,
							"tableid": "FW_WFH",
							"colid": "WLDSSL",
							"opiniontype": 0,
							"cresult": "",
							"opinion": this.form.suggest,
							"opinionimage": ""
						}).then(res => {
							uni.showToast({
								icon: "success",
								title: "保存成功"
							})
						})
					}
				})
			},
			zcszsdwClick(index) {
				this.zcszsdwIndex = index
			},

			subChange() {},

			czsubChange(e) {
				this.doFlow(this.fwInfo[e.currentIndex])
			},
			doFlow(fwInfo) {
				this.flowData = {
					SENDERRORS: {}
				}
				queryFormeditSignByCopyrightid({
					"copyrightid": this.urlParams.copyrightid
				}).then(res => {
					//console.log("data1", res)
				})
				flowdirectionQueryData({
					"directionid": this.urlParams.directionid
				}).then(res => {
					//console.log("data2", res)
				})
				queryDataSendByTarget({
					"codefuncbutton": fwInfo.CODE, //--
					"directionid": this.urlParams.directionid,
					"directionidtarget": fwInfo.DIRECTIONIDTARGET, //--
					"userid": "0c67561f-5d0d-48fd-a90b-90d69a98fbac",
					"routeinfoid": this.urlParams.routeinfoid,
					"routeid": this.urlParams.routeid
				}).then(res => {
					Object.assign(this.flowData, res.data[0])
					console.log(this.flowData)
					if (this.flowData.SENDERRORS && this.flowData.SENDERRORS.MESSAGE != "") {
						this.flowData.CODEFUNCTITLE = this.flowData.SENDERRORS.MESSAGE;
						return
					}

					if (this.flowData.ENDNODETYPE == 1 || this.flowData.CODEFUNCBUTTON == "SENDCOPYEND" || this.flowData
						.CODEFUNCBUTTON == "SENDPERUSAL" || this.flowData.CODEFUNCBUTTON == "SENDCOMMIT") {} else if (this
						.flowData.ENDNODETYPE == 3) {
						//弹窗
						// self.showContent = true;
						// self.forwardUser = [];
						// this.flowDataList = this.flowData["DIRECTION.CHILD"];
						// $.each(this.flowDataList, function(index, item) {
						// 	item.forwardUser = index;
						// 	self.forwardUser.push([])
						// 	item.people1 = item.SENDUSERS.USERS.USERS; //人员
						// 	item.people1.forEach(function(item) {
						// 		item.show = item.USERS.length > 0;
						// 		item.childshow = false;
						// 		item.USERS.forEach(function(item1) {
						// 			item1.show = true;
						// 		})
						// 	})
						// 	item.people2 = item.SENDUSERS.USERSALL.USERS; //人员
						// 	item.people2.forEach(function(item) {
						// 		item.show = true;
						// 		item.childshow = false;
						// 		item.USERS.forEach(function(item1) {
						// 			item1.show = true;
						// 		})
						// 	})
						// 	item.people = JSON.parse(JSON.stringify(item.people1));
						// })
						// self.activeDirection = this.flowDataList[0]
						// self.roleList = self.activeDirection.SENDUSERS.USERS.ROLES;
					} else {
						this.forwardUser = []
						this.flowDataList = [];
						this.flowDataList.push(this.flowData)
						for (let i = 0; i < this.flowDataList.length; i++) {
							let item = this.flowDataList[0]
							item.forwardUser = i;
							this.forwardUser.push([])
							item.people1 = item.SENDUSERS.USERS.USERS; //人员
							item.people1.forEach(function(item) {
								item.show = item.USERS.length > 0;
								item.childshow = false;
								item.USERS.forEach(function(item1) {
									item1.show = true;
								})
							})
							item.people2 = item.SENDUSERS.USERSALL.USERS; //人员
							item.people2.forEach(function(item) {
								item.show = true;
								item.childshow = false;
								item.USERS.forEach(function(item1) {
									item1.show = true;
								})
							})
							item.people = JSON.parse(JSON.stringify(item.people1));
						}
						this.activeDirection = this.flowDataList[0]
						this.roleList = this.flowData.SENDUSERS.USERS.ROLES;
						let n = 0;
						this.flowDataList[0].people1.forEach(function(item) {
							n = n + item.USERS.length;
						})
						if (n == 1) { //如果推荐人员唯一时，缺省直接赋值转发，可以重新选择
							this.showContent = false;
							this.adduserAll(2);
							var usernameStr = "";
							this.flowDataList[0].people1.forEach(function(item) {
								if (item.USERS.length == 1) {
									usernameStr = item.USERS[0].USERNAME
								}
							})
							this.flowData.CODEFUNCTITLE = "确认后将转发【" + usernameStr + "】";
						} else {
							//弹窗
							this.showContent = true;
						}
					}
				})

			},

			adduserAll() {
				this.activeDirection.people.forEach((item, index) => {
					item.USERS.forEach((item1, index1) => {
						if (item1.show == true) {
							this.forwardUser[this.activeDirection.forwardUser].push(item1);
							item1.checked = false;
							item1.show = false;
						}
					})
				})
				this.forwardUser[this.activeDirection.forwardUser].forEach(function(x, y) {
					x.type = y == 0 ? 1 : 2
				})
			},

			submit() {
				console.log(this.flowDataList)
				console.log(this.forwardUser)
				let sendUser = []
				if (this.flowData.ENDNODETYPE == 1) {
					sendUser.push({
						DIRECTIONID: this.flowData.DIRECTIONID,
						MAINUSERID: "STOP",
						COPYUSERID: ""
					})
				} else if (this.flowData.CODEFUNCBUTTON == "SENDCOPYEND" || this.flowData.CODEFUNCBUTTON ==
					"SENDPERUSAL" || this.flowData.CODEFUNCBUTTON == "SENDCOMMIT") {
					sendUser.push({
						DIRECTIONID: this.flowData.DIRECTIONID,
						MAINUSERID: "",
						COPYUSERID: ""
					})
				} else {
					let n = 0,
						text = [];
					this.flowDataList.forEach((item) => {
						if (this.forwardUser[item.forwardUser].length > 0) {
							n++
							sendUser.push({
								DIRECTIONID: item.DIRECTIONID,
								MAINUSERID: this.forwardUser[item.forwardUser].filter(function(item1) {
									return item1.type == 1
								}).map(function(item1) {
									return item1.USERID
								}).join("|"),
								COPYUSERID: this.forwardUser[item.forwardUser].filter(function(item1) {
									return item1.type == 2
								}).map(function(item1) {
									return item1.USERID
								}).join("|")
							})
						} else {
							text.push(item.CNAME)
						}
					})
					if (n == 0) {
						// 未选择人员，请选择至少一项人员！
						return
					}
					}
				let data = {
					"codefuncbutton": this.flowData.CODEFUNCBUTTON,
					"directionid": this.urlParams.directionid,
					"userid": "0c67561f-5d0d-48fd-a90b-90d69a98fbac",
					"routeinfoid": this.urlParams.routeinfoid,
					"routeid": this.urlParams.routeid,
					"data": JSON.stringify(sendUser)
				}
				saveDataSend(data).then(res => {
					console.log(res)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	body {
		padding: 100rpx 50rpx;
		box-sizing: border-box;
	}

	.topBtn {
		width: calc(100vw - 100rpx);
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		//margin-bottom: 20rpx;
		border-bottom: 1rpx solid #cccccc;

		.u-subsection {
			width: 600rpx;
		}

		.btn {
			display: flex;
			align-items: center;
			justify-content: center;

			uni-button {
				margin-left: 20rpx;
			}
		}
	}

	.content {
		background-color: #ebebeb;
		border-radius: 20rpx;
		width: calc(100vw - 100rpx);
		height: calc(100vh - 300rpx);
		text-align: center;
	}

	.handleFile {
		width: calc(100vw - 100rpx);
		height: calc(100vh - 300rpx);
		display: flex;
		justify-content: space-around;
		align-items: center;

		uni-scroll-view {
			width: 45vw;
			height: 100%;
			background-color: #fff;
			text-align: center;
			padding: 20rpx;
			box-sizing: border-box;
		}
	}

	.selected {
		height: 400rpx;
	}

	.mySelectTree {
		width: 400rpx;
	}

	.lybut {
		height: 50rpx;
		width: 200rpx;
		margin-top: 10rpx;
		margin-left: 20rpx;
	}
</style>