<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-6">
						<span class="filter_label">无人车点位</span>
					</view>
					<view class="zd-col-18 filter_select">
						<uni-data-select v-model="index1" :localdata="options1"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料类别</span>
					</view>
					<view class="zd-col-18 filter_select">
						<uni-data-select v-model="index2" :localdata="options2"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">关键字</span>
					</view>
					<view class="zd-col-14">
						<input type="text" placeholder="输入物料关键字" class="filter_input" v-model="keyword" @focus="handleFocus">
					</view>
					<button class="mini-btn" type="primary" @tap="_queryMaterialInfo">查询</button>
				</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>
								<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_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.class_name}}</td>
								<td>{{e.supp_name}}</td>
								<td>{{e.produce_name}}</td>
								<td>{{e.quality_code}}</td>
								<td>{{e.unit_name}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-11 button-primary" :class="{'button-info': !(this.index1 && this.checkedArr.length)}" @tap="toSure">确认</button>
			<button class="zd-col-11 button-primary" @tap="toQuit">退出</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryCarPoint, queryClassType, queryMaterialInfo} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				keyword: null,
				options1: [],
				// options1: [{text: '点位01', value: '01'}, {text: '点位02', value: '02'}, {text: '点位03', value: '03'}],
				index1: '',
				options2: [],
				index2: '',
				dataList: [],
				// dataList: [{material_code: 'm001', qty: 100, checked: false, initialQty: 100}, {material_code: 'm002', qty: 200, checked: false, initialQty: 200}, {material_code: 'm003', qty: 300, checked: false, initialQty: 300}, {material_code: 'm004', qty: 400, checked: false, initialQty: 400}],
				allCheck: false,
				checkedArr: []
			};
		},
		onLoad (options) {
			this.title = options.title
			this._queryCarPoint()
			this._queryClassType()
		},
		methods: {
			handleFocus () {
				this.keyword = null
			},
			async _queryCarPoint () {
				try {
					let res = await queryCarPoint()
					if (res) {
						this.options1 = res.data
					} else {
						this.options1 = []
					}
				} catch (e) {
					this.options1 = []
				}
			},
			
			async _queryClassType () {
				try {
					let res = await queryClassType()
					if (res) {
						this.options2 = res.data
					} else {
						this.options2 = []
					}
				} catch (e) {
					this.options2 = []
				}
			},
			async _queryMaterialInfo () {
				try {
					let res = await queryMaterialInfo(this.index2, this.keyword)
					if (res && res.data.length > 0) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []
					}
				} catch (e) {
					this.dataList = []
				}
			},
			toAllCheck () {
				this.allCheck = !this.allCheck
				this.dataList.map(el => {
					el.checked = this.allCheck
				})
				this.checkedArr = this.dataList.filter(el => el.checked === true)
			},
			toCheck (e) {
				e.checked = !e.checked
				this.checkedArr = this.dataList.filter(el => el.checked === true)
				this.allCheck = this.checkedArr.length === this.dataList.length
			},
			toSure () {
				if (this.index1 && this.checkedArr.length) {
					// let selobj = this.options1.find(item => item.value === this.index1)
					this.checkedArr.forEach(item => {
						item.point_code = this.index1
						item.pcsn = ''
						item.qty = ''
					})
					this.$store.dispatch('setPublicArr', this.checkedArr)
					uni.navigateBack()
				}
			},
			toQuit () {
				uni.navigateBack()
			}
		}
	}
</script>