<template>
	<view v-if="info">
		<view class="order-header">
			<view class="order-header-body">
				<view class="order-header-left">
					<image src="../../images/address.png" mode="aspectFit"></image>
				</view>
				<view class="order-header-main">
					<view>所属科室：{{info.depName}}</view>
					<view>联系人：{{info.contact}}</view>
					<view>联系电话：{{info.phone}}</view>
				</view>
				<view class="order-header-right">
					<span v-if="info.status == 0">待分配</span>
					<span v-if="info.status == 1">进行中</span>
					<span v-if="info.status == 2">已完成</span>
					<span v-if="info.status == 3">已作废</span>
					<span  v-if="info.status == 4">已结算</span>
					<span v-if="info.status == 5">已评价</span>
				</view>
			</view>
		</view>
		<view class="container">
			<view class="order-box">
				<view class="order-box-title">订单基础信息</view>
				<view class="order-box-list">
					<view class="order-box-list-title">病人姓名</view>
					<view class="order-box-list-val">{{info.name}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">病人性别</view>
					<view class="order-box-list-val">
						<span v-if="info.gender == 1">男</span>
						<span v-if="info.gender == 2">女</span>
					</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">病人年龄</view>
					<view class="order-box-list-val">{{info.age}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">病人床号</view>
					<view class="order-box-list-val">{{info.bed}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">所患疾病</view>
					<view class="order-box-list-val">{{info.ill}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">服务类型</view>
					<view class="order-box-list-val">{{info.cateName}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">单价(/人/天)</view>
					<view class="order-box-list-val">{{info.price}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">开始时间</view>
					<view class="order-box-list-val">{{info.start}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">结束时间</view>
					<view class="order-box-list-val">{{info.end}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">结束时间</view>
					<view class="order-box-list-val">{{info.end}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">预收金</view>
					<view class="order-box-list-val">{{info.preMoney}}</view>
				</view>
				<view class="order-box-list">
					<view class="order-box-list-title">订单金额</view>
					<view class="order-box-list-val">{{info.amount}}</view>
				</view>
				<view class="order-box-list2">
					<view class="order-box-list-title">备注</view>
					<view class="order-box-list-val">{{info.remark?info.remark:'无'}}</view>
				</view>
			</view>
			<view class="order-box2">
				<view>订单号：{{info.sn}}</view>
				<view>下单日期：{{info.createTime}}</view>
				<view v-if="info.finishTime">完成日期：{{info.finishTime}}</view>
				<view v-if="info.canceTime">作废日期：{{info.canceTime}}</view>
				<view v-if="info.cancelUserName">作废人：{{info.cancelUserName}}</view>
				<view v-if="info.cancelReason">作废原因：{{info.cancelReason}}</view>
			</view>
			
			<view v-if="info.todo.length > 0">
				<view>工单信息</view>
				<view v-for="(item,index) in info.todo" class="order-box2">
					<view>调度：{{item.userName}}</view>
					<view>护工：{{item.workerName}}</view>
					<view>开始时间：{{item.start}}</view>
					<view>结束时间：{{item.end}}</view>
					<view>备注：{{item.remark}}</view>
					<view>状态：
					<span v-if="item.status == 0">已作废</span>
					<span v-if="item.status == 1">服务中</span>
					<span v-if="item.status == 2">已结束</span>
					</view>
					<view>派发日期：{{item.createTime}}</view>
					
				</view>
			</view>
			<view v-if="info.status ==5">
				<view>评价信息</view>
				<view  class="order-box2">
					<view>评价内容：{{info.comment.content}}</view>
					<view>评价分数：	<uni-rate :readonly="true" :value="info.comment.score"/></view>
					<view>评价时间：{{info.comment.createTime}}</view>

				</view>
			</view>
			<view v-if="info.complain.length > 0">
				<view>投诉信息</view>
				<view v-for="(item,index) in info.complain" class="order-box3">
					<view>投诉内容：{{item.content}}</view>
					<view>状态：
						<span v-if="item.status === 0">待处理</span>
						<span v-if="item.status === 1">已处理</span>
					</view>
					<view>投诉时间：{{item.createTime}}</view>
					<view v-if="item.status === 1">处理人：{{item.dealName}}</view>
					<view v-if="item.status === 1">备注：{{item.remark}}</view>
					<view v-if="item.status === 1">处理时间：{{item.dealTime}}</view>
				</view>
			</view>
			<view v-if="info.pays.length > 0">
				<view>预收金信息</view>
				<view v-for="(item,index) in info.pays" class="order-box3">
					<view>金额：￥{{item.money}}</view>
					<view>退款金额：￥{{item.money2}}</view>
					<view>支付方式：
					<span v-if="item.type === 2">线上</span>
					<span v-if="item.type === 1">线下</span>
					</view>
					<view>日期：{{item.payTime}}</view>
				</view>
			</view>
			

			<view v-if="info.status == 0||info.status == 1" class="btn" @click="finishBtn()">预收金</view>
			<view style="margin-top: 10px" v-if="info.status == 4" class="btn" @click="commentBtn()">评价</view>
			<view style="margin-top: 10px" v-if="(info.status!= 0 && info.status!=3) && info.complain.length==0" class="btn" @click="complaintBtn()">投诉</view>
			<!-- <view v-if="info.status == 1" class="btn btn-success" @click="finishBtn()">完成</view>
			<view v-if="info.status != 3&&info.status != 4" class="btn btn-danger" @click="cancelBtn()">作废</view> -->
		</view>

		<cmodal :show.sync="cancelShow" title="作废订单" @confirm="cancelConfirm">
		    <textarea v-model="cancelReason" style="height: 150rpx;" placeholder="请输入作废原因"></textarea>
		</cmodal>
		<cmodal :show.sync="finishShow" title="预收金" @confirm="finishConfirm"  @cancel="cancelFinishConfirm">
			<view class="form-box3">
			  <view class="form-box-label">金额 <text class="text-red">*</text></view>
			  <view class="form-box-content">
			    <input type="number" v-model="amount" placeholder="请填写金额" />
			  </view>
			</view>
		</cmodal>
		<cmodal :show.sync="tsShow" title="投诉" @confirm="tsConfirm"  @cancel="cancelTsConfirm">
			<view class="form-box3">
				<view class="form-box-label">投诉内容<text class="text-red">*</text></view>
				<view class="form-box-content">
					<input type="textarea" v-model="tsContent" placeholder="请输入投诉内容" />
				</view>
			</view>
		</cmodal>
		<cmodal :show.sync="pjShow" title="评价" @confirm="pjConfirm"  @cancel="cancelPjConfirm">
			<view class="form-box3">
				<view class="form-box-label">评价内容</view>
				<view class="form-box-content">
					<input type="textarea" v-model="pjContent" placeholder="请输入评价内容" />
				</view>

			</view>
			<view class="form-box3">
				<view class="form-box-label">评分<text class="text-red">*</text></view>
				<view class="form-box-content">
					<uni-rate v-model="pjScore" @change="pjonChange"/>
				</view>

			</view>
		</cmodal>
	</view>
</template>

<script>
	var app = getApp();
	import cpicker from "../../components/cpicker/cpicker.vue";
	import wpicker from "../../components/w-picker/w-picker.vue";
	import cmodal from "../../components/cmodel/cmodel.vue";
	export default {
		components: {
			cpicker,
			wpicker,
			cmodal
		},
		data() {
			return {
				info: null,
				flag: 0,
				id: 0,
				cancelReason: '',
				cancelShow: false,
				end: '',
				finishShow: false,
				tsShow: false,
				pjShow: false,
				endShow: false,
				amount: '',
				wx: null,
                tsContent:'',
                pjContent:'',
                pjScore:'',
			}
		},
		onLoad(option) {
			this.id = option.id;
			let that = this;
			if (this.$wechat && this.$wechat.isWechat()) {//获取定位经纬度
				this.$wechat.getWx(function (res) {
					console.log(res)
					that.wx = res;
				});
			}
		},
		onShow() {
			var that = this;
			app.ajaxReadyCallback = res => {  //各个接口统一回调方法
				var apiname = res.data.apiname;
				console.log(res);
				if(apiname == 'order'){
					that.info = res.data.data;
				}else if(apiname == 'payOrder'){
					that.getPayParam(res.data.data.payId);
				}else if(apiname == 'pay'){
					if(that.wx){
						that.wx.chooseWXPay({
							timestamp: res.data.data.timestamp,
							nonceStr: res.data.data.noncestr,
							package: res.data.data.package,
							signType: res.data.data.signtype,
							paySign: res.data.data.paysign,
							success: function (r) {
								// 支付成功后的回调函数
								if (r.errMsg == "chooseWXPay:ok") {
									window.location.reload();
								} else {
									uni.showToast({
										title: '支付失败',
										icon: 'none',
										duration: 2000
									})
								}
							},
							cancel: function(r) {},
							fail:function(r){
								uni.showToast({
									title: '支付失败',
									icon: 'none',
									duration: 2000
								})
							}
						});
					}else{
						uni.showToast({
							title: '支付调用失败，请刷新重试',
							icon: 'none',
							duration: 2000
						})
					}
				}else if (apiname=='complain'){
                    uni.showToast({
                        title: '投诉成功',
                        icon: 'none',
                        duration: 2000
                    })
                    this.getOrderDetail();
				}else if (apiname=='comment'){
                    uni.showToast({
                        title: '评价成功',
                        icon: 'none',
                        duration: 2000
                    })
                    this.getOrderDetail();
                }
			}
			
			this.getOrderDetail();
		},
		methods: {
            pjonChange(e) {
				this.pjScore = e.value;
            },
			getOrderDetail(){
				app.ajax({
					url: app.globalData.serverUrl + 'PhOrders/detail',
					type: 'POST',
					apiname: 'order',
					data:{
						id: this.id
					}
				});
			},
			sendOrder(id){
				uni.navigateTo({
				    url: '/pages/order/send?orderId='+id
				});
			},
			cancelBtn(){
				this.cancelShow = true;
				console.log(orderId,this.cancelShow);
			},
			cancelConfirm(){
				app.ajax({
					url: app.globalData.serverUrl + 'Order/cancel',
					type: 'POST',
					apiname: 'cancel',
					data: {
						cancelReason: this.cancelReason,
						id: this.id
					}
				});
			},
			selectEnd(){
				this.endShow = true;
			},
			onConfirm(e,type){
				this.end = e.result;
			},
			finishBtn(){
				this.finishShow = true;
				this.endShow = false;
				this.amount = '';
			},
            complaintBtn(){
                this.tsShow = true;
                this.tsContent = '';
			},
            commentBtn(){
                this.pjShow = true;
                this.pjContent = '';
                this.pjScore = '';
            },
			finishConfirm(){
				if(!this.amount || this.amount <= 0){
					uni.showToast({
						title: '未输入金额',
						icon: 'none',
						duration: 2000
					})
                    this.finishShow = true;
                    return;
				}
				app.ajax({
					url: app.globalData.serverUrl + 'PhOrders/payOrder',
					type: 'POST',
					apiname: 'payOrder',
					data: {
						orderId: this.id,
						money: this.amount
					}
				});
			},
            tsConfirm(){
                if(!this.tsContent){
                    uni.showToast({
                        title: '请输入投诉内容',
                        icon: 'none',
                        duration: 2000
                    })
					this.tsShow = true;
                    return;
                }
                app.ajax({
                    url: app.globalData.serverUrl + 'PhOrders/complain',
                    type: 'POST',
                    apiname: 'complain',
                    data: {
                        orderId: this.id,
                        content: this.tsContent
                    }
                });
            },
            pjConfirm(){
                if(!this.pjScore){
                    uni.showToast({
                        title: '评分不能为空',
                        icon: 'none',
                        duration: 2000
                    })
                    this.pjShow = true;
                    return;
                }
                app.ajax({
                    url: app.globalData.serverUrl + 'PhOrders/comment',
                    type: 'POST',
                    apiname: 'comment',
                    data: {
                        orderId: this.id,
                        content: this.pjContent,
                        score: this.pjScore,
                    }
                });
            },
			getPayParam(payId){
				app.ajax({
					url: app.globalData.serverUrl + 'PhOrders/pay',
					type: 'POST',
					apiname: 'pay',
					data: {
						payId: payId
					}
				});
			},
			
			cancelFinishConfirm(){
				this.amount = '';
				this.finishShow = false;
			},
            cancelTsConfirm(){
                this.tsContent = '';
                this.tsShow = false;
            },
            cancelPjConfirm(){
                this.pjContent = '';
                this.pjScore = '';
                this.pjShow = false;
            },
			getUrlCode (name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		overflow-x: hidden;
	}
	.container{
		overflow: hidden;
	}
	.btn{
		// width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 34rpx;
		font-weight: bold;
		border-radius: 10rpx;
		background-color: #FFFFFF;
		color: var(--themeColor);
		// margin: 10rpx;
	}
	.btn.btn-success{
		background-color: #5cb85c;
		color: #FFFFFF;
	}
	.btn.btn-danger{
		background-color: #FF7576;
		color: #FFFFFF;
	}
	.order-box2{
		width: 670rpx;
		height: auto;
		border-radius: 10rpx;
		background-color: #FFFFFF;
		overflow: hidden;
		padding: 30rpx 20rpx;
		margin: 20rpx 0;
		color: #666666;
		font-size: 28rpx;
	}
	.order-box{
		width: 100%;
		height: auto;
		border-radius: 10rpx;
		background-color: #FFFFFF;
		overflow: hidden;
	}
	.order-box-title{
		position: relative;
		width: 680rpx;
		height: 90rpx;
		line-height: 90rpx;
		padding-left: 30rpx;
		color: #333333;
		font-size: 34rpx;
		font-weight: bold;
	}
	.order-box-title::before{
		content: '';
		width: 5rpx;
		height: 28rpx;
		position: absolute;
		left: 16rpx;
		top: 32rpx;
		background-color: var(--themeColor);
	}
	.order-box-list{
		width: 670rpx;
		min-height: 90rpx;
		height: auto;
		padding: 0 20rpx;
		overflow: hidden;
	}
	.order-box-list .order-box-list-title{
		width: 270rpx;
		height: 90rpx;
		line-height: 90rpx;
		float: left;
		color: #333333;
		font-size: 34rpx;
		font-weight: bold;
	}
	.order-box-list .order-box-list-val{
		width: 380rpx;
		min-height: 62rpx;
		float: right;
		color: #333333;
		font-size: 30rpx;
		font-weight: bold;
		padding-top: 28rpx;
		text-align: right;
	}
	.order-box-list2{
		width: 670rpx;
		min-height: 90rpx;
		height: auto;
		padding: 0 20rpx;
		overflow: hidden;
	}
	.order-box-list2 .order-box-list-title{
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		color: #333333;
		font-size: 34rpx;
		font-weight: bold;
	}
	.order-box-list2 .order-box-list-val{
		width: 630rpx;
		min-height: 50rpx;
		height: auto;
		color: #333333;
		font-size: 30rpx;
		font-weight: bold;
		background-color: #F7F7F7;
		border-radius: 10rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}
	
	.order-header{
		background-color: var(--themeColor);
		width: 100%;
		height: auto;
		padding: 20rpx;
	}
	.order-header .order-header-body{
		width: 650rpx;
		height: 124rpx;
		padding: 30rpx;
		background-color: #FFFFFF;
		border-radius: 10rpx;
	}
	.order-header-left{
		width: 100rpx;
		height: 124rpx;
		float: left;
	}
	.order-header-left image{
		width: 70rpx;
		height: 70rpx;
		margin-top: 30rpx;
	}
	.order-header-main{
		width: 400rpx;
		height: 124rpx;
		float: left;
		color: #333333;
		font-size: 26rpx;
	}
	.order-header-main view{
		padding: 5rpx 0;
	}
	.order-header-right{
		width: 120rpx;
		height: 124rpx;
		line-height: 124rpx;
		float: right;
		color: var(--themeColor);
		font-size: 34rpx;
		text-align: right;
	}
	
	.order-box3{
		width: 670rpx;
		height: auto;
		border-radius: 10rpx;
		background-color: #FFFFFF;
		overflow: hidden;
		padding: 30rpx 20rpx;
		margin: 20rpx 0;
		color: #666666;
		font-size: 28rpx;
		border-left: 5px solid var(--themeColor);
	}
</style>
