<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 v-if="materials.length>0">
				<checkbox-group @change="checkboxChange" >
					<uni-list-item v-for="(item,index) in materials" :key="index">
						<!-- 自定义 header -->
						<view slot="header" class="slot-box">
							<checkbox :value="item.materialCode" />
						</view>
						<!-- 自定义 body -->
						<view slot="body" class="slot-box">
							{{item.materialName}}
						</view>
						<!-- 自定义 footer-->
						<view slot="footer" class="slot-box">
							{{item.standardPrice}} 元
						</view>
					</uni-list-item>
				</checkbox-group>
			</uni-list>
			<uni-list v-else>
				<checkbox-group @change="checkboxChange" >
					<uni-list-item >						
						<!-- 自定义 body -->
						<view slot="body" class="slot-box">
							该材料合同中没有任何材料
						</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 {GetMaterials} from '@/common/api/MaterialInOut.js';
	
	export default {
		data() {
			return {
				materials: [],
				checkboxValues: [],
				searchVal: ''
			}
		},
		onLoad: function() {
			//console.info('授权');
			//console.info(this.$auth);
			this.$util.persistLogin(this);
		},
		created: function() {
			this.getMaterials();
		},
		methods: {
			getMaterials: function() {
				var that = this;
				var projectCode = this.$store.state.projectCode;
				let contractCode = this.$util.getQuery("contractCode");
				let searchVal=that.searchVal;
				uni.showLoading({
					title: "页面努力加载中，请稍候...",
					mask: true
				});
				GetMaterials(projectCode, contractCode,searchVal).then((res) => {
					uni.hideLoading();
					console.info('GetMaterials',res);
					//console.info(projectCode);
					that.materials=[];
					//return;
					res.forEach(function(item, index, array) {
						that.$set(that.materials, index, item);
					});
					 
				});
			},
			checkboxChange: function(e) {
				var items = this.materials,
					values = e.detail.value;
				this.checkboxValues = values.join(',');
				console.info("values",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('submitForm',this.checkboxValues);
				if(this.checkboxValues.length<=0){
					uni.showToast({
						title:'请选择材料',
						icon:'none',
						duration:3000
					});
					return;
				}
				//先记录值，下面this指向会不一样
				let relationContractCodes = this.checkboxValues;
				let arr=relationContractCodes.split(',');
				let selectChecked=[];
				this.materials.forEach(function(item,index,array){
					arr.forEach(function(item1,index1,array1){
						if(item.materialCode==item1){
							item.inQty=0.00;
							item.noQty=0.00;
							selectChecked.push(item);
							
						}
					});
				});
				console.info('submitForm selectChecked',this.selectChecked);
				//将数据发射到父级监听事件中
				uni.$emit('materials', selectChecked);
				//关闭当前窗口
				uni.navigateBack({});
			},
			search(res) { //回车搜索
				this.searchvalue = res.value; //赋值
				
				this.getMaterials(); //调用搜索方法
			},
			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>
