<template>
  <view class="detail-page">
    <!-- 顶部导航栏 -->
    <custom-navbar 
      title="订单详情" 
      :show-back="true"
	  title-color="#333333"
	  backgroundColor="transparent"
    ></custom-navbar>
    <view class="content-part">
    <!-- 订单状态 -->
    <view class="status-section">
      <text class="status-text">{{orderInfo.state == 1 ? '待支付' : orderInfo.state == 2 ? '待接单' : orderInfo.state == 3 ? '待服务' : orderInfo.state == 4 ? '待完成' : orderInfo.state == 5 ? '已完成' : orderInfo.state == 6 ? '待评价' : orderInfo.state == 7 ? '已取消' : ''}}</text>
    </view>
    
    <!-- 到家订单 客户以及手艺人信息 -->
    <view class="service-card" v-if="orderInfo.order_kind==1">
      <view class="location-info">
        <image src="/static/images/gohome_location.png" class="location-icon"></image>
        <view class="address-info">
          <view class="name-phone">
            <text class="name">{{reservation_address_arr.name}}</text>
            <text class="phone">{{reservation_address_arr.call_phone}}</text>
          </view>
          <text class="address">{{reservation_address_arr.server_address}}{{reservation_address_arr.house_number}}</text>
        </view>
      </view>
	  <image class="btn-map"
	  	   src="/static/images/map_icon.png"
	  	   mode="aspectFit"
	  	   @click="openMap()"></image>
	  <image class="btn-phone"
	  	   src="/static/images/order_phone.png"
	  	   mode="aspectFit"
	  	   @click="contactService(reservation_address_arr.call_phone)"></image>
    </view>
	<!-- 到店订单客户信息 -->
	<view class="service-card" v-if="orderInfo.order_kind==2">
	  <view class="location-info">
	    <image src="/static/images/gohome_location.png" class="location-icon"></image>
	    <view class="address-info">
	      <view class="name-phone">
	        <text class="name">{{orderInfo.order_username}}</text>
	        <text class="phone">{{orderInfo.phone_user}}</text>
	      </view>
	    </view>
	  </view>
	  <image class="btn-phone"
	  	   src="/static/images/order_phone.png"
	  	   mode="aspectFit"
	  	   @click="contactService(orderInfo.phone_user)"></image>
	</view>
	
    <!-- 预约信息 -->
    <view class="booking-card">
      <view class="service-item">
        <image
			:src="orderInfo.server_photo[0]"
			v-if="orderInfo.server_photo && orderInfo.server_photo.length!=0"
			mode="aspectFill"
			class="service-image"></image>
        <view class="service-info">
          <text class="service-name text-overflow">{{orderInfo.server_title}}</text>
          <text class="service-price">¥{{orderInfo.order_money}}</text>
        </view>
        <text class="service-count">×1</text>
      </view>
	  <view class="service-real">
		<text class="real-lable">实际支付金额：</text>
		<text class="real-price">￥{{orderInfo.pay_money}}</text>
	  </view>
    </view>
    
    <!-- 订单信息 -->
    <view class="order-card">
      <text class="card-title">订单信息</text>
      <view class="info-item">
        <text class="label">订单编号</text>
        <text class="value">{{orderInfo.number}}</text>
      </view>
	  <view class="info-item" v-if="orderInfo.order_kind == 1">
	    <text class="label">到家时间</text>
	    <text class="value">{{orderInfo.reservation_time}}</text>
	  </view>
      <view class="info-item">
        <text class="label">下单时间</text>
        <text class="value">{{orderInfo.add_time}}</text>
      </view>
      <view class="info-item" v-if="orderInfo.state != 1">
        <text class="label">支付时间</text>
        <text class="value">{{orderInfo.pay_time}}</text>
      </view>
      <view class="info-item" v-if="orderInfo.state != 1">
        <text class="label">支付方式</text>
        <text class="value">{{orderInfo.pay_kind == 1 ? '账户余额' : orderInfo.pay_kind == 2 ? '微信支付' : orderInfo.pay_kind == 3 ? '支付宝支付' : ''}}</text>
      </view>
      <view class="info-item" v-if="orderInfo.state != 1 && orderInfo.state != 2 && orderInfo.state != 3">
        <text class="label">开始服务时间</text>
        <text class="value">{{orderInfo.server_start_time}}</text>
      </view>
      <view class="info-item" v-if="orderInfo.state != 1 && orderInfo.state != 2 && orderInfo.state != 3 && orderInfo.state != 4">
        <text class="label">服务完成时间</text>
        <text class="value">{{orderInfo.server_end_time}}</text>
      </view>
    </view>
    </view>
    <!-- 底部按钮 -->
	<view class="bottom-buttons">
		<!-- <view class="contact-button" @click="contactService(orderInfo.phone_user)">
		  <image src="/static/images/tell_icon.png" class="tell-icon"></image>
		  <text class="contact-text">联系TA</text>
		</view> -->
		<view class="handel-button btn-border"
			  v-if="orderInfo.state == 2 || orderInfo.state == 3"
			  @click.stop="cancelOrder($event, orderInfo)">
			<text class="button-text">取消订单</text>
		</view>
		<view class="handel-button btn-bg"
			  v-if="orderInfo.state == 2"
			  @click.stop="confirmOrder($event, orderInfo)">
			<text class="button-text">确认订单</text>
		</view>
		<view class="handel-button btn-bg"
			  v-if="orderInfo.state == 3"
			  @click.stop="startOrder($event, orderInfo)">
			<text class="button-text">开始服务</text>
		</view>
		<view class="handel-button btn-bg"
			  v-if="orderInfo.state == 4"
			  @click.stop="endOrder($event, orderInfo)">
			<text class="button-text">服务完成</text>
		</view>
	</view>
	
	<serviceCodeInput
		  :show="showInput"
		  @close="showInput = false"
		  @confirm="onConfirm"
		/>
	
	<!-- 获取权限提示匡内容 -->
	<view class="permission"
		  :class="{ transform: isShowPer }">
		<view class="per-tit">美融融plus 对拨打电话权限申请说明</view>
		<view class="per-cont">当您需要联系商家或平台客服的时候，需要获取拨打电话权限。</view>
	</view>
	
	</view>
</template>

<script>
import request from '../../utils/request'
import _public from '../../utils/public'
import serviceCodeInput from '../../components/service-code-input/service-code-input.vue';
import locationService from '../../utils/locationService';
import permissionUtils from '../../utils/per'
export default {
  components: { serviceCodeInput },
  data() {
    return {
	  isShowPer: false,
	  id: '',
      staffInfo: {
        avatar: '/static/images/staff.jpg',
        name: '中医理疗谢飞',
        rating: 5
      },
	  showInput: false,
	  userInfo: {},
      orderInfo: {},
	  reservation_address_arr: {},
	  artisanType: getApp().globalData.artisanType,
	  user_syr: {}
    }
  },
  onLoad(options) {
	this.id = options.id
	if(options.id) {
		// 请求订单详情
		this.getUserInfo();
		this.getOrderDetail();
	}
  	
  },
  methods: {
	getUserInfo() {
	  const type = this.artisanType == 1 ? 2 : this.artisanType == 2 ? 3 : 1;
	  request.post('/user/getuser',{type}).then(result=>{
		this.userInfo = result.data;
      })
    },
	getOrderDetail() {
	  let type = this.artisanType == 1 ? 2 : this.artisanType == 2 ? 3 : '';
	  request.post('/user/orderdetail',{id: this.id,type}).then(res=>{
	  	this.orderInfo = res.data;
		this.reservation_address_arr = res.data.reservation_address_arr;
	  	console.log(this.orderInfo)
	  })
	},
	
	// 取消订单
	cancelOrder(e, order) {
	  // app阻止事件冒泡
	  // #ifdef APP-PLUS
	  e.preventDefault();
	  e.stopPropagation();
	  // #endif
	  if(order.state == 1) {
	  	uni.showModal({
	  	  title: '取消订单',
	  	  content: '确定要取消该订单吗？',
	  	  success: (res) => {
	  	    if (res.confirm) {
	  	      // TODO: 取消订单
	  		  let data = {
	  			orderNo: order.number,
	  			amount: order.pay_money,
	  		  	cancel_state: this.artisanType + 1,
	  		  	cancel_id: this.userInfo.id
	  		  }
	  		  request.post('/user/cancelorder',data).then(res=>{
				if(res.state == 1) {
				  uni.showToast({
				  	title: '取消成功！',
				  	icon: 'none'
				  })
				  this.getOrderDetail();
				  // 修改上一页页面信息
				  const pages = getCurrentPages();
				  const prevPage = pages[pages.length - 2];
				  if(prevPage) {
				  	prevPage.$vm.getOrderNumber();
				  	prevPage.$vm.getOrderList();
				  }
				}else {
				  uni.showToast({
				  	title: res.msg,
				  	icon: 'none'
				  })
				}
	  		  }).catch(err=>{
	  			console.log(err)
	  		  })
	  	    }
	  	  }
	  	})
	  }else {
	  	uni.showModal({
	  	  title: '取消订单',
	  	  content: '确定要取消该订单吗？（订单取消后，支付金额将原路退回）',
	  	  success: (res) => {
	  	    if (res.confirm) {
	  	      // TODO: 取消订单
	  		  let data = {
	  			orderNo: order.number,
	  			amount: order.pay_money,
	  		  	cancel_state: this.artisanType + 1,
	  		  	cancel_id: this.userInfo.id
	  		  }
	  		  let url = '';
	  		  if(order.pay_kind == 1) {
	  			url = '/user/cancelbalance'
	  		  }else if(order.pay_kind == 2) {
	  			url = '/user/wechatrefund'
	  		  }else if(order.pay_kind == 3) {
	  			url = '/user/alirefund'
	  		  }
	  		  console.log(data,url)
	  		  request.post(url,data).then(res=>{
				if(res.state == 1) {
					uni.showToast({
						title: '取消成功！',
						icon: 'none'
					})
					this.getOrderDetail();
					// 修改上一页页面信息
					const pages = getCurrentPages();
					const prevPage = pages[pages.length - 2];
					if(prevPage) {
						prevPage.$vm.getOrderNumber();
						prevPage.$vm.getOrderList();
					}
				}else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
	  		  	
	  		  }).catch(err=>{
	  			console.log(err)
	  		  })
	  	    }
	  	  }
	  	})
	  }
	},
	// 确认接单
	confirmOrder(e, order) {
	  // app阻止事件冒泡
	  // #ifdef APP-PLUS
	  e.preventDefault();
	  e.stopPropagation();
	  // #endif
	  request.post('/user/ordersure',{id: order.id}).then(res=>{
		uni.showToast({
			title: '已接单',
			icon: 'none'
		})
		this.getOrderDetail();
		// 修改上一页页面信息
		const pages = getCurrentPages();
		const prevPage = pages[pages.length - 2];
		if(prevPage) {
			prevPage.$vm.getOrderNumber();
			prevPage.$vm.getOrderList();
		}
		
	  })
	},
	// 开始服务
	startOrder(e, order) {
	  // app阻止事件冒泡
	  // #ifdef APP-PLUS
	  e.preventDefault();
	  e.stopPropagation();
	  // #endif
	  this.showInput = true
	},
	// 输入完成
	onConfirm(code) {
		this.showInput = false
		request.post('/user/startserver',{id: this.orderInfo.id,server_code: code,order_shopid: this.userInfo.id}).then(res=>{
			console.log(res)
			if(res.state == 1) {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.getOrderDetail();
				// 修改上一页页面信息
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				if(prevPage) {
					prevPage.$vm.getOrderNumber();
					prevPage.$vm.getOrderList();
				}
			}else if(res.state == 2) {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
			this.handelOrder = {};
		})
	},
	// 完成服务
	endOrder(e, order) {
		// app阻止事件冒泡
		// #ifdef APP-PLUS
		e.preventDefault();
		e.stopPropagation();
		// #endif
		request.post('/user/endserver',{id: order.id,order_shopid: this.userInfo.id}).then(res=>{
			if(res.state == 1) {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.getOrderDetail();
				// 修改上一页页面信息
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				if(prevPage) {
					prevPage.$vm.getOrderNumber();
					prevPage.$vm.getOrderList();
				}
			}
		})
	},
	// 显示权限说明弹窗
	showPermissionDialog(title, content) {
	  return new Promise((resolve) => {
		uni.showModal({
		  title,
		  content,
		  confirmText: '去开启',
		  success(res) {
			resolve(res.confirm);
		  }
		});
	  });
	},
	// 联系用户
	async contactService(phone) {
	  // TODO: 实现联系客服功能
	  // uni.navigateTo({
	  // 	url: '/pages/contact/contact?phone='+phone
	  // })
	  const systemInfo = uni.getSystemInfoSync()
	  if(systemInfo.platform === 'ios') {
	  	uni.makePhoneCall({
	  		phoneNumber: phone //仅为示例
	  	})
	  }else {
		let permission = '';
		if(systemInfo.platform === 'ios') {
			permission = 'phone'
		}else {
			permission = 'android.permission.CALL_PHONE'
		}
		this.isShowPer = true;
		const firstRequest = !plus.storage.getItem(`perm_${permission}`)
		if(firstRequest) {
			this.isShowPer = true;
		}
		// 1. 检查权限
		const { granted } = await permissionUtils.checkPermission('phone', '需要拨打电话权限，方便您联系商家或平台');
		if (granted) {
			this.isShowPer = false;
		  uni.makePhoneCall({
		  	phoneNumber: phone //仅为示例
		  })
		  return;
		}
		this.isShowPer = false;
		// 2. 显示权限说明弹窗
		const confirm = await this.showPermissionDialog(
		  '拨打电话权限申请',
		  '我们需要拨打电话权限，方便您联系商家或平台'
		);
		
		if (!confirm) return;
		
		// 3. 请求权限
		const result = await permissionUtils.requestPermission('phone', '需要拨打电话权限，方便您联系商家或平台');
		console.log('result',result)
		if (result) {
		  uni.makePhoneCall({
		  	phoneNumber: phone //仅为示例
		  })
		  return;
		} else {
		  locationService.openAppSettings();
		  return;
		  // uni.showToast({ title: '相机权限被拒绝', icon: 'none' });
		}
	  }
	  
	  
	},
	
	// 打开手机已有地图
	openMap() {
		let that = this;
		
		// #ifdef MP-WEIXIN
		uni.openLocation({
			success: function (res) {
				console.log('打开系统位置地图成功');
			},
			fail: function (error) {
				console.log(error);
			}
		});
		// #endif
		// #ifdef APP-PLUS
		_public.goMap({
			latitude: parseFloat(that.reservation_address_arr.latitude),
			longitude: parseFloat(that.reservation_address_arr.longitude),
			name: that.reservation_address_arr.house_number,
			address: that.reservation_address_arr.server_address
		})
		// #endif
	},
  }
}
</script>

<style>
page {
	position: relative;
	background-color: #fff;
}
.detail-page::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 1200rpx !important;
  background-image: url('/static/images/bg.png');
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
  z-index: 1;
}
.content-part {
	position: relative;
	left: 0;
	right: 0;
	z-index: 9;
}
/* 导航栏样式 */
.custom-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 88rpx;
  background-color: transparent;
  display: flex;
  align-items: center;
  padding: 0 32rpx;
  z-index: 100;
  /* #ifdef H5 */
  padding-top: 0;
  /* #endif */
  /* #ifdef APP-PLUS || MP-WEIXIN */
  padding-top: var(--status-bar-height);
  /* #endif */
}

.nav-back {
  width: 48rpx;
  height: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  width: 32rpx;
  height: 32rpx;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 36rpx;
  font-weight: 500;
  color: #FFFFFF;
}

/* 状态区域 */
.status-section {
  /* background: linear-gradient(135deg, #FF6B9D 0%, #FF4D6B 100%); */
  height: 160rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  /* padding-top: 88rpx; */
  /* #ifdef APP-PLUS || MP-WEIXIN */
  /* padding-top: calc(88rpx + var(--status-bar-height)); */
  /* #endif */
}
 
 


.status-text {
  font-size: 40rpx;
  color: #FFFFFF;
  font-weight: 500;
}

 /* 服务人员卡片 */
.service-card {
  margin: 24rpx;
  background-color: #FFFFFF;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 16rpx 0 rgba(210, 213, 224, 0.50);
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  align-items: center;
}

.location-info {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: center;
}

.location-icon {
  width: 60rpx;
  height: 60rpx;
  margin-right: 32rpx;
}

.address-info {
  flex: 1;
}

.name-phone {
  display: flex;
  align-items: center;
}

.name {
  font-size: 32rpx;
  color: #333333;
  font-weight: 500;
  margin-right: 16rpx;
}

.phone {
  font-size: 28rpx;
  color: #666666;
}

.address {
  font-size: 28rpx;
  color: #666666;
  margin-top: 8rpx;
}

.btn-map,.btn-phone {
  width: 64rpx;
  height: 64rpx;
  flex-shrink: 0;
}
.btn-map {
  margin: 0 28rpx;
}
/* 预约信息卡片 */
.booking-card {
  margin: 24rpx;
  background-color: #FFFFFF;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 16rpx 0 rgba(210, 213, 224, 0.50);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24rpx;
}

.card-title {
  font-size: 32rpx;
  color: #333333;
  font-weight: 500;
}

.update-time {
  font-size: 28rpx;
  color: #333333;
  padding-right: 4rpx;
}
.update-change {
  font-size: 28rpx;
  color: #666666;
}
.service-item {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-start;
  align-items: center;
  margin-bottom: 32rpx;
}

.service-image {
  width: 160rpx;
  height: 160rpx;
  border-radius: 16rpx;
  margin-right: 24rpx;
  background-color: #e2e2e2;
}

.service-info {
  flex: 1;
  height: 160rpx;
  display: flex;
  flex-flow: column nowrap;
  justify-content: space-between;
  align-items: flex-start;
  margin-right: 24rpx;
}

.service-name {
  font-size: 28rpx;
  color: #333333;
  margin-bottom: 8rpx;
}

.service-price {
  font-size: 32rpx;
  color: #FF4D6B;
  font-weight: 500;
}

.service-count {
  font-size: 28rpx;
  color: #666666;
}
.service-real {
  display: flex;
  flex-flow: row nowrap;
  justify-content: flex-end;
  align-items: baseline;
  padding-top: 24rpx;
  border-top: 1rpx solid rgba(0, 0, 0, 0.05);
}
.real-lable {
  font-size: 24rpx;
  color: #333333;
}
.real-price {
  font-size: 32rpx;
  font-weight: 500;
  color: #FF4767;
}
/* 订单信息卡片 */
.order-card {
  margin: 24rpx;
  background-color: #FFFFFF;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 16rpx 0 rgba(210, 213, 224, 0.50);
}

.info-item {
  display: flex;
  justify-content: space-between;
  margin-top: 16rpx;
}

.label {
  font-size: 28rpx;
  color: #999999;
}

.value {
  font-size: 28rpx;
  color: #333333;
}

/* 底部按钮 */
.bottom-buttons {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 128rpx;
  background-color: #FFFFFF;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  justify-content: flex-end;
  box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  padding: 0 24rpx 0 78rpx;
  /* #ifdef H5 */
  padding-bottom: env(safe-area-inset-bottom);
  /* #endif */
  z-index: 999;
}
.contact-button {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
}
.contact-text {
  font-size: 24rpx;
  color: #666666;
}
.tell-icon {
  width: 36rpx;
  height: 36rpx;
  margin-right: 8rpx;
}
.handel-button {
  width: 240rpx;
  height: 64rpx;
  display: flex;
  flex-flow: row nowrap;
  justify-content: center;
  align-items: center;
  border-radius: 32rpx;
  margin-left: 20rpx;
}
.btn-border {
  border: 1rpx solid #FF4767;
  background-color: #fff;
  box-sizing: border-box;
  margin-left: 20rpx;
}
.btn-bg {
  background-color: #FF4767;
}
.button-text {
  font-size: 28rpx;
  font-weight: 400;
  color: #FFFFFF;
}
.btn-border .button-text {
  font-size: 28rpx;
  font-weight: 400;
  color: #FF4767;
}
/* APP适配 */
/* #ifdef APP-PLUS */
.permission.transform {
	top: calc(var(--status-bar-height) + 88rpx + 20rpx);
	opacity: 1;
	visibility: visible;
}
/* #endif */
/* 平台适配 */
/* #ifdef H5 */
.detail-page {
  padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
}
/* #endif */

/* #ifdef MP-WEIXIN */
.custom-nav {
  padding-top: calc(var(--status-bar-height) + constant(safe-area-inset-top));
  padding-top: calc(var(--status-bar-height) + env(safe-area-inset-top));
}
/* #endif */
</style>
