<template>
	<view class="book-item">
		<view class="book-info">
			<image class="book-cover" :src="book.cover" mode="aspectFill" />
			<view class="book-detail">
				<view class="top-info">
					<view class="book-title">{{ book.bookName }}</view>
					<view class="book-tags">
						<text class="tzs tag-text" v-if="book.suit==1">套装书</text>
						<text class="kmdb tag-text" v-if="book.maxNum>1">可卖多本</text>
					</view>
				</view>

				<view class="flex flex-j-b flex-a-c">
					<view class="book-price">回收价： ¥{{ book.recycleMoney }}</view>
					<u-number-box class="number-box" bg-color="#38c148" color="#ffffff" v-model="book.num" :min="1"
						:max="book.maxNum||40" @blur="onQuantityChange" @minus="addReduceNum(-1)"
						@plus="addReduceNum(1)"></u-number-box>
				</view>
			</view>
			<view class="delete-btn" @tap="onDelete">
				<u-icon name="close" size="26" color="#999"></u-icon>
			</view>
		</view>

		<common-dialog ref="deleteDialog" title="温馨提示" @confirm="confirmDelete">
			<text>确定删除这本图书吗？</text>
		</common-dialog>
	</view>
</template>

<script>
	import commonDialog from '@/components/common-dialog.vue';
	export default {
		components: {
			commonDialog
		},
		props: {
			book: {
				type: Object,
				required: true
			}
		},
		data() {
			return {}
		},
		methods: {
			onDelete() {
				this.$refs.deleteDialog.openPopup()
			},
			confirmDelete() {
				this.$emit('delete', this.book)
			},
			// /api/token/order/addReduceNum
			addReduceNum(changeNum) {
				uni.$u.http.post('/token/order/addReduceNum', {
					orderId: this.book.orderId,
					isbn: this.book.isbn,
					changeNum
				}).then(res => {
					if (res.data == 1) {
						this.$emit('quantity-change', this.book)
					}
				})
			},
			onQuantityChange(data) {
				uni.$u.http.post('/token/order/changeNum', {
					orderId: this.book.orderId,
					isbn: this.book.isbn,
					afterNum: data.value
				}).then(res => {
					if (res.data == 1) {
						this.$emit('quantity-change', this.book)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.book-item {
		background: #FFFFFF;
		padding: 20rpx;
		margin-top: 20rpx;
		border-radius: 10rpx;

		.book-info {
			display: flex;
			position: relative;

			.book-cover {
				width: 140rpx;
				height: 196rpx;
				border-radius: 8rpx;
			}

			.tag-text {
				font-family: Source Han Sans CN;
				font-weight: 400;
				font-size: 24rpx;
				color: #FFFFFF;
			}

			.tzs {
				width: 91rpx;
				height: 30rpx;
				background: linear-gradient(263deg, #98E05F, #0DE3AC);
				border-radius: 2rpx;
				padding: 4rpx 10rpx;
				margin-right: 10rpx;
			}

			.kmdb {
				width: 117rpx;
				height: 30rpx;
				background: linear-gradient(263deg, #F7CB6B, #FBA980);
				border-radius: 2rpx;
				padding: 4rpx 10rpx;
				margin-right: 10rpx;
			}

			.book-detail {
				flex: 1;
				margin-left: 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				:v-deep .u-number-input {
					background: #F9F9F9 !important;
					border-radius: 6rpx;
				}

				.book-title {
					max-width: 400rpx;
					font-size: 28rpx;
					color: #333;
					line-height: 1.4;
					margin-bottom: 20rpx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					overflow: hidden;
					font-family: Source Han Sans CN;
					font-weight: bold;
				}

				.book-price {
					font-family: Source Han Sans CN;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
				}
			}

			.delete-btn {
				position: absolute;
				right: 0;
				top: -10rpx;
				padding: 10rpx;
			}
		}
	}
</style>