<template>
	<view class="sales-edit-wrap flex flex_column">
		<!-- 基础信息 -->
		<view class="sales-info-con">
			<u-section @click="infoModal=true" title="基础信息" sub-title="查看更多" :line-color="$config('primaryColor')"></u-section>
			<view class="info-item-box">
				<text class="info-item-tit color_6">客户名称</text>
				<text>{{detailData && detailData.buyerName || '--'}}</text>
			</view>
			<view class="info-item-box">
				<text class="info-item-tit color_6">销售单号</text>
				<text>{{detailData && detailData.salesBillNo || '--'}}</text>
			</view>
		</view>
		<!-- 产品列表 -->
		<view class="sales-product-con">
			<!-- 标题 -->
			<view class="sales-product-header">
				<u-tabs :show-bar="false" :active-item-style="activeItemStyle" :list="tabsList" :is-scroll="false" :current="current" @change="changeTabs"></u-tabs>
			</view>
			<view class="flex flex_column" v-show="current == 0">
			<!-- 合计 -->
			<view ref="salesTotal" v-if="detailData" class="sales-total-box border-b font_13 flex justify_between">
				<view class="sales-total-con flex_1">
					<view>
						总款数：<text class="sales-total-num" :style="{ color: $config('warringColor') }">
								{{ (detailData.totalCategory || detailData.totalCategory == 0) ? toThousands(detailData.totalCategory) : '--' }}</text>；
						总数量：<text class="sales-total-num" :style="{ color: $config('warringColor') }">
								{{ (detailData.totalQty || detailData.totalQty == 0) ? toThousands(detailData.totalQty) : '--' }}
							</text>；
					</view>
					<view>
						总售价：<text class="sales-total-num" :style="{ color: $config('warringColor') }">
								￥{{ detailData.totalAmount || detailData.totalAmount == 0 ? toThousands(detailData.totalAmount, 2) : '--' }}
							</text>；
						折后总售价：<text class="sales-total-num" :style="{ color: $config('warringColor') }">
								￥{{ detailData.discountedAmount || detailData.discountedAmount == 0 ? toThousands(detailData.discountedAmount, 2) : '--' }}
							</text>；
					</view>
				</view>
			</view>
			<!-- 产品列表 -->
			<view class="sales-list-box">
				<scroll-view class="sales-list-con" :style="{height:scrollH+'upx'}" scroll-y @scrolltolower="onreachBottom">
					<view class="sales-list-item border-b flex align_center justify_between" v-for="(item, index) in listData" :key="item.id">
						<view class="font_13 flex_1 flex justify_between">
							<view class="pimgs">
								<u-image :src="item.productMainImage?item.productMainImage:`../../static/${$config('themePath')}/def_img@2x.png`" width="128" height="128" border-radius="10"></u-image>
								<view v-if="item.oosFlag == 1" class="sign" :style="{backgroundColor: $config('errorColor')}">急</view>
								<view v-if="item.price < item.cost" class="sign" :style="{backgroundColor: $config('errorColor')}">亏</view>
								<view v-if="item.price == 0" class="sign" :style="{backgroundColor: $config('errorColor')}">赠</view>
							</view>
							<view class="sales-item-main flex_1">
								<view class="sales-item-name">{{item.productName || '--'}}</view>
								<view class="sales-item-txt">{{item.productCode || '--'}}</view>
								<!-- <view class="sales-item-txt color_6" style="font-size: 24upx;" v-if="item.warehouseEntity&&item.warehouseEntity.name || item.warehouseLocationEntity&&item.warehouseLocationEntity.name">{{item.warehouseEntity&&item.warehouseEntity.name}} > {{item.warehouseLocationEntity&&item.warehouseLocationEntity.name}}</view> -->
								<view class="sales-item-txt flex align_center justify_between">
									<view class="sales-item-priceInfo">
										<view :style="{color: $config('errorColor'), display: 'inline-block'}">￥<text style="font-size: 30upx;">{{toThousands(item.price||0, 2)}}</text></view>
										<text style="display: inline-block;margin: 0 10upx;">X</text> 
										<text style="font-size: 30upx;">{{toThousands(item.qty||0)}}</text>
										<!-- {{item.productUnit}} -->
									</view>
									<view class="sales-item-price">￥{{item.totalAmount || item.totalAmount == 0?toThousands(item.totalAmount, 2):0}}</view>
								</view>
								<!-- <view class="sales-item-price" v-if="detailData&&detailData.discountAmount">折后：￥{{toThousands(item.discountedAmount||0, 2)}}</view> -->
							</view>
						</view>
					</view>
					<view v-if="listData && listData.length == 0">
						<u-empty :src="`/static/${$config('themePath')}/def_no_data@3x.png`" icon-size="150" :text="noDataText" mode="list" :margin-top="50"></u-empty>
					</view>
					<view style="padding: 20upx;" v-else>
						<u-loadmore v-if="totalNum>listData.length || status=='loading'" :status="status" />
					</view>
				</scroll-view>
			</view>
			</view>
			<!-- 收款记录列表 -->
			<view class="flex flex_column" v-show="current == 1">
			<view class="sales-list-box">
				<scroll-view class="sales-list-con" :style="{height:scrollH+'upx'}" scroll-y>
					<view class="sales-list-item flex align_center justify_between" v-for="(con,i) in payRecode" :key="">
						<view class="sales-list-l">
							<view>{{con.createDate}}</view>
							<view>{{con.payType}}</view>
						</view>
						<view class="sales-list-r income" >
							{{con.price *1 > 0?'+':'-'}}￥{{con.price}}
						</view>
					</view>
					<view v-if="payRecode.length==0">
						<u-empty :src="`/static/${$config('themePath')}/def_no_data@3x.png`" icon-size="150" :text="noDataText" mode="list" :margin-top="50"></u-empty>
					</view>
				</scroll-view>
			</view>
			</view>
		</view>
		<!-- 基础信息 -->
		<orderInfoModal :infoFlag="true" :openModal="infoModal" :infoData="detailData" @close="infoModal=false" />
	</view>
</template>

<script>
	
	import OrderInfoModal from './orderInfoModal.vue'
	import { toThousands } from '@/libs/tools'
	import { salesDetailList, salesDetailDel, salesDel, salesDetailBySn, salesSubmit, salesDiscount, salesDetailUpdate,queryFlowByBizSn } from '@/api/sales'
	export default{
		components: { OrderInfoModal },
		data(){
			return{
				tabsList:[{
					name:'产品列表'
				},{
					name:'收款记录'
				}],
				current:0,
				activeItemStyle:{
					color:this.$config('primaryColor'),
					borderBottom:'4rpx solid '+this.$config('primaryColor')
				},
				toThousands,
				
				listData: [],
				pageNo: 1,
				pageSize: 10,
				totalNum: 0,
				status: 'loadmore',
				noDataText: '暂无数据',
				scrollH: 916,
				
				infoModal: false,
				discountModal: false,
				commonModal: false,
				modalText: '确认要删除吗？',
				detailData: null,  // 销售单详情
				openProductModal: false,  // 编辑产品信息弹框是否显示
				productData: null,  // 当前操作的产品信息
				productInfo: null,  // 编辑产品需要的参数
				editInfo: null,  // 添加/编辑 提交的值
				commonType: 1,  // 公共弹框类型  1删除产品，2删除销售单，3编辑产品时库存不足
				payRecode:[]
			}
		},
		onLoad(options) {
			if(options && options.salesBillId){
				this.salesBillSn = options.salesBillId;
				this.init();
			}
		},
		onUnload() {
			uni.$off('refreshEditBL')
		},
		methods: {
			//获取收款记录
			getRecodeData(pageNo){
				if (pageNo) {
					this.pageNo = pageNo
				}
				queryFlowByBizSn({bizSn: this.salesBillSn}).then(res => {
					if (res.status == 200) {
						this.payRecode =res.data
					}
			
				})
			},
			changeTabs(index){
				this.current= index
			},
			init(){
				this.getList(1)
				this.getDetail()
				this.getRecodeData(1)
			},
			// 销售单详情
			getDetail(){
				salesDetailBySn({ salesBillSn: this.salesBillSn }).then(res => {
					if(res.status == 200){
						this.detailData = res.data
					}else{
						this.detailData = null
					}
					this.toggle = false
				})
			},
			// 列表
			getList(pageNo){
				const _this = this
				if (pageNo) {
					this.pageNo = pageNo
				}
				let params = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					salesBillSn: this.salesBillSn
				}
				this.status = "loading"
				salesDetailList(params).then(res => {
					if (res.status == 200) {
						if(res.data && res.data.list){
							res.data.list.map(item => {
								item.productName = item.productEntity && item.productEntity.name || item.dealerProductEntity && item.dealerProductEntity.name
								item.productCode = item.productEntity && item.productEntity.code || item.dealerProductEntity && item.dealerProductEntity.code
								item.productUnit = item.productEntity && item.productEntity.unit || item.dealerProductEntity && item.dealerProductEntity.unit
							})
							if(this.pageNo>1){
								this.listData = this.listData.concat(res.data && res.data.list || [])
							}else{
								this.listData = res.data.list || []
							}
						}else{
							this.listData = []
						}
						this.totalNum = res.data && res.data.count || 0
					} else {
						this.listData = []
						this.totalNum = 0
						this.noDataText = res.message
					}
					this.status = "loadmore"
				})
			},
			// scroll-view到底部加载更多
			onreachBottom() {
				if(this.listData.length < this.totalNum){
					this.pageNo += 1
					this.getList()
					this.getRecodeData()
				}
			}
		}
	}
</script>

<style lang="scss">
	.sales-edit-wrap{
		width: 100%;
		.color_6{
			color: #666;
		}
		.font_13{
			font-size: 26upx;
		}
		.border-b{
			border-bottom: 1px solid #e4e7ed;
		}
		.sales-info-con{
			background-color: #fff;
			margin-bottom: 6upx;
			>view{
				padding: 14upx 20rpx;
				border-bottom: 1px solid #e4e7ed;
			}
			>view:last-child{
				border-bottom: none;
			}
			.info-item-box{
				.info-item-tit{
					display: inline-block;
					width: 20%;
				}
			}
		}
		.sales-product-con{
			background-color: #fff;
			.sales-product-header{
				/deep/.u-tab-item{
					border-bottom:1rpx solid #e4e7ed;
				}
			}
			.sales-total-box{
				align-items: flex-end;
				padding: 16upx 20rpx;
				.sales-total-con{
					color: #666;
					overflow: hidden;
					view{
						padding: 4upx 0;
						.sales-total-num{
							font-size: 24upx;
							color: rgb(48, 49, 51);
						}
					}
				}
				.sales-total-btn{
					display: inline-block;
					margin-bottom: 6upx;
				}
			}
			.sales-list-box{
				flex-grow: 1;
				.sales-list-con{
					.sales-list-item{
						padding: 20upx;
						.pimgs{
							margin-right: 20upx;
							position: relative;
							.sign{
								position: absolute;
								right: -2upx;
								top: -2upx;
								color: #fff;
								border-radius: 50%;
								width: 40upx;
								height: 40upx;
								text-align: center;
							}
						}
						.sales-item-main{
							.sales-item-name{
								font-size: 28upx;
								color: rgb(48, 49, 51);
								font-weight: bold;
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 1;
							}
							.sales-item-txt{
								margin: 8upx 0;
							}
							.sales-item-price{
								text-align: right;
							}
						}
						.btn-box{
							padding: 30upx 30upx;
						}
						.sales-list-l{
							font-size: 20rpx;
							view{
								&:last-child{
									font-size: 26rpx;
									margin-top:10rpx;
								}
							}
						}
						.sales-list-r.spending{
							color:$uni-color-error;
						}
						.sales-list-r.income{
							color:$uni-color-success;
						}
					}
					.sales-list-item:last-child{
						border: none;
					}
				}
			}
		}
		.sales-btn-box{
			background-color: #fff;
			padding: 12upx 50upx;
			.handle-btn{
				width: 47%;
			}
			.edit-btn{
				width: 100%;
			}
		}
	}
</style>
