<template>
	<view class="container">
		<view class="order-info">
			<image :src="orderInfo.hospitalVo.logoUrl" mode="aspectFill"></image>
			<view class="info-content">
				<span class="name">{{orderInfo.hospitalVo.name || '-'}}</span>
				<span class="address">
					<u-icon name="map" color="#999999" size="14"></u-icon>
					<span class="txt">{{orderInfo.hospitalVo.address || '-'}}</span>
				</span>
				<!-- <span class="area">所属区：{{orderInfo.hospitalVo.areaName || '-'}}</span> -->
			</view>
		</view>
		<view class="info-box">
			<view class="info-item">
				<span class="title">所在科室</span>
				<span class="txt">{{orderInfo.hospitalDepartmentName || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">房床号</span>
				<span class="txt">{{orderInfo.roomNumber || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item info-item-spa">
				<span class="title">备注</span>
				<span class="txt">{{orderInfo.remarks || '-'}}</span>
			</view>
		</view>
		<view class="info-box">
			<view class="info-item">
				<span class="title">就诊人</span>
				<span class="txt">{{orderInfo.patientVo.name || '-'}}（{{orderInfo.patientVo.mobile || '-'}}）</span>
			</view>
		</view>
		<view class="info-box">
			<view class="info-item">
				<span class="title">服务产品</span>
				<span class="txt">{{orderInfo.serviceName || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">期望时间</span>
				<span class="txt">{{orderInfo.expectedTime || '-'}}</span>
			</view>
		</view>

		<!-- 基本信息 -->
		<view class="info-box">
			<span class="title-m">基本信息</span>
			<view class="info-item">
				<span class="title">订单号</span>
				<span class="txt">{{orderInfo.orderNo || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">下单时间</span>
				<span class="txt">{{orderInfo.createTime || '-'}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">下单数量</span>
				<span class="txt">{{orderInfo.orderNum || '-'}}（{{ getUnitTxt(orderInfo.serviceUnit) }}）</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">销售价格</span>
				<span class="txt">￥{{orderInfo.sellingPrice || "0.00"}}</span>
			</view>
			<u-line></u-line>
			<view class="info-item">
				<span class="title">应付金额</span>
				<span class="txt">￥{{orderInfo.totalPrice || "0.00"}}</span>
			</view>
		</view>
		<view class="info-box">
			<span class="title-m">退款信息</span>

			<view class="form-box">
				<u--form labelWidth="140rpx" :model="formData" :rules="rules" ref="uForm">
					<u-form-item label="退款原因" prop="refundReason" borderBottom @click="showReason = true" required>
						<u-input :border="none" disabled v-model="formData.refundReason" disabledColor="#ffffff"
							placeholder="请选择" inputAlign="right" />
						<u-icon slot="right" name="arrow-right" color="#999999"></u-icon>
					</u-form-item>
					<u-form-item label="退款数量" prop="refundQuantity" borderBottom required>
						<view class="switch-box">
							<u-number-box v-model="formData.refundQuantity" :max="orderInfo.orderNum"
								min="1"></u-number-box>
						</view>
					</u-form-item>

					<u-form-item label="退款金额" prop="refundAmount" borderBottom required>
						<u--input type="number" maxlength="9" v-model="formData.refundAmount"
							placeholder-class="input-placeholder" border="none" clearable placeholder="请输入"
							inputAlign="right"></u--input>
					</u-form-item>

					<u-form-item label="退款方式" prop="refundWayName" borderBottom>
						<u--input v-model="formData.refundWayName" placeholder-class="input-placeholder" border="none"
							clearable placeholder="请输入" disabled inputAlign="right"></u--input>
					</u-form-item>


					<u-form-item label="退款说明" prop="refundDesc" borderBottom labelPosition="top">
						<u--textarea v-model="formData.refundDesc" placeholder="请输入退款说明，非必填"
							height="200rpx"></u--textarea>
					</u-form-item>

				</u--form>
			</view>
		</view>

		<view class="footer-btn">
			<span class="btn" @click="submitForm">确认提交</span>
		</view>


		<u-action-sheet :show="showReason" :actions="reasonList" title="请选择性别" @close="showReason = false"
			@select="reasonSelect" safeAreaInsetBottom>
		</u-action-sheet>
	</view>
</template>

<script>
	import {
		dictData
	} from "@/api/home.js"
	import {
		getOrderDetail,
		getServiceUnit,
	} from '@/api/order.js'
	import {
		addAfterSaleOrder
	} from '@/api/hospital.js'
	export default {
		data() {
			return {
				unitList: [],
				orderInfo: {},
				orderId: '',
				showReason: false,
				formData: {
					refundReason: '',
					refundQuantity: 1,
					refundAmount: '',
					refundWay: '原路返回'
				},
				reasonList: [],
				rules: {
					refundReason: [{
						required: true,
						message: '请选择退款原因',
						trigger: ['blur', 'change'],

					}],
					// refundQuantity: [{
					// 	required: true,
					// 	message: '请输入退款数量',
					// 	trigger: ['blur', 'change'],

					// }],
					// refundAmount: [{
					// 	required: true,
					// 	message: '请输入退款金额',
					// 	trigger: ['blur', 'change'],
					// }],
				}
			}
		},
		//计算refundAmount
		computed: {
			refundAmount() {
				this.formData.refundAmount = this.formData.refundQuantity * this.orderInfo.sellingPrice;
				return this.formData.refundQuantity * this.orderInfo.sellingPrice;
			}

		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(options) {
			if (options.id) {
				this.orderId = options.id;
			}
			this.getServiceUnit();
			this.getReasonList()
			this.getRefundMethod()
		},
		onShow() {
			this.getInfoById(this.orderId);
		},
		methods: {

			//获取退款原因
			getReasonList() {
				let that = this;
				dictData('reason_refund').then(res => {
					let data = res.data;
					data.forEach(item => {
						item.name = item.dictLabel
					})
					that.reasonList = data;

				}, err => {
					console.log(err);
				}).finally(() => {

				});
			},

			//获取退款方式
			getRefundMethod() {
				let that = this;
				dictData('refund_way').then(res => {
					let data = res.data;
					data.forEach(item => {
						item.name = item.dictLabel
					})
					that.formData.refundWay = data[0].dictValue;
					that.formData.refundWayName = data[0].name
				}, err => {
					console.log(err);
				}).finally(() => {

				});
			},

			reasonSelect(e) {
				this.formData.refundReason = e.dictValue;
			},
			getUnitTxt(value) {
				let obj = this.unitList.find(item => item.code == value);;
				return obj ? obj.value : ''
			},
			getInfoById(id) {
				let that = this;
				uni.showLoading({
					title: '加载中',
					mask: true,
				});
				getOrderDetail(id).then(res => {
						if (res.code == 200) {
							let data = res.data;
							that.orderInfo = data;
						}
					})
					.catch((err) => {
						console.log(err);
					})
					.finally(() => {
						uni.hideLoading();
					});
			},
			//查询单位
			getServiceUnit() {
				let that = this;
				getServiceUnit().then(res => {
						if (res.code == 200) {
							that.unitList = res.data.serviceUnit;
						}

					})
					.catch((err) => {
						console.log(err);
					})

			},

			submitForm() {
				console.log("formData", this.formData);
				this.$refs.uForm.validate().then((res) => {
						if (res) {
							this.formData.orderId = this.orderId;
							addAfterSaleOrder(this.formData).then(res => {
								uni.$u.toast('添加成功');
								setTimeout(() => {
									uni.navigateBack({
										delta: 1,
									});
								}, 1000)
							}, err => {
								console.log(err);
							}).finally(() => {

							});
						}
					})
					.catch((errors) => {
						console.log(",errors", errors);
						uni.$u.toast('校验失败');
					});
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 20rpx 20rpx 200rpx;

		.order-info {
			display: flex;
			align-items: center;
			padding: 20rpx;
			background: #fff;
			border-radius: 10rpx;
			margin-bottom: 20rpx;

			image {
				width: 180rpx;
				height: 180rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
			}

			.info-content {
				display: flex;
				flex-direction: column;
				flex: 1;

				.name {
					word-break: break-all;
					font-size: 32rpx;
					color: #333;
					font-weight: bold;
				}

				.address {
					font-size: 28rpx;
					color: #666;
					margin-top: 20rpx;

					width: 100%;
					font-weight: 500;
					line-height: 35rpx;
					display: flex;
					justify-content: flex-start;
					align-items: flex-start;
					overflow: hidden; //超出的文本隐藏
					text-overflow: ellipsis; //溢出用省略号显示
					white-space: normal; //处理元素中的 空白
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					display: -moz-box;
					-moz-line-clamp: 2;
					-moz-box-orient: vertical;
					overflow-wrap: break-word;
					word-break: break-all;

					::v-deep .u-icon {
						display: inline-block;
					}

					.txt {
						margin-left: 15rpx;
					}
				}

				.area {
					font-size: 28rpx;
					color: #666;
					margin-top: 10rpx;
				}
			}
		}

		.info-box {
			padding: 20rpx;
			background: #fff;
			border-radius: 10rpx;
			margin-bottom: 20rpx;

			.info-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx 0;

				.title {
					width: 250rpx;
					font-size: 28rpx;
					color: #333;
				}

				.txt {
					/* flex: 1; */
					max-width: 540rpx;
					text-align: left;
					font-size: 28rpx;
					color: #757575;
				}
			}

			.info-item-spa {
				align-items: flex-start;
				flex-direction: column;

				.txt {
					margin-left: 0;
					margin-top: 20rpx;
					text-align: left;
				}
			}

			.tip-txt {
				padding: 30rpx 0;
				font-size: 28rpx;
				color: #333;
			}
		}

		.title-m {
			/* 竖线和文字对齐 */
			display: flex;
			align-items: center;
			padding: 20rpx 0;
			font-size: 28rpx;
			color: #333;
			font-weight: bold;
			line-height: 28rpx;

			/* 左边加一个竖线 */
			&::before {
				content: '';
				display: inline-block;
				width: 6rpx;
				height: 30rpx;
				background: #4B91D1;
				margin-right: 10rpx;
			}
		}

		.form-box {
			padding: 20rpx;
			background-color: #fff;

			::v-deep .u-input {
				padding: 15rpx !important;
			}

			::v-deep .u-upload {
				margin-top: 15rpx;
			}

			.upload-box {
				margin-top: 20rpx;

				.name-box {
					display: flex;
					align-items: center;
					margin-top: 20rpx;

					.btn {
						padding: 10rpx 20rpx;
						background-color: #4B91D1;
						color: #fff;
						border-radius: 10rpx;
						margin-left: 20rpx;
						border: 1rpx solid #4B91D1;
					}

					.btn2 {
						background-color: #fff;
						color: #FF0000;
						border: 1rpx solid #FF0000;
					}
				}
			}

			.switch-box {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 20rpx 0;

				.name {
					margin-right: 20rpx;
					font-size: 30rpx;
				}
			}
		}

		.footer-btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			padding: 50rpx 30rpx;
			box-sizing: border-box;
			font-size: 28rpx;
			background-color: #fff;
			box-shadow: 0 -2rpx 30rpx #c5c5c53a;

			.btn {
				display: block;
				width: 100%;
				padding: 20rpx 0;
				border: 1rpx solid #4B91D1;
				background-color: #4B91D1;
				border-radius: 40rpx;
				color: #fff;
				text-align: center;
			}
		}

	}
</style>