<template>
	<view class="zd_container">
		<!-- 查询物料 -->
		<nav-bar :title="title" :inner="true"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">物料信息</span>
					</view>
					<view class="zd-col-24">
						<input type="text" class="filter_input" v-model="val1">
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th @tap="toAllCheck"><uni-icons :type="allCheck ? 'checkbox' : 'circle'" size="24" color="#4e6ef2"></uni-icons></th>
								<th>物料名称</th>
								<th>物料编码</th>
								<th>数量</th>
								<th>批次号</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td @tap="toCheck(e)"><uni-icons :type="e.checked ? 'checkbox' : 'circle'" size="24" color="#4e6ef2"></uni-icons></td>
								<td>{{e.material_name}}</td>
								<td>{{e.material_code}}</td>
								<td><input type="number" class="sin_input" v-model="e.qty" @blur="handleBlur(e)"></td>
								<td><input type="text" class="sin_input" v-model="e.pcsn"></td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
			<uni-load-more color="#007AFF" iconType="circle" :status="status" :icon-size="14" :content-text="contentText" v-if="dataList.length > 0"/>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 button-default" @tap="toEmpty">清空</button>
			<button class="zd-col-8 button-primary" @tap="searchList">查询</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !checkedArr.length}" @tap="toSure">确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getMaterialList} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				dataList: [],
				// dataList: [{material_id: 'n1', material_name: 'name1', checked: false, qty:1, pcsn:'p001'},{material_id: 'n2', material_name: 'name2',  qty:2, checked: false},{material_id: 'n3', material_name: 'name3',  qty:1, checked: false}],
				pkId: '',
				pkObj: {},
				allCheck: false,
				checkedArr: [],
				reload: false,
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				totalCount: 0,
				pageNum: 1,
				pageSize: 10
			};
		},
		onLoad (options) {
			this.title = options.title
			this.searchList()
		},
		methods: {
			handleBlur (e) {
				if (e.qty) {
					if (e.qty < 0) {
						e.qty = 0
					} else {
						e.qty = e.qty.replace(/[^0-9]/g, '')
						e.qty = e.qty.replace(/^0+/, '') || '0'
					}
				} else {
					// uni.showToast({
					// 	title: '数量必填',
					// 	icon: 'none'
					// })
				}
			},
			searchList () {
				this.dataList = []
				this.pageNum = 1
				this._getMaterialList()
			},
			async _getMaterialList () {
				let res = await getMaterialList( this.val1, this.pageNum + '', this.pageSize + '')
				// this.dataList = res.data
				this.totalCount = res.totalElements
				if (res.totalElements > 0) {
					const dataMap = res.data
					this.dataList = this.reload ? dataMap : this.dataList.concat(dataMap)
					this.dataList.map(el => {
						this.$set(el, 'checked', false)
					})
					this.reload = false
				} else {
					this.dataList = []
				}
				if (this.totalCount == this.dataList.length) {
					this.reload = false
					this.status = 'noMore'
				}
			},
			onReachBottom () {
				if (this.totalCount > this.dataList.length) {
					this.status = 'loading'
					setTimeout(() => {
						this.pageNum++
						this._getMaterialList()
					}, 1000)
				} else { //停止加载
					this.status = 'noMore'
				}
			},
			toAllCheck () {
				this.allCheck = !this.allCheck
				this.dataList.map(el => {
					el.checked = this.allCheck
				})
			},
			toCheck (e) {
				e.checked = !e.checked
				this.checkedArr = this.dataList.filter(el => el.checked === true)
				this.allCheck = this.checkedArr.length === this.dataList.length
			},
			toEmpty () {
				this.val1 = ''
				this.dataList = []
				this.pageNum = 1
				this.pkId = ''
			},
			toSure () {
				if (!this.checkedArr.length) {
					return
				}
				let allValid = this.checkedArr.every(item => item.qty !== '');
				if (!allValid) {
					uni.showToast({
						title: '数量必填',
						icon: 'none'
					})
					return
				}
				this.$store.dispatch('setPublicArr', this.checkedArr)
				uni.navigateBack()
			}
		}
	}
</script>