<template>
	<view class="zd_container">
		<!-- <nav-bar title="管芯备货"></nav-bar> -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label">{{$t('filter.area')}}</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :placeholder="$t('uni.dataSelect.placeholder')" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="filter_item is-required">
					<view class="filter_label_wraper">
						<span class="filter_label">{{$t('filter.corespecifications')}}</span>
					</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :placeholder="$t('uni.dataSelect.placeholder')" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>{{$t('grid.number')}}</th>
								<th class="th_2">{{$t('filter.pallet-number')}}</th>
								<th>{{$t('grid.coretype')}}</th>
								<th>{{$t('grid.corename')}}</th>
								<th>{{$t('filter.quantity')}}</th>
								<th>{{$t('filter.point')}}</th>
								<th>{{$t('filter.area')}}</th>
								<th>{{$t('grid.areaname')}}</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @click="toCheck(e)" :class="{'checked': e.material_code === pkId}">
								<td>{{Number(i) + 1}}</td>
								<td class="td_2">{{e.vehicle_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.total_num}}</td>
								<td>{{e.point_code}}</td>
								<td>{{e.product_area}}</td>
								<td>{{e.product_area_name}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-5 btn-submit btn-default letter-30" @tap="clearUp">{{$t('button.clear')}}</button>
			<button class="zd-col-8 btn-submit btn-success letter-30" :class="{'btn-info': !index2}" :disabled="disabled" @tap="handleConfirm">{{$t('button.stockup')}}</button>
			<button class="zd-col-8 btn-submit btn-success" @tap="_showPapervehicleView">{{$t('button.refresh')}}</button>
		</view>
		<up-top ref="UT" :scrollTop="top"></up-top>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import NumberInput from '@/components/NumberInput.vue'
	import { confirmAction } from '@/utils/utils.js'
	import {showPapervehicleView, moveStock, queryProductArea, queryPaperMaterial} from '@/utils/getData4.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			NumberInput
		},
		data() {
			return {
				pkId: '',
				pkObj: {},
				// val3: '',
				title: '',
				top: 0,
				options1: [],
				index1: '',
				options2: [],
				index2: '',
				disabled: false,
				dataList: []
			};
		},
		onLoad (options) {
			this.title = options.title
			this._queryProductArea()
			this._queryPaperMaterial()
			this._showPapervehicleView()
		},
		onPageScroll(e) {
			this.$refs.UT.topData(e.scrollTop)
		},
		methods: {
			/** 区域下拉框查询 */
			async _queryProductArea () {
				let res = await queryProductArea()
				this.options1 = [...res.rows]
			},
			selectChange1(e) {
				this.index1 = e
				if (e) {
					this._queryPaperMaterial(e)
				} else {
					this.index2 = ''
				}
			},
			/** 管芯规格下拉框 */
			async _queryPaperMaterial (e) {
				let res = await queryPaperMaterial(e)
				this.options2 = [...res.rows]
			},
			selectChange2(e) {
				this.index2 = e
			},
			toCheck (e) {
				this.pkId = this.pkId === e.material_code ? '' : e.material_code
				this.pkObj = this.pkId === e.material_code ? e : {}
			},
			async _paperQueryPaperMaterial () {
				let res = await paperQueryPaperMaterial()
				this.dataList = [...res.rows]
			},
			async handleConfirm() {
				if (!this.index1 || !this.index2) {
					return
				}
				const isConfirmed = await confirmAction("确认操作", "确定要执行此操作吗？")
				if (isConfirmed) {
					this._moveStock()
				}
			},
			async _moveStock () {
				this.disabled = true
				try {
					let res = await moveStock(this.pkObj)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.disabled = false
					this.pkId = ''
					this.pkObj = {}
					this.dataList = []
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.index1 = ''
				this.index2 = ''
				this.options2 = []
				this.upL = ''
				this.upR = ''
				this.lowL = ''
				this.lowR = ''
				this.disabled = false
			},
			async _showPapervehicleView () {
				let res = await showPapervehicleView(this.index1, this.index2)
				if (res) {
					this.dataList = [...res.rows]
				}
			}
		}
	}
</script>
<style>
.custom-input {
  border: 1px solid #ccc;
  border-radius: 4px;
  /* padding: 8px 12px; */
  font-size: 16px;
  width: 100%;
  height: 30px;
  line-height: 30px;
}
</style>
