<template>
	<view class="shuntBack-detail-wrap">
		<u-navbar back-text="调回详情" :border-bottom="false" :background="{backgroundColor: $config('primaryColor')}" back-icon-color="#fff" :back-text-style="{ color: '#fff' }">
			<template v-slot:right>
				<view 
				style="padding: 0 30upx;color: #ffffff;" 
				@click="refundModal = true" 
				v-if="basicInfoData&&basicInfoData.shelfInfo.state!='WRITE_OFF'&&basicInfoData.shelfInfo.state!='SUSPEND'">
					<u-icon name="chuangjiandingdan" custom-prefix="iscm-icon"></u-icon>
					<text style="margin-left: 6rpx;">生成销售退货单</text>
				</view>
			</template>
		</u-navbar>
		
		<view class="shuntBack-detail-body">
			<view class="head-info" v-if="basicInfoData">
				<view class="states">
					<view>
						<u-icon :name="pageType=='success'?'icon_complete':'icon_cancel'" custom-prefix="iscm-icon" size="48"></u-icon>
						<text>{{pageType=='success'?'已完成':'已取消'}}</text>
					</view>
				</view>
				<view>
					<view class="label"><u-icon name="icon_order" custom-prefix="iscm-icon" size="32"></u-icon><text>调回单号</text></view>
					<view class="info-txt">{{basicInfoData.recallBillNo || '--'}}</view>
				</view>
				<view>
					<view class="label"><u-icon name="ison_shelf" custom-prefix="iscm-icon" size="32"></u-icon><text>货架名称</text></view>
					<view class="info-txt">{{basicInfoData.shelfInfo&&basicInfoData.shelfInfo.shelfName || '--'}}</view>
				</view>
			</view>
			<view class="part-list">
				<!-- 调回产品 -->
				<partList :list="partList" title="调回产品" model="view" :fromPage="pageType=='success'?'shuntBackDetail':'shuntBackDetailc'" ref="partList" noDataText="暂无产品"></partList>
			</view>
		</view>
		<!-- 生成销售退货单弹窗 -->
		<common-modal :showTitle="false" :openModal="refundModal" @confirm="refundModalConfirm" @close="refundModal=false">
			<view style="font-size: 28upx;">
				<view style="padding: 10upx;" class="flex align_center justify_between" v-if="basicInfoData">
					<view>产品总款数:<text style="color: red;">{{basicInfoData.totalCategory}}</text></view>
					<view>产品总件数:<text style="color: red;">{{basicInfoData.viewQty}}</text></view>
				</view>
				<view v-if="salesOrderList.length">
					<view style="padding: 0 10upx 10upx;font-size: 26rpx;">此调回单已经存在对应的销售退货单?</view>
					<view style="padding: 0 10upx 20upx;font-size: 26rpx;">确认再次生成销售退货单吗?</view>
					<scroll-view scroll-y="true" style="height: 200rpx;">
						<view 
						v-for="item in salesOrderList" 
						:key="item.id"
						style="color: #00aaff;border-bottom: 1px solid #eee;padding: 10upx 0;" 
						class="flex align_center justify_between">
							<text>{{item.salesReturnNo}}</text>
							<!-- <view>
								<u-icon name="arrow-right"></u-icon>
							</view> -->
						</view>
					</scroll-view>
				</view>
				<view v-else>
					<view style="padding: 0 10upx 20upx;">确认生成销售退货单吗?</view>
				</view>
			</view>
		</common-modal>
	</view>
</template>

<script>
	import { shelfRecallFindBySn, shelfRecallDetail,generateSaleReturnBill } from '@/api/shelfRecall'
	import partList from '@/pages/common/partList.vue'
	import commonModal from '@/pages/common/commonModal.vue'
	export default {
		components: { partList,commonModal },
		data() {
			return {
				recallBillSn: '',
				basicInfoData:null,
				partList: [],
				customStyle: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					background: this.$config('primaryColor')
				},
				pageType: '',
				refundModal:false,
				salesOrderList:[]
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.$config('primaryColor')
			})
		},
		onLoad(option) {
			this.recallBillSn = option.sn
			this.pageType = option.type
			this.getDetail()
			this.getPartList()
		},
		methods: {
			// 查询详情
			getDetail(){
				shelfRecallFindBySn({ sn: this.recallBillSn }).then(res => {
					if(res.status == 200){
						this.basicInfoData = res.data || null;
						this.salesOrderList = res.data.salesReturnBillList ? res.data.salesReturnBillList:[];
						//#ifdef APP-PLUS
						let webView = this.$scope.$getAppWebview();
						let zeroFlag=this.partList.every(item=>{
							return item.confirmQty ==0
						})
						if(res.data.billState == 'CANCEL' ||zeroFlag ){
							webView.setTitleNViewButtonStyle(0,{
								"color": "transparent",
								"width": "0px"
							})
						}
						//#endif
					}else{
						this.basicInfoData = null
					}
				})
			},
			// 查询列表
			getPartList(){
				const _this = this
				shelfRecallDetail({ recallBillSn: this.recallBillSn }).then(res => {
					if(res.status == 200 && res.data){
						res.data.map(item =>{
							item.confirmQty = item.confirmQty ? Number(item.confirmQty) : 0
							item.putQty = item.confirmQty ? Number(item.confirmQty) : 0
						})
						this.partList = res.data || []
					}else{
						this.partList = []
					}
				})
			},
			// 生成销售退货单
			refundModalConfirm(){
				this.showLoading("正在生成销售退货单...")
				generateSaleReturnBill({ recallBillSn: this.recallBillSn }).then(res => {
				  if (res.status == 200) {
				   uni.showToast({
				   	title:res.message
				   })
				   this.getDetail();
				  setTimeout(()=>{
					   this.refundModal = false 
				  },800)
				  }else{
					 uni.showToast({
					 	title:res.message,
						icon:'none'
					 }) 
				  }
				   uni.hideLoading()
				})
			}
		}
	}
</script>

<style lang="less">
.shuntBack-detail-wrap{
	position: relative;
	width: 100%;
	height: 100%;
	overflow: hidden;
	.shuntBack-detail-body{
		padding: 0 30rpx;
		height: 100%;
		overflow: auto;
		> view{
			padding: 10rpx 25rpx;
			background-color: #fff;
			margin-bottom: 20rpx;
			border-radius: 25rpx;
			box-shadow: 2rpx 3rpx 5rpx #eee;
		}
		.head-info{
			.states{
				border: 0;
				padding: 20rpx 0;
				> view{
					color: #191919;
					text-align: center;
					font-size: 40rpx;
					text{
						margin-left: 20rpx;
					}
				}
			}
			.info-txt{
				word-break: break-word;
			}
			font-size: 30rpx;
			> view{
				display: flex;
				border-bottom: 2rpx solid #f5f5f5;
				padding: 20rpx 0;
				> view:last-child{
					flex-grow: 1;
					width: 80%;
				}
				&:last-child{
					border:0;
				}
			}
			.label{
				display: flex;
				align-items: center;
				width: 220rpx;
				height: 44rpx;
				color: #7C7D7E;
				text{
					margin-left: 10rpx;
				}
			}
		}
	}
	.shuntBack-detail-footer{
		padding: 26upx 32upx 30upx;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		box-shadow: 3px 1px 7px #eee;
	}
}
</style>