<template>
	<view class="zd_container">
		<nav-bar :title="title"></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-17">
						<search-box
						v-model="val1"
						@handleChange="handleChange1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">载具号</span>
					</view>
					<view class="zd-col-17">
						<search-box
						v-model="val2"
						@handleChange="handleChange"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">目的地</span>
					</view>
					<view class="zd-col-17 filter_select">
						<uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view v-if="index === '1'" class="zd-row">
					<view class="zd-col-7">
						<span class="filter_label">下道工序</span>
					</view>
					<view class="zd-col-17 filter_select">
						<uni-data-select v-model="index2" :localdata="options1"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th @tap="addRow">
									<view style="display: flex;align-items: center;font-weight: normal;">
										<uni-icons type="plus-filled" size="28" color="#4e6ef2"></uni-icons>
									</view>
								</th>
								<th>订单号</th>
								<th>物料号</th>
								<th>物料数量</th>
								<th>交期时间</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td @tap="addRow">
									<view style="display: flex;align-items: center;">
										<uni-icons type="plus-filled" size="28" color="#4e6ef2"></uni-icons>
									</view>
								</td>
								<td><input type="text" class="td_input" v-model="e.order_code"></td>
								<td><input type="text" class="td_input" v-model="e.material_code"></td>
								<td><input type="number" class="td_input" v-model="e.material_qty"></td>
								<td>{{e.due_date}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
			<!-- <view v-show="dataList.length > 0 && dataList[0].order_code.indexOf('OR') !== -1" class="zd_wrapper">
				<view class="zd-row">
					<view class="zd-col-7">
						<span class="filter_label">下一道工序</span>
					</view>
					<view class="zd-col-17 filter_select">
						<uni-data-select v-model="index1" :localdata="options1"></uni-data-select>
					</view>
				</view>
			</view> -->
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-8 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-15 button-primary" :class="{'button-info': !val1 || !val2 || (index === '1' && !index2)}" :disabled="disabled" @tap="_handheldBlanking">入库确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {regionList, getSD01GroupLog, handheldBlanking} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				options: [{ value: '1', text: '货架' }, { value: '2', text: '内部' }, { value: '3', text: '外协' }, { value: '5', text: '其他加工' }],
				index: '',
				options1: [],
				index1: '',
				index2: '',
				disabled: false,
				dataList: [],
				flag: false
			};
		},
		onLoad (options) {
			this.title = options.title
			this._regionList()
		},
		methods: {
			handleChange (e) {
				if (e) {
					if (this.val1) {
						this._getSD01GroupLog(this.val1, e)
					}
				}
			},
			handleChange1 (e) {
				if (e) {
					if (this.val2) {
						this._getSD01GroupLog(e, this.val2)
					}
				}
			},
			
			selectChange (e) {
				this.dataList = []
				this.index1 = ''
				this.index2 = ''
			},
			async _regionList () {
				let res = await regionList()
				this.options1 = [...res.content]
				this.options1.map(el => {
					this.$set(el, 'text', el.label)
				})
			},
			async _getSD01GroupLog (v1, v2) {
				if (v1 !== 'SD01') {
					return
				}
				try {
					let current = await getSD01GroupLog(v1, v2)
					if (current && current?.material) {
						if (current?.material) {
							uni.showModal({
								title: '提示',
								cancelText: '取消',
								confirmText: '确定',
								content: '已发现近期从SD01出库的组盘信息，是否自动填充？',
								success: (res) => {
									if (res.confirm) {
										this.dataList = [...current.material]
										this.flag = true
										if (current?.regionCode) {
											this.index = '1'
											this.index2 = current.regionCode
										}
									}
								}
							})
						}
					}
				} catch (e) {
					console.log(e)
				}
			},
			addRow () {
				this.dataList.push({order_code: '', material_code: '', material_qty: 0, due_date: this.getCurrentDateTime()})
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.index = ''
				this.dataList = []
				this.index1 = ''
				this.index2 = ''
				this.disabled = false
				this.flag = false
			},
			async _handheldBlanking () {
				this.disabled = true
				if (!this.val1 || !this.val2) {
					this.disabled = false
					return
				}
				let arr = []
				this.dataList.map(el => {
					if (el.order_code !== '' && el.material_qty !== '') {
						arr.push(el)
					}
				})
				try {
					let res = await handheldBlanking(this.index, this.val1, this.index1, this.val2, arr, this.index2, this.flag)
					this.clearUp()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			getCurrentDateTime() {
				const now = new Date()
				const year = now.getFullYear()
				const month = String(now.getMonth() + 1).padStart(2, '0') // 月份从0开始，需要加1
				const day = String(now.getDate()).padStart(2, '0')
				const hours = String(now.getHours()).padStart(2, '0')
				const minutes = String(now.getMinutes()).padStart(2, '0')
				const seconds = String(now.getSeconds()).padStart(2, '0')
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
			}
		}
	}
</script>

<style lang="stylus">
</style>
