<template>
	<view class="zd_container">
		<!-- 人工叫料 -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view>
					<view class="filter_label">{{$t('label.OutWarehouseArea')}}</view>
					<view class="filter_select">
						<uni-data-select :placeholder="$t('utils.selectPlaceholder')" :emptyTips="$t('utils.selectEmptyTips')" v-model="index" :localdata="options"></uni-data-select>
					</view>
				</view>
				<view>
					<view class="filter_label">{{$t('label.MaterialInquiry')}}</view>
					<view class="zd-row">
						<view class="zd-col-18">
							<input type="text" class="filter_input" v-model="val2">
						</view>
						<button class="mini-btn" type="primary" size="mini" style="margin-right: 0;margin-left: 10rpx;" @tap="_linegetMaterialDtl">{{$t('button.search')}}</button>
					</view>
				</view>
				<view class="is-required">
					<view class="filter_label">{{$t('label.EndPosition')}}</view>
					<search-box v-model="val1"/>
				</view>
				<view class="is-required">
					<view class="filter_label">{{$t('label.DestinationFloor')}}</view>
					<view class="filter_select">
						<uni-data-select :placeholder="$t('utils.selectPlaceholder')" :emptyTips="$t('utils.selectEmptyTips')" v-model="index1" :localdata="options1"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>{{$t('th.WarehouseLocation')}}</th>
								<th>{{$t('label.CarrierCode')}}</th>
								<th>{{$t('label.MaterialName')}}</th>
								<th>{{$t('label.MaterialCode')}}</th>
								<th>{{$t('th.Quantity')}}</th>
								<th>{{$t('th.Unit')}}</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @tap="toChek(e)" :class="{'checked': e.checked}">
								<td>{{e.struct_code}}</td>
								<td>{{e.storagevehicle_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.qty_unit_name}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="clearUp">{{$t('button.clear')}}</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': allChecked.length === 0 || !val1 || !index1}" @tap="_callMaterialConfirm">{{$t('button.confirm')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getSect, getFloor, linegetMaterialDtl, callMaterialConfirm} from '@/utils/getData.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				val1: '',
				val2: '',
				options1: [],
				index1: '',
				dataList: [],
				allChecked: [],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._getSect()
			this._getFloor()
		},
		methods: {
			async _getSect () {
				try {
					let res = await getSect()
					if (res) {
						this.options = [...res]
					} else {
						this.options =[]
					}
				} catch (e) {
					this.options = []
				}
			},
			async _getFloor () {
				try {
					let res = await getFloor()
					if (res) {
						this.options1 = [...res]
					} else {
						this.options1 =[]
					}
				} catch (e) {
					this.options1 = []
				}
			},
			async _linegetMaterialDtl () {
				this.allChecked = []
				try {
					let res = await linegetMaterialDtl(this.index, this.val2)
					if (res.status === '200') {
						this.dataList = [...res.data]
						this.dataList.map(el => {
							this.$set(el, 'checked', false)
						})
					} else {
						this.dataList = []
					}
				} catch (e) {
					this.dataList = []
				}
			},
			toChek (e) {
				e.checked = !e.checked
				this.allChecked = this.dataList.filter(el => el.checked === true)
			},
			async _callMaterialConfirm () {
				this.disabled = true
				if (!this.allChecked.length === 0 || !this.val1 || !this.index1) {
					this.disabled = false
					return
				}
				try {
					let res = await callMaterialConfirm(this.val1, this.allChecked, this.index1)
					if (res.status === '200') {
						this.clearUp()
					}
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.index = ''
				this.val1 = ''
				this.val2 = ''
				this.index1 = ''
				this.allChecked = []
				this.dataList = []
			}
		}
	}
</script>
