<template>
	<view class="m_right">
		<view class="m_right_hander">
			<button size="mini" class="m_btn_red" @click="addProject()">添加项目</button>
		</view>
		<view class="m_right_container">
			<table style="width: 100%;border: 1px solid #DDDDDD;border-collapse: collapse">
				<thead class="m-table-hander">
					<tr>
						<!-- <th>
							<icon type="" style="color:#EA252C;" class="font_family" :class="checke?'icon-fuxuankuang_weixuanzhong4':'icon-fuxuankuang-true'"></icon>
						</th> -->
						<th>项目名称</th>
						<th>项目创建人</th>
						<th>项目创建时间</th>
						<th>项目描述</th>
						<!-- <th>项目推荐</th> -->
						<!-- <th>上线</th> -->
						<th>删除</th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="(dataUser,dataUserI) in dataUsers_all" :key="dataUserI">
						<!-- <th>
							<icon type="" style="color:#EA252C;" class="font_family" :class="checke?'icon-fuxuankuang_weixuanzhong4':'icon-fuxuankuang-true'"></icon>
						</th> -->
						<th>{{dataUser.ProjectName}}</th>
						<th>{{dataUser.CreateUserName}}</th>
						<th>{{dataUser.CreateTime}}</th>
						<th>{{dataUser.Type}}</th>
						<!-- <th>{{dataUser.ProjectName}}</th> -->
						<!-- <th style="position: relative;"><button type="default" size="mini" class="m_online" @click="onlineProject(dataUser)">上线</button></th> -->
						<th style="position: relative;"><button type="default" size="mini" class="m_delete" @click="deleteProject(dataUser)">删除</button></th>
					</tr>
					
				</tbody>
			</table>
		</view>
		<view class="m_right_footer">
			<view class="m_paging">
				<text class="m_paging_item m_paging_upper" v-show="bShowLastPageBtn" @click="last()">上一页</text>
				<text class="m_paging_item" :key="i" v-for="i in pageTotalNum" @click="paging(i)" :class="i==currentPage?'p_act':''">
					{{i}}
				</text>
				<text class="m_paging_item m_paging_lower" v-show="bShowNextPageBtn" @click="next()">下一页</text>
			</view>
		</view>
		<popForm ref="popForm"></popForm>
		<popAlert ref="popAlert"></popAlert>
	</view>
</template>

<script>
	import popForm from '@/components/popForm.vue'
	import popAlert from '@/components/popAlert.vue'
	export default {
		name: "project",
		components: {
			popForm,
			popAlert,
		},
		data() {
			return {
				pageTotalNum:1,
				currentPage:1,
				totalItem:2,
				dataUsers_all: [],
				checke: false,
				bShowLastPageBtn:false,
				bShowNextPageBtn:false,
			}
		},
		methods: {
			paging(i) {
				uni.showLoading({
				    title: '加载中'  
				});
				
				let fromIndex = this.totalItem*(i-1);
				let toIndex = fromIndex+this.totalItem;
				
				let data = {
					"UserID": mydata_userInfo.userID,
					"BeginNum": fromIndex, //用户数组索引
					"EndNum": toIndex, //用户数组索引结束
					"ItemType":""//道具 角色 场景  空为全部
				}
				
				uni.request({
					header: {
						'Content-Type': 'application/json;charset=UTF-8'
					},
					url: mydata_api + '/backstage/getprojects',
					method: 'POST',
					data: data,
					dataType: 'json',
					success: (res) => {
						//100成功
						if (res.data.Code == 100) {
							console.log("请求全部项目", res);
							this.dataUsers_all = res.data.Projects;
							this.pageTotalNum = Math.ceil(res.data.AllNumOfList/this.totalItem);
							this.currentPage = i;
							this.showNextOrLastPageBtn();
						}
						//200失败
						else {
							console.log('suc200', res);
						}
						uni.hideLoading();					
					},
					fail: (res) => {
						console.log("请求失败****");	
						uni.hideLoading();
					}
				});
			},
			initPage() {
				this.paging(1);
			},
			last()
			{
				this.currentPage--;
				this.paging(this.currentPage);
			},
			next()
			{
				this.currentPage++;
				this.paging(this.currentPage);
			},
			showNextOrLastPageBtn()
			{ 
				if(this.pageTotalNum==this.currentPage)
				{
					this.bShowLastPageBtn = true;
					this.bShowNextPageBtn = false;
					console.log('11111')
				}
				else if(1==this.currentPage)
				{
					this.bShowLastPageBtn = false;
					this.bShowNextPageBtn = true;
					console.log('2222')
				}
				else
				{
					this.bShowLastPageBtn = true;
					this.bShowNextPageBtn = true;
					console.log('333333')
				}
				
				if(this.pageTotalNum==1){
					this.bShowLastPageBtn = false;
					this.bShowNextPageBtn = false;
					console.log('444444')
				}
			},
			addProject()
			{
				uni.chooseFile({
					success: (chooseFileRes) => 
					{
						console.log('选择文件成功')
						const tempFilePaths = chooseFileRes.tempFilePaths;
												
						this.$refs.popForm.isShow(true);
						this.$refs.popForm.setTitle('添加项目');
						
						let _self = this;
						let callback = function(content_arr){
							
							let data = {
								"CreateUserID":mydata_userInfo.userID,
								"ProjectName":content_arr[0].input,
								"Describe":content_arr[1].input,
								"Type":content_arr[2].input,
							}
							
							uni.showLoading({
							    title: '上传中'  
							});
							
							uni.uploadFile({
								url: mydata_api + '/project/createprojectnew', 
								filePath: tempFilePaths[0],
								name: 'file',
								formData:data,
								success: (uploadFileRes) => {								
									console.log("创建项目成功", uploadFileRes);
									_self.paging(_self.currentPage);																	
									uni.hideLoading();
								},
								fail: (res) => {
									console.log("创建项目失败");
									uni.hideLoading();
								}
							});
						};
						
						this.$refs.popForm.setContent([
							{
								'name':'项目名称',
								'input':'',
								'type' :'text'
							},
							{
								'name':'项目描述',
								'input':'',
								'type' :'text'
							},
							{
								'name':'项目类型',
								'input':'',
								'type' :'text'
							},
						],callback);						
					}
				});
			},
			onlineProject(dataUser)
			{
				
				// this.$refs.popAlert.isShow(true);
				// this.$refs.popAlert.setTitle('提示');
				// let _self = this;		
				// let callback = function(){
				// 	let data = {
				// 		"UserID":mydata_userInfo.userID,
				// 		"ProjectID" : dataUser.Id.toString()
				// 	}
					
				// 	uni.showLoading({
				// 	    title: '上线中'  
				// 	});
					
				// 	uni.request({
				// 		header: {
				// 			'Content-Type': 'application/json;charset=UTF-8'
				// 		},
				// 		url: mydata_api + '/backstage/deleteproject',
				// 		method: 'POST',
				// 		data: data,
				// 		dataType: 'json',
				// 		success: (res) => {
				// 			//100成功
				// 			if (res.data.Code == 100) {
				// 				console.log("上线成功", res);
				// 				_self.paging(_self.currentPage);
				// 			}
				// 			//200失败
				// 			else {
				// 				console.log('上线失败200', res);
				// 			}
							
				// 			uni.hideLoading();
				// 		},
				// 		fail: (res) => {
				// 			console.log("上线失败");
				// 			uni.hideLoading();
				// 		}
				// 	});
				// }
				
				// this.$refs.popAlert.setContent('是否删除项目',callback);
			},
			deleteProject(dataUser)
			{
				this.$refs.popAlert.isShow(true);
				this.$refs.popAlert.setTitle('提示');
				let _self = this;		
				let callback = function(){
					let data = {
						"UserID":mydata_userInfo.userID,
						"ProjectID" : dataUser.Id.toString()
					}
					console.log('data=',data)
					uni.showLoading({
					    title: '删除中'  
					});
					
					uni.request({
						header: {
							'Content-Type': 'application/json;charset=UTF-8'
						},
						url: mydata_api + '/backstage/deleteproject',
						method: 'POST',
						data: data,
						dataType: 'json',
						success: (res) => {
							//100成功
							if (res.data.Code == 100) {
								console.log("删除项目成功", res);
								_self.paging(_self.currentPage);
							}
							//200失败
							else {
								console.log('删除项目失败200', res);
							}
							
							uni.hideLoading();
						},
						fail: (res) => {
							console.log("删除项目失败");
							uni.hideLoading();
						}
					});
				}
				
				this.$refs.popAlert.setContent('是否删除项目',callback);
			}
		}
	}
</script>

<style lang="scss">
	@import '../../assets/icon/iconfont.css';
	// 允许文字被选择
	th
	{
	moz-user-select: -moz-text;
	-moz-user-select: text;
	-o-user-select: text;
	-khtml-user-select: text;
	-webkit-user-select: text;
	-ms-user-select: text;
	user-select: text;
	}
	
	.m_right {
		width: 100%;
		margin-top: 5rpx;
		padding: 0 150rpx;
		background-color: #fff;
		box-shadow: 3px 0px 6px 0px rgba(0, 0, 0, 0.1);

		.m_right_hander {
			text-align: right;
			height: 250rpx;
			position: relative;

			.m_btn_red {
				position: absolute;
				height: 80rpx;
				line-height: 80rpx;
				padding: 0 40rpx;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
				background: #EA252C;
				color: #fff;
			}
		}

	}

	tbody tr {
		// border-top: 2rpx solid #DDDDDD !important;
		border: 2rpx solid #DDDDDD !important;
	}
	
	th {
		border: 2rpx solid #DDDDDD !important;
	}

	tbody tr th {
		font-weight: 400 !important;
		height: 140rpx;
		line-height: 140rpx;
	}

	// tbody tr:hover {
	// 	// color: #fff;
	// 	background: #FDEBEC;
	// 	// opacity: 0.08;
	// }

	.m-table-hander {
		background-color: #FFF8F7;
		height: 140rpx;
		line-height: 140rpx;
		font-family: MicrosoftYaHei-Bold, MicrosoftYaHei;
		font-weight: bold;
	}

	.m_edit {
		// position: absolute;
		// top: 50%;
		// left: 50%;
		// transform: translate(-50%, -50%);
		background: rgb(67, 127, 250);
		color: rgb(255, 255, 255);
		// min-width: 130rpx;
	}

	.m_delete {
		// position: absolute;
		// top: 50%;
		// left: 50%;
		// min-width: 130rpx;
		// transform: translate(-50%, -50%);
		background: rgb(234, 37, 44);
		color: rgb(255, 255, 255);
	}

	.m_online {
		// background: #437FFA;
		// position: absolute;
		// top: 50%;
		// left: 50%;
		// min-width: 130rpx;
		// transform: translate(-50%, -50%);
		background: #437FFA;
		color: rgb(255, 255, 255);
	}

	.m_offline {
		background: #FD813B;
		position: absolute;
		top: 50%;
		left: 50%;
		min-width: 130rpx;
		transform: translate(-50%, -50%);
		color: rgb(255, 255, 255);
	}

	.m_right_footer {
		margin-top: 80rpx;
	}

	.m_paging {
		text-align: right;

		.m_paging_item {
			padding: 10rpx 18rpx;
			border: 1rpx solid #DDDDDD;
			border-radius: 8rpx;
			margin-right: 10rpx;
		}

		.p_act {
			background: #EA252C;
			color: #fff;
			border: inherit;
		}
	}

	.m_paging_item:hover {
		background: #EA252C;
		color: #fff;
		border: inherit;
	}

	.m_act1 {
		color: #FF0019;
	}
</style>
