<template>
	<view>
		<!--pages/orderDet/orderDet.wxml-->
		<!-- 售后 -->
		<view class="examine" v-if="orderRefundList.length > 0 && isOldOrder()" @tap="gorefundDetOld">
			<view class="cont dflex">
				<view
					v-if="
						(orderRefundList[0].refundState == 39 && orderRefund.refundType == 1) ||
							(orderRefundList[0].refundState == 40 && orderRefund.refundType == 1) ||
							orderRefundList[0].refundState == 30 ||
							orderRefundList[0].refundState == 31
					"
				>
					<text style="font-size:28rpx;color:#FE0419">您已提交退款申请，等待审核中</text>
					<view style="font-size:24rpx;color:#999999">{{ orderRefund.createDat ? orderRefund.createDat : '' }}</view>
				</view>
				<view
					v-if="
						(orderRefundList[0].refundState == 41 && orderRefund.refundType == 1) ||
							(orderRefundList[0].refundState == 35 && orderRefund.refundType == 2 && orderRefund.refundMethod != 2)
					"
				>
					<text style="font-size:28rpx;color:#FE0419" v-if="orderRefund.refundMethod == null && orderRefundList[0].refundState == 35">
						您提交的退款申请审核完成,请选择退货方式
					</text>
					<text style="font-size:28rpx;color:#FE0419" v-else>
						您提交的退款申请审核完成{{ orderRefundList[0].refundState == 41 && orderRefund.refundType == 1 ? '，等待卖家退款中' : '，请填写物流信息' }}
					</text>
					<view style="font-size:24rpx;color:#999999">
						{{ orderRefundList[0].refundState == 41 && orderRefund.refundType == 1 ? orderRefund.refundReviewTime : orderRefund.refundGoodReviewTime }}
					</view>
				</view>
				<view v-if="orderRefundList[0].refundState == 36 && orderRefund.refundType == 2 && orderRefund.refundMethod == 2">
					<text style="font-size:28rpx;color:#FE0419">您提交的退款申请审核完成，请您把商品送回门店</text>
					<view style="font-size:24rpx;color:#999999">
						{{ orderRefundList[0].refundState == 41 && orderRefund.refundType == 1 ? orderRefund.refundReviewTime : orderRefund.refundGoodReviewTime }}
					</view>
				</view>
				<block v-if="orderRefund.refundType == 1">
					<view v-if="orderRefundList[0].refundState == 32">
						<text style="font-size:28rpx;color:#FE0419">您提交的退款申请，审核失败，请联系商家</text>
						<view style="font-size:28rpx">失败原因：{{ !orderRefund.refundFalseRes ? '' : orderRefund.refundFalseRes }}</view>
						<view style="font-size:24rpx;color:#999999">{{ orderRefund.refundOverTime ? orderRefund.refundOverTime : '' }}</view>
					</view>
				</block>
				<block v-if="orderRefund.refundType == 2">
					<view v-if="orderRefundList[0].refundState == 32">
						<text style="font-size:28rpx;color:#FE0419">您提交的退货申请，审核失败，请联系商家</text>
						<view style="font-size:28rpx">失败原因：{{ !orderRefund.refundFalseRes ? '' : orderRefund.refundFalseRes }}</view>
						<view style="font-size:24rpx;color:#999999">{{ orderRefund.refundOverTime ? orderRefund.refundOverTime : '' }}</view>
					</view>
					<view v-if="orderRefundList[0].refundState == 36 && (orderRefund.refundMethod == 1 || orderRefund.refundMethod == 3)">
						<text style="font-size:28rpx;color:#FE0419">商品已寄回，等待卖家确认收货</text>
						<view style="font-size:24rpx;color:#999999">{{ orderRefund.refundMethod == 1 ? orderRefund.expressTime : orderRefund.refundGoodReviewTime }}</view>
					</view>
					<view v-if="orderRefundList[0].refundState == 40 || orderRefundList[0].refundState == 39 || orderRefundList[0].refundState == 41">
						<text style="font-size:28rpx;color:#FE0419">您寄回的商品卖家已接收，等待卖家退款中</text>
						<view style="font-size:24rpx;color:#999999">{{ orderRefund.receiveTime ? orderRefund.receiveTime : '' }}</view>
					</view>
				</block>
				<view v-if="orderRefundList[0].refundState >= 42">
					<block v-if="orderRefundList[0].refundState == 43">
						<text style="font-size:28rpx;color:#FE0419">退款完成</text>
						<view style="font-size:24rpx;color:#999999">{{ orderRefund.refundOverTime ? orderRefund.refundOverTime : '' }}</view>
					</block>
					<block v-else>
						<text style="font-size:28rpx;color:#FE0419">退款失败</text>
						<view style="font-size:24rpx;color:#999999">{{ refundFalseRes ? refundFalseRes : '' }}</view>
					</block>
				</view>
				<image src="/mine/static/mine/images/tuikuan.png"></image>
			</view>
		</view>
		<!-- 处方 -->
		<!-- <view class="status mdaddr1 bgfff r10" v-if="((state>=20 && state<30)||state==1)&&isOtc==1">
	<view :class="'li ' + (state==1  ? 'ing' : '')" >
		<text></text>待付款
	</view>
	<view :class="'li ' + (state>20  ? 'ing' : '')">
		<text></text>开处方
	</view>
	<view :class="'li ' + (state ==22  ? 'ing' : '')" v-if="state==22">
		<text></text>开方失败
	</view>
	<view :class="'li ' + (state >=25  ? 'ing' : '')">
		<text></text>处方审核
	</view>
	<view :class="'li ' + (state ==27 ? 'ing' : '')" v-if="state==27">
		<text></text>审方失败
	</view>
</view> -->
		<!-- 退货-->
		<!-- <view class="status" wx:if="{{state == 9 || state == 10 || state>=40}}">
		<view class="li {{state ==40  ? 'ing' : ''}}">
			<text></text>退款审核
		</view>
		<view class="li {{state ==9 ? 'ing' : ''}}">
			<text></text>退还商品
		</view>		
		<view class="li {{state ==41 ? 'ing' : ''}}">
			<text></text>待退款
		</view>
		<view class="li {{state ==42 ? 'ing' : ''}}" wx:if="{{state==42}}">
			<text></text>退款失败
		</view>
		<view class="li {{state ==43 ? 'ing' : ''}}" wx:if="{{state==43}}">
			<text></text>退款完成
		</view>
	</view> -->
		<view class="top">
			<view class="status" v-if="(state < 8 && state >= 1) || (state >= 20 && state < 30)">
				<view :class="'li ' + (state >= 1 ? 'ing' : '')">
					<text></text>
					待付款
				</view>
				<view :class="'li ' + (state >= 20 || state > 1 ? 'ing' : '')" v-if="isOtc == 1">
					<text></text>
					开处方
				</view>
				<view :class="'li ' + (state == 22 ? 'ing' : '')" v-if="state == 22">
					<text></text>
					开方失败
				</view>
				<view :class="'li ' + (state >= 25 || (state > 1 && state != 22) ? 'ing' : '')" v-if="isOtc == 1">
					<text></text>
					{{ state != 22 ? '处方审核' : '重新开方' }}
				</view>
				<view :class="'li ' + (state == 27 ? 'ing' : '')" v-if="state == 27">
					<text></text>
					审方失败
				</view>

				<view :class="'li ' + (state > 1 && state < 20 ? 'ing' : '')">
					<text></text>
					{{ mode != 1 ? stateName : '待自提' }}
				</view>
				<view :class="'li ' + (state > 3 && state < 20 ? 'ing' : '')" v-if="mode != 1">
					<text></text>
					待收货
				</view>
				<view :class="'li ' + ((mode != 1 && state == 7) || (mode == 1 && state == 6) ? 'ing' : '')">
					<text></text>
					{{ mode != 1 ? '已完成' : '已自提' }}
				</view>
			</view>
		</view>
		<!-- 			<view :class="'status mdaddr1 bgfff ' + (true?'r10':'r11')" v-if="state>=39||state>=30">
				<view :class="'li ' + (state>=39||state>=30?'ing':'')">
					<text></text>待审核
				</view>
				<view :class="'li ' + (state>=32?'ing':'')" v-if="state==32">
					<text></text>审核失败
				</view>
				<block v-else>
					<block v-if="orderRefund.refundType==2&&(orderRefund.refundMethod==1)">
						<view :class="'li ' + (state>=35?'ing':'')">
							<text></text>待寄回
						</view>
						<view :class="'li ' + (state>=36?'ing':'')">
							<text></text>退货中
						</view>
					</block>
					<block
						v-if="orderRefund.refundType==2&&(orderRefund.refundMethod==2|| orderRefund.refundMethod==3)">
						<view :class="'li ' + (state>=36?'ing':'')">
							<text></text>退货中
						</view>
					</block>
					<view
						:class="'li ' + ((state>=41&&orderRefund.refundType==1)||(state>=39&&orderRefund.refundType==2)?'ing':'')">
						<text></text>退款中
					</view>
					<view :class="'li ' + (state>=42?'ing':'')">
						<text></text>已退款
					</view>
				</block>
			</view> -->
		<!-- 门店自提 -->
		<block v-if="mode == 1">
			<view class="tihuo bgfff r10 dflex" v-if="state >= 3 && state <= 7">
				<view class="tihuoma dflex">
					<image src="/static/pages/images/thico.png"></image>
					提货号码：
					<text>{{ code }}</text>
				</view>
				<view class="thmbtn" @tap="showCode" v-if="codeUrl">
					<image src="/static/pages/images/ewmico.png"></image>
					查看提货码
				</view>
			</view>
			<view class="mdaddr bgfff r10">
				<!-- 		<view class="status mdaddr1 bgfff " v-if="state>=39||state>=30">
			<view :class="'li ' + (state>=39||state>=30?'ing':'')">
				<text></text>待审核
			</view>
			<view :class="'li ' + (state>=32?'ing':'')" v-if="state==32">
				<text></text>审核失败
			</view>
			<block v-else>
				<block v-if="orderRefund.refundType==2&&(orderRefund.refundMethod==1)">
					<view :class="'li ' + (state>=35?'ing':'')">
						<text></text>待寄回
					</view>
					<view :class="'li ' + (state>=36?'ing':'')">
						<text></text>退货中
					</view>
				</block>
				<block v-if="orderRefund.refundType==2&&(orderRefund.refundMethod==2||orderRefund.refundMethod==3)">
					<view :class="'li ' + (state>=36?'ing':'')">
						<text></text>退货中
					</view>
				</block>
				<view :class="'li ' + ((state>=41&&orderRefund.refundType==1)||(state>=39&&orderRefund.refundType==2)?'ing':'')">
					<text></text>退款中
				</view>
				<view :class="'li ' + (state>=42?'ing':'')">
					<text></text>已退款
				</view>
			</block>
		</view> -->
				<view class="mdinfo">
					<view class="areas dflex">
						自提点：{{ shopName ? shopName : merchantName }}
						<image src="/static/pages/images/dwico.png" class="dwico"></image>
					</view>
					<text>地址：{{ shopAddress ? shopAddress : address }}</text>
					<text>营业时间： 8:30~22:30 节假日不休息</text>
				</view>
				<view class="mdphone" @tap="callPhone">
					门店电话：
					<text>{{ merchantPhone ? merchantPhone : '无' }}</text>
					<image src="/static/pages/images/call.png"></image>
				</view>
				<image src="/static/pages/images/dzbg.png" class="dzbg"></image>
			</view>
		</block>
		<!-- 门店自提 -->
		<!-- 物流配送 -->
		<view class="top" v-else>
			<view class="addr">
				<text class="areas">{{ address }}</text>
				<view class="addrb ddflex">
					<view class="fflex">
						收货人： {{ name }}
						<text>{{ phone }}</text>
					</view>
				</view>
				<!-- <view class="qdkf ddflex" v-if="state <= 3">如需修改地址，请联系<button open-type="contact" hover-class="none">售后客服</button></view>
				<view class="qdkf ddflex" v-if="state > 3 && state < 20 && state != 7">订单已发货，不支持修改地址</view>
			 -->	
			 <image src="/static/pages/images/dzbg.png" class="dzbg"></image>
			</view>
			<!-- &&state!=7订单未完成 -->
			<view class="psfs dflex" v-if="deliverState" @tap="goUrl" :data-url="url">
				<image src="/static/pages/images/psico.png" class="psico"></image>
				<view class="flex">
					<view v-if="deliverExtend == 1">{{ deliverMode }}：{{ deliverState }}</view>
					<view v-else>查看快递</view>
					<text v-if="deliverExtend == 1">{{ deliverTime }}</text>
				</view>
				<image src="/static/pages/images/more.png" class="rico1"></image>
			</view>
			<!-- 配送人员&&state!=7 -->
			<view class="psy dflex" v-if="deliverMode == '自主配送'">
				<text>配送员信息</text>
				<view class="psy-info flex dflex">
					<view class="psy-phone dflex" @tap="callDeliver">
						<image src="/static/pages/images/psyico.png"></image>
						电话骑士
					</view>
					<view class="psy-name">{{ deliveryUserName }}</view>
				</view>
			</view>
		</view>
		<!-- 产品信息 -->

		<view class="list" v-if="state == 8 || state == 1">
			<view class="dpname">
				<image class="l-s-img" src="/static/pages/images/dpico.png"></image>
				{{ shopName ? shopName : merchantName }}
			</view>
			<view v-for="(item, index) in products" :key="index" class="li" @tap="toDetail" :data-id="item.id">
				<view class="li-info dflex">
					<view class="cimgs">
						<image :src="item.pic" mode="aspectFit" class="cimg"></image>
						<image src="/static/pages/images/jishida.png" mode="aspectFit" class="type" v-if="orderType == 5"></image>
						<image src="/static/pages/images/tuangou.png" mode="aspectFit" class="type" v-if="orderType == 2"></image>
					</view>
					<view class="zhinfo">
						<view class="zhtit">{{ item.title }}</view>
						<view v-if="item.propertiesList" class="guigecontent">
							规格：
							<text v-for="(itemGuige, index2) in item.propertiesList" :key="index2" class="guige" decode="true">{{ itemGuige.value }}&nbsp;</text>
						</view>
						<view class="jiage">
							<view class="zhjia" v-if="orderType != 11">￥{{ item.salePrice }}</view>
							<view class="zhjia" v-if="orderType == 11">{{ item.integral }}积分+￥{{ item.salePrice }}</view>
							<view class="num">×{{ item.quantity }}</view>
						</view>
					</view>
				</view>
				<!-- <view class="lis wuliu dflex">
					<image src="../../static/pages/images/psico_y.png" class="lis-img"></image>
					<view class="flex y-txt">查看物流</view>
					<image src="../static/mine/images/rico2.png" class="rico"></image>
				</view>
				<view class="lis zhuang dflex">
					<image src="../static/mine/images/tuikuan.png" class="lis-img"></image>
					<view class="flex r-txt">售后信息</view>
					<image src="../static/mine/images/rico2.png" class="rico"></image>
				</view> -->
			</view>
		</view>
		<view v-else class="list" v-for="(items, index) in deliverListss" :key="index">
			<view class="dpname" @click="deliverLog(items)">
				<image class="l-s-img" src="/static/pages/images/dpico.png"></image>
				<view class="flex">
					<!-- {{ items.supplierName ? items.supplierName : (shopName ? shopName : merchantName) + '自营' }} -->
					{{ shopName ? shopName : merchantName }}
				</view>
				<view :class="'dflex ' + (items.state == 2 || items.state == 3 ? 'dd-stared' : 'dd-sta ')" v-if="mode != 1">
					{{ items.state == 1 ? '未发货' : items.state == 2 ? '待收货' : items.state == 3 ? '已签收' : items.state == 4 ? '已完成' : '' }}
					<image src="/static/pages/images/more.png" class="rico" v-if="items.state == 2 || items.state == 3" />
				</view>
			</view>
			<view v-for="(item, index1) in items.extendDTOList" :key="index1" class="li">
				<view class="li-info dflex" @tap="toDetail" :data-id="item.productId">
					<view class="cimgs">
						<image :src="item.pic" mode="aspectFit" class="cimg"></image>
						<image src="/static/pages/images/jishida.png" mode="aspectFit" class="type" v-if="orderType == 5"></image>
						<image src="/static/pages/images/tuangou.png" mode="aspectFit" class="type" v-if="orderType == 2"></image>
					</view>
					<view class="zhinfo">
						<view class="zhtit">{{ item.productName }}</view>
						<view v-if="item.specsList" class="guigecontent">
							规格：
							<text v-for="(itemGuige, index2) in item.specsList" :key="index2" class="guige" decode="true">{{ itemGuige.value }}&nbsp;</text>
						</view>
						<view class="jiage">
							<view class="zhjia" v-if="orderType != 11">￥{{ item.money }}</view>
							<view class="zhjia" v-if="orderType == 11">{{ item.integral }}积分+￥{{ item.money }}</view>
							<view class="num">×{{ item.deliverQuantity }}</view>
						</view>
					</view>
				</view>
				<view
					class="lis zhuang dflex"
					v-if="item.refundOrderList && item.refundOrderList.length > 0"
					v-for="(its, idx) in item.refundOrderList"
					@click="gorefundDet(its)"
					:data-its="its"
					:key="idx"
				>
					<image src="../static/mine/images/tuikuan.png" class="lis-img"></image>
					<view class="flex r-txt">{{ refundHintText(its) }}</view>
					<image src="../static/mine/images/rico2.png" class="rico"></image>
				</view>
			</view>
		</view>
		<view class="ddinfo">
			<view class="li">
				<text>订单编号</text>
				<view class="ddid dflex">
					{{ id }}
					<text class="copy" @tap="copy()">复制</text>
				</view>
			</view>
			<view class="li">
				<text>创建时间</text>
				<view>{{ createDate }}</view>
			</view>
			<view class="li" v-if="deliverTime">
				<text>{{ mode == 1 ? '自提' : '发货' }}时间</text>
				<view>{{ deliverTime }}</view>
			</view>
			<view class="li">
				<text>订单金额</text>
				<view class="price">￥{{ money }}</view>
			</view>
			<view class="li" v-if="expressFee">
				<text>快递费</text>
				<view class="price">￥{{ expressFee }}</view>
			</view>
			<view class="li" v-if="discountMoney && orderType != 11">
				<text>优惠券</text>
				<view class="price">-￥{{ discountMoney }}</view>
			</view>
			<view class="li" v-if="discountMoney && orderType == 11">
				<text>使用积分</text>
				<view class="price">{{ discountMoney }}</view>
			</view>
			<view class="li" v-if="balancePayMoney">
				<text>余额扣除</text>
				<view class="price">-￥{{ balancePayMoney }}</view>
			</view>
			<view class="li" v-if="offlinePaymentMoney">
				<text>线下余额扣除</text>
				<view class="price">-￥{{ offlinePaymentMoney }}</view>
			</view>
			<view class="li">
				<text>{{state==1||state==8?'应付':'实付'}}金额</text>
				<view class="price">￥{{ payMoney }}</view>
			</view>
		</view>
		<view class="ddinfo" v-if="note">
			<view class="li">
				<text>备注：</text>
				<view>{{ note }}</view>
			</view>
		</view>

		<view class="ddinfo" v-if="state == 22 && orderDrugUser.reasons">
			<view class="dli">
				<text>开方失败原因：</text>
				<view>{{ orderDrugUser.reasons }}</view>
			</view>
		</view>

		<view class="placeholder-view" :style="'height:' + (bottomBlankHeight + 100) + 'rpx'"></view>
		<view class="bot" :style="'padding-bottom:' + (bottomBlankHeight + 20) + 'rpx'">
			<view class="bot-view" @tap="deleteOrder" v-if="state == 8">删除订单</view>
			<view class="bot-view" @tap="cancelOrder" v-if="state == 1">取消订单</view>
			<block v-if="config.is_open_invoice == 1 && (state == 2 || state == 3 || state == 4 || state == 5 || state == 6 || state == 7)">
				<view class="bot-view" @tap="applyInvoice" v-if="invoiceState == 0">申请开票</view>
				<view class="bot-view" @tap="toInvoice" v-else>查看发票</view>
			</block>
			<view class="bot-view" @tap="txOrder" v-if="state == 3 && isReminder == 1">提醒发货</view>

			<block v-if="orderType != 4">
				<!-- <view
					class="sta-btn"
					@tap="refundmod"
					:data-id="id"
					:data-index="index"
					v-if="
						isRefundStock == 1||(refund > 0 && state >= 2 && state <= 6 && orderType !== 4 && isrefund == 1 && payTime != null && reimburse && canRefund) ||
							(state >= 20 && state < 30 && isOtc == '1' && isrefund == 1 && payTime != null && reimburse && canRefund)
					"
				>
					申请售后
				</view> -->
				<block v-if="state != 1 && state != 8 && state != 9 && state != 10">
					<block v-if="state >= 30 && state <= 43"></block>
					<view v-else class="sta-btn bot-view" @tap="isRefundStock == 1 ? refundmod() : refundmods()">申请售后</view>
				</block>
			</block>
			<view @tap="payOrder" v-if="state == 1" class="confirm bot-view">立即付款</view>
			<view class="confirm bot-view" @tap="confirmOrder" :data-index="index" v-if="state == 5 || item.state == 6" :data-id="id">确认收货</view>
			<navigator
				:url="'/activity/group/ptDetail/ptDetail?id=' + groupId + '&merchantId=' + merchantId"
				v-if="orderType == 4 && state != 8 && groupId && state > 1"
				class="confirm bot-view"
			>
				我的拼团
			</navigator>
			<navigator
				v-if="state > 5 && (state != 20 && state != 22 && state != 27)"
				open-type="reLaunch"
				hover-class="none"
				:url="header.appId == 'ZQ1VK5oc17I387E' ? '/pages/timelyDelivery/index' : '/pages/index/index'"
				class="confirm bot-view"
			>
				继续购物
			</navigator>
			<navigator v-if="state == 22 || state == 20 || state == 27" hover-class="none" :url="'/prescription/Consultation/index?orderId=' + id" class="confirm bot-view">
				重新开方
			</navigator>
			<contact-button button-text="联系客服" class-name="confirm bot-view" v-if="state == 3 || state == 25 || state == 26"></contact-button>

			<!-- <navigator url="/pages/shop/shop"wx:if="{{state > 5 }}" open-type="reLaunch" hover-class="none" class="confirm">继续购物</navigator> -->
		</view>

		<!-- 自提取货码 -->
		<block v-if="isShow">
			<view class="mask" @tap="eliminate"></view>
			<view class="qhpop">
				<view class="close"><image src="/static/pages/images/close.png" @tap="eliminate"></image></view>
				<view class="thma">
					我的提货码
					<text>{{ code }}</text>
				</view>
				<view class="qrcode">
					<!-- <canvas class='canvas' canvas-id='canvas' bindlongtap='save' style="width:150px;height:150px"></canvas> -->
					<image :src="codeUrl"></image>
				</view>
				<view class="save" @tap="saveImage">保存到相册</view>
			</view>
		</block>
		<canvas canvas-id="invite" class="canvas" style="width:295px;height:428px;"></canvas>
		<!-- 申请售后弹窗 -->
		<view class="ceng" v-if="popShow" @click="cancel()"></view>
		<view class="popup" v-if="popShow">
			<view class="pop-tit">提示</view>
			<view class="pop-tip">当前{{ stateName }}，如有售后问题，请咨询客服</view>
			<view class="pop-btn dflex">
				<view class="pop-cancel fflex" @click="cancel()">取消</view>
				<view class="fflex"><contact-button class-name="pop-contact fflex" button-text="咨询客服"></contact-button></view>
			</view>
		</view>
	</view>
</template>

<script>
// pages/orderDet/orderDet.js
const app = getApp(); // const QRCode = require('../../utils/weapp.qrcode.js')
// const QRCode = require('../../utils/weapp.qrcode.js')
const req = require('../../utils/request.js');
const requsetmessage = require('../../utils/requestmessage.js');
// state : 1 未付款 2 待确认 3 待发货 4 待提货 5 配送中 6 已签收 7 订单完成
//         8 订单取消 9 退货中 10 完成退货 11 支付失败 15 进行中
// 		售后状态
// 	    30 售后待审核 32 售后审核失败 35 商品待退回 36 商品退回中
// 		39 退款申请 40 退款待支付 41 退款中 42 退款失败
// 		43 退款成功
// 		处方订单
// 		20 未开方 21 开方中 22 开方失败 25 待审方
// 		26 审方中 27 审方失败,
// 		49 用户取消售后
export default {
	data() {
		return {
			bottomBlankHeight: app.globalData.isIPhoneX ? 68 : 0,
			deliverListss: '',
			isShow: false,
			isrefund: '',
			id: '',
			config: '',
			show: false,
			codeUrl: '',
			deliverExtend: 0,
			url: '',
			imgUrl: '',
			pageList: '',
			orderRefund: '',
			header: req.header,
			orderRefundList: [],
			reimburse: false,
			canRefund: true,
			state: '',
			popShow: false
		};
	},

	components: {},
	props: {},
	onLoad: async function(options) {
		this.query = options;
		let id = options.id;
		this.setData({
			isrefund: options.isrefund
		}); // req.getRequest('/api/order/detail', {
		//   id: id
		// }, data => {
		//   that.setData(data);
		//   if (data.state >= 30 && data.state <= 43) {
		//     this.getrefundDet(id)
		//   }
		// })

		this.setData({
			id: id
		});
		// console.log(options.code);
		await this.getConfig();
	},

	async onShow() {
		let that = this;
		await req.getRequest(
			'/api/order/detail',
			{
				id: this.id
			},
			data => {
				this.deliverListss = data.deliverList;
				// console.log('获取到的退款单11111>>>>>',data.orderRefundList);
				// console.log('获取到的退款单>>>>>',this.deliverListss);
				data.refund = data.products.filter(it => it.quantity - (it.refundStock ? it.refundStock : 0) > 0).length;
				// console.log(data)
				that.setData(data);
				that.state = data.state;
				if (!data.orderRefundList) {
					this.orderRefundList = [];
					this.reimburse = true;
				} else if (data.orderRefundList.length < 1) {
					this.orderRefundList = [];
					this.reimburse = true;
				} else {
					// this.getrefundDet(data.orderRefundList[0].id);
					this.orderRefund = data.orderRefundList[0];
					let orderRefundList = data.orderRefundList;
					this.orderRefundList = orderRefundList;
					if (orderRefundList[0].refundState == 43 || orderRefundList[0].refundState == 32) {
						this.reimburse = true;
					} else {
						this.reimburse = false;
					}
				}
				// if (data.state >= 30 && data.state <= 43) {
				// 	this.getrefundDet(this.id);
				// }

				if (data.mode == 1 && data.state >= 3 && data.state <= 7) {
					this.getCodeUrl();
					this.generatePoster();
				}
				if (data.mode != 1 && data.state >= 5 && data.state <= 7) {
					this.deliverExtendFun();
				}
			}
		);
	},

	methods: {
		goUrl(e) {
			// var dataset = e.currentTarget.dataset;
			let url = e.currentTarget.dataset.url;
			if (this.deliverState == '完成') {
				url = url + '&isReceived=true';
			}
			uni.navigateTo({
				url: url
			});
		},

		eliminate() {
			this.setData({
				isShow: false
			});
		},

		refundHintText(orderRefundItem) {
			var text = '';
			if (
				(orderRefundItem.refundState == 39 && (orderRefundItem.refundType == 1||orderRefundItem.refundType==3)) ||
				(orderRefundItem.refundState == 40 && (orderRefundItem.refundType == 1||orderRefundItem.refundType==3)) ||
				orderRefundItem.refundState == 30 ||
				orderRefundItem.refundState == 31
			) {
				text = '您已提交退款申请，等待审核中';
			}
			if (
				((orderRefundItem.refundState == 41 || orderRefundItem.refundState == 40) && (orderRefundItem.refundType == 1||orderRefundItem.refundType==3)) ||
				(orderRefundItem.refundState == 35 && orderRefundItem.refundType == 2 && orderRefundItem.refundMethod != 2)
			) {
				if (orderRefundItem.refundMethod == null && orderRefundItem.refundState == 35) {
					text = '您提交的退款申请审核完成,请选择退货方式';
				} else {
					text =
						'您提交的退款申请审核完成' +
						((orderRefundItem.refundState == 41 || orderRefundItem.refundState == 40) && (orderRefundItem.refundType == 1||orderRefundItem.refundType==3) ? '，等待卖家退款中' : '，请填写物流信息');
				}
			}
			if (orderRefundItem.refundState == 36 && orderRefundItem.refundType == 2 && orderRefundItem.refundMethod == 2) {
				text = '您提交的退款申请审核完成，请您把商品送回门店';
			} else if (orderRefundItem.refundType == 1||orderRefundItem.refundType==3) {
				if (orderRefundItem.refundState == 32) {
					text = '您提交的退款申请，审核失败，请联系商家' + (!orderRefundItem.refundFalseRes ? '' : '；原因：') + orderRefundItem.refundFalseRes;
				}
			} else if (orderRefundItem.refundType == 2) {
				if (orderRefundItem.refundState == 32) {
					text = '您提交的退货申请，审核失败，请联系商家' + (!orderRefundItem.refundFalseRes ? '' : '；失败原因：') + orderRefundItem.refundFalseRes;
				} else if (orderRefundItem.refundState == 36 && (orderRefundItem.refundMethod == 1 || orderRefundItem.refundMethod == 3)) {
					text = '商品已寄回，等待卖家确认收货';
				} else if (orderRefundItem.refundState == 40 || orderRefundItem.refundState == 39 || orderRefundItem.refundState == 41) {
					text = '您寄回的商品卖家已接收，等待卖家退款中';
				}
			}
			if (orderRefundItem.refundState >= 42) {
				if (orderRefundItem.refundState == 43) {
					text = '退款完成';
				} else {
					if (orderRefundItem.refundState == 49) {
						text = '已取消售后';
					} else {
						text = '退款失败';
					}
				}
			}
			return text;
		},

		getConfig() {
			var _this = this;

			return new Promise((resolve, reject) => {
				req.g(
					'/api/config',
					res => {
						// console.log('配置数据====' + JSON.stringify(res));

						_this.setData({
							config: res
						});

						resolve();
					},
					true
				);
			});
		},

		getCodeUrl() {
			let that = this; //获取小程序码

			const params = {
				// page: 'pages/index/index',
				page: 'pages/web/web',
				scene: this.query.code
			}; // return new Promise((resolve, reject) => {

			req.getRequest('/api/program/codev', params, url => {
				// console.log(url);
				that.setData({
					show: true,
					codeUrl: url
				});
				that.generatePoster(url); //   resolve();
				// })
			});
		},

		// 获取分单发货的
		deliverExtendFun() {
			req.postRequest(
				'/api/order/deliverExtend',
				{
					id: this.query.id
				},
				res => {
					let url;

					if (res && res.length >= 2) {
						url = '/mine/deliverExtend/deliverExtend?orderId=' + this.id;
						this.setData({
							deliverExtend: 2,
							url: url
						});
					} else {
						url = '/mine/logistics/logistics?id=' + this.deliverId + '&orderId=' + this.id;
						this.setData({
							deliverExtend: 1,
							url: url
						});
					}
				}
			);
		},

		//检验是否为老的订单退款数据
		isOldOrder() {
			if (this.orderRefundList.length > 0) {
				var rRefundList = [];
				if (this.deliverList && this.deliverList.length > 0) {
					for (var i = 0; i < this.deliverList.length; i++) {
						var extendDTOList = this.deliverList[i].extendDTOList;
						for (var k = 0; k < extendDTOList.length; k++) {
							rRefundList = rRefundList.concat(extendDTOList[k].refundOrderList);
						}
					}
				}
				if (this.orderRefundList[0].refundState == 49) {
					//售后取消
					return false;
				} else {
					return rRefundList.length <= 0;
				}
			}
			return false;
		},

		deliverLog(items) {
			if (items.mode != 1 && (items.state == 2 || items.state == 3)) {
				uni.navigateTo({
					url: '/mine/logistics/logistics?id=' + items.id + '&orderId=' + items.orderId
				});
			}
		},

		generatePoster(url) {
			let that = this;
			that.generate(url, imgUrl => {
				that.setData({
					imgUrl: imgUrl
				});
			});
		},

		isAuth(fun) {
			if (!uni.saveImageToPhotosAlbum) {
				uni.showModal({
					title: '提示',
					content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
				});
				return;
			} //可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope

			// #ifndef H5
			uni.getSetting({
				success(res) {
					// console.log(res);

					if (!res.authSetting['scope.writePhotosAlbum']) {
						// 接口调用询问
						uni.authorize({
							scope: 'scope.writePhotosAlbum',

							success() {
								// console.log("2-授权《保存图片》权限成功");
								fun(true);
							},

							fail() {
								fun(false);
							}
						});
					} else {
						// 拒绝授权
						fun(true);
					}
				},

				fail(res) {
					fun(false);
				}
			});
			// #endif
		},

		saveImage() {
			let that = this;
			that.isAuth(success => {
				if (success) {
					// console.log("图片地址=====" + that.imgUrl);
					uni.saveImageToPhotosAlbum({
						filePath: that.imgUrl,

						success(res) {
							uni.showToast({
								title: '图片保存成功',
								icon: 'none',

								success() {},

								fail() {
									uni.showToast({
										title: '图片保存失败'
									});
								}
							});
						}
					});
				}
			});
		},

		getImageInfo(url) {
			return new Promise((resolve, reject) => {
				if (!url) {
					resolve();
					return false;
				}

				uni.getImageInfo({
					src: url,
					success: resolve,
					fail: reject
				});
			});
		},

		generate(url, success) {
			// 获取商品图片
			// const picPromise = this.getImageInfo(this.data.config.Distribution_Poster_Background)
			// const avatarPromise = this.getImageInfo(this.data.config.CONFIG_PROJECT_LOGO)
			// 获取小程序码图
			const codePromise = this.getImageInfo(url);
			let that = this;
			Promise.all([codePromise]).then(([code]) => {
				// 创建绘图上下文
				const ctx = uni.createCanvasContext('invite', this); // 绘制背景，填充满整个canvas画布

				let width = 295,
					height = 428; //默认背景

				ctx.setFillStyle('#f6f6f6');
				ctx.fillRect(0, 0, width, height); //白色背景

				ctx.save();
				that.roundRect(ctx, 0, 0, 428, 500, 10);
				ctx.restore();
				ctx.fillStyle = '#333';
				ctx.font = '20px PingFang SC';
				ctx.fillText('我的提货码', 100, 65);

				if (code) {
					ctx.drawImage(code.path, 62, 140, 165, 165);
				}

				ctx.save();
				ctx.fillStyle = '#FE0419';
				ctx.font = '18px PingFang SC';
				ctx.fillText(that.query.code, 130, 100); //logo
				// ctx.save();
				// ctx.beginPath()
				// ctx.arc(35, 35, 20, 0, 2 * Math.PI, true) //画一个圆形裁剪区域
				// ctx.clip() //裁剪
				// ctx.drawImage(avatar.path, 15, 15, 40, 40) //绘制图片
				// 完成作画

				ctx.draw(false, function() {
					uni.canvasToTempFilePath(
						{
							canvasId: 'invite',
							success: function(res) {
								success.call(this, res.tempFilePath);
							},
							fail: function(res) {
								// console.log(res);
							}
						},
						that
					);
				});
			});
		},

		/**
		 *
		 * @param {CanvasContext} ctx canvas上下文
		 * @param {number} x 圆角矩形选区的左上角 x坐标
		 * @param {number} y 圆角矩形选区的左上角 y坐标
		 * @param {number} w 圆角矩形选区的宽度
		 * @param {number} h 圆角矩形选区的高度
		 * @param {number} r 圆角的半径
		 */
		roundRect: function(ctx, x, y, w, h, r) {
			// 开始绘制
			ctx.beginPath(); // 因为边缘描边存在锯齿，最好指定使用 transparent 填充
			// 这里是使用 fill 还是 stroke都可以，二选一即可

			ctx.setFillStyle('white'); // ctx.setStrokeStyle('transparent')
			// 左上角

			ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5); // border-top

			ctx.moveTo(x + r, y);
			ctx.lineTo(x + w - r, y);
			ctx.lineTo(x + w, y + r); // 右上角

			ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2); // border-right

			ctx.lineTo(x + w, y + h - r);
			ctx.lineTo(x + w - r, y + h); // 右下角

			ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5); // border-bottom

			ctx.lineTo(x + r, y + h);
			ctx.lineTo(x, y + h - r); // 左下角

			ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI); // border-left

			ctx.lineTo(x, y + r);
			ctx.lineTo(x + r, y); // 这里是使用 fill 还是 stroke都可以，二选一即可，但是需要与上面对应

			ctx.fill(); // ctx.stroke()

			ctx.closePath(); // 剪切

			ctx.clip();
		},

		showCode() {
			this.setData({
				isShow: true
			});
		},

		callPhone: function() {
			uni.makePhoneCall({
				phoneNumber: this.merchantPhone
			});
		},

		payOrder: async function() {
			let that = this;
			if (!this.id) return false;
			let mode = that.mode;
			if (that.mode == 2 && that.orderType == 5) {
				mode = 3;
			}
			if (that.isOtc == 1) {
				await requsetmessage.recipel(mode).then(res => {});
			} else if (mode == 1) {
				await requsetmessage.selfRaising().then(res => {
					// console.log("自提", res);
				});
			} else if (mode == 2) {
				await requsetmessage.distribution().then(res => {
					// console.log("配送", res);
				});
			} else {
				await requsetmessage.jisda().then(res => {
					// console.log("及时达", res);
				});
			}
			req.payOrder(this.id, success => {
				if (success) {
					//支付成功
					if (this.isOtc == 1) {
						app.globalData.redirectTab('product/pay/pay?isSuccess=true&orderId=' + this.id + '&isRx=1');
					} else {
						app.globalData.redirectTab('product/pay/pay?isSuccess=true');
					}
					// let d = {};
					// d.state = 3;
					// that.setData(d);
				} else {
					req.msg('支付失败');
				}
			});
		},
		txOrder() {
			req.postRequest(
				'/api/order/reminderOrder',
				{
					orderId: this.id
				},
				() => {
					req.msg('发货提醒成功');
				}
			);
		},

		cancelOrder() {
			let that = this;
			req.msgConfirm('确定取消该订单？', () => {
				req.postRequest(
					'/api/order/cancel',
					{
						id: this.id
					},
					() => {
						let d = {};
						d.state = 8;
						that.setData(d);
					}
				);
			});
		},

		confirmOrder(event) {
			//确认收货
			// let index = event.currentTarget.dataset.index;
			// let page = this.data.pageList[index];
			let id = event.currentTarget.dataset.id;
			let that = this;
			req.msgConfirm('确认执行该操作？', () => {
				req.postRequest(
					'/api/order/receiving',
					{
						id: id
					},
					() => {
						let d = {};
						d['stateName'] = '订单完成';
						d['state'] = 7;
						that.setData(d);
						uni.navigateBack();
					}
				);
			});
		},

		deleteOrder() {
			let that = this;
			req.msgConfirm('确定删除该订单？', () => {
				req.postRequest(
					'/api/order/delete',
					{
						id: this.id
					},
					() => {
						let pages = getCurrentPages();
						var prevPage = pages[pages.length - 2];
						let pageList = prevPage.data.pageList;
						pageList.splice(pageList.findIndex(it => it.id == that.id), 1);
						prevPage.$vm.setData({
							pageList: pageList
						});
						uni.navigateBack();
					}
				);
			});
		},

		toDetail(e) {
			let id = e.currentTarget.dataset.id;
			app.globalData.openPage('product/detail/detail?id=' + id);
		},

		refundmod(event) {
			//申请退款
			uni.navigateTo({
				url: '/mine/refund/refundType/refundType?id=' + this.id
			});
		},

		refundmods() {
			this.popShow = true;
		},
		cancel() {
			this.popShow = false;
		},

		// 退款详情
		gorefundDetOld() {
			uni.navigateTo({
				url: '/mine/refund/refundDetails/refundDetails?id=' + this.orderRefund.id + '&isrefund=' + this.orderRefund.refundType
			});
		},

		gorefundDet(its) {
			// console.log('进入退款详情>>>>>', its);
			uni.navigateTo({
				url: '/mine/refund/refundDetails/refundDetails?id=' + its.id + '&isrefund=' + its.refundType
			});
		},

		getrefundDet(id) {
			req.getRequest(
				'/api/orderRefund/queryRefund',
				{
					orderId: id
				},
				data => {
					this.setData({
						orderRefund: data
					});
				}
			);
		},

		//电话骑士
		callDeliver() {
			uni.makePhoneCall({
				phoneNumber: this.deliveryUserPhone
			});
		},
		copy() {
			let that = this;
			let id = this.id;
			uni.setClipboardData({
				data: id,
				complete() {
					req.msg('复制成功');
				}
			});
		},
		// 申请开票
		applyInvoice() {
			uni.navigateTo({
				url: '/invoice/apply/apply?orderId=' + this.id + '&money=' + this.money
			});
		},
		toInvoice() {
			uni.navigateTo({
				url: '/mine/invoice/invoice'
			});
		}
	}
};
</script>
<style>
@import './orderDet.css';
</style>
