<template>
	<view class="page">
		<view class="uni-flex uni-row">
			<sl-filter :ref="'slFilter'" :menuList.sync="menuList" @result="filterConfirm"></sl-filter>
			<view class="otherFixed" style="width: 25%;">
				<view class="text" style="width: 94%;" @click="filterDate">{{timeData.begin==''?'申请时间':(timeData.begin+'~'+timeData.end)}}</view>
			</view>
		</view>
		<view class="mybox" v-if="listCards.length>0">
			<view v-for="(item,index) in listCards" :key="index" class="example-box" @click="clickDetail(item)">
				<repair-block :title="item.title" :titleNumber="item.titleNumber" :note="item.note" 
				:extra="item.extra"  :tagType="item.tagType"
				:contentList="item.contentList"></repair-block>
			</view>
			<uni-load-more  :status="loading_status" ></uni-load-more>
		</view>
		<view class="mybox" v-else>
			<no-record ></no-record>
		</view>
		<uni-calendar ref="calendar" :lunar="false" :range="true"  @confirm="confirmDate"  @cancel='cancleData'/>
	</view>
</template>

<script>
	import slFilter from '@/components/xinsoft-sl-filter/xinsoft-sl-filter.vue';
	import repairBlock from '@/components/xinsoft-dev-repair/xinsoft-dev-repair.vue'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import noRecord from '@/components/xinsoft-no-record/xinsoft-no-record';
	import uniCalendar from '@/components/uni-calendar/uni-calendar.vue'
	
	export default {
		components: {
			repairBlock,uniLoadMore,noRecord,uniCalendar,slFilter
		},
		data() {
			return {
				current_num:1,
				listCards: [],
				menuList: [{
						'title': '发起人',
						'isMutiple': false,
						'key': 'createBy',
						'detailList': [],
						'defaultSelectedIndex':[0]
					},
					{
						'title': '待处理',
						'isMutiple': false,
						'key': 'status',
						'detailList': [],
						'defaultSelectedIndex':[1]
					},
					{
						'title': '责任人',
						'isMutiple': false,
						'key': 'heldPerson',
						'detailList': [],
						'defaultSelectedIndex':[this.j]
					}
				],
				loading_status: 'more',//more（loading前）、loading（loading中）、noMore（没有更多了）
				pageSize:5,
				page:1,
				filter:{},
				//日历变量。
				timeData:{
					begin:"",
					end:'' 
				},
				j:0
			}
		},
		onLoad() {
			var _this = this;
			var userInfo  = uni.getStorageSync('userInfo');	
			
			this.filter = {
				status: 56,
				heldPerson: userInfo.name
			}
			//任务状态初始化
			this.$http.request({
			  url: '/apis/dict/getDictsByType',
			  params: {
			    type: "task_status"
			  },
			}).then(res=>{
				if(res.statusCode == '200'){
					_this.menuList[1].detailList = [
						{
							'title': '不限',
							'value': ''
						}
					];
					var useType = res.data.datas;
					for (let i = 0; i < useType.length; i++) {
					    var myVal=
					    	{
					    		'title': useType[i].name,
					    		'value': useType[i].id
					    	}
					    _this.menuList[1].detailList.push(myVal);
					}
				}
			}).catch(err=>{
			});
			
			//发起人员
			this.$http.request({
			  url: '/apis/common/getUsers',
			  params:{
				  deptId:userInfo.deptId
			  }
			}).then(res=>{
				if(res.statusCode == '200'){
					_this.menuList[0].detailList = [
						{
							'title': '不限',
							'value': ''
						}
					];
					var useType = res.data.datas;
					for (let i = 0; i < useType.length; i++) {
					    var myVal=
					    	{
					    		'title': useType[i].name,
					    		// 'value': useType[i].id,
								'value': useType[i].name
					    	}
						_this.menuList[0].detailList.push(myVal);
					}
				}
			}).catch(err=>{
			});
			
			//责任人员  //两个接口合并，有问题。不知道为什么会这样，所以这里分开调用。
			this.$http.request({
			  url: '/apis/common/getUsers',
			  params:{
				  deptId:userInfo.deptId
			  }
			}).then(res=>{
				if(res.statusCode == '200'){
					_this.menuList[2].detailList = [
						{
							'title': '不限',
							'value': ''
						}
					];
					var useType = res.data.datas;
					var j = 0;
					for (let i = 0; i < useType.length; i++) {
						//判断当前当前登陆人的姓名。
						if(useType[i].name == userInfo.name){
							//记录当前的 j.
							 j = i+1;  //因为多了一个不限，所有序号加1
						}
					    var myVal=
					    	{
					    		'title': useType[i].name,
					    		// 'value': useType[i].id,
								'value': useType[i].name
					    	}
					    _this.menuList[2].detailList.push(myVal);
					}
					// _this.j = j;
					// console.log(_this.j);j
					_this.menuList[2].defaultSelectedIndex[0] = j;
					// _this.menuList[0].defaultSelectedIndex=[0];
				}
			}).catch(err=>{
			});
			
			//初始化当前列表。
			this.dataList();
		},
		onReachBottom: function() {
			//下拉刷新的时候请求一次数据
			if(this.loading_status=='noMore'){
				return false;
			}
			this.page++;
		    this.dataList();
		},
		onPullDownRefresh: function() {
			this.listCards=[];
			this.page=1;
			this.loading_status="more";
			//触底的时候请求数据，即为上拉加载更多
			this.dataList();
		},
		methods: {
			filterConfirm(f){
				this.filter = f;
				this.listCards=[];
				this.page=1;
				this.loading_status="more";
				this.dataList();
			},
			dataList(){
				this.loading_status="loading";
				
				var filterRes = this.filter;
				filterRes.pageno = this.page;
				filterRes.pagesize = this.pageSize;
				filterRes.startTime = this.timeData.begin;
				filterRes.endTime = this.timeData.end;
				this.$http.request({
				  url: '/apis/taskMain/list',
				  params:filterRes,
				}).then(res=>{
					uni.stopPullDownRefresh();
					if(!res.data.data){
						this.loading_status="noMore";
					}else if(res.data.data.datas.length<this.pagesize){
						this.loading_status="noMore";
					}else{
						this.loading_status="more";
					}
					if(res.data.code == 0){
						if(res.data.data.datas.length>0){
							this.listCards =  this.listCards.concat(this.formaterData(res.data.data.datas));
						}
					}
				}).catch(err=>{
					this.loading_status="noMore";
				});
			},
			
			clickDetail(item){
				//点击详情
				if(item.status != '146'){
					uni.navigateTo({
					    url: 'taskDetail?id='+item.id
					});
				}else{
					//重置下
					uni.removeStorageSync('taskInfo');
					//146 表示暂存状态，再次编辑。
					uni.navigateTo({
					    url: 'addTask?id='+item.id
					});
				}
				
			},
			formaterData(data){
				var resData = [];
				data.forEach((vals,index)=>{
					var combineJSON = {
						id: vals.id,
						title: vals.statusName,
						contentList: [
							{
								id:1,
								isDouble:false,
								labelName:'发起人',
								labelContent:vals.applyPersonName
							},						
							{
								id:2,
								isDouble:false,
								labelName:'任务编号',
								labelContent:vals.taskNo||''
							},
							{
								id:3,
								isDouble:false,
								labelName:'责任人',
								labelContent:vals.heldPersonName||''
							},
							{
								id:4,
								isDouble:false,
								labelName:'任务主题',
								labelContent:vals.title||''
							},
						],
						note: vals.createTime,
						status:vals.statusId
					}
					resData.push(combineJSON);
				});
				return resData;
			},
			filterDate() {
				//关闭之前filter。
				// document.getElementById("popupRef").style.display="none";
				this.$refs.calendar.open();
			},
			confirmDate(e){
				if(e.range.begin && e.range.end){
					this.timeData = {
						begin:e.range.begin+' 00:00:00',
						end:e.range.end+' 00:00:00'
					}
					this.listCards=[];
					this.page=1;
					this.loading_status="more";
					this.dataList();
				}
			}
		},
		onNavigationBarButtonTap(obj) {
			//重置下
			uni.removeStorageSync('taskInfo');
			uni.navigateTo({
			    url: 'addTask'
			});
		}
	}
</script>

<style scoped>
	>>>.select-tab-fixed-top{
		left: 0;
		width: 75%;
	}
</style>
