<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>
				<radio-group @change="radioChange">
					<uni-list-item v-for="(item,index) in requisitionList" :key="index">
						<!-- 自定义 header -->
						<view slot="header" class="slot-box-header">
							<radio :value="item.requisitionCode" />
						</view>
						<!-- 自定义 body -->
						<view slot="body" class="slot-box-body">
							{{item.requisitionName}}
						</view>
						<!-- 自定义 footer-->
						<view slot="footer" class="slot-box-footer">
							{{item.requisitionDate}}
						</view>
					</uni-list-item>
				</radio-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 {
		SelectRequisitionList
	} from "@/common/api/requisitionApi.js";
	export default {
		data() {
			return {
				requisitionList: [],
				radioValue: [],
				searchVal: ''
			}
		},
		onLoad: function() {
			//console.info('授权');
			//console.info(this.$auth);
			this.$util.persistLogin(this);
		},
		created: function() {

			this.getrequisitionList();
		},
		methods: {
			getrequisitionList: function() {
				var that = this;
				let projectcode = this.$store.state.projectCode;
				let userCode=this.$util.getState(this,'userCode');
				let searchValue = this.searchVal;
				uni.showLoading({
					title: "页面努力加载中，请稍候...",
					mask: true
				});
				SelectRequisitionList(projectcode, userCode,searchValue).then((res) => {
					uni.hideLoading();
					console.info('SelectRequisitionList', res);
					//console.info(projectCode);
					//console.info(res);
					//return;
					res.forEach(function(item, index, array) {
						that.$set(that.requisitionList, index, item);
					});
					//console.info(that.contractList);
				});
			},
			radioChange: function(evt) {
				//console.info(evt);
				//console.info(evt.target.value);
				//console.info('contractList',this.contractList);
				for (let i = 0; i < this.requisitionList.length; i++) {
					if (this.requisitionList[i].requisitionCode === evt.target.value) {
						console.info(evt.target.value);
						this.current = i;
						this.radioValue = this.requisitionList[i];
						console.info('this.radioValue', this.radioValue);
						break;
					}
				}
			},
			submitForm(e) {
				console.info('submitForm',this.radioValue);
				//先记录值，下面this指向会不一样
				let relationContractCodes = this.radioValue;
				if (relationContractCodes.length<=0) {
					uni.showToast({
						title: '请选择申请单',
						duration: 3000,
						icon: 'none'
					})
					return;
				}
				
				let selectChecked = [];
				selectChecked.push({
						requisitionCode: relationContractCodes.requisitionCode,
						requisitionName: relationContractCodes.requisitionName 
					});
				console.info('selectChecked',selectChecked);
				//return;
			//将数据发射到父级监听事件中
			uni.$emit('requisition', selectChecked);
			//关闭当前窗口，返回上一页
			uni.navigateBack({});
		},
		search(res) { //回车搜索
			this.searchvalue = res.value; //赋值
			this.requisitionList = [];
			this.getrequisitionList(); //调用搜索方法
		},
		input(res) {
			this.searchVal = res.value
		},
		cancel(res) {
			console.info('点击取消，输入值为：' + res.value);

		}
	}
	}
</script>

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

	.slot-box-body {
		margin: 5px 5px;
		width: 180px;
	}

	.slot-box-footer {
		margin: 5px 5px;
		width: 180px;
	}

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

	}

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