<template>
	<view class="">
		<scroll-view scroll-y="true" class="DrawerPage margin-bottom-xxxl">
			<cu-custom bgColor="bg-blue" :isBack="true">
				<block slot="backText">项目管理</block>
				<block slot="content"></block>
				<block slot="right">
					<view class=" flex justify-center">
					 	<!-- <button  class="margin-top-ssm cu-btn round bg-blue text-blue"><text class="cuIcon-add"></text></button> -->
					</view>
				</block>
			</cu-custom>
			<view class="cu-list menu margin-bottom-xl shadow-lg margin-top-sm">
		        <!-- 菜单-->
		        <view class="cu-item " style="height: 100%;" v-for="(item,index) in list" :key="index" :class="index==0 ? 'margin-bottom-sm':'' ">
					<view class="content padding-tb-sm" v-if="index==0">
						<view>
							 <text class="text-black text-bold text-xl">{{item.project_name}}</text>
					    </view>
						<view>
							<text class="text-black text-lg span-wrap">{{item.contractName}}</text>
						</view>
						<view class="flex justify-start">
							<text class="text-gray text-sm">{{item.startTime | timeStamp}} 加入</text>
							<button @click="current(item)" class="cu-btn round line-orange"  style="margin-left: auto;" >
								<image class="login-img" src="/static/user/star.png"></image> <text class="text-orange">当前项目</text>
							</button>
						</view>		
					</view>
					<view class="content padding-tb-sm content_b" v-else>
						<view>
							 <text class="text-black text-bold text-xl">{{item.project_name}}</text>
					    </view>
						<view>
							<text class="text-black text-lg span-wrap">{{item.contractName}}</text>
						</view>
						<view class="flex justify-start">
							<text class="text-gray text-sm">{{item.startTime | timeStamp}} 加入</text>
							<button class="cu-btn round line-blue" @click="current(item)" style="margin-left: auto;">
								<text class="text-blue">设为当前</text> 
							</button>
						</view>		
					</view>
		        </view>  					   
		    </view>
			<view class="flex justify-center text-gray margin-bottom-big">没有更多内容了~</view>
		</scroll-view>
    </view>
</template>

<script>
    export default { 
        data () {
            return {
			 list:[],
			 type:"",
           }
        },
		onLoad(e) {
			if(e.type){
				this.type=e.type;
			}
			this.findProject();
		},
		filters:{
			timeStamp: function(value) {
				if (!value) return '';
				var now = new Date(value);
				var year = now.getFullYear();
				var month = now.getMonth() + 1;
				if (month < 10) {
					month = '0' + month
				}
				var date = now.getDate();
				if (date < 10) {
					date = '0' + date
				}
				return year + "-" + month + "-" + date			
			},
		},
        methods: {
			findProject(){
				 var that =this;
				 var userInfo = uni.getStorageSync("userInfo")
				 that.userInfo = userInfo;
				  that.http.request('/app/findProject', { userId:userInfo.id}).then((result)=>{
					   let lastData=[];
					   let datas = result.datas;
					   var project =  uni.getStorageSync("porject"+"_"+uni.getStorageSync("userInfo").id);
					   if(project){
						uni.setStorageSync("storeName",project.store_name);
					   	var id = project.contractId;
					   	datas.forEach((item,index,arry)=>{
					   		 if(item.contractId!=id){
					   			 lastData.push(item);//存放没有选择的
					   		 }else{
					   			  that.list.push(item)//存放已选择的
					   		 }
					   	})
					   }
					   lastData.forEach((item,index,arry)=>{
					   	 that.list.push(item);
					   })
				  });
			},
			current(data){
				 if(this.type==1){//首页跳转过来的
					 //如果选择，则设置权限
					 uni.setStorageSync("storeName",data.store_name);
					 uni.setStorageSync("porject"+"_"+uni.getStorageSync("userInfo").id,data);	
					 uni.navigateBack({
					     delta: 1
					 });
				 }else{
					 //如果选择，则设置权限
					 uni.setStorageSync("storeName",data.store_name);
					 uni.setStorageSync("porject"+"_"+uni.getStorageSync("userInfo").id,data);	
					 this.$prompt.none("设置成功");
					 setTimeout(function(){
						 uni.redirectTo({
						     url:'/pages/user/project/project'
						 })
					 },200)
				 }
			}
		}
    }
</script>

<style lang="scss">
    .box {
		margin-top:-20rpx;
	}

	.box view.cu-bar {
		margin-top: 20upx;
	}
	.page {
		height: 100Vh;
		width: 100vw;
	}
	
	.page.show {
		overflow: hidden;
	}
	.cu-list.menu-avatar>.cu-item .content {
	    position: absolute;
	    left: 10px;
	    width: calc(100% - 48px - 30px - 60px - 10px);
	    line-height: 1.6em;
	}
	.text-black{
		color:#101010;
	}
	.cu-list.menu>.cu-item:after {
		border-bottom: 1px dashed #fff !important;
	}
	.content_b{border-bottom: 1px solid #E3e1e1;}
	.cu-item:last-child .content_b{border-bottom: none;}
	.span-wrap{
		word-break: break-all;white-space: normal;
	}
</style>