<template>
	<view class="page">
		<u-checkbox-group @change="checkboxGroupChange">
			<view class="item u-flex" v-for="(item,index) in list" :key="index">
				<u-checkbox :name='item.id' shape="circle" v-model="item.checked"></u-checkbox>
				<view class="box">
					<image :src="item.image" style="width: 120rpx ;height: 160rpx;" mode=""></image>
				</view>
				<view class=""
					style="justify-content: space-between;padding: 4rpx 0 ;height: 160rpx;display: flex;flex-direction: column;">
					<view class="">
						<view class="title u-line-1">{{item.title}}</view>
						<view class="writer" v-if='item.price>0'>资源类型：付费资源</view>
						<view class="writer" v-if='item.price==0'>资源类型：免费资源</view>
					</view>
					<view class="money">¥{{item.price}}</view>
				</view>
			</view>
		</u-checkbox-group>
		<view class="" style="height: 160rpx;"></view>
		<view class="bottom u-flex u-row-between">
			<view class="u-flex">
				<view class="">
					<u-checkbox-group>
						<u-checkbox @change="checkedAll" shape="circle" v-model="all"></u-checkbox>
					</u-checkbox-group>
					<text class="All">全选</text>
				</view>
				<view class="" style="margin-left: 24rpx;">
					<view class="">
						<text class="all">合计：</text>
						<text class="mi">¥</text>
						<text class="mi" style="font-size: 36rpx;">{{Number(sum).toFixed(2)}}</text>
					</view>
					<view class="">
						<text class="num">{{aaa.length}}</text>
						<text class="fcai">份资料</text>
					</view>
				</view>
			</view>
			<view class="btn" @click="orderinfo">
				下载资源
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				change: 0,
				all: false,
				list: [],
				series_id: '',
				orderid: '',
				sum: 0,
				aaa: []
			};
		},
		onLoad(options) {
			this.series_id = options.id
			this.getSeriesBooks()
		},
		methods: {
			getsum() {
				this.sum = 0
				for (var i = 0; i < this.list.length; i++) {
					if (this.list[i].checked) {
						this.sum += Number(this.list[i].price)
					}
				}
			},
			checkboxChange(e) {
				// this.$set(this.list[e.name], 'checked', e.value)
				this.getsum()
			},
			// 系列列表
			getSeriesBooks() {
				this.$u.post('api/books/getSeriesBooks', {
					series_id: this.series_id
				}).then(res => {
					this.list = res.data
					this.list.map(item => {
						item.checked = false
					})
					this.list = JSON.parse(JSON.stringify(this.list))
				})
			},
			//订单确认
			orderinfo() {
				var b = this.aaa.toString()
				console.log(b);
				this.$u.post('api/order/createOrder', {
					goods_id: b,
					type: 0
				}).then(res => {
					if (res.code == 1) {
						this.orderid = res.data.id
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/index/orderinfo?id=' + this.orderid + "&index=" + 2
							})
						}, 800)
					}
				})
			},
			//全选
			checkedAll(e) {
				console.log(e);
				if (e.value == true) {
					this.list.map(val => {
						val.checked = true;
					})
					this.list.map(item => {
						this.aaa.push(item.id)
					})
					// this.aaa = this.list

					console.log('aaa', this.aaa);
				} else {
					this.list.map(val => {
						val.checked = false;
					})
					this.aaa = []
				}
				this.getsum()
			},
			checkboxGroupChange(e) {
				if (e.length == this.list.length) {
					this.all = true
				} else {
					this.all = false
				}
				this.getsum()
				this.aaa = e
				// this.list.map(val => {
				// 	if (val.checked == false) {
				// 		this.all = false
				// 	}
				// })
			},
		}
	}
</script>

<style lang="scss">
	.num {
		font-size: 28rpx;
		font-family: JDZhengHT, JDZhengHT;
		font-weight: 400;
		color: #CF1534;
	}

	.fcai {
		font-size: 24rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		color: #131415;
	}

	.All {
		font-size: 24rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		color: #888888;
		margin-left: -16rpx
	}

	.mi {
		font-size: 28rpx;
		font-family: JDZhengHT, JDZhengHT;
		font-weight: 400;
		color: #CF1534;
	}

	.all {
		font-size: 26rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		color: #131415;
	}

	.btn {
		width: 252rpx;
		height: 88rpx;
		background: #06A971;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
	}

	.money {
		font-size: 32rpx;
		font-family: SFPro, SFPro;
		font-weight: 400;
		color: #CF1534;
	}

	.title {
		width: 402rpx;
		font-size: 28rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		color: #222222;
	}

	.writer {
		font-size: 24rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		color: #444444;
		margin-top: 12rpx;
	}

	.box {
		width: 160rpx;
		height: 160rpx;
		border: 1rpx solid rgba(151, 151, 151, 0.1);
		padding: 0 20rpx;
	}

	.bottom {
		width: 100%;
		height: 166rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		padding: 0 28rpx;
	}

	.item {
		width: 100%;
		height: 216rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		margin-top: 20rpx;
		padding: 28rpx 24rpx;
	}

	.page {
		background-color: #f4f4f4;
		min-height: 100vh;
		padding: 0 28rpx;
		overflow: hidden;
	}
</style>