<template>
	<view class="replenishment-outWarehousing-wrap">
		<u-navbar back-text="补货出库" :border-bottom="false" :background="{backgroundColor: $config('primaryColor')}" back-icon-color="#fff" :back-text-style="{ color: '#fff' }">
			<view slot='right' style="padding: 0 30upx;color: #ffffff;" @click="creatSalesOrder">
				<u-icon name="chuangjiandingdan" custom-prefix="iscm-icon"></u-icon>
				<text style="margin-left: 6rpx;">生成销售单</text>
			</view>
		</u-navbar>
		<view class="replenishment-outWarehousing-body">
			<view class="head-info" v-if="basicInfoData">
				<view class="states">
					<view>
						<u-icon name="icon_out" custom-prefix="iscm-icon" size="48"></u-icon>
						<text>待出库</text>
					</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="label"><u-icon name="icon_position" custom-prefix="iscm-icon" size="32"></u-icon><text>客户地址</text></view>
					<view class="info-txt">{{basicInfoData.customerAddr}}</view>
				</view>
			</view>
			<view class="part-list">
				<!-- 配件列表 -->
				<partList :list="partList" title="配件列表" model="view" fromPage="replenishmentOut" ref="partList" noDataText="暂无配件"></partList>
			</view>
		</view>
		<view class="replenishment-outWarehousing-footer">
			<u-button class="button" @click="goSendOutGoods" :custom-style="customDefalutStyle" hover-class="none" shape="circle">扫码出库</u-button>
			<u-button class="button" @click="confirmModal=true" :loading="loading" type="success" :custom-style="customStyle" hover-class="none" shape="circle">一键出库</u-button>
		</view>
		<!-- 一键出库弹框 -->
		<common-modal :openModal="confirmModal" title="确认信息无误并进行出库吗？" confirmText="确认" @confirm="modalConfirm" @close="confirmModal=false">
			<u-input type='textarea' v-model="remarks" :maxlength="100" placeholder="请输入出库备注(最多100字符)" border :height="100"></u-input>
		</common-modal>
		<!-- 生成销售单 -->
		<common-modal v-if="showModal" :showTitle="false" :openModal="showModal" @confirm="showModalConfirm" @close="showModal=false">
			<view style="font-size: 28upx;">
				<view style="padding: 10upx;margin: 20upx 0;" class="flex align_center justify_between">
					<view>产品总款数：<text style="color: red;">{{partList.length}}</text></view>
					<view>产品总件数：<text style="color: red;">{{totalNums}}</text></view>
				</view>
				<view v-if="salesOrderList.length">
					<view style="padding: 0 10upx 10upx;">此补货单已经存在对应的销售单？</view>
					<view style="padding: 0 10upx 20upx;">确认再次生成销售单吗？</view>
					<view>
						<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"
						@click="viewSales(item)"
						>
							<text>{{item.salesBillNo}}</text>
							<view>
								<u-icon name="arrow-right"></u-icon>
							</view>
						</view>
					</view>
				</view>
				<view v-else>
					<view style="padding: 0 10upx 20upx;">确认生成销售单？</view>
					<view>
						<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.orderNo}}</text>
							<view>
								<u-icon name="arrow-right"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
		</common-modal>
	</view>
</template>

<script>
	import commonModal from '@/pages/common/commonModal.vue'
	import { shelfReplenishDetail, shelfReplenishDetailList, shelfReplenishOutStock, queryRelationSalesBill } from '@/api/shelfReplenish'
	import partList from '@/pages/common/partList.vue'
	export default {
		components: { partList, commonModal },
		data() {
			return {
				loading: false,
				replenishBillSn: '',
				basicInfoData:null,
				partList: [],
				customStyle: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					background: this.$config('primaryColor')
				},
				customDefalutStyle: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					color: this.$config('primaryColor'),
					background: '#fff'
				},
				confirmModal: false,
				remarks: '',
				showModal: false
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.$config('primaryColor')
			})
		},
		onLoad(option) {
			this.replenishBillSn = option.sn
			this.getDetail()
			this.getPartList()
			this.getSalesOrder()
		},
		computed: {
			totalNums() {
				let ret = 0
				this.partList.map(item => {
					ret = ret + item.qty
				})
				return ret
			}
		},
		methods: {
			// 查询详情
			getDetail(){
				shelfReplenishDetail({ sn: this.replenishBillSn }).then(res => {
					if(res.status == 200){
						this.basicInfoData = res.data || null
					}else{
						this.basicInfoData = null
					}
				})
			},
			// 查询列表
			getPartList(){
				const _this = this
				shelfReplenishDetailList({ replenishBillSn: this.replenishBillSn }).then(res => {
					if(res.status == 200 && res.data){
						res.data.map(item =>{
							item.confirmQty = item.confirmQty ? Number(item.confirmQty) : 0
						})
						this.partList = res.data || []
					}else{
						this.partList = []
					}
				})
			},
			// 一键出库
			modalConfirm(){
				const arr = []
				const _this = this
				_this.partList.map((item, index) => {
				  arr.push({
				    productSn: item.productSn,
				    confirmQty: item.confirmQty,
				    replenishBillDetailSn: item.replenishBillDetailSn,
					id: item.id
				  })
				})
				const params = {
				  replenishBillSn: _this.replenishBillSn,
				  detailList: arr,
				  remarks: _this.remarks,
				  shelfSn: _this.basicInfoData && _this.basicInfoData.shelfSn
				}
				_this.showLoading('正在出库...')
				shelfReplenishOutStock(params).then(res => {
					console.log(res)
				  if (res.status == 200) {
					_this.remarks = ''
					uni.$emit('refreshBL')
					uni.$emit("refreshBhList",'WAIT_CHECK')
					uni.navigateBack()
				  }
				   _this.toashMsg(res.message)
				   uni.hideLoading()
				})
			},
			// 发货出库
			goSendOutGoods(){
				uni.navigateTo({ url: "/pages/replenishmentManage/sendOutGoods?sn="+this.replenishBillSn+'&shelfSn='+this.basicInfoData.shelfSn })
			},
			// 查询关联销售单
			getSalesOrder () {
			  queryRelationSalesBill({ replenishBillSn: this.replenishBillSn }).then(res => {
			    if (res.status == 200) {
			      this.salesOrderList = res.data
			    } else {
			      this.salesOrderList = []
			    }
			  })
			},
			// 生成销售单
			creatSalesOrder(){
				this.showModal = true
			},
			showModalConfirm(){
				this.showLoading("正在生成销售单...")
				createSalesBill({ replenishBillSn: this.replenishBillSn }).then(res => {
					if (res.status == 200) {
						this.getSalesOrder()
					}
					this.showModal = false
					this.toashMsg(res.message)
					uni.hideLoading()
				})
			},
			// 查看销售单
			viewSales(data){
				uni.navigateTo({ url: '/pages/sales/edit?pageType=detail&data='+JSON.stringify(data) })
			}
		}
	}
</script>

<style lang="less">
	page {
		height: 100vh;
	}
.replenishment-outWarehousing-wrap{
	position: relative;
	width: 100%;
	height: 100%;
	overflow: hidden;
	padding-bottom: 136upx;
	.replenishment-outWarehousing-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;
				}
			}
		}
	}
	.replenishment-outWarehousing-footer{
		padding: 26upx 32upx 30upx;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		box-shadow: 3px 1px 7px #eee;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.button{
			width: 45%;
		}
	}
}
</style>
