<style lang="scss" scoped>
	.container{
		height: 100%;
		background:#F6F6F6;
		padding-bottom:20rpx;
		
		scroll-view{
			flex-grow: 1;
			height:1rpx;

			.item{
				margin-top:0;
				display:flex;
				&>.img{
					display:flex;
					flex-direction: column;
					justify-content: space-between;
					margin-right:28rpx;
					width:164rpx;
					.u-transition{
						flex-shrink: 0;
					}
					&>text{
						color:rgba(var(--c),1);
						background:rgba(var(--c),0.1);
						font-size:24rpx;
						padding:10rpx;
						text-align: center;
						margin-bottom:-10rpx;
						&:before{
							margin-right:6rpx;
						}
					}
				}
				.info{
					flex-grow: 1;
					width:1rpx;
					.title{
						display:flex;
						align-items: center;
						font-weight: bold;
						text{
							margin-left:auto;
							color:#333;
						}
					}
					.adder{
						color:#999;
						font-size:24rpx;
						margin-top: 16rpx;
					}
					.data{
						display:flex;
						justify-content: space-between;
						align-items: center;
						margin-top:16rpx;
						color:#999;
						font-size:24rpx;
						text:nth-child(2){
							color:#10C176;
							font-weight: bold;
							font-size: 32rpx;
						}
					}
					.btns{
						display: flex;
						justify-content: space-between;
						align-items: center;
						border-top:2rpx solid #D8D8D8;
						margin:20rpx 0 -30rpx 0;
						text{
							padding:16rpx 0 30rpx 0;
							color:#999;
							font-size:24rpx;
							&:before{
								color:#10C176;
								margin-right:8rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
<template>
	<view class="container flex">
		<d-navbar style="--bg:#10C176;--c:#fff;" text="仓库管理" isBack>
			<text class="plant-xinjian" slot="right" @click="toAdd()"/>
		</d-navbar>

		<d-search v-model="searchVal.search" @search="clean();search()"/>
		
		<scroll-view scroll-y @scrolltolower="search">
			<view class="card item" v-for="(v,k) in list" :key="k" @click="toDetail(v)">
				<view class="img">
					<u-image width="100%" height="144rpx" radius="16rpx" mode="aspectFill" :src="`${host}/${v.warehousePic[0]}`">
						<text slot="error" class="plant-xiaopangchetupianjiazaishibai" style="font-size:45rpx"/>
					</u-image>
					<text v-if="v.goodsType==2" class="plant-zhongzhi" style="--c:16, 193, 118;">农产品</text>
					<text v-else class="plant-nongziguanli" style="--c:248, 203, 54;">农资</text>
				</view>
				<view class="info">
					<view class="title">{{v.warehouseName}} <text class="plant-youbian"/></view>
					<view class="adder">{{v.warehouseAddress}}</view>
					<view class="data">
						<text>库存种类：{{v.inventoryCount||0}}种</text> <text>{{v.warehouseVolume||0}}㎡</text>
					</view>
					<view class="btns">
						<text class="plant-xiugai" @click.stop="toAdd(v)">修改</text>
						<text class="plant-shanchu" @click.stop="del(k)">删除</text>
						<text class="plant-chakan" @click.stop="$u.route({url:'pages/stock/mana',params:{id:v.id, goodsType:({1:'nz',2:'ncp'})[v.goodsType]}})">查看库存</text>
					</view>
				</view>
			</view>
			<u-empty v-if="list.length==0" text="为查询到相关信息" width="70%" icon="/static/noData.png"/>
		</scroll-view>
	</view>
</template>
<script>
import request,{host} from '@/common/request'
	export default {
		data(){
			return{
				searchVal:{
					baseId: this.$store.state.baseInfo.id,
					search:"",
					pageNo:1,
					pageSize:6
				},
				list:[],
				total:0,
				host:host('imgUrl'),
			}
		},
		onLoad(options){
			this.search()
		},
		methods:{
			clean(){
				this.list=[]
				this.total=0
				this.searchVal.pageNo=1
			},
			async search(){
				if(this.total==0||this.list.length<this.total){
					var res=await request("/api/WarehouseManageApi/queryWarehouseList",{
						method:"post",
						body: this.searchVal
					})
					if(res.statu){
						this.list=this.list.concat(res.data.records.map(v=>({...v,warehousePic:(v.warehousePic||'').split(",").filter(v=>v)})))
						this.total=res.data.total
						this.searchVal.pageNo++
						if(this.list.length==this.total)uni.showToast({title:"加载完成！",icon:"none"})
					}else{
						uni.showModal({
							title:"提示",
							content:res.msg||"获取仓库列表失败！",
							showCancel:false,
						})
					}
				}
			},
			del(index){
				uni.showModal({
					title:"提示",
					content:"确定删除？",
					success: async res=>{
						if(res.confirm){
							var res=await request("/api/WarehouseManageApi/delete",{
								method:"delete",
								params:{id: this.list[index].id}
							})
							if(res.statu){
								if(this.list.length>10){
									this.list.splice(index,1)
								}else{
									this.clean()
									this.search()
								}
								uni.showToast({
									title:"删除成功！",
									icon:"success"
								})
							}else{
								uni.showToast({
									title:"删除失败！",
									icon:"error"
								})
							}
						}
					}
				})
			},
			toAdd(e){
				uni.navigateTo({
					url:`/pages/warehouse/info`,
					events:{update:()=>{
						this.clean()
						this.search()
					}},
					success(res){
						res.eventChannel.emit("detail", e)
					}
				})
			},
			toDetail(e){
				uni.navigateTo({
					url:"/pages/warehouse/detail",
					success(res){
						res.eventChannel.emit('detail', {...e})
					}
				})
			}
		}
	}
</script>
