<template>
	<view class="moreShelfPart flex flex_column">
		<view class="moreShelfPart-search">
			<u-search
			placeholder="请输入货架名称搜索" 
			v-model="shelfName" 
			:show-action="isGobleSearch" 
			@input="change"
			@focus="isGobleSearch=true"
			@blur="isGobleSearch=false"
			@custom="getShelfList" 
			@search="getShelfList" 
			@clear="clearSearch"
			:action-style="{'color': '#fff', 'font-size': '24upx', 'background-color': '#57a3f3', 'border-radius': '6upx', 'padding': '12upx 0'}">
			</u-search>
		</view>
		<view class="moreShelfPart-body">
			<view class="nav-right">
				<view 
				class="nav-right-item" 
				v-for="(item, index) in shelfList" 
				:key="item.id" 
				@click="viewDetail(item)"
				>
					<view class="item-info">
						<view class="item-name">
							<text>{{item.shelfName}}</text>
						</view>
						<view class="item-detail" v-if="item.checkState=='WAIT'">
							<view>
								盈亏总数量：
								<text class="item-detail-text">{{item.totalProfitLossQty}}</text>
							</view>
							<view>
								盈亏总金额：
								<text class="item-detail-text">{{item.totalProfitLossCost}}</text>
							</view>
							<view></view>
						</view>
						<view class="item-detail" v-else>
							<view>
								产品款数：
								<text class="item-detail-text">{{item.totalCategory}}</text>
							</view>
							<view>
								可用库存：
								<text class="item-detail-text">{{item.totalStockQty}}</text>
							</view>
							<view>
								冻结库存：
								<text class="item-detail-text">{{item.totalFreezeQty}}</text>
							</view>
						</view>
					</view>
					<view class="item-info button-box justify_between align_center" v-if="item.checkState=='WAIT'">
						<text>{{item.checkStateDictValue}}</text>
						<view>
							<u-button @click.stop="delCheck(item)" size="mini" :hair-line="false" shape="circle" plain type="error" hover-class="none" >删除盘点</u-button>
							<u-button @click.stop="viewDetail(item)" size="mini" :hair-line="false" shape="circle" plain type="primary" hover-class="none" >继续盘点</u-button>
						</view>
					</view>
				</view>
				<view v-if="shelfList&&shelfList.length==0">
					<u-empty v-if="status!='loading'" :src="`/static/${$config('themePath')}/def_no_data@3x.png`" icon-size="180" :text="noDataText" mode="list" :margin-top="50"></u-empty>
				</view>
				<view style="padding: 20upx;" v-if="shelfList.length">
					<u-loadmore :status="status" />
				</view>
			</view>
		</view>
		<freezeQtyModal :openModal="showModal" :infoData="tempData" @close="showModal=false" @ok="toCheck"></freezeQtyModal>
	</view>
</template>

<script>
	import { selectShelfStockCheck, stockCheckDeleteBySn } from '@/api/stockCheck'
	import freezeQtyModal from './freezeQtyModal'
	import { clzConfirm } from '@/libs/tools'
	export default {
		components: {
			freezeQtyModal
		},
		data() {
			return {
				shelfName: '',
				isGobleSearch: false,
				shelfList: [],
				noDataText: '暂无货架',
				status: 'loading',
				tempData: null,
				showModal: false
			}
		},
		onShow() {
			this.getShelfList()
		},
		methods: {
			clearSearch(){
				this.shelfName = ''
				this.shelfList = []
				this.getShelfList()
			},
			change(v){
				if(v==''){
					this.clearSearch()
				}
			},
			// 获取数字货架列表
			getShelfList(){
				const _this = this
				_this.status = 'loading'
				selectShelfStockCheck().then(res => {
					uni.hideLoading()
					if(res.status==200){
						let list = res.data.filter(item => item.shelfName.indexOf(this.shelfName)>=0)
						if (list && list.length){
							_this.status = 'nomore'
							_this.shelfList = list
						} else {
							_this.shelfList = []
							_this.status = 'nomore'
							_this.noDataText = '没有查询到相关货架'
						}
					}else{
						_this.status = 'loadmore'
						_this.shelfList = []
						_this.noDataText = res.message ? res.message : '网络似乎出错了，请稍后再试'
					}
				})
			},
			// 删除盘点单
			delCheck(row){
				const _this = this
				clzConfirm({
				  title: '提示',
				  content: '删除后不可恢复，确定删除吗？',
				  success (ret) {
					if (ret.confirm || ret.index == 0) {
						uni.showLoading({
							title:"正在删除.."
						})
						stockCheckDeleteBySn({ stockCheckSn: row.stockCheckSn }).then(res => {
						  if (res.status == 200) {
							  uni.showToast({
								icon: "none",
								title: res.message
							  })
							  _this.getShelfList()
						  }
						  uni.hideLoading()
						})
					}
				  }
				})
			},
			viewDetail(item){
				this.tempData = item
				// 如果有冻结库存
				if(item.totalFreezeQty>0){
					 this.showModal = true
				}else{
					// 去库存盘点
					this.toCheck()
				}
			},
			toCheck(){
				uni.navigateTo({
					url:"/pages/stockCheck/startCheck?data="+encodeURIComponent(JSON.stringify(this.tempData))
				})
			}
		}
	}
</script>

<style lang="less">
	.moreShelfPart{
		width: 100%;
		height: 100vh;
		background-color: #f8f8f8;
		.moreShelfPart-search{
			padding: 20rpx;
		}
		.moreShelfPart-body{
			flex-grow: 1;
		}
		.nav-right{
			padding: 0 30upx;
			height: calc(100vh - 110rpx);
			box-sizing: border-box;
			overflow: auto;
			.nav-right-item{
				padding:20upx;
				border: 2rpx solid #f5f5f5;
				border-radius: 15upx;
				margin-bottom: 20upx;
				box-shadow: 3rpx 3rpx 8rpx #eee;
				background-color: #ffff;
				&:active{
					background: #f8f8f8;
				}
				.item-info{
					.item-name{
						font-size: 30upx;
						display: flex;
						align-items: center;
					}
					.item-detail{
						margin-top: 15rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						color: #9DA8B5;
						font-size: 26upx;
						.item-detail-text{
							font-size: 26upx;
							color: #333;
						}
					}
				}
				.button-box{
					display: flex;
					margin-top: 20upx;
					button{
						margin: 0 10upx;
					}
					> text{
						font-size: 24upx;
						color: #00aaff;
					}
				}
			}
		}
	}
</style>
