<template>
	<view class="all">
		<view class="navList flex">
			<view v-for="(item, index) in navList" :key="index" class="navItem"
				:class="{ activeItem: tabCurrentIndex === index,tip:index == 0 }" @click="tabClick(index)">{{ item }}
			</view>
		</view>
		<view style="padding-top: 44px;" v-if="tabCurrentIndex == 0">
			<view class="listBox">
				<view class="titleTetx">数量</view>
				<view class="flex listTpl">
					<input type="number" class="inputBox" v-model="number" placeholder="请输入数量" />
					<view class="flex_item">
						<view class="listTip">USDT</view>
						<view class="listAll" @click="number=money">全部</view>
					</view>

				</view>
				<view class="flex tipBox">
					<view class="tip1">可用余额{{money}}USDT</view>
					<view class="tip1">限额{{baseData.min_sell}}到{{baseData.max_sell}}个</view>
				</view>
			</view>
			<view class="listBox" style="padding-top: 0rpx;">
				<view class="listTpl">
					<input type="number" class="inputBox" v-model="price" placeholder="请输入单价" />
				</view>
				<view class="flex tipBox">
					<view class="tip1">单价不得大于参考汇率</view>
				</view>
			</view>
			<view class="submission">
				<button class="golden" type="golden" hover-class="none" @click="submission">一键卖币</button>
			</view>
			<view class="tips">*参考汇率{{baseData.us_price}}</view>
		</view>
		<view class="listItemBox" v-if="tabCurrentIndex == 1">
			<view class="listItem" v-for="item,index in list" :key="index">
				<view class="itemInfo flex">
					<view class="flex_item">
						<image src="/static/image/img20.png" style="width: 55rpx;height: 55rpx;" mode="widthFix"></image>
						<view class="name">{{item.real_name}}</view>
					</view>
					<image src="/static/image/img21.png" style="width: 40rpx;height: 31rpx;" mode="widthFix"></image>
				</view>
				<view class="itemTpl">{{item.price}}</view>
				<view class="itemTip flex">
					<view class="tipBox">
						<view class="tipText">数量:{{item.num}}</view>
						<view class="tipText">{{item.add_time|dateFormat}}</view>
					</view>
					<view class="tipBtn" @click="commitorder(item)">买入</view>
				</view>
			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import {
		getsell,
		sellPush,
		sellBuy
	} from '@/api/game.js';
	import {
		getIndex
	} from '@/api/index.js';
	import {
		mapState
	} from 'vuex';
	import {
		qianBao
	} from '@/api/wallet.js';
	import dayjs from '@/libs/dayjs/dayjs.min.js';
	export default {
		computed: {
			...mapState('user', ['hasLogin'])
		},
		filters: {
			dateFormat: function(value) {
				return dayjs(value * 1000).format('YYYY/MM/DD HH:mm');
			}
		},
		data() {
			return {
				tabCurrentIndex: 1,
				navList: ['卖出', '买入'],
				number: '',
				price: '',
				page: 1,
				limit: 10,
				list: [],
				loadingType: 'more',
				baseData:{
					min_sell:'',
					max_sell:''
				},
				loaded:false,
			}
		},
		onNavigationBarButtonTap(res) {
			uni.navigateTo({
				url: '/pages/index/order'
			})
		},
		onShow() {
			this.init();
		},
		onLoad() {
			this.getIndex()
		},
		methods: {
			qianBao() {
				const that = this;
				qianBao().then((res) => {
					that.money = +res.data.like_usdt;
				})
			},
			getIndex(){
				getIndex({})
					.then(({
						data
					}) => {
						this.baseData = data
						console.log(data);
					})
					.catch(e => {});
			},
			// 购买订单
			commitorder(item) {
				uni.showModal({
					title: '请输入支付密码',
					editable:true,
					success: res => {
						if (res.confirm) {
							if (res.content) {
								uni.showLoading({
									title: '支付中。。。',
									mask: true
								});
								sellBuy({
										id: item.id,
										trade_psw: res.content,
									}).then((
										data
									) => {
										uni.hideLoading();
										uni.showModal({
											title: '提示',
											content: data.msg,
											showCancel: false,
										});
									})
									.catch(e => {
										uni.hideLoading()
										console.log(e);
									});
							}
						}
					},
				});
			},
			// 挂出
			submission() {
				sellPush({
						type: 'USDT',
						num: this.number,
						price: this.price,
						phone: '',
						trade_psw: '',
						sell_type: 'money',

					}).then(({
						data
					}) => {

					})
					.catch(e => {
						console.log(e);
					});
			},
			init() {
				this.page = 1;
				this.list = [];
				this.loadingType = 'more';
				this.loadData();
				this.qianBao();
			},
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			async loadData(source) {
				const that = this;
				//这里是将订单挂载到tab列表下
				let navItem = that;
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				getsell({
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						// 保存我的总金额
						let arr = data.list.map(e => {
							e.num = e.num * 1;
							e.price = e.price * 1;
							return e;
						});
						navItem.list = navItem.list.concat(arr);
						if (navItem.limit == data.length) {
							navItem.page++;
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						that.loaded = true;
					})
					.catch(e => {
						console.log(e);
					});
			},
		}
	};
</script>

<style lang="scss">
	.all {
		width: 750rpx;
		height: 100%;
		background-color: #051137;
		padding-top: var(--status-bar-height);
		color: #fff;
	}

	.navList {
		padding: 20rpx 50rpx;
		background: #1F2A4A;
		line-height: 1;
		position: fixed;
		top: 44px;
		left: 0;
		width: 100%;
		z-index: 9;

		.navItem {
			color: #fff;
			font-size: 30rpx;
			text-align: center;
			width: 50%;

			&.activeItem {
				color: #0C5AFA;
				position: relative;

				&:after {
					content: '';
					position: absolute;
					left: 36%;
					bottom: -20rpx;
					width: 30%;
					height: 8rpx;
					// transform: translateX(-50%);
					border-bottom: 4rpx solid #0C5AFA;
					border-radius: 0rpx 20rpx 0rpx 0rpx;
				}
			}

			&.tip {
				border-right: 1rpx solid #333D5B;
			}
		}
	}

	.listBox {
		padding: 62rpx 32rpx;

		.titleTetx {
			font-weight: bold;
			font-size: 24rpx;
			color: #FFFFFF;
			padding-bottom: 25rpx;
		}

		.tipBox {
			padding-top: 15rpx;
			font-size: 24rpx;
		}
	}

	.listTpl {
		border-bottom: 1rpx solid #6A7288;
		padding-bottom: 25rpx;

		.inputBox {
			font-size: 35rpx;
			color: #FFFFFF;
		}

		.listTip {}

		.listAll {
			padding-left: 30rpx;
			font-size: 24rpx;
			color: #0C5AFA;
		}
	}

	.submission {
		padding: 80rpx 25rpx;
		padding-bottom: 25rpx;

		.golden {
			background: #0C5AFA;
			color: #ffffff;
		}
	}

	.tips {
		text-align: center;
	}

	.listItemBox {
		padding-top: 44px;

		.listItem {
			padding: 34rpx 34rpx;
			border-bottom: 1rpx solid #6A7288;

			.name {
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #FFFFFF;
				padding-left: 25rpx;
			}

			.itemTpl {
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #0C5AFA;
				padding-top: 25rpx;
			}

			.itemTip {
				.tipText {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #C1C1C1;
					padding-top: 15rpx;
				}

				.tipBtn {
					font-family: PingFang SC;
					font-weight: bold;
					font-size: 24rpx;
					color: #FFFFFF;
					background: linear-gradient(90deg, #0C5AFA, #1356FF);
					border-radius: 7rpx;
					padding: 15rpx 35rpx;
				}
			}
		}
	}
</style>