<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.unit}}
						</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 {GetMaterialList} from '@/common/api/commonApi.js';
	import {
		InsertZRelationSafeQualityCheckMaterial
	} from "@/common/api/SafeQualityCheckApi.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;
				 
				let searchVal=that.searchVal;
				uni.showLoading({
					title: "页面努力加载中，请稍候...",
					mask: true
				});
				GetMaterialList(searchVal).then((res) => {
					uni.hideLoading();
					console.info('GetMaterialList',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);
				let _this=this;
				if(this.checkboxValues.length<=0){
					uni.showToast({
						title:'请选择材料',
						icon:'none',
						duration:3000
					});
					return;
				}
				//先记录值，下面this指向会不一样
				let relationContractCodes = this.checkboxValues;
				let SafeQualityCheckCode=_this.$util.getQuery("SafeQualityCheckCode");
				let arr=relationContractCodes.split(',');
				for (let i = 0; i < arr.length; i++) {
					var zRelationSafeQualityCheckMaterial={safeQualityCheckCode:SafeQualityCheckCode,materialCode:arr[i]};
					InsertZRelationSafeQualityCheckMaterial(zRelationSafeQualityCheckMaterial);
				} 
				 
				//跳转
				uni.navigateTo({
					url:'../GetRelationMaterialList/GetRelationMaterialList?id='+SafeQualityCheckCode
				});
			},
			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>
