<template>
	<view>
		<view>
			<uni-search-bar :radius="100" @confirm="search" @input="input" @cancel="cancel"></uni-search-bar>
		</view>
		<uni-forms ref="form" @submit="submitForm">
			<uni-list>
				<checkbox-group @change="checkboxChange">
					<uni-list-item v-for="(item,index) in contractList" :key="index">
						<!-- 自定义 header -->
						<view slot="header" class="slot-box">
							<checkbox :value="item.contractCode" />
						</view>
						<!-- 自定义 body -->
						<view slot="body" class="slot-box">
							{{item.contractID}}
						</view>
						<!-- 自定义 footer-->
						<view slot="footer" class="slot-box">
							{{item.contractName}}
						</view>
					</uni-list-item>
				</checkbox-group>
			</uni-list>
			<view class="uni-btn-v uni-column">
				<button type="primary" form-type="submit" style="border-radius: 15px;">选择成功，确认</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		GetRelationContract
	} from "@/common/api/requisitionApi.js";
	export default {
		data() {
			return {
				contractList: [],
				checkboxValues: [],
				searchVal: ''
			}
		},
		onLoad: function() {
			//console.info('授权');
			//console.info(this.$auth);
			this.$util.persistLogin(this);
		},
		created: function() {
			this.getcontractList();
		},
		methods: {
			getcontractList: function() {
				var that = this;
				var projectCode = this.$store.state.projectCode;
				let searchValue = this.searchVal;
				uni.showLoading({
					title: "页面努力加载中，请稍候...",
					mask: true
				});
				GetRelationContract(projectCode, searchValue).then((res) => {
					uni.hideLoading();
					console.info('getcontractList');
					//console.info(projectCode);
					console.info(res);
					//return;
					res.forEach(function(item, index, array) {
						that.$set(that.contractList, index, item);
					});
					//console.info(that.contractList);
				});
			},
			checkboxChange: function(e) {
				var items = this.contractList,
					values = e.detail.value;
				this.checkboxValues = values.join(',');
				console.info("values");
				console.info(values);
				console.info(this.checkboxValues);
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if (values.includes(item.value)) {
						this.$set(item, 'checked', true)
					} else {
						this.$set(item, 'checked', false)
					}
				}
			},
			submitForm(e) {
				console.info(this.checkboxValues);
				//先记录值，下面this指向会不一样
				let relationContractCodes = this.checkboxValues;
				let arr=relationContractCodes.split(',');
				let selectChecked=[];
				this.contractList.forEach(function(item,index,array){
					arr.forEach(function(item1,index1,array1){
						if(item.contractCode==item1){
							selectChecked.push({contractCode:item.contractCode,contractID:item.contractID,contractName:item.contractName,estimateCash:0.00});
							
						}
					});
				});
				//将数据发射到父级监听事件中
				uni.$emit('fire', selectChecked);
				//关闭当前窗口
				uni.navigateBack({});
			},
			search(res) { //回车搜索
				this.searchvalue = res.value; //赋值
				this.contractList=[];
				this.getcontractList(); //调用搜索方法
			},
			input(res) {
				this.searchVal = res.value
			},
			cancel(res) {
				console.info('点击取消，输入值为：' + res.value);
				
			}
		}
	}
</script>

<style scoped>
	.slot-box {
		margin: 5px 5px;
	}

	.uni-btn-v {
		position: fixed;
		bottom: 0;
		width: 100%;

	}
	uni-view{
		font-size:15px;
	}
</style>
