<template>
	<view class="replenishment-detail-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" 
			v-if="pageType=='success'&&totalNums&&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>
		</u-navbar>
		<view class="replenishment-detail-body" :style="`backgroundImage:linear-gradient(180deg, ${$config('primaryColor')}, #F5F6F7 12%)`">
			<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="dingdanhao" custom-prefix="iscm-icon" size="32"></u-icon><text>补货单号</text></view>
					<view class="info-txt">{{basicInfoData.replenishBillNo || '--'}}</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 v-if="pageType=='success'">
					<view class="label"><u-icon name="icon_delivery" custom-prefix="iscm-icon" size="32"></u-icon><text>配送方式</text></view>
					<view class="info-txt">{{basicInfoData.dispatchTypeDictValue || '--'}}</view>
				</view> -->
				<view v-if="pageType=='success'">
					<view class="label"><u-icon name="xinxi" custom-prefix="iscm-icon" size="32"></u-icon><text>出库备注</text></view>
					<view class="info-txt">{{basicInfoData.remarks || '--'}}</view>
				</view>
			</view>
			<view class="part-list">
				<!-- 补货产品 -->
				<partList :list="partList" title="配件列表" model="view" :fromPage="pageType=='success'?'replenishmentDetail':'replenishmentDetailc'" ref="partList" noDataText="暂无产品"></partList>
			</view>
		</view>
		<!-- 生成销售单 -->
		<common-modal v-if="confirmModal" :top="salesOrderList.length?'20vh':'40vh'" :showTitle="false" :openModal="confirmModal" @confirm="modalConfirm" @close="confirmModal=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 5upx;">此补货单已经存在对应的销售单?</view>
					<view style="padding: 0 10upx 20upx;">确认再次生成销售单吗?</view>
					<scroll-view :scroll-y="true" style="max-height: 18vh;">
						<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>
					</scroll-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 { 
		shelfReplenishDetail, 
		shelfReplenishDetailList, 
		shelfReplenishPutStock, 
		queryRelationSalesBill,
		createSalesBill} from '@/api/shelfReplenish'
	import { salesDetailBySn } from '@/api/sales'
	import partList from '@/pages/common/partList.vue'
	import commonModal from '@/pages/common/commonModal.vue'
	export default {
		components: { partList, commonModal },
		data() {
			return {
				confirmModal: false,
				replenishBillSn: '',
				basicInfoData:null,
				partList: [],
				salesOrderList:[],
				customStyle: {
					borderRadius:'100rpx',
					fontSize:'30rpx',
					background: this.$config('primaryColor')
				},
				pageType: ''
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.$config('primaryColor')
			})
		},
		onBackPress() {
			if(this.showModal){
				this.showModal = false
				return true
			}
		},
		onLoad(option) {
			this.replenishBillSn = option.sn
			this.pageType = option.type
			this.getDetail()
			this.getPartList()
			this.getSalesOrder()
		},
		computed: {
			totalNums() {
				let ret = 0
				this.partList.map(item => {
					ret = ret + item.putQty
				})
				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
				this.showLoading()
				shelfReplenishDetailList({ replenishBillSn: this.replenishBillSn }).then(res => {
					console.log(res.data)
					uni.hideLoading()
					if(res.status == 200 && res.data){
						this.partList = res.data || []
					}else{
						this.partList = []
					}
				})
			},
			// 查询关联销售单
			getSalesOrder () {
			  queryRelationSalesBill({ replenishBillSn: this.replenishBillSn }).then(res => {
			    if (res.status == 200) {
			      this.salesOrderList = res.data
			    } else {
			      this.salesOrderList = []
			    }
			  })
			},
			// 生成销售单
			creatSalesOrder(){
				this.confirmModal = true
			},
			modalConfirm(){
				this.showLoading("正在生成销售单...")
				createSalesBill({ replenishBillSn: this.replenishBillSn }).then(res => {
					if (res.status == 200) {
						this.getSalesOrder()
					}
					this.confirmModal = false
					this.toashMsg(res.message)
					uni.hideLoading()
				})
			},
			// 查看销售单
			viewSales(data){
				salesDetailBySn({ salesBillSn: data.salesBillSn }).then(res => {
				  if (res.data) {
				    uni.navigateTo({ url: '/pages/sales/edit?pageType=detail&data='+JSON.stringify(data) })
				  } else {
				    this.toashMsg('此销售单已被删除!')
				  }
				})
			}
		}
	}
</script>

<style lang="less">
.replenishment-detail-wrap{
	position: relative;
	width: 100%;
	height: 100%;
	overflow: hidden;
	.replenishment-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;
				}
			}
		}
	}
	.replenishment-detail-footer{
		padding: 26upx 32upx 30upx;
		background-color: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		box-shadow: 3px 1px 7px #eee;
	}
}
</style>