<template>
	<view class="content">
		<view @click="addInterestsList(item)" class="ul-item" v-for="(item, index) in state.list" :key="index">
			<image style="width: 100%; height: 170rpx; border-radius: 20rpx"
				:src="`${$imgUrl}applyCard/product-bg.png`"></image>
			<view class="item-value">
				<image class="icon-tip"
					:src="state.sysType === 'WECHAT'?`${$imgUrl}applyCard/wechat.png` : `${$imgUrl}applyCard/alibaba.png`">
				</image>
				<view class="content">
					<view class="title">
						{{ item.productName }}
					</view>
				</view>
				<view class="money">
					￥：{{ item.discountPrice * 0.01 }}
				</view>
			</view>
		</view>

		<!-- <view class="item" v-for="(item,index) in state.list" :key='index'>
			<view class="title" @click="addInterestsList(item)">
				<view>
					<view v-if='item.children' style="display: inline-block;">
						<image :class="item.isShow?'imageChange':'imageDefault'" @click.prevent="changeIsShow(index)"
							src="../../static/image/icon-back.png" alt="" srcset=""></image>
					</view>
					<text><text>{{item.productName}}</text>&nbsp;&nbsp;<text>¥{{item.discountPrice * 0.01}}</text></text>
				</view>
			</view>
			<view v-if="item.isShow" class="children" v-for="(itemChild,index) in item.children" :key='index'>
				<view>
					<text>{{itemChild.productName}}</text>&nbsp;&nbsp;<text>{{itemChild.discountPrice * 0.01}}</text>
				</view>
			</view>
		</view> -->
	</view>
</template>

<script setup lang="ts">
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import { reactive } from "vue";
	import { equityProductsApi } from "@/utils/network/api.js";
	import { stringToJson } from "@/utils/network/encryption";
	import { request } from "@/utils/network/request";
	import { etcQYAction } from "@/utils/network/api.js";
	import { fileURL } from "@/datas/fileURL.js";
	import navBar from "@/components/nav-bar/nav-bar2.vue";
	import navBgCar from "./components/nav-bg-car4";
	import { msg, navTo } from "@/utils/utils";
	import {
		checkOrderStatus,
		wechatAppID,
		wechatPayConfigId,
		wechatSecret,
		addProduct,
		queryDetails,
		getOpenidApi
	} from "@/utils/network/api";
	import {
		getItem,
		StorageKeys,
		setItem
	} from "@/utils/storage";
	const imgURL = `${fileURL}image/`;

	const state = reactive({
		openid: "",
		id: "",
		clientFee: "",
		list: [],  //权益数据
		choiceValue: [],  //选择的权益数据的下标
		choiceValueComplete: [],   //选择的权益数据的具体数据
		isValueCard: "",
		type: 0,
		userType: 0,
		promoteId: "",  //产品id
		totalAmount: 0,  //产品+权益产品金额
		changeCardType: "",//储值卡转记账卡选权益产品
		applyId: "",
		orderId: "",
		sysType: ""
	})

	onLoad((option : any) => {
		// #ifdef MP-ALIPAY
		state.sysType = 'ALI';
		// #endif
		// #ifdef MP-WEIXIN
		state.sysType = 'WECHAT';
		// #endif
		console.log("promoteId", option)
		state.clientFee = option.clientFee;
		state.userType = option.userType;
		state.type = option.type;
		state.isValueCard = option.isValueCard;
		state.promoteId = option.promoteId;
		state.totalAmount = option.fee;
		// 储值卡转记账卡传过来的参数
		state.changeCardType = option.changeCardType;
		state.applyId = option.applyId;
		state.orderId = option.orderId;
		getList();
		getOpenID();

		console.log("option", option)
	})
	const addInterestsList = (item) => {
		console.log("item", item)
		state.totalAmount = Number(state.totalAmount) + Number(item.discountPrice)
		if (state.changeCardType == "1") {
			navTo(
				`/subpackage/after-sale/to-bookkeeping-card/mailing_information?orderId=${state.orderId}&&applyId=${state.applyId}&&productId=${state.promoteId}&&totalAmount=${state.totalAmount}&&equityId=${item.equityId}`
			)
		} else {
			// 正常流程
			uni.redirectTo({
				url: `/subpackage/orders/essential-information?promoteId=${getItem("promoteId")}&userType=${state.userType}&type=${state.type}&isValueCard=${state.isValueCard}&totalAmount=${state.totalAmount}&equityId=${item.equityId}`,
			});
		}
	}


	const getList = () => {
		const options = {
			type: 2,
			data: {
				"promoteId": state.promoteId,
			},
			method: 'POST',
			showLoading: true,
		}
		request(equityProductsApi, options).then((res) => {
			const data = stringToJson(res.bizContent);
			for (var i = 0; i < data.data.length; i++) {
				data.data[i]['isShow'] = false;
			}
			state.list = data.data
			console.log("222", data.data)
		})
	}
	const change = (e) => {
		console.log(e);
	}

	//获取微信小程序openid
	const getOpenID = () => {
		// #ifdef MP-WEIXIN
		uni.login({
			provider: "weixin",
			success: function (e) {
				console.log(e);
				getOpenid(e.code);
			},
		});
		//#endif
	};
	const getOpenid = (code) => {
		const options = {
			type: 2,
			data: {
				"jsCode": code
			},
			method: "POST",
			showLoading: true,
		};
		// #ifdef MP-WEIXIN
		request(getOpenidApi, options).then((res) => {
			const result = stringToJson(res.bizContent);
			console.log("获取微信小程序openid", result);
			const openidData = stringToJson(result.data);
			state.openid = openidData.openid;
			setItem("QYorder", state);
		});
		// #endif
	}
	const changeIsShow = (index) => {
		console.log(index, state.list[index]['isShow'], !state.list[index]['isShow'])
		state.list[index]['isShow'] = !state.list[index]['isShow']
		console.log("state.list", state.list)
	}
	const getValue = (value) => {
		if (state.choiceValue.length == 0) {
			console.log("第一次")
			state.choiceValue.push(value)
			console.log("state.choiceValue", state.choiceValue)
		} else {
			var index = state.choiceValue.indexOf(value);
			if (index > -1) {//大于0 代表存在，
				state.choiceValue.splice(index, 1);//存在就删除
			} else {
				state.choiceValue.push(value)
			}
			console.log("state.choiceValue", state.choiceValue)
		}

	}
</script>

<style scoped lang="scss">
	.button {
		height: 80rpx;
		background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
		border-radius: 40rpx;
		font-size: 32rpx;
		font-weight: 400;
		color: #ffffff;
		line-height: 80rpx;
		margin: 20rpx;
	}

	.imageDefault {
		height: 40rpx;
		width: 40rpx;
		transform: rotate(270deg);
		margin-right: 12rpx;
		color: white;
	}

	.imageChange {
		height: 40rpx;
		width: 40rpx;
		transform: rotate(90deg);
		margin-right: 12rpx;
		color: white;
	}

	.item {
		width: 90%;
		margin: 20rpx auto;
		background-color: rgb(41, 199, 207);
		border-radius: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		color: white;
		font-size: 32rpx;

	}

	.item>.title {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.children {
		margin-left: 50rpx;
		margin-top: 16rpx;
	}

	.children>view {
		height: 50rpx;
		line-height: 50rpx;
	}

	.show {
		display: inline-block;
	}

	.hide {
		display: none;
	}

	checkbox {
		transform: scale(0.8);
	}

	.ul-item {
		padding: 5px 20rpx;
		display: flex;
		align-items: center;

		.item-value {
			padding: 20rpx;
			position: absolute;
			display: flex;
			align-items: center;
			width: 80%;

			.content {
				width: 51%;

				.title {
					font-size: 32rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #ffffff;

				}

				.tip {
					font-size: 24rpx;
					font-family: Microsoft YaHei;
					font-weight: 400;
					color: #ffffff;
				}
			}
		}

		.icon-tip {
			width: 100rpx;
			height: 100rpx;
		}

		.content {
			margin-left: 40rpx;
		}
	}

	.money {
		color: white;
	}
</style>