<template>
	<view class="content">
		<view class="center">
			<block v-for="(item,index) in pageList" :key="index">
				<view class="box" @click="toDetail(item.id)">
					<view class="top hflex acenter jbetween">
						<view class="order-no">订单号<span style="color: #333">{{item.order_no}}</span></view>
						<view class="text_red" v-if="item.refund_status == 1">等待审核</view>
						<view class="text_blue" v-if="item.refund_status == 2">同意退款</view>
						<view class="text_blue" v-if="item.refund_status == 3">拒绝退款</view>
					</view>
					<view class="time">下单时间：{{item.create_at}}</view>
					<view class="good">
						<block v-for="(item2,index2) in item.goods_item" :key="index2">
							<view class="hflex acenter good_item">
								<image :src="item2.goods_cover" class="img"></image>
								<view class="good_center">
									<view class="name text_hide">{{item2.goods_name}}</view>
									<view class="spec text_hide">{{item2.goods_spec}}</view>
								</view>
								<view class="vflex jend">
									<view class="price">￥<span style="font-size: 30rpx;">{{item2.price_selling}}</span></view>
									<view class="num">X{{item2.stock_sales}}</view>
								</view>
							</view>
						</block>
						<view class="hflex jend">
							<view class="good_bottom">共{{item.goods_item.length}}件商品 实付款￥<span class="bottom_price">{{item.payment_amount}}</span></view>
						</view>
					</view>
					<view class="box_bottom hflex jend" v-if="item.refund_status == 1">
						<view class="btn1 hflex acenter jcenter" @click.stop="apply1(item.id,2)">拒绝申请</view>
						<view class="btn1 btn2 hflex acenter jcenter" @click.stop="apply1(item.id,1)">同意退款</view>
					</view>
					<view class="box_bottom hflex jend" v-if="item.refund_status == 2">
						<view class="btn1 hflex acenter jcenter" @click.stop="dele(item.id)">删除记录</view>
						<view class="btn1 btn2 hflex acenter jcenter" @click.stop="toDetail(item.id)">查看详情</view>
					</view>
					<view class="box_bottom hflex jend" v-if="item.refund_status == 3">
						<view class="btn1 hflex acenter jcenter" style="color: #9A9A9A;">已拒绝申请</view>
					</view>
				</view>
				
			</block>
		</view>
		<u-popup :show="show_refuse" mode="center" round="10" @close="close" :safeAreaInsetBottom="false">
			<view class="popu">
				<view class="popu_title hflex acenter jcenter">拒绝原因</view>
				<u-textarea v-model="reason" confirmType="done" placeholder="请填写拒绝原因" border="none" ></u-textarea>
				<view class="popu_bottom hflex acenter jcenter">
					<view class="btn1 hflex acenter jcenter" @click="close">取消</view>
					<view class="btn1 btn2 hflex acenter jcenter" @click="refuse">提交</view>
				</view>
			</view>
		</u-popup>
		<u-popup :show="show_agree" mode="center" round="10" @close="close" :safeAreaInsetBottom="false">
			<view class="popu">
				<view class="popu_title hflex acenter jcenter">确认退款</view>
				<view class="popu_bottom hflex acenter jcenter">
					<view class="btn1 hflex acenter jcenter" @click="close">取消</view>
					<view class="btn1 btn2 hflex acenter jcenter" @click="agree">确认</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				pageList: [],
				page: 1,
				limit: 10,
				total: 1,
				show_refuse: false,
				reason: '',
				show_agree: false,
				id: '',
			}
		},
		onLoad() {
			that = this
			uni.startPullDownRefresh();
		},
		onShow() {
			that.getList()
		},
		onPullDownRefresh() {
			that.page = 1
			that.pageList = []
			that.getList()
		},
		methods: {
			getList() {
				$api.req({
					url: '/data/api.business.Order/refund_list',
					data: {
						page: that.page
					}
				}, function(res) {
					if(res.code == 1) {
						for(var i=0;i<res.data.list.length;i++) {
							for(var j=0;j<res.data.list[i].goods_item.length;j++) {
								var temp = res.data.list[i].goods_item[j].goods_cover.split('|')
								res.data.list[i].goods_item[j].goods_cover = temp[0]
							}
						}
						for(var i=0;i<res.data.list.length;i++) {
							for(var j=0;j<res.data.list[i].goods_item.length;j++) {
								res.data.list[i].goods_item[j].goods_spec = $api.resetspec(res.data.list[i].goods_item[j].goods_spec)
							}
						}
						
						if(that.page == 1) {
							that.pageList = res.data.list
						} else {
							that.pageList = that.pageList.concat(res.data.list)
						}
						// console.log(that.pageList);
						that.total = res.data.page.total
						that.limit = res.data.page.limit
						uni.stopPullDownRefresh();
					}
				})
			},
			apply1(id,type) {
				
				that.id = id
				// console.log(type);
				if(type == 1) {
					that.show_agree = true
				} else {
					that.show_refuse = true
				}
			},
			close() {
				that.show_refuse = false
				that.show_agree = false
			},
			refuse() {
				$api.req({
					url: '/data/api.business.Order/examine_refund',
					method: 'POST',
					data: {
						order_id: that.id,
						type: 2,
						reason: that.reason
					}
				}, function(res) {
					if(res.code == 1) {
						that.page = 1
						that.getList()
						that.close()
					}
				})
			},
			agree() {
				$api.req({
					url: '/data/api.business.Order/examine_refund',
					method: 'POST',
					data: {
						order_id: that.id,
						type: 1
					}
				}, function(res) {
					if(res.code == 1) {
						that.page = 1
						$api.info('已同意退款，支付金额原路返回。')
						setTimeout(() => {
							that.getList()
						}, 1000)
						
						that.close()
					}
				})
			},
			dele(id) {
				$api.req({
					url: '/data/api.business.Order/del_refund',
					method: 'POST',
					data: {
						order_id: id
					}
				}, function(res) {
					if(res.code == 1) {
						$api.info(res.info)
						that.page = 1
						that.getList()
					}
				})
			},
			toDetail(id) {
				$api.jump('/pages/order/refundDetail?id=' + id)
			},
			onReachBottom() {
				if (Number(that.page) * Number(that.limit) >= Number(that.total)) {
					$api.info("没有更多了")
				} else {
					that.page++
					that.getList(that.cid)
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		.center {
			width: 100%;
			box-sizing: border-box;
			padding: 0 30rpx;
			min-height:100vh;
			background: #F4F4F4;
			.box {
				background: #FFFFFF;
				border-radius: 20px;
				width: 100%;
				margin: 20rpx 0 0;
				box-sizing: border-box;
				padding: 0 20rpx;
				.top {
					padding: 24rpx 0 14rpx;
					.order-no {
						font-size: 24rpx;
						font-weight: 400;
						color: #333333;
						line-height: 30rpx;
					}
					.text_red {
						font-size: 24rpx;
						font-weight: 400;
						color: #FF2020;
						line-height: 34rpx;
					}
					.text_blue {
						font-size: 24rpx;
						font-weight: 500;
						color: #506DFF;
						line-height: 34rpx;
					}
				}
				.time {
					font-size: 24rpx;
					font-weight: 400;
					color: #555555;
					line-height: 34rpx;
					padding: 0 0 24rpx;
				}
				.good {
					width: 100%;
					padding: 24rpx 0 20rpx;
					border-top: 1rpx solid #F4F4F4;
					border-bottom: 1rpx solid #F4F4F4;
					.good_item {
						padding-bottom: 32rpx;
					}
					.good_item:nth-last-child(1) {
						padding-bottom: 0;
					}
					.img {
						width: 180rpx;
						height: 180rpx;
					}
					.good_center {
						margin: 0 20rpx;
						width: 338rpx;
						.name {
							font-size: 30rpx;
							font-weight: 400;
							color: #222222;
							line-height: 42rpx;
							padding-bottom: 20rpx;
						}
						.spec {
							width: max-content;
							max-width: 300rpx;
							background: #F5F5F5;
							border-radius: 12rpx;
							font-size: 20rpx;
							font-weight: 400;
							color: #888888;
							line-height: 28rpx;
							box-sizing: border-box;
							padding: 12rpx;
						}
					}
					.price {
						font-size: 20rpx;
						font-weight: 400;
						color: #222222;
						line-height: 24rpx;
						padding: 0 0 16rpx;
					}
					.num {
						font-size: 24rpx;
						font-weight: 500;
						color: #888888;
						line-height: 28rpx;
					}
					.good_bottom {
						font-size: 22rpx;
						font-weight: 400;
						color: #272727;
						line-height: 32rpx;
						padding: 26rpx 0 0;
					}
					.bottom_price {
						font-size: 32rpx;
						color: #222222;
						font-weight: bold;
						line-height: 24px;
						line-height: 24px;
					}
				}
				.box_bottom {
					padding: 24rpx 0;
					.btn1 {
						width: 180rpx;
						height: 60rpx;
						border-radius: 36rpx;
						border: 1rpx solid #787878;
						font-size: 28rpx;
						font-weight: 400;
						color: #5D5D5D;
						line-height: 40rpx;
						margin-left: 24rpx;
					}
					.btn2 {
						background: #506DFF;
						border: none;
						color: #FFFFFF;
					}
				}
			}
		}
		.popu {
			width: 570rpx;
			margin: 0 auto;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 0 30rpx;
			.popu_title {
				font-size: 36rpx;
				font-weight: 500;
				color: #222222;
				line-height: 50rpx;
				padding: 44rpx 0 36rpx;
			}
			.u-textarea {
				background: #F2F2F2 !important;
				border-radius: 20rpx !important;
				margin-bottom: 54rpx;
			}
			.popu_bottom {
				padding-bottom: 52rpx;
				.btn1 {
					width: 220rpx;
					height: 76rpx;
					border-radius: 38rpx;
					border: 2rpx solid #506DFF;
					font-size: 32rpx;
					font-weight: 400;
					color: #506DFF;
					line-height: 44rpx;
				}
				.btn2 {
					background: #506DFF;
					color: #FFFFFF;
					margin-left: 50rpx;
				}
			}
			
		}
	}
</style>