<template>
	<view class="lists">
		<view class="status_bar" :style="navHeight"></view>
		<view class="header-bg">
			<image class="header-bg-img" src="@/static/image/nav_bg@3x.png" mode="scaleToFill"></image>
		</view>
		<view class="main">
			<view class="list-wrapper" :style="scrollHeight">
				<list class="list-block" scrollable loadmoreoffset="1" show-scrollbar="false">
					<cell class="view scroll-area">
						<view class="order-main view">
							<!-- <view class="operaBtn">
								<button class="btn" type="primary" @tap="">
									<text class="btn-text">确定</text>
								</button>
								<button class="btn btn-back" type="default" @tap="">
									<text class="btn-text">取消</text>
								</button>
							</view> -->
							<view class="order-main-middle view">
								<view class="block-detal">
									<view class="block-detal-item">
										<text class="detal-item-label">销售折扣编码</text>
										<text class="detal-item-value">{{ INFO.fee_code }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">终端编码</text>
										<text class="detal-item-value">{{ INFO.terminal_code }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">终端名称</text>
										<text class="detal-item-value">{{ INFO.terminal_name }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">产品编码</text>
										<text class="detal-item-value">{{ INFO.product_code }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label line-height">产品描述</text>
										<text class="middle-pro-des">{{ INFO.product_name }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">核销状态</text>
										<text class="detal-item-value">{{ INFO.state_text }}</text>
									</view>
									<view class="block-detal-item block-detal-item-last">
										<text class="detal-item-label">总金额</text>
										<text class="detal-item-value red">{{ INFO.total_amount }}</text>
									</view>
								</view>
							</view>
						</view>

						<view class="order-middle view" :class="{ 'order-middle-auto': !ishowReceiveDetail }" ref="receiveInfo" :style="receiveInfoHeightComputed">
							<view class="item-block item-block-name">
								<text class="item-block-label">明细</text>
							</view>
							<view class="middle view" v-for="(item, index) in ITEM" :key="index">
								<view class="block-detal">
									<view class="block-detal-item">
										<text class="detal-item-label">行号</text>
										<text class="detal-item-value">{{ item.item_code }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">箱码</text>
										<text class="detal-item-value">{{ item.bar_code }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">瓶码</text>
										<text class="detal-item-value">{{ item.bottle_code }}</text>
									</view>
									<view class="block-detal-item block-detal-item-last">
										<text class="detal-item-label">金额</text>
										<text class="detal-item-value red">{{ item.amount }}</text>
									</view>
								</view>
							</view>
							<view class="float-btn-block">
								<view class="float-btn" @click="handleShowReceiveDetail">
									<text class="float-btn__text">{{ ishowReceiveDetail ? '收起' : '展示完整信息' }}</text>
									<image class="icon-arrow" :class="{ 'icon-arrow-rotate': !ishowReceiveDetail }" src="@/static/image/arrow_up@3x.png" mode="aspectFit"></image>
								</view>
							</view>
						</view>

						<view class="order-middle view" :class="{ 'order-middle-auto': !ishowHistoryDetail }" ref="receiveInfo" :style="receiveInfoHistroyComputed">
							<view class="item-block item-block-name">
								<text class="item-block-label">操作记录</text>
							</view>
							<view class="middle view" v-for="(item, index) in history" :key="index">
								<view class="block-detal">
									<view class="block-detal-item">
										<text class="detal-item-label">操作员姓名</text>
										<text class="detal-item-value">{{ item.oper_name }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">操作状态</text>
										<text class="detal-item-value">{{ item.status_text }}</text>
									</view>
									<view class="block-detal-item">
										<text class="detal-item-label">处理日期</text>
										<text class="detal-item-value">{{ item.changin_date }}</text>
									</view>
									<view class="block-detal-item block-detal-item-last">
										<text class="detal-item-label">处理时间</text>
										<text class="detal-item-value">{{ item.changin_time }}</text>
									</view>
								</view>
							</view>
							<view class="float-btn-block">
								<view class="float-btn" @click="handleShowHistoryDetail()">
									<text class="float-btn__text">{{ ishowHistoryDetail ? '收起' : '展示完整信息' }}</text>
									<image class="icon-arrow" :class="{ 'icon-arrow-rotate': !ishowHistoryDetail }" src="@/static/image/arrow_up@3x.png" mode="aspectFit"></image>
								</view>
							</view>
						</view>
					</cell>
				</list>
			</view>
		</view>
	</view>
</template>

<script>
import { mapState } from 'vuex';

import { getDhkById, getSubmitDhk } from '@/servers/loan.js';

export default {
	data() {
		return {
			ishowReceiveDetail: false,
			ishowHistoryDetail: false,
			scrollTop: 0,
			orderDetail: {
				ITEM: [],
				INFO: {},
				history: []
			},
			receiveInfoHeight: '100',
			receiveHistoryHeight: '100',
			code: ''
		};
	},
	computed: {
		...mapState(['sysinfo', 'userInfo']),
		navHeight() {
			return {
				height: `${this.sysinfo.safeArea.top + 44}px`
			};
		},
		ITEM() {
			return this.orderDetail.ITEM || [];
		},
		INFO() {
			return this.orderDetail.INFO || {};
		},
		history() {
			return this.orderDetail.history || {};
		},
		receiveInfoHeightComputed() {
			return {
				height: this.receiveInfoHeight ? `${this.receiveInfoHeight}rpx` : 'auto'
			};
		},
		receiveInfoHistroyComputed() {
			return {
				height: this.receiveHistoryHeight ? `${this.receiveHistoryHeight}rpx` : 'auto'
			};
		},
		scrollHeight() {
			return {
				height: `${this.sysinfo.safeArea.height - 58}px`
			};
		}
	},
	onLoad(option) {
		this.code = option.code;
		this.getOrderDetail(option.code);
	},
	onNavigationBarButtonTap(e) {
		const mode = e.index == '1' ? 'SUBMIT' : 'CANCEL';
		this.changeStatus(mode);
	},
	methods: {
		showConfirmation(message, confirmCallback, cancelCallback) {
			uni.showModal({
				title: '提示',
				content: message,
				showCancel: true,
				cancelText: '取消',
				cancelColor: '#000000',
				confirmText: '确认',
				confirmColor: '#3963bc',
				success: function (res) {
					if (res.confirm) {
						// 用户点击确定
						confirmCallback && confirmCallback();
					} else if (res.cancel) {
						// 用户点击取消
						cancelCallback && cancelCallback();
					}
				}
			});
		},
		async changeStatus(mode) {
			let end_ = '';
			if(mode == 'SUBMIT' && this.orderDetail.INFO.flag){
				end_ = '\r\n该终端本次申请使用'+ this.orderDetail.INFO.sheet_num+'张进货折扣券，需进货'+ this.orderDetail.INFO.sheet_num+'件使用。'
			}
			const str = '终端：' + this.orderDetail.INFO.terminal_name + ' ，折扣金额为：' + this.orderDetail.INFO.total_amount+'元。'+end_;
			this.showConfirmation(
				str,
				() => {
					console.log('用户点击了确认');
					// 这里执行确认后的逻辑
					this.submitBtn(mode);
				},
				() => {
					console.log('用户点击了取消');
					// 这里执行取消后的逻辑
				}
			);
		},
		async submitBtn(mode) {
			const params = {
				mode,
				user_bp: this.userInfo.code,
				role: 'WLY001',
				data: [this.orderDetail.INFO]
			};
			const res = await getSubmitDhk(params);
			uni.showToast({
				title: res.message,
				duration: 2000,
				icon: 'none'
			});
			if (res.msgtype === 'E') {
				return;
			}
			this.getOrderDetail(this.code);
		},
		handleBtn() {
			if (this.orderDetail?.INFO?.state - 0 != 0) {
				const webView = this.$scope.$getAppWebview();
				webView.setTitleNViewButtonStyle(0, {
					width: 0,
					fontSize: 0
				});
				webView.setTitleNViewButtonStyle(1, {
					width: 0,
					fontSize: 0
				});
			}
		},
		async getOrderDetail(code) {
			try {
				const params = {
					fee_code: code,
					USER_BP: this.userInfo.code,
					role: 'WLY001'
				};
				const {
					data: { head, history, item }
				} = await getDhkById(params);
				this.orderDetail = {
					INFO: head,
					ITEM: item,
					history: history
				};
				this.handleBtn();
				this.handleShowReceiveDetail();
				this.handleShowHistoryDetail();
			} catch (e) {
				//TODO handle the exception
			}
		},
		handleShowReceiveDetail() {
			this.ishowReceiveDetail = !this.ishowReceiveDetail;
			if (this.ishowReceiveDetail === true) {
				const heightNum = this.ITEM.length > 1 ? 200 : 200;
				this.receiveInfoHeight = `${440 * this.ITEM.length + heightNum}`;
				// this.receiveInfoHeight = '290'
			} else {
				this.receiveInfoHeight = '200';
			}
		},
		handleShowHistoryDetail() {
			this.ishowHistoryDetail = !this.ishowHistoryDetail;
			if (this.ishowHistoryDetail === true) {
				const heightNum = this.history.length > 1 ? 220 : 220;
				this.receiveHistoryHeight = `${350 * this.history.length + heightNum}`;
			} else {
				this.receiveHistoryHeight = '200';
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.view {
	flex-direction: column;
}

.header-bg {
	height: 284rpx;
	opacity: 0.72;
	margin-top: -88rpx;
}

.header-bg-img {
	width: 750rpx;
}

.lists {
	position: relative;
	flex-direction: column;
	flex: 1;
	background: #f0f4f5;
	// overflow-y: scroll;
}

.main {
	position: relative;
	flex-direction: column;
	margin-top: -164rpx;
	padding: 0 16rpx;
}

.list-wrapper {
	display: flex;
	flex-direction: column;
}

.scroll-area {
	padding-bottom: 26rpx;
}

.list-block {
	display: flex;
	flex-direction: column;
}

.order-main {
	flex: 1;
	background: #ffffff;
	// border-radius: 16rpx 100rpx 16rpx 16rpx;
	padding-top: 30rpx;
	margin-bottom: 16rpx;
}

.title-info {
	align-items: center;
	// margin-right: 26rpx;
}

.order-main-middle {
	position: relative;
	padding: 26rpx 48rpx 40rpx;
}

.middle-title {
	display: flex;
	align-items: center;
	margin-bottom: 32rpx;
}

.middle-title__text {
	font-weight: 500;
	font-size: 32rpx;
	color: $text-base-color;
	font-family: PingFangSC, PingFangSC-Medium;
}

.text-block {
	flex: 1;
	flex-direction: column;
	margin-bottom: 32rpx;
}

.text-block-last {
	margin-bottom: 0;
}

.middle-date {
	font-size: 28rpx;
	color: $text-base-color;
	margin-top: 16rpx;
}

.middle-date-des {
	font-size: 24rpx;
	color: #999;
}

.text-block-box {
	display: flex;
	justify-content: space-between;
	margin-bottom: 32rpx;
}

.text-block-box-last {
	margin-bottom: 0;
}

.middle-date-right {
	display: flex;
	align-items: center;
	justify-content: flex-end;
	text-align: right;
}

.order-middle {
	position: relative;
	// background-color: #fff;
	padding-bottom: 120rpx;
	margin-bottom: 16rpx;
	overflow: hidden;
}

.order-middle-auto {
	// height: 290rpx;
}

.empty-block {
	height: 8px;
	background: #f0f4f5;
}

.middle {
	background-color: #fff;
	border-radius: 16rpx;
	padding: 48rpx;
	margin-bottom: 16rpx;
}

.item-block {
	overflow: hidden;
}

.item-block-label {
	font-size: 28rpx;
	color: #333;
	font-weight: 600;
}

.item-block-name {
	padding-top: 24rpx;
	padding-bottom: 32rpx;
}

.icon-file {
	width: 29px;
	height: 35px;
}

.block-detal {
	display: flex;
	flex-direction: column;
}

.block-detal-item {
	display: flex;
	margin-bottom: 32rpx;
	justify-content: space-between;
}

.block-detal-item-last {
	margin-bottom: 0;
}

.detal-item-label {
	color: #999;
	font-size: 24rpx;
	flex: 1;
}
.line-height {
	line-height: 118rpx;
}
.detal-item-file {
	display: flex;
	flex-direction: column;
}

.detal-item-value {
	color: #333;
	font-size: 28rpx;
	width: 200%;
	flex: 3;
}
.detal-total-label {
	color: #999;
	font-size: 24rpx;
}
.detal-total-value {
	color: #333;
	font-size: 28rpx;
}

.item-value-intotal {
	color: #333;
	font-size: 28rpx;
	font-weight: 500;
}

.item-value-intotal-num {
	color: #ff1d32;
	font-size: 28rpx;
	font-weight: 500;
}

.btn {
	// width: 116rpx;
	// height: 54rpx;
	// line-height: 50rpx;
	font-size: 24rpx;
	// border: 1px solid $wly-primary-color;
	border-radius: 30rpx;
	margin-left: 40rpx;
}
.btn-back {
	background: $wly-primary-color;
}
.btn:after {
	border: 0;
}

.btn-text {
	color: #fff;
	font-size: 24rpx;
}

.float-btn-block {
	position: absolute;
	left: 0;
	bottom: 0;
	z-index: 10;
	width: 714rpx;
	height: 120rpx;
	background-color: #fff;
	display: flex;
	align-items: center;
	justify-content: center;
}

.float-btn {
	padding: 0 20px;
	height: 60rpx;
	border: 0.5px solid #d8d8d9;
	border-radius: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.float-btn__text {
	font-size: 24rpx;
}

.icon-arrow {
	width: 7px;
	height: 4px;
	margin-left: 16rpx;
	margin-top: 2px;
	transition-property: transform;
	transition-duration: 0.15s;
	transform: rotate(0deg);
}

.icon-arrow-rotate {
	transform: rotate(180deg);
}

.diy-entry {
	width: 160rpx;
	height: 50rpx;
	line-height: 46rpx;
	border: 1px solid #f61d30;
	background: #fff;
	border-radius: 32rpx;
	margin-left: 16rpx;
}

.diy-icon {
	width: 24rpx;
	height: 20rpx;
	margin-right: 8rpx;
}

.diy-entry-text {
	color: #f61d30;
	font-size: 12px;
}
.middle-unit__text {
	font-size: 24rpx;
}
.middle-pro-des {
	display: flex;
	flex: 3;
	font-size: 28rpx;
	// height: 96rpx;
	color: #333;
	margin-top: 10rpx;
	// align-items: center;
}
.operaBtn {
	display: flex;
	justify-content: end;
	margin-right: 48rpx;
}
.red {
	color: #f61d30;
	font-weight: 600;
}
</style>
