<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-24 filter_select">
						<uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">点位</span>
					</view>
					<view class="zd-col-24 filter_select">
						<search-box
						v-model="val1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">物料</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input" v-model="currentData.material_name" @tap="toJump">
					</view>
				</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.is_used}}</td>
								<td>{{e.point_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.measure_unit_id}}</td>
								<td>{{e.containers.join(',')}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
			<!-- <uni-load-more color="#007AFF" iconType="circle" :status="status" :icon-size="14" :content-text="contentText" v-if="dataList.length > 0"/> -->
		</view>
		<view class="zd-row submit-bar">
			<!-- <button class="zd-col-6 button-default" @tap="clearUp">清空</button> -->
			<button class="zd-col-6 button-primary" @tap="searchList">查询</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !checkedArr.length}" :disabled="disabled" @tap="_lock">锁定</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !checkedArr.length}" :disabled="disabled2" @tap="_unlock">解锁</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getRegions, getPointInfo, lock, unlock} from '@/utils/getData4.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '1',
				currentData: {},
				dataList: [],
				allCheck: false,
				checkedArr: [],
				options: [],
				index: '',
				disabled: false,
				disabled2: false,
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._getRegions()
		},
		onShow() {
			if (this.$store.getters.publicObj !== '') {
				this.currentData = this.$store.getters.publicObj
				this.$store.dispatch('setPublicObj', '')
			}
		},
		methods: {
			searchList () {
				this.dataList = []
				this._getPointInfo()
			},
			toAllCheck () {
				this.allCheck = !this.allCheck
				this.dataList.map(el => {
					el.checked = this.allCheck
				})
			},
			toCheck (e) {
				e.checked = !e.checked
				this.checkedArr = this.dataList.filter(el => el.checked === true)
				this.allCheck = this.checkedArr.length === this.dataList.length
			},
			toJump () {
				uni.navigateTo({
					url: '/pages/common/mater-list2?title=查询物料'
				})
			},
			async _getPointInfo () {
				let mcode = this.currentData.material_code || ''
				let res = await getPointInfo( this.index, this.val1, mcode)
				this.dataList = res.data
				// this.totalCount = res.totalElements
				// if (res.totalElements > 0) {
				// 	const dataMap = res.data
				// 	this.dataList = this.reload ? dataMap : this.dataList.concat(dataMap)
				// 	this.reload = false
				// } else {
				// 	this.dataList = []
				// }
				// if (this.totalCount == this.dataList.length) {
				// 	this.reload = false
				// 	this.status = 'noMore'
				// }
			},
			async _getRegions () {
				try {
					let res = await getRegions()
					if (res) {
						this.options = res.data
					} else {
						this.options =[]
					}
				} catch (e) {
					this.options = []
				}
			},
			selectChange (e) {
				this.index = e
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.index = ''
				this.index2 = ''
				this.disabled = false
				this.disabled2 = false
			},
			async _lock () {
				this.disabled = true
				if (!this.checkedArr.length) {
					this.disabled = false
					return
				}
				try {
					let res = await lock(this.checkedArr)
					if (res.code === '200') {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.clearUp()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.disabled = false
					}
				} catch (e) {
					this.disabled = false
				}
			},
			async _unlock () {
				this.disabled2 = true
				if (!this.checkedArr.length) {
					this.disabled2 = false
					return
				}
				try {
					let res = await unlock(this.checkedArr)
					if (res.code === '200') {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.clearUp()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.disabled2 = false
					}
				} catch (e) {
					this.disabled2 = false
				}
			}
		}
	}
</script>

<style lang="stylus">

</style>
