<template>
	<view class="content">
		<view style="width: 100%;margin-top: 20rpx;"><text class="title">检查对象</text></view>
		<view class="location-container"  @click="showObjectPicker">
			<text class="value">{{selObjectText}}</text>
			<image class="arrow-right" src="../../static/common/arrow_right_gray.png">
		</view>
		<view style="width: 100%;margin-top: 20rpx;"><text class="title">检查项类型</text></view>
		<view class="location-container" @click="showTypePicker">
			<text class="value">{{selectItemText}}</text>
			<image class="arrow-right" src="../../static/common/arrow_right_gray.png">
		</view>
		<button hover-class="submithover"  @click="onSubmitClick" class="submit-button">
			<text class="submittext">开始检查</text>
		</button>
		<Picker
		    mode="selector"
		    @confirm="onConfirm" 
		    ref="picker" 
		    themeColor="#f00"
		    :selectList="itemList"
		></Picker>
	</view>
</template>

<script>
import Picker from "@/components/w-picker/w-picker.vue";
	export default {
		data() {
			return {
				showActionSheet: false,
				itemList: [],
				taskId:"",
				selObject:"",
				selObjectText:"",
				selectItem:"",
				selectItemText:"",
				objectarr:[],
				itemarr:[],
				curType:"object",
				objectData:[],
				itemData:[]
			}
		},
		onLoad(option) {
			this.taskId = option.taskId
			this.loadTaskObject();
		},
		methods: {
			itemClick: function() {
				
			},
			closeActionSheet: function() {
				this.showActionSheet = false
			},
			onConfirm(data){
				if(this.curType == "object"){
					this.selObject = data.checkArr.value;
					this.selObjectText = data.checkArr.label;
					this.itemarr = [];
					this.selectItem = "";
					this.selectItemText = "";
					this.loadItemData();
				}else{
					this.selectItem = data.checkArr.value;
					this.selectItemText = data.checkArr.label;
				}
			},
			onSubmitClick(){
				 var objectData = this.findObjectData();
				 var itemData = this.findItemData();
				 uni.navigateTo({
				 	url:"security-checkitem"
				 })
				 uni.$emit("sendCheckItem",[objectData,itemData,this.taskId]);
			},
			showObjectPicker(){
				this.itemList = this.objectarr;
				this.curType = "object"
				this.$refs.picker.show()
			},
			showTypePicker(){
				if(this.selObject == ""){
					return 
				}
				this.itemList = this.itemarr;
				this.curType = "item";
				this.$refs.picker.show()
			},
			loadTaskObject(){
				var user = {};
				user.id = this.GLOBALUTIL.user.userid;
				user.name = encodeURI(this.GLOBALUTIL.user.username);
				uni.request({
				    url: this.GLOBALUTIL.url+'/safety/v1/check-task-scope/'+this.taskId, 
					method: 'GET',
				    header: {
				        'Accept': 'application/json',
				        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
						 'user' : JSON.stringify(user)
				    },
					body:null,
				    success: (res) => {
						var data = res.data;
						this.objectData = data;
						
						var itemList = data.map((opt)=>{
							var status = opt.execute_state?"(已完成)":""
							return {label:opt.name+status,value:opt.id};
						})
						this.objectarr = itemList;
						this.itemList = itemList; 
				    }
				})
			},
			loadItemData(){
				var user = {};
				user.id = this.GLOBALUTIL.user.userid;
				user.name = encodeURI(this.GLOBALUTIL.user.username);
				uni.request({
				    url: this.GLOBALUTIL.url+'/safety/v1/check-task-lists/'+this.taskId+"?check_site_id="+this.selObject, 
					method: 'GET',
				    header: {
				        'Accept': 'application/json',
				        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
						 'user' : JSON.stringify(user)
				    },
					body:null,
				    success: (res) => {
						var data = res.data;
						this.itemData = data;
						var itemList = data.map((opt)=>{
							var status = opt.execute_state?"(已完成)":""
							return {label:opt.content+status,value:opt.id};
						})
						this.itemarr = itemList;
				    }
				})
			},
			findObjectData(){
				var obj = {};
				this.objectData.map((opt)=>{
					if(opt.id == this.selObject){
						obj = opt;
					}
				})
				return obj;
			},
			findItemData(){
				var obj = {};
				this.itemData.map((opt)=>{
					if(opt.id == this.selectItem){
						obj = opt;
					}
				})
				return obj;
			}
			
		},
		components: {
			Picker
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 30rpx;
		color: #8f8f94;
		width: 100%;
	}
	.loginbtn {
		width:180rpx;
		height:200rpx;
	}
	.login-hover {
		width:200rpx;
		height:200rpx;
		background-color:#00CE47;
	}
	.testview {
		width:209rpx;
		height:200rpx;
		background-color:linear-gradient(red, yellow, blue);
	}
	.location-container{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-end;
		width: 100%;
		height: 60rpx;
		border-bottom: 1rpx solid #898989;
		
	}
	.arrow-right{
		width:40rpx;
		height:40rpx;
	}
	.camera-button {
		display:flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width:200rpx;
		height: 200rpx;
	}
	.camera {
		width:50rpx;
		height:50rpx;
	}
	.submit-button {
		display: flex;
		width:600rpx;
		height:60rpx;
		align-items: center;
		justify-content: center;
		background: linear-gradient(-45deg, rgba(87, 225, 181, 1) 0%, rgba(0, 63, 255, 1) 100%);
		border-radius: 15rpx;
		margin-top: 80rpx;
	}
	.submittext {
		font-size: 14px;
		color: #FFFFFF;
		font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
	}
	.submithover{
		opacity: 0.7;
	}
	.detail {
		width:400rpx;
		height:100rpx;
		border: 1rpx solid #000000;
	}
	.status_bar {
		  height: var(--status-bar-height);
		  width: 100%;
	 }
	.value {
		font-size: 30rpx;
		color: #000000;
		width: 100%;
	}
</style>
