<template>
	<!-- 我的钢瓶 -->
	<view>
		<view class="head_sticky">
			<view class="head_mine_title">我的钢瓶</view>
			<view style="width: 100%;">
				<u-tabs :current="cylinderNumber" :list="bottleList" lineWidth="40" :scrollable="false"
					@click="bottleClick"></u-tabs>
			</view>
		</view>
		<view style="width: 100%;border-bottom: 1rpx solid #e4e7ed;">
			<u-tabs :list="list4" :current="belowStandard" lineWidth="20" :scrollable="false" lineHeight="7"
				:lineColor="`url(${lineBg}) 100% 100%`" @click="standardClick"
				itemStyle="padding-left: 15px; padding-right: 15px; height: 34px;" v-if="current == 3">
			</u-tabs>
		</view>
		<!-- 在这里放置可滚动的内容 -->
		<view v-if="list.length > 0">
			<u-swipe-action>
				<u-swipe-action-item :name="index" :options="optionsList" v-for="(item,index) in list" :key="index"
					@click="event=>slideDelete(event,item)" :ref="'swipeAction' + index">
					<view class="card_dinay" @click="goCylinder(item)">
						<view style="display: flex;align-items: center;">
							<view class="item_current">{{index + 1}}</view>
							<view class="item_title1">{{item.inner_code}}</view>
						</view>
						<view class="iconfont icon-yehuaqiping icon_cylinder" :style="{color:getColor()}"></view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
			<view style="width: 100%;height: 150rpx;"></view>
		</view>
		<view class="empty_information" v-else>
			<u-empty mode="list" text="暂无钢瓶"></u-empty>
		</view>
		<view class="card_button_bottom">
			<view class="bottom_btn_flex">
				<u-button class="but_allot" type="primary" text="添加" @click="addCylinder"></u-button>
			</view>
			<view style="flex: 1;" v-if="disqualification">
				<view class="bottom_btn_flex" v-if="allotFlag">
					<u-button class="but_allot" type="primary" text="调拨" @click="transferCylinders"></u-button>
				</view>
				<view class="bottom_btn_flex" v-else>
					<u-button class="but_allot" type="success" text="充装" @click="transferCylinders"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			tabNumber: {
				type: Number,
				default: 0
			},
		},
		data() {
			return {
				lineBg: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAOCAYAAABdC15GAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAFxSURBVHgBzZNRTsJAEIb/WTW+lpiY+FZPIDew3ABP4GJ8hxsI9zBpOYHeQDwBPQI+mRiRvpLojtPdYhCorQqF/6GdbGd2vvwzBXZcNAt4oj1ANeUoAT5iqkUjbEFLHNmhD1YPEvpZ3ghkGlVDCkc94/BmHMq998I5ONiY1ZBfpKAyuOtgAc5yOEDmYEWNh32BHF91sGHZHmwW4azciN9aQwnz3SJEgOmte+R2tdLprTYoa50mvuomlLpD4Y3oQZnov6D2RzCqI93bWOHaEmAGqQUyRBlZR1WfarcD/EJ2z8DtzDGvsMCwpm8XOCfDUsVOCYhiqRxI/CTQo4UOvjzO7Pow18vfywneuUHHUUxLn55lLw5JFpZ8bEUcY8oXdOLWiHLTxvoGpLqoUmy6dBT15o/ox3znpoycAmxUsiJTbs1cmxeVKp+0zmFIS7bGWiVghC7Vwse8jFKAX9eljh4ggKLLv7uaQvG9/F59Oo2SouxPu7OTCxN/s8wAAAAASUVORK5CYII=',
				list: [],
				bottleList: [{
					name: '重瓶区',
				}, {
					name: '空瓶区',
				}, {
					name: '不合格瓶区'
				}],
				current: 1,
				Pagination: {
					PageIndex: 1,
					PageSize: 20,
					Total: 0,
				},
				loadingMore: true,
				allotFlag: true,
				disqualification: true,
				optionsList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				list4: [{
					name: '超期',
				}, {
					name: '报废'
				}],
				belowStandard: 0,
				cylinderNumber: 0,
			}
		},
		watch: {
			tabNumber: {
				handler: function(newUser, oldUser) {
					if (newUser) {
						this.cylinderNumber = newUser
					}
				},
				immediate: true,
				deep: true
			}
		},
		mounted() {
			var userInfo = this.$cache.getCache('userInfo')
			this.optionsList = this.options
			this.getList()
		},
		methods: {
			// 刷新列表
			refreshList() {
				this.list = []
				this.Pagination.PageIndex = 1
				this.getList()
			},
			// 删除钢瓶
			slideDelete(event, value) {
				console.log(event,value,24)
				if (this.current == 1) {
					this.delSteelCylinder(value.id, event.name)
				} else if (this.current == 2) {
					let arr = []
					if (event.index == 0) {
						// 超期
						arr.push(value.inner_code)
						this.editSteelCylinder('extended', arr, event.name)
					} else if (event.index == 1) {
						// 报废
						arr.push(value.inner_code)
						this.editSteelCylinder('scrap', arr, event.name)
					} else if (event.index == 2) {
						this.delSteelCylinder(value.id, event.name)
					}
				} else if (this.current == 3) {
					if (this.belowStandard == 0) {
						let arr = []
						if (event.index == 0) {
							arr.push(value.inner_code)
							this.editSteelCylinder('2', arr, event.name)
						} else {
							this.delSteelCylinder(value.id, event.name)
						}
					} else if (this.belowStandard == 1) {
						this.delSteelCylinder(value.id, event.name)
					}
				}
			},
			// 修改钢瓶状态
			editSteelCylinder(type, list, index) {
				this.$api.put('/api/gas-cylinder-status', {
					status: type,
					innerCodeList: list,
				}).then(res => {
					if (res.code == 200) {
						this.list.splice(index, 1);
						uni.$u.toast(res.msg)
						let idx = 'swipeAction' + index
						this.$refs[idx][0].closeHandler()
					} else {
						uni.$u.toast(res.data.msg)
					}
				})
			},
			// 删除钢瓶
			delSteelCylinder(id, index) {
				this.$api.delete('/api/gas-cylinder-status', {
					id: id,
				}).then(res => {
					if (res.code == 200) {
						this.list.splice(index, 1);
						uni.$u.toast(res.msg)
						let idx = 'swipeAction' + index
						this.$refs[idx][0].closeHandler()
					} else {
						uni.$u.toast(res.data.msg)
					}
				})
			},
			// 触底事件
			bottomingEvent() {
				if (!this.loadingMore) {
					this.getList()
				}
			},
			bottleClick(value) {
				this.cylinderNumber = value.index
				var userInfo = this.$cache.getCache('userInfo')
				if (userInfo.provUser && value.index == 1) {
					if (userInfo.provUser.userType == 5) {
						this.allotFlag = false
					}
				} else {
					this.allotFlag = true
					this.disqualification = true
				}
				if (userInfo.provUser && value.index == 2) {
					if (userInfo.provUser.userType == 5) {
						this.disqualification = true
						this.allotFlag = true
					}
				} else {
					this.disqualification = true
				}
				if (value.index == 1) {
					let arr = [{
						text: '超期',
						style: {
							backgroundColor: '#ff9900'
						}
					}, {
						text: '报废',
						style: {
							backgroundColor: '#fab6b6'
						}
					}, {
						text: '删除',
						style: {
							backgroundColor: '#f56c6c'
						}
					}]
					this.optionsList = arr
				} else if (value.index == 2) {
					if (this.belowStandard == 0) {
						let arr1 = [{
							text: '空瓶',
							style: {
								backgroundColor: '#ff9900'
							}
						}, {
							text: '删除',
							style: {
								backgroundColor: '#f56c6c'
							}
						}]
						this.optionsList = arr1
					} else {
						this.optionsList = this.options
					}
				} else {
					this.optionsList = this.options
				}
				this.current = value.index + 1
				this.Pagination.PageIndex = 1
				this.list = []
				this.getList()
			},
			// 超期/报废
			standardClick(event) {
				this.belowStandard = event.index
				if (event.index == 0) {
					let arr1 = [{
						text: '空瓶',
						style: {
							backgroundColor: '#ff9900'
						}
					}, {
						text: '删除',
						style: {
							backgroundColor: '#f56c6c'
						}
					}]
					this.optionsList = arr1
				} else {
					this.optionsList = this.options
				}
				this.Pagination.PageIndex = 1
				this.list = []
				this.getList()
			},
			// 获取列表
			getList() {
				uni.showLoading({
					mask: true,
				});
				this.loadingMore = true;
				let state = ''
				if (this.current == 3) {
					if (this.belowStandard == 0) {
						state = 'extended'
					} else {
						state = 'scrap'
					}
				} else {
					state = this.current
				}
				this.$api.get('/api/gas-cylinder-status', {
					page: this.Pagination.PageIndex,
					pageSize: this.Pagination.PageSize,
					status: state,
				}).then(res => {
					if (res.code == 200) {
						const data = res.data.list
						if (this.loadingMore == true && data) {
							this.list = this.list.concat(data);
						}
						this.list.forEach(item => {
							item.show = false
						})
						if (data.length < this.Pagination.PageSize) {
							this.loadingMore = true
						} else {
							this.loadingMore = false
							this.Pagination.PageIndex++
						}
					}
					uni.hideLoading();
				}).catch(() => {
					uni.hideLoading();
				})
			},
			getColor() {
				if (this.current == 1) {
					return '#19be6b'
				} else if (this.current == 2) {
					return '#ff9900'
				} else if (this.current == 3) {
					return '#fa3534'
				}
			},
			// 添加钢瓶
			addCylinder() {
				let statuNum = ''
				let hintTitle = ''
				if (this.current == 3) {
					if (this.belowStandard == 0) {
						statuNum = 'extended'
						hintTitle = '添加超期瓶'
					} else if (this.belowStandard == 1) {
						statuNum = 'scrap'
						hintTitle = '添加报废瓶'
					}
				} else {
					if (this.current == 1) {
						hintTitle = '添加重瓶'
					} else if (this.current == 2) {
						hintTitle = '添加空瓶'
					}
					statuNum = this.current
				}
				uni.navigateTo({
					url: '/pages/information/cylinder?status=' + statuNum + '&title=' + hintTitle
				});
			},
			// 钢瓶详情
			goCylinder(event){
				uni.navigateTo({
					url: '/pages/information/cylinderDetails?inner_code=' + event.inner_code
				});
			},
			// 调拨钢瓶
			transferCylinders() {
				let statuNum = ''
				statuNum = this.current
				if (this.current == 3) {
					if (this.belowStandard == 0) {
						statuNum = 'extended'
					} else if (this.belowStandard == 1) {
						statuNum = 'scrap'
					}
				} else {
					statuNum = this.current
				}
				uni.navigateTo({
					url: '/pages/information/transfer?status=' + statuNum + '&allotFlag=' + this.allotFlag
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.head_mine_title {
		display: flex;
		align-items: center;
		justify-content: center;
		padding-top: 20rpx;
		font-size: 36rpx;
		font-weight: 600;
	}

	.head_sticky {
		position: -webkit-sticky;
		position: sticky;
		top: 0;
		/* 设置吸顶的位置 */
		background: #fff;
		z-index: 1000;
		padding-top: var(--status-bar-height);
	}

	.card_dinay {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: calc(100% - 40rpx);
		border-bottom: 1rpx solid #e4e7ed;
		padding: 30rpx 20rpx;
	}

	.item_current {
		margin-left: 10px;
		min-width: 40rpx;
		color: #82848a;
	}

	.item_title1 {
		margin-left: 5rpx;
	}

	.icon_cylinder {
		font-size: 50rpx;
	}

	.card_button_bottom {
		position: fixed;
		bottom: 20rpx;
		left: 0;
		right: 0;
		background-color: #fff;
		height: 120rpx;
		margin-bottom: 80rpx;
		padding-bottom: constant(safe-area-inset-bottom); //兼容 IOS<11.2
		padding-bottom: env(safe-area-inset-bottom); //兼容 IOS>11.2
		display: flex;
		align-items: flex-start;
		z-index: 2025;
	}

	.bottom_btn_flex {
		margin-top: 20rpx;
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 2026;
	}

	.but_allot {
		width: 90%;
		z-index: 2027;
	}

	.empty_information {
		margin-top: 30%;
	}
</style>