<template>
	<div>
		<div class="container">
			<div class="container-box">
				<div class="adv-type">
					<div v-for="item in typeList" :key="item.dataCode" @click="advmaterial(item)">
						<div v-if="(roleStatus <= 1 && item.dataName=='信息汇总') || (roleStatus < 4 && item.dataName=='信息填报')">
							<span><i class="el-icon-folder-opened"></i></span>
							<span>{{item.dataName}}</span>
						</div>

					</div>
				</div>
				<div class="adv-box">
					<div class="adv-tit" @click="jumpadv('1')" v-if="roleStatus < 3">
						<i class="el-icon-loading"></i>
						<span>进行中 </span>
					</div>
					<div class="adv-tit" @click="jumpadv('2')" v-if="roleStatus < 3">
						<i class="el-icon-notebook-1"></i>
						<span>已结束 </span>
					</div>
					<div class="adv-tit adv-add" @click="advadd" v-if="roleStatus == 0">
						<i class="el-icon-plus"></i>
						<span>申请</span>
					</div>
				</div>
			</div>
		</div>

		<el-dialog title="添加" :visible.sync="dialogStatus" width="50%" :destroy-on-close="true"
			:modal-append-to-body="false" :close-on-click-modal="false" :fullscreen="true">
			<el-form :model="infolist" ref="infolist" :rules="rules"
				style="height: calc(100vh - 180px);overflow-y: scroll;">
				<el-form-item prop="woName" class="info-line online">
					<span>统计名称</span>
					<el-input v-model="infolist.woName" placeholder="统计名称"></el-input>
				</el-form-item>

				<el-form-item prop="time" class="info-line online">
					<span>统计周期</span>
					<el-date-picker v-model="infolist.time" type="daterange"></el-date-picker>
				</el-form-item>

				<el-form-item class="info-line online">
				    <span>填报人</span>
				    <deptTree v-if="!disableStatus" :closeList="closeList" class="tree" @treeCheck="treeCheck">
				    </deptTree>
				    <div :class="!disableStatus?'tree treeUser':'tree treeUserb'">
				        <p v-for="(item,index) in treeList" :key="index">{{item.receiveName}}
				            <i @click="deletess(item,index)" class="el-icon-error"></i>
				        </p>
				    </div>
				</el-form-item>
				<el-form-item class="info-line online">
				    <span>负责人</span>
					<div class="tree treeUser" style="width: 30%;margin-right: 1%;">
					    <p v-for="(item,index) in deptList" :key="index" @click="choseDepta(item)">{{item.ou}}</p>
					</div>
					<div :class="!disableStatus?'tree treeUser':'tree treeUserb'" style="width: 30%;margin-right: 1%;">
					    <p v-for="(item,index) in treeListp" :key="index">
							<span>{{item.ou}}</span>
							<span class="p-btn" @click="fzperson(item)">负责人</span>
					        <i @click="deletes(item,index)" class="el-icon-error"></i>
					    </p>
					</div>
					<div :class="!disableStatus?'tree treeUser':'tree treeUserb'" style="width: 30%;">
					    <p v-for="(item,index) in treeListp" :key="index">{{item.loginNameStr}}</p>
					</div>
				</el-form-item>
			</el-form>
			<div style="text-align: right;margin-top: 20px">
				<el-button @click="dialogCli(1)" type="primary">确 定</el-button>
				<el-button @click="dialogCli(2)">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="选择负责人" :visible.sync="dialogStatusp" width="50%" :destroy-on-close="true"
			:modal-append-to-body="false" :close-on-click-modal="false">
			<el-form :model="infolist" ref="infolist" style="height: 50vh;overflow-y: scroll;">
				<div class="treeUserp">
				    <p v-for="(item,index) in treeListponly" :key="index" @click="chofz(item)" v-if="item.loginNameStr">
                        {{item.groupName}} -
                        {{item.loginNameStr}}
                    </p>
				</div>
			</el-form>
		</el-dialog>
	</div>
</template>
<script>
	import deptTree from "../../../components/deptTreeUser.vue"
	import deptTreep from "../../../components/deptTreeP.vue"
	export default {
		components: {
			deptTree,
			deptTreep
		},
		data() {
			const woName = (rule, value, callback) => {
				if (!this.infolist.woName) {
					callback(new Error('不能为空'))
				} else {
					callback()
				}
			}
			const time = (rule, value, callback) => {
				if (!this.infolist.time) {
					callback(new Error('不能为空'))
				} else {
					callback()
				}
			}
			return {
				rules: {
					woName: [{
						required: true,
						trigger: 'blur',
						validator: woName
					}],
					time: [{
						required: true,
						trigger: 'change',
						validator: time
					}]
				},
				typeList: [{
						dataCode: '1',
						dataName: '信息填报'
					},
					{
						dataCode: '2',
						dataName: '信息汇总'
					},
				],
				dialogStatus: false,
				disableStatus: false,
				dialogStatusp: false,
				infolist: {},
				treeList:[],
				treeListp:[],
				treeListponly:[],
				closeList:[],
				defaultList:[],
				depttype:0,
				fadept:{},
				deptList:[],
				// roleList: {
				// 	isProvinceRole: false,
				// 	isCityRole: false,
				// 	isCountyRole: false,
				// 	isOtherRole: false,
				// }
				roleStatus: '',  //0 省 1地市 2区县 3其他
				roleList:[]
			}
		},
		methods: {
			dialogCli(v) {
				let _this = this;
				if (v === 1) {
					let param = {};
					param.woName = this.infolist.woName;
					this.infolist.time?param.startTime = this.$formatDate(this.infolist.time[0], "YYYY-MM-DD"):'';
					this.infolist.time?param.endTime = this.$formatDate(this.infolist.time[1], "YYYY-MM-DD"):'';
					param.dutyList = [];
					for (let i = 0; i < this.treeListp.length; i++) {
						param.dutyList.push({
							dutyNo:this.treeListp[i].loginNoStr,
							dutyName:this.treeListp[i].loginNameStr,
							groupId:this.treeListp[i].o,
							groupName:this.treeListp[i].ou,
						})
					}
					param.fillList = [];
                    console.log(this.treeList)
					for (let i = 0; i < this.treeList.length; i++) {
						param.fillList.push({
							fillNo:this.treeList[i].receiveNo,
							fillName:this.treeList[i].label,
						})
					}
					for (let i = 0; i < this.treeList.length; i++) {
						for (let ii = 0; ii < this.treeListp.length; ii++) {
							if(this.treeListp[ii].loginNoStr == this.treeList[i].receiveNo){
								this.$message({
									message: '负责人和填报人重复！',
									type: 'error'
								});
								return
							}
						}
					}
                    this.$refs.infolist.validate(valid => {
                        if (valid) {
                            this.$http({
                            	url: "/market/cMemberWo/add",
                            	method: "post",
                            	headers: {
                            		"Content-Type": "application/json",
                            	},
                            	data: param,
                            }).then((res) => {
                            	if (res.data.result === 1) {
                            		_this.$message({
                            			message: res.data.desc,
                            			type: 'error'
                            		});
                            	} else {
                            		_this.$message({
                            			message: '成功',
                            			type: 'success'
                            		});
                            		_this.infolist = {};
                            		_this.treeList = [];
                            		_this.treeListp = [];
                            		_this.dialogStatus = false;
                            	}
                            });
                        }
                    })
				} else if (v === 2) {
					this.infolist = {};
					this.treeList = [];
					this.treeListp = [];
					this.dialogStatus = false;
				}
			},
			chofz(v){
				this.dialogStatusp = false;
				for (let i = 0; i < this.treeListp.length; i++) {
					if(this.treeListp[i].o == this.fadept.o){
						this.treeListp[i].loginNameStr = v.loginNameStr;
						this.treeListp[i].loginNoStr = v.loginNoStr;
					}
				}
			},
			fzperson(v){
				this.fadept= v;
				this.dialogStatusp = true;
                this.$http({
                    url: "/sysmgr/sysuserinfo/queryList",
                    method: "post",
                    headers: {
                        "Content-Type": "application/json",
                    },
                    data: {
                        groupId: v.o,
                    },
                }).then((res) => {
                   this.treeListponly = res.data;
                });
				this.$http({
				    url: "/sysmgr/csysdept/queryRegionDeptList",
				    method: "post",
				    headers: {
				        "Content-Type": "application/json",
				    },
				    data: {
				        parentorgid: v.o,
				    },
				}).then((res) => {
                   res.data.forEach(item=>{
                       this.$http({
                           url: "/sysmgr/sysuserinfo/queryList",
                           method: "post",
                           headers: {
                               "Content-Type": "application/json",
                           },
                           data: {
                               groupId: item.o,
                           },
                       }).then((ress) => {
                          this.treeListponly.push({bumen:item.ou})
                          this.treeListponly=this.treeListponly.concat(ress.data);
                       });
                   })
				});
			},
			deletess(val, index) {
                this.closeList = [];
                for (var i = 0; i < this.treeList.length; i++) {
                    if (this.treeList[i] == val) {
                        // this.treeList.splice(i, 1);
                    } else {
                        this.closeList.push(this.treeList[i]);
                    }
                }
			},
            deletes(val, index) {
			    this.treeListp.splice(index, 1);
			},
			treeCheck(v){
			    this.treeList = v;
			},
			choseDepta(v){
				let x = 'no';
				for (let i = 0; i < this.treeListp.length; i++) {
					if(this.treeListp[i].o == v.o){
						x = i;
					}
				}
				if(x != 'no'){
					this.treeListp.splice(x, 1)
				}else{
					this.treeListp.push(v);
				}
			},
			advadd() {
				this.dialogStatus = true;
				this.$http({
				    url: "/sysmgr/csysdept/queryRegionDeptList",
				    method: "post",
				    headers: {
				        "Content-Type": "application/json",
				    },
				    data: {},
				}).then((res) => {
				    this.deptList = res.data[0].children;
				});
			},
			jumpadv(v) {
				if(v == 1){
					this.$router.push({
						path: "/haveInHand",
					});
					this.setabList("进行中任务", "/haveInHand");
				}else{
					this.$router.push({
						path: "/Completed",
					});
					this.setabList("已完成", "/Completed");
				}
			},
			advmaterial(v) {
				if(v.dataCode == 1){
                    this.$router.push({
                    	path: "/empInfoFilling",
                    });
                    this.setabList("信息填报", "/empInfoFilling");
                    this.$http({
                        url: "/market/cMemberFill/queryMkMemberFillInfo",
                        method: "post",
                        headers: {
                            "Content-Type": "application/json",
                        },
                        data: {},
                    }).then((res) => {
                        if(res.data){
                            this.$router.push({
                            	path: "/empInfoFilling",
                            });
                            this.setabList("信息填报", "/empInfoFilling");
                        }
                    });
				}else{
					this.$router.push({
						path: "/dataStatistics",
					});
					this.setabList("数据统计", "/dataStatistics");
				}
			},
			setabList(n, p) {
				let params = {
					children: "",
					name: n,
					rountPath: p,
					target: "_self",
				};
				for (let i = 0; i < this.$store.state.tabList.length; i++) {
					if (this.$store.state.tabList[i].name === params.name) {
						this.$store.state.tabList[i] = params;
					}
				}
				let set = new Set([...this.$store.state.tabList, params]);
				set.add(params);
				this.$store.commit("setDefaultActive", params.rountPath);
				this.$store.commit("setTabList", Array.from(set));
			},
			getlist() {
				// this.$http({
				//     url: "/market/cadv/queryMkAdvStsNum",
				//     method: "post",
				//     headers: {
				//         "Content-Type": "application/json",
				//     },
				//     data: {},
				// }).then((res) => {
				//     this.info = res.data
				// });
			},

			getUser() {
				this.userInfo = JSON.parse(window.sessionStorage.userInfo);
				console.log(this.userInfo);
				let newArr = [];
				
				if(this.userInfo.cityName == null && this.userInfo.countyName == null){
					this.roleStatus = 0;
					return;
				}else if(this.userInfo.cityName != null && this.userInfo.countyName == null){
					 
					 this.$http({
						url: "/market/cMemberWo/queryProvTraceBase",
						method: "post",
						headers: {
							"Content-Type": "application/json",
						},
						data: {
							sts: 0
						},
					}).then((res) => {
						res.data.body.forEach(item=>{
							newArr.push(...item.dutyNo.split(","));
						})
						let n = newArr.filter(item=>{
							return  item == this.userInfo.loginNoStr
						  })
						//   console.log(n);
						  if(n.length != 0){
							  this.roleStatus = 1;
						  }else{
							  this.roleStatus = 3;
						  }
					});
					return;
				}else if(this.userInfo.cityName != null && this.userInfo.countyName != null){
					this.$http({
						url: "/market/cMemberWo/queryProvTraceBase",
						method: "post",
						headers: {
							"Content-Type": "application/json",
						},
						data: {
							sts: 0
						},
					}).then((res) => {
						res.data.body.forEach(item=>{
							newArr.push(...item.dutyNo.split(","));
						})
						let n = newArr.filter(item=>{
							return  item == this.userInfo.loginNoStr
						  })
						//   console.log(n);
						  if(n.length != 0){
							  this.roleStatus = 2;
						  }else{
							  this.roleStatus = 3;
						  }
					});
					// this.roleStatus = 2;
					return;
				}else{
					this.roleStatus = 3;
					return;
				}
			},
		},
		mounted() {
			this.getUser();
			this.getlist();
		},
		created() {

		}
	}
</script>
<style scoped lang="scss">
	.container-box {
		padding: 0 30px;
		height: calc(100% - 40px);
		overflow-y: scroll;
		padding-bottom: 20px;
	}

	.adv-box {
		margin-top: 20px;
		display: flex;
		flex-wrap: nowrap;
		// justify-content: space-between;

		div {
			display: inline-block;
			cursor: pointer;

			.el-card {
				width: 100%;
			}

			i {
				margin-right: 10px;
			}
		}

		.adv-tit {
			width: 28%;
			border: 1px solid transparent;
			border-radius: 5px;
			height: 60px;
			line-height: 60px;
			padding-left: 20px;
			box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
			overflow: hidden;
			margin-right: 2%;

			em {
				float: right;
				margin-right: 20px;
				font-style: normal;
				color: #0074D9;
			}
		}

		.adv-tit:hover {
			border: 1px solid #CFE8FC;
			background: #CFE8FC;
		}

		.adv-add {
			width: 100px;
		}

	}

	.adv-type {
		margin-top: 20px;
		display: flex;
		// justify-content: space-between;
		flex-wrap: wrap;
		// border: 1px solid #ddd;
		border-radius: 5px;
		padding: 20px;
		box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);

		div {
			width: 24%;
			text-align: center;
			height: 80px;
			overflow: hidden;
			min-width: 100px;
			cursor: pointer;
			padding-top: 10px;
			margin: 10px 0;
			div{
				width: 100%;
				padding-top: 0;
				margin: 0;
			}
		}

		div:hover {
			background: #CFE8FC;
			border-radius: 5px;
		}

		span {
			width: 100%;
			display: inline-block;
			height: 40px;

			// line-height: 40px;
			i {
				color: #0074D9;
				font-size: 36px;
			}
		}
	}

	.info-line {
		width: 100%;
		display: block;
		padding-left: 20px;

		div {
			width: 50%;
			display: inline-block;
		}

		span {
			width: 80px;
			display: inline-block;
			text-align: left;

			i {
				color: red;
				display: inline-block;
				padding-right: 5px;
			}
		}

		.el-select,
		.el-input {
			width: calc(100% - 100px);
		}
		.tree {
		    width: calc(50% - 60px);
		    display: inline-block;
		    margin-right: 20px;
		    height: 300px;
		    overflow-y: scroll;
			p{
				overflow: hidden;
				display: flex;
				justify-content: space-between;
			}
			.p-btn{
				width: 50px!important;
				cursor: pointer;
				color: #0074DA;
			}
			span{
				display: inline-block;
				width: calc(100% - 100px);
			}

		    .el-icon-error {
		        float: right;
		        font-size: 20px;
		        margin-top: 9px;
		        cursor: pointer;
		    }
		}

		.treeUser {
		    margin: 0;
		    border: 1px solid #ddd;

		    p {
		        background: #f4f4f4;
		        padding: 0 20px;
		        margin-bottom: 5px;
		    }
		}

		.treeUserb {
		    width: calc(100% - 100px);
		    border: 1px solid #ddd;
		    background: #f4f4f4;
		    border-radius: 3px;
		    height: auto;
		    overflow: hidden;

		    p {
		        display: inline-block;
		        padding: 0 20px;
		        margin-bottom: 5px;
		    }
		}
	}

	.treeUserp{
		p{
			padding: 5px;
		}
		p:hover{
			background: #01BEF0;
			color: #fff;
			cursor: pointer;
		}
	}

	.online {
		width: 100%;

		.el-select {
			width: calc(100% - 120px);
		}

		span {
			vertical-align: top;
		}

		.el-textarea,
		.el-date-editor {
			width: calc(100% - 100px);
			display: inline-flex;
		}
	}
</style>
