<template>
	<view class="whole">
		
		<view class="contBox">
			<view class="oneBox">
				<view class="box">
					<view class="row">
						<view class="tit">订单编号</view>
						<view class="text">{{orderInfo.order_sn}}</view>
					</view>
					<view class="row">
						<view class="tit">开票金额</view>
						<view class="text bold">￥{{amount}}</view>
					</view>
				</view>
				
				<view class="box">
					<view class="row">
						<view class="tit">发票类型</view>
						<view class="text">增值税电子发票</view>
					</view>
					<view class="row">
						<view class="tit" @click="showTips = true">发票内容<up-icon name="question-circle" /></view>
						<view class="text">明细</view>
					</view>
					<view class="row">
						<view class="tit">抬头类型</view>
						<view class="text flex">
							<view class="it" @click="info.body_type = 1">
								<span v-if="info.body_type == 1" class="iconfont icon-checked"></span>
								<span v-else class="iconfont icon-circle"></span>
								个人或事业单位
							</view>
							<view class="it" @click="info.body_type = 2">
								<span v-if="info.body_type == 2" class="iconfont icon-checked"></span>
								<span v-else class="iconfont icon-circle"></span>
								企业
							</view>
						</view>
					</view>
					<view class="row">
						<view class="tit">发票抬头<text>*</text></view>
						<input type="text" v-model="info.body" placeholder="请输入发票抬头" />
					</view>
					<view class="row" v-if="info.body_type == 2">
						<view class="tit">税号<text>*</text></view>
						<input type="text" v-model="info.no" placeholder="请输入税号" />
					</view>
				</view>
				
				<view class="btnBox">
					<view class="btn" @click="nextStep()">下一步</view>
				</view>
			</view>
			
			<up-popup :show="showNext" mode="right" :customStyle="customStyle">
				<view class="contBox">
					<view class="box">
						<view class="row">
							<view class="tit">开票金额</view>
							<view class="text bold">￥{{amount}}</view>
						</view>
						<view class="row">
							<view class="tit">发票类型</view>
							<view class="text">增值税电子发票</view>
						</view>
						<view class="row">
							<view class="tit">抬头类型</view>
							<view class="text">{{ info.body_type == 2 ? '企业' : '个人或事业单位'}}</view>
						</view>
						<view class="row">
							<view class="tit">发票内容</view>
							<view class="text">{{info.content}}</view>
						</view>
						<view class="row">
							<view class="tit">发票抬头</view>
							<view class="text">{{info.body}}</view>
						</view>
						<view class="row" v-if="info.body_type == 2">
							<view class="tit">税号</view>
							<view class="text">{{info.no}}</view>
						</view>
					</view>
					
				</view>
				<view class="btnBox">
					<view class="btn one" @click="showNext = false">返回修改</view>
					<view class="btn btn1" @click="commitApply()">确认提交</view>
				</view>
			</up-popup>
			
		</view>
		
		<up-modal :show="showTips" :showConfirmButton="false" :showCancelButton="false">
			<view>
				<view class="refTitle">发票内容说明</view>
				<view class="mesg">・发票内容将显示详细商品名称与价格信息。<br>
					・部分商家可能开具发票内容为商品所属类别及价格信息，如有特殊需求，请向管家客服咨询。
				</view>
				<view style="padding: 50rpx;">
					<view class="iknow" @click="showTips = false">我知道了</view>
				</view>
			</view>
		</up-modal>
	
	</view>
	
</template>

<script>
	import { ref, reactive, toRefs } from 'vue'
	import { get, post } from '@/api/request.js'
	import { showToast } from '@/components/common.js'
	
	export default {
		setup() {
			const data = reactive({
				customStyle: {
					height: '100vh',
					width: '100vw'
				},
				order_id: 0,
				orderInfo: {},
				info: {
					content: '明细',
					body_type: 2,
					body: '',
					no: ''
				},
				isLoading: false,
				Color: uni.getStorageSync('theme_color'),
				showTips: false,
				showNext: false,
				amount: 0
			})
			
			async function getOrderInfo() {
				await get(`/api/v1/order/${data.order_id}`).then((res) => {
					data.orderInfo = res.data
					data.amount = ((data.orderInfo.total_price * 100 - data.orderInfo.refund_amount * 100) / 100).toFixed(2)
				})
			}
			
			function commitApply() {
				uni.showLoading({
					mask: true,
					title: '正在提交'
				})
				let params = {
					order_id: data.order_id,
					type: 1,
					body_type: data.info.body_type,
					body: data.info.body,
					tax_number: data.info.no
				}
				if(params.body_type == 1) {
					params.tax_number = ''
				}
				post(`/api/v1/orderInvoice`, params).then((res) => {
					uni.hideLoading()
					uni.showToast({
						title: '提交成功',
						icon: 'success',
						duration: 1000,
						mask: true,
						success() {
							setTimeout(() => {
								uni.redirectTo({ url: '/pages/mine/invoice/detail?id=' + res.data.order_invoice_id })
							}, 1000)
						}
					})
				})
			}
			
			function nextStep() {
				if(!data.info.body) {
					return showToast('请填写发票抬头')
				}
				if(!data.info.no && data.info.body_type == 2) {
					return showToast('请输入税号')
				}
				data.showNext = true
			}
			
			function getLastConfig() {
				get(`/api/v1/orderInvoice/last`).then((res) => {
					if(res.data) {
						data.info.body_type = res.data.body_type
						data.info.body = res.data.body
						data.info.no = res.data.tax_number
					}
				})
			}
			
			return {
				...toRefs(data),
				getOrderInfo,
				commitApply,
				nextStep,
				getLastConfig
			}
		},
		async onLoad(options) {
			this.order_id = options.id
			// await this.$onLaunched
			await this.getOrderInfo()
			this.getLastConfig()
		}
	}
</script>

<style lang="scss" scoped>
	.whole{
		width: 100%;
		height: 100vh;
		.contBox{
			padding: 24rpx;
			box-sizing: border-box;
			height: 100%;
			background-color: #f5f5f5;
			.box{
				border-radius: 10rpx;
				background-color: #fff;
				margin-bottom: 24rpx;
				padding: 16rpx 0;
			}
			.row{
				padding: 20rpx 24rpx;
				font-size: 30rpx;
				color: #666;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.tit{
					color: #000;
					text{
						color: #f00;
						font-size: 24rpx;
					}
				}
				.text{
					width: calc(100% - 200rpx);
					text-align: right;
					.it{
						display: flex;
						align-items: center;
						margin-left: 20rpx;
					}
				}
				.bold{
					color: #000;
					font-weight: 600;
				}
				input{
					text-align: right;
					color: #666;
				}
				.flex{
					display: flex;
					align-items: center;
					justify-content: flex-end;
				}
			}
		}
		
		.refTitle{
			font-size: 30rpx;
			color: #000;
			padding: 0 0 30rpx;
			font-weight: bold;
			position: relative;
			text-align: center;
			margin-bottom: 10rpx;
		}
		.mesg{
			font-size: 28rpx;
			color: #303030;
			line-height: 46rpx;
			height: 500rpx;
		}
		.iknow{
			width: 100%;
			height: 80rpx;
			background: v-bind('Color');
			font-size: 30rpx;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 80rpx;
		}
	}
	.loadbox{
		padding: 200rpx 0;
		text-align: center;
		color: #666;
		position: fixed;
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		background-color: #fff;
		left: 0;
		top: 0;
		z-index: 100;
	}
	.btnBox{
		width: 100%;
		padding: 24rpx;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #fff;
		display: flex;
		align-items: center;
		.btn{
			width: 100%;
			height: 80rpx;
			background: v-bind('Color');
			font-size: 30rpx;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 8rpx;
			&.btn1{
				margin-left: 5%;
			}
			&.one{
				background: #eee;
				color: #333;
			}
		}
	}
	.iconfont{
		font-size: 18px;
	}
	.icon-checked{
		color: v-bind('Color');
	}
</style>
