<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-8">
						<span class="filter_label">产线站点编号</span>
					</view>
					<view class="zd-col-16">
						<search-box
						v-model="val1"
						@handleChange="handleChange1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-8">
						<span class="filter_label">出库单据编号</span>
					</view>
					<view class="zd-col-16 zd-row">
						<view class="filter_num">{{editValue.toString()}}</view>
						<search-box
						v-model="val2"
						@handleChange="handleChange2"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-8">
						<span class="filter_label">已选物料数</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_input" style="font-size: 34rpx; color: #ff6a00; font-weight: 700;">{{selectedNum}}</span>
					</view>
					<view class="zd-col-8">
						<span class="filter_label">可选物料数</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_input" style="font-size: 34rpx; color: #4e6ef2; font-weight: 700;">{{selectedNum !== null ? (9 - selectedNum) : null}}</span>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>选择</th>
								<th class="td_3"><view class="td_3">物料名称</view></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>
									<uni-icons v-show="e.status === '1'" @tap="toCheck(e)" :type="e.checked ? 'checkbox' : 'circle'" size="24" color="#4e6ef2"></uni-icons>
									<uni-icons v-show="e.status !== '1'" type="smallcircle-filled" size="24" color="#fff"></uni-icons>
								</td>
								<td class="td_3">{{e.material_name}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_status}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.unit_name}}</td>
								<td>{{e.prd_ppbom_no}}</td>
								<td>{{e.vehicle_code}}</td>
								<td>{{e.pcsn}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': checkData.length === 0}" :disabled="disabled" @tap="_ctuOutConfirm">出库物料提交</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryTargetPoint, getCtuOrderList, ctuOutConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				editValue: [], // 出库单据编号集合
				selectedNum: null, // 已选物料数
				dataList: [],
				disabled: false,
				checkData: []
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			handleChange1 (e) {
				this._queryTargetPoint(e)
			},
			// 产线站点编码验证
			async _queryTargetPoint (e) {
				try {
					let res = await queryTargetPoint(e)
					if (res.code === '200') {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.val1 = res.site_code
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.val1 = ''
					}
				} catch (e) {
					this.val1 = ''
				}
			},
			handleChange2 (e) {
				if (this.editValue.indexOf(e) === -1) {
				  this._getCtuOrderList(e)
				} else {
					uni.showToast({
						title: '出库单据编号已存在',
						icon: 'none'
					})
				}
				setTimeout(() => {
					this.val2 = null
				}, 300)
			},
			async _getCtuOrderList (e) {
				try {
					let res = await getCtuOrderList(this.val1, e)
					if (res.code === '200') {
						this.checkData = []
						for (let i = 0; i < res.content.length; i++) {
							if (this.selectedNum < 9 && res.content[i].status === '1') {
								this.$set(res.content[i], 'checked', true)
								this.selectedNum += 1
							} else {
								this.$set(res.content[i], 'checked', false)
							}
						}
						this.dataList = [...this.dataList, ...res.content]
						let arr = this.dataList.filter(el => el.checked === true)
						this.checkData = arr
						if (res.data !== null) {
							this.editValue.push(res.data)
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				} catch (e) {
					console.log(e)
				}
			},
			toCheck (e) {
				if (!e.checked && this.selectedNum >= 9) {
					return
				} else if (!e.checked && this.selectedNum < 9) {
					e.checked = true
					this.selectedNum++
				} else if (e.checked) {
					e.checked = false
					this.selectedNum--
				}
				let arr = this.dataList.filter(el => el.checked === true)
				this.checkData = arr
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.selectedNum = null
				this.dataList = []
				this.disabled = false
				this.checkData = []
				this.editValue = []
			},
			async _ctuOutConfirm () {
				this.disabled = true
				if (this.checkData.length === 0) {
					this.disabled = false
					return
				}
				try {
					let res = await ctuOutConfirm(this.val1, this.editValue.toString(), this.checkData)
					if (res.code === '200') {
						this.clearUp()
					}
					this.disabled = false
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>
