<template>
	<view class="mainBox">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" title="出库管理" @clickLeft="back"></uni-nav-bar>

		<view class="tab-title">
			<view class="tab_box rx-sc">
				<view class="tab_item" v-for="(item, index) in tabList" :key="index" v-text="item.label"
					:class="{active: pickTabIndex == index}" @click="changeChartsTab(index)"></view>

				<view class="more_search">
					<image src="~@/static/moreSearch.svg" mode="" @click="searchVisible = true"></image>
				</view>
			</view>
		</view>
		<card :list="tableList"></card>
		<CommonFooter leftText="新增出库" @leftClick="addStock('hand')" />

		<!-- 搜索组件 -->
		<u-popup :show="searchVisible" closeOnClickOverlay mode="top" @close="searchVisible = false" @open="openSearch">
			<view class="search_list">
				<u-form labelPosition="left" :model="popupInfo" labelWidth="180" labelAlign="left" class="baseForm">
					<u-form-item label="出库时间:" class="required-form" borderBottom prop="assetType">
						<uni-datetime-picker v-model="popupInfo.range" :border="true" :icon="false" type="daterange" />
					</u-form-item>
					<u-form-item label="出库类型:" class="required-form" borderBottom prop="bizType">
						<zxz-uni-data-select :localdata="outputSceneState" v-model="popupInfo.bizType" dataValue='value'
							dataKey="text" filterable format='{text}'></zxz-uni-data-select>
					</u-form-item>
					<u-form-item label="出库单号:" class="required-form" borderBottom prop="sourceBizNo">
						<uni-easyinput v-model="popupInfo.sourceBizNo" :inputBorder="false" placeholder="请输入" />
					</u-form-item>
					<u-form-item label="物品名称:" class="required-form" borderBottom prop="categoryName">
						<uni-easyinput v-model="popupInfo.categoryName" :inputBorder="false" placeholder="请输入" />
					</u-form-item>
					<u-form-item label="物品编码:" class="required-form" borderBottom prop="categoryCode">
						<uni-easyinput v-model="popupInfo.categoryCode" :inputBorder="false" placeholder="请输入" />
					</u-form-item>
				</u-form>
			</view>
			<view class="operate_box rx-bc">
				<u-button size="small" class="u-reset-button" @click="handleReset">
					重置
				</u-button>
				<u-button type="success" size="small" class="u-reset-button" @click="handleSearch">
					确定
				</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		outputSceneState
	} from '../common'
	import {
		getOutInList
	} from '@/api/warehouseManagement'
	import card from './components/card.vue'
	import CommonFooter from '.././components/CommonFooter.vue'
	export default {
		components: {
			card,
			CommonFooter
		},
		data() {
			return {
				outputSceneState,
				page: 1,
				size: 10,
				isEnd: false,
				searchVisible: false,
				tabList: [{
						value: '',
						label: '全部'
					},
					{
						value: 3,
						label: '已驳回'
					},
					{
						value: 2,
						label: '审核通过'
					},
					{
						value: 1,
						label: '审核中'
					},
					{
						value: 0,
						label: '未提交'
					}
				],
				pickTabIndex: 0,
				popupInfo: {
					status: '',
					range: [],
					bizType: '',
					sourceBizNo: '',
					categoryName: '',
					categoryCode: ''
				}, //筛选数据
				tableList: []
			}
		},
		onShow() {
			this.getFirstList()
		},
		//触底加载
		onReachBottom: function() {
			if (this.isEnd) {
				uni.showToast({
					title: '已经没有更多数据了！',
					icon: 'none',
					duration: 1000 //提示持续时间
				})
				return
			}
			// 显示加载图标
			uni.showLoading({
				title: '数据加载中'
			})
			this.getMoreLists()
		},
		methods: {
			handleReset() {
				this.popupInfo = {
					status: '',
					range: [],
					bizType: '',
					sourceBizNo: '',
					categoryName: '',
					categoryCode: ''
				}
				this.pickTabIndex = 0
				this.tableList = []
				this.getFirstList()
			},
			handleSearch() {
				this.getFirstList()
			},
			statusChange(code) {
				let index = this.popupInfo.status.indexOf(code)
				if (index === -1) {
					this.popupInfo.status.push(code)
				} else {
					this.popupInfo.status.splice(index, 1)
				}
			},
			// 搜索弹窗
			openSearch() {
				// console.log('open');
			},
			getFirstList() {
				this.page = 1
				this.searchVisible = false
				this.isEnd = true
				uni.showLoading({
					title: '数据加载中'
				})
				this.getList(this.popupInfo)
			},
			getMoreLists() {
				//获取更多数据
				this.page++
				this.getList(this.popupInfo)
			},
			//获取列表信息
			getList() {
				uni.showLoading({
					title: '加载中'
				})
				let data = {
					...this.popupInfo,
					pageNum: this.page,
					size: this.size,
					type: 2
				}
				if (data.range?.length) {
					data.startTime = data.range[0]
					data.endTime = data.range[1]
				}
				delete data.range
				getOutInList(data)
					.then(res => {
						if (this.page === 1) {
							this.tableList = res.list
						} else {
							this.tableList.push(...res.list)
						}
						this.isEnd = this.tableList.length >= res.count
					})
					.then(() => {
						uni.hideLoading()
					})
			},
			//切换tab
			changeChartsTab(index) {
				this.pickTabIndex = index
				this.popupInfo.status = this.tabList[index].value
				this.handleSearch()
			},
			//新增入库单
			addStock(type) {
				uni.navigateTo({
					url: '/pages/warehouse/outHouse/addStock?type=' + type
				})
			},
			//查看详情
			viewDetails(id, code) {
				uni.navigateTo({
					url: '/pages/warehouse/outHouse/details?id=' + id + '&code=' + code
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mainBox {
		padding-bottom: 120rpx;
	}

	.tab-title {
		display: flex;
		align-items: center;
		background-color: #fff;
		position: relative;

		.tab_box {
			width: 100%;
			height: 68rpx;
		}

		.tab_item {
			height: 68rpx;
			line-height: 68rpx;
			padding: 0 20rpx;
			font-size: 32rpx;
			// color: #979C9E;

		}

		.more_search {
			display: flex;
			align-items: center;
			height: 70rpx;
			line-height: 70rpx;

		}

		image {
			width: 52rpx;
			height: 52rpx;
			margin-left: 10rpx;
		}

		.active {
			box-sizing: border-box;
			border-bottom: 6rpx solid $theme-color;
			color: $theme-color;
		}
	}

	.search_list {
		min-height: 100rpx;

		/deep/ .baseForm {
			padding:  40rpx 20rpx 20rpx 20rpx;
		}
	}

	.operate_box {
		padding: 10rpx 32rpx;

		/deep/ .u-button {
			width: 40%;
		}
	}
</style>