<template>
	<view class="content">
		<view class="usdtTitle flex">
			<view class="left flex">
				<image class="img margin-r-10" src="../../static/icon/hy02.png" mode="widthFix"></image>
				<view class="margin-r-10">
					BTC/USDT
				</view>
				<view class="font-color-green">
					10086
				</view>
			</view>
			<view class="right">
				<image class="img" src="../../static/icon/hy01.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="box  flex padding-t-20 padding-b-30">
			<view class="item">
				<view class="itemTitle margin-b-20">
					交易方向
				</view>
				<view class="buttomBox flex">
					<view @click="typeIndex=1" class="buttom " :class="{action:typeIndex==1}">
						上漲
					</view>
					<view @click="typeIndex=2" class="buttom " :class="{action:typeIndex==2,'bg-danger':typeIndex==2}">
						下跌
					</view>
				</view>
				<view class="tabType flex ">
					<view class="tab" @click="jyType=1" :class="{action:jyType==1}">
						限價委託
					</view>
					<view class="tab" @click="jyType=2" :class="{action:jyType==2}">
						市價委託
					</view>
				</view>
				<view class="inputBox margin-b-20" v-if="jyType==1">
					<input v-model="xzMoney" class="input" placeholder="請輸入價格" type="number">
				</view>
				<view class="itemTitle  margin-b-20">
					交易個數
				</view>
				<view class="inputBox flex margin-b-20">
					<input v-model="payNum" class="input" placeholder="請輸入交易數量" type="number">
					<view class="rightInputBox flex">
						<image @click="payNum>0?payNum--:false" class="img" src="../../static/icon/hy04.png"
							mode="scaleToFill"></image>
						<view class="jg">
						</view>
						<image @click="payNum++" class="img" src="../../static/icon/hy03.png" mode="scaleToFill">
						</image>
					</view>
				</view>
				<view class="itemTitle margin-b-20">
					槓桿倍數
				</view>
				<view class="itemLeftList flex margin-b-20">
					<view class="ll" @click="changeGG(ind,ls)" :class="{action:ind==ggIndex}"
						v-for="(ls,ind) in ggList">
						{{ls.num}}
					</view>
				</view>
				<view class="inputBox flex margin-b-20">
					<input v-model="ggBs" class="input" placeholder="請輸入槓桿倍率" type="number">
				</view>
				<view class="flex margin-b-20">
					<view class="itemTitle">
						止損價
					</view>
					<view class="inputBox flex">
						<input v-model="zsMoney" placeholder="請輸入止損價格" class="input" type="number">
					</view>
				</view>
				<view class="flex margin-b-20">
					<view class="itemTitle">
						止盈價
					</view>
					<view class="inputBox flex">
						<input v-model="zyMoney" placeholder="請輸入止盈價格" class="input" type="number">
					</view>
				</view>
				<view class="flex margin-b-20">
					<view class="itemTitle">
						保證金
					</view>
					<view class="itemTitle">
						0
					</view>
				</view>
				<view class="flex margin-b-20">
					<view class="itemTitle">
						可用合約餘額
					</view>
					<view class="itemTitle">
						0
					</view>
				</view>
				<view class="flex margin-b-20">
					<view class="itemTitle">
						手續費
					</view>
					<view class="itemTitle">
						0
					</view>
				</view>
				<view class="buttomSubmit" :class="{'bg-success':typeIndex==1,'bg-danger':typeIndex==2}">
					{{typeIndex==1?'做多':'做空'}}
				</view>
			</view>
			<view class="item padding-l-20">
				<view class="itemName flex">
					<view class="itemMinName">
						價格
					</view>
					<view class="itemMinName">
						數量
					</view>
				</view>
				<view class="itemList">
					<view class="list flex font-color-green">
						<view class="li top">
							166666
						</view>
						<view class="li top">
							0000.00
						</view>
					</view>
					<view class="llMax font-color-green">
						10086
					</view>
					<view class="list flex font-color-red">
						<view class="li bottom">
							166666
						</view>
						<view class="li bottom">
							0000.00
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="box margin-t-30 padding-t-30 padding-b-30">
			<view class="flex listTitleBox">
				<view class="leftBottom flex">
					<view @click="ddTypeChage(0)" class="titleType margin-r-30" :class="{action:ddType==0}">
						限價委託
					</view>
					<view @click="ddTypeChage(1)" class="titleType" :class="{action:ddType==1}">
						持倉
					</view>
				</view>
				<view class="rightBottom flex">
					<image class="img" src="../../static/icon/hy02.png" mode="widthFix"></image>
					<view class="more">
						全部訂單
					</view>
				</view>
			</view>
			<view v-for="(item,ind) in loadData[ddType].list" class="usdtList padding-t-30 padding-b-30">
				<view class="flex">
					<view class="name">
						<text v-if="item.direction==1" class="font-color-green">
							做多
						</text>
						<text v-if="item.direction==2" class="font-color-red">
							做空
						</text>
						<text>
							BTC/USDT
						</text>
						<text v-if="ddType==1"
							:class="{'font-color-green':item.direction==1,'font-color-red':item.direction==2}">
							0.00
						</text>
					</view>
					<view @click="clearorder(item)" v-if="ddType==0" class="usdtListButtom">
						撤銷
					</view>
					<view @click="closeorder(item)" v-if="ddType==1" class="usdtListButtom">
						一鍵平倉
					</view>
				</view>
				<view class="tip ">
					<text v-if="item.direction==1">
						委託時間：{{item.wttime}}
					</text>
					<text v-if="item.direction==2">
						開倉時間：{{item.addtime}}
					</text>
				</view>
				<view class="flex">
					<view v-if="item.direction==1" class="tipList">
						<view class="nameTip">
							限價
						</view>
						<view class="text">
							{{item.wt_price}}
						</view>
					</view>
					<view v-if="item.direction==2" class="tipList">
						<view class="nameTip">
							開倉單價
						</view>
						<view class="text">
							{{item.price}}
						</view>
					</view>

					<view class="tipList">
						<view class="nameTip">
							保證金
						</view>
						<view class="text">
							{{item.bond}}
						</view>
					</view>
					<view class="tipList">
						<view class="nameTip">
							交易個數
						</view>
						<view class="text">
							{{item.num}}
						</view>
					</view>
					<view class="tipList">
						<view class="nameTip">
							槓桿倍數
						</view>
						<view class="text">
							{{item.ggan}}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		levertadeLaverorder,
		closeorder,
		clearorder
	} from '@/api/heyue.js';
	export default {
		data() {
			return {
				//保存幣種類型
				moneytypeList:[],
				// 当前选中的杠杆倍数
				ggIndex: 0,
				ggList: [{
					num: 5
				}, {
					num: 10
				}, {
					num: 20
				}, {
					num: 50
				}, {
					num: 100
				}],
				//当前选中的交易类别
				typeIndex: 1,
				// 交易类型
				jyType: 1,
				//交易数量
				payNum: 0,
				// 杠杆倍数
				ggBs: 0,
				// 限价委托价格
				xzMoney: 0,
				// 止损价格
				zsMoney: 0,
				// 止盈价格
				zyMoney: 0,
				ddType: 1, //1委托订单2持仓订单3成交订单
				loadData: [{
						state: 1,
						text: '限价委托',
						loadingType: 'more',
						list: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					},
					{
						state: 2,
						text: '持仓',
						loadingType: 'more',
						list: [],
						page: 1, //当前页数
						limit: 10 //每次信息条数
					}
				]
			}
		},
		onLoad() {
			this.levertadeLaverorder();
		},
		onShow() {

		},
		onReachBottom() {
			this.levertadeLaverorder();
		},
		onReady() {

		},
		methods: {
			// 平倉
			closeorder(item) {
				const that = this;
				uni.showModal({
					title: '平倉',
					content: '是否立即平倉，確認後不可修改',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title: '請求中...',
								mask: true
							});
							closeorder({
								id: item.id
							}).then((e) => {
								uni.hideLoading()
								uni.showToast({
									title: '平倉成功',
									icon: 'success'
								});
								that.ddTypeInit()
							}).catch(() => {
								uni.hideLoading()
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 撤銷
			clearorder(item) {
				const that = this;
				uni.showModal({
					title: '撤銷',
					content: '是否立即撤銷，確認後不可修改',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title: '請求中...',
								mask: true
							});
							clearorder({
								id: item.id
							}).then((e) => {
								uni.hideLoading()
								uni.showToast({
									title: '撤銷成功',
									icon: 'success'
								});
								that.ddTypeInit()
							}).catch(() => {
								uni.hideLoading()
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			// 获取订单商品
			levertadeLaverorder(source) {
				//这里是将订单挂载到tab列表下
				let index = this.ddType;
				let navItem = this.loadData[index];
				let state = navItem.state;
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return;
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';
				uni.showLoading({
					title: '數據加載中',
					mask: true
				});
				levertadeLaverorder({
						type: state,
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						list
					}) => {
						uni.hideLoading()
						let arr = list.map(e => {
							e.direction = +e.direction;
							e.bond = +e.bond
							e.price = +e.price
							e.wt_price = +e.wt_price
							return e;
						});
						navItem.list = navItem.list.concat(arr);
						navItem.page++;
						if (navItem.limit == list.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						uni.hideLoading()
						console.log(e);
					});



			},
			// 修改杠杆倍数
			changeGG(ind, item) {
				this.ggBs = item.num
				this.ggIndex = ind
			},
			// 列表切换
			ddTypeChage(ind) {
				this.ddType = ind;
				this.levertadeLaverorder('tabChange');
			},
			// 初始化重新加載
			ddTypeInit() {
				// 初始化數據
				let item = this.loadData[this.ddType];
				item.loadingType = 'more';
				item.list = [];
				item.page = 1;
				item.limit = 10;
				item.loaded = false;
				this.levertadeLaverorder();
			}
		}
	}
</script>

<style lang="scss">
	.usdtTitle,
	.box {
		padding: 0 20rpx;
		width: 750rpx;
		background-color: #FFFFFF;

	}

	.usdtTitle {
		border-bottom: 1px solid $border-color-light;
		line-height: 1;

		.left {
			font-size: 32rpx;
			font-weight: bold;

			.img {
				width: 36rpx;
			}

		}

		.right {
			.img {
				width: 48rpx;
			}
		}
	}

	.box {
		line-height: 1;
		align-items: flex-start;

		.itemTitle {
			font-weight: bold;
			font-size: $font-sm;
		}

		.item {
			flex-grow: 1;
			flex-shrink: 0;
		}

		.buttomBox {
			font-size: $font-sm;

			.buttom {
				width: 200rpx;
				padding: 20rpx 0;
				text-align: center;
				border-radius: 10rpx;
				background-color: #F5F5F5;

				&.action {
					color: #FFFFFF;
					background-color: $color-green;
				}
			}
		}

		.tabType {
			justify-content: space-around;

			.tab {
				padding-top: 30rpx;
				padding-bottom: 20rpx;
				color: #545D6F;
				font-size: $font-sm;
				font-weight: bold;

				&.action {
					color: $uni-color-warning;
				}
			}
		}

		.inputBox {
			height: 60rpx;
			border-radius: 10rpx;
			padding: 0 20rpx;
			display: flex;
			align-items: center;
			border: 1px solid $border-color-light;

			.input {
				font-size: $font-sm;
				flex-grow: 1;
			}

			.rightInputBox {
				.jg {
					border: 1px solid $border-color-light;
					height: 50rpx;
					margin: 0 30rpx;
				}

				.img {
					width: 28rpx;
					height: 28rpx;
				}
			}
		}

		.itemLeftList {
			justify-content: space-around;

			.ll {
				border: 1px solid $border-color-light;
				width: 74rpx;
				height: 54rpx;
				line-height: 54rpx;
				text-align: center;
				font-size: $font-sm;
				font-weight: bold;

				&.action {
					background-color: #F5F5F5;
				}
			}
		}

		.buttomSubmit {
			color: #FFFFFF;
			height: 64rpx;
			line-height: 64rpx;
			text-align: center;
			font-size: $font-sm;
			border-radius: 10rpx;
		}

		.itemName {
			padding-bottom: 20rpx;

			.itemMinName {
				font-size: 22rpx;
				color: #5D6677;
			}

		}

		.itemList {
			.list {
				font-size: 20rpx;
				padding: 10rpx 0;
			}

			.llMax {
				padding: 30rpx 0;
				font-weight: bold;
				font-size: $font-lg;
			}
		}

		.listTitleBox {
			border-bottom: 1px solid $border-color-light;
			align-items: flex-start;

			.leftBottom {
				flex-shrink: 0;
				font-size: $font-lg;
				font-weight: bold;

				.action {
					border-bottom: 2px solid $uni-color-warning;
				}

				.titleType {
					padding-bottom: 20rpx;
				}
			}

			.rightBottom {
				font-size: $font-base;
				color: #707A8A;
				flex-shrink: 0;

				.img {
					width: 36rpx;
				}
			}
		}

		.usdtList {
			color: #707A8A;
			border-bottom: 1px solid $border-color-light;

			.name {
				font-size: $font-lg;
				font-weight: bold;
			}

			.usdtListButtom {
				font-size: $font-base;
				background-color: #F5F5F5;
				color: $uni-color-warning;
				border-radius: 10rpx;
				padding: 10rpx 20rpx;
			}

			.tip {
				padding-top: 10rpx;
				padding-bottom: 30rpx;
				font-size: $font-base;
			}

			.tipList {
				font-size: $font-sm;
				text-align: center;

				.nameTip {
					padding-bottom: 20rpx;
				}
			}
		}
	}
</style>
