<template>
	<view class="page1">
		<view style="background: #fff;border: 1px solid #eee;">
			<uni-segmented-control :current="current" :values="itemsGYS" @clickItem="onClickItem" styleType="text"
				activeColor="#007aff"></uni-segmented-control>
		</view>
		<view class="content">
			<view v-show="current === 1">
				<view v-for="(item,index) in dataList" :key="index" style="margin-top: 20rpx;">
					<uni-table border stripe emptyText="暂无更多数据">
						<uni-tr style="background: #eee;">
							<uni-th align="center" width="85">货号</uni-th>
							<uni-th align="center" width="50">名称</uni-th>
							<uni-th align="center" width="70">利润</uni-th>
							<uni-th align="center" width="70">利润率(%)</uni-th>
						</uni-tr>
						<uni-tr>
							<uni-td align="center">{{item.productSn == null ?'暂无':item.productSn}}</uni-td>
							<uni-td align="center">{{item.productName == null ?'暂无':item.productName}}</uni-td>
							<uni-td align="center">{{userType == 0?item.profit:'0'}}</uni-td>
							<uni-td align="center">{{userType == 0?item.profitProportion:'0'}}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
			</view>
			<view v-show="current === 0">
				<view v-for="(item,index) in dataList" :key="index">
					<view style="width: 100%;height: 160rpx;line-height: 80rpx;">
						<view style="width: 100%;text-align: center;font-weight: bold;font-size: 30rpx;">
							日期:{{item.saleDate | removeStr}}
						</view>
						<view style="width: 100%;text-align: center;border-left: 1px solid #eee;">
							操作人:{{item.createByName}}
						</view>
					</view>
					<uni-table border stripe emptyText="暂无更多数据">
						<uni-tr style="background: #eee;">
							<uni-th align="center" width="85">客户</uni-th>
							<uni-th align="center" width="50">销售额</uni-th>
							<uni-th align="center" width="70">利润</uni-th>
							<uni-th align="center" width="70">总成本</uni-th>
						</uni-tr>
						<uni-tr @tap="gotoDetail(item.saleId)">
							<uni-td align="center">{{item.userName}}</uni-td>
							<uni-td align="center">{{item.totalAmount}}</uni-td>
							<uni-td align="center">{{userType == 0?item.profit:'0'}}</uni-td>
							<uni-td align="center">{{userType == 0?item.totalCost:'0'}}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
			</view>
			<view v-show="current === 2">
				<view v-for="(item,index) in dataList" :key="index" style="margin-top: 20rpx;">
					<uni-table border stripe emptyText="暂无更多数据">
						<uni-tr style="background: #eee;">
							<uni-th align="center" width="50">客户</uni-th>
							<uni-th align="center" width="70">利润</uni-th>
							<uni-th align="center" width="70">利润率(%)</uni-th>
						</uni-tr>
						<uni-tr>
							<uni-td align="center">{{item.userName}}</uni-td>
							<uni-td align="center">{{userType == 0?item.profit:'0'}}</uni-td>
							<uni-td align="center">{{userType == 0?item.profitProportion:'0'}}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataType:'',
				dataList:[],
				range:[],
				current: 0,
				itemsGYS:['拿货(按订单)', '拿货(按商品)','拿货(按客户)'],
				status: 'loadmore',
				userType:uni.getStorageSync('type'),
				pageNum:1,
				pages:1,
				currentIndex:0
			}
		},
		filters: {
			removeStr(val) {
				
				if(val == null){
					return "";
				}else{
					var dotIndex = val.indexOf('.');
					if (dotIndex !== -1) {
					    return val.substring(0, dotIndex);
					}
					return val;
				}
			}
		},
		onReachBottom() {
			if (this.pageNum >= this.pages) return;
			this.status = 'loading';
			this.pageNum ++;
			this.getDetailList(this.currentIndex);
		},
		onLoad(option) {
			this.dataType = option.value
			this.range = JSON.parse(option.range)
		},
		onShow(){
			if(uni.getStorageSync('noLoad') == true){
				uni.removeStorageSync('noLoad')
			}else{
				this.getDetailList('0')
			}
			
		},
		methods: {
			getDetailList(type){
				this.currentIndex = type
				this.status = 'loading';
				let url,data
				if(type == '1'){
					url = '/app/sale/totalAmountDetailByProduct'
					data = {
						startDate:this.range[0],
						endDate:this.range[1],
						pageNum:this.pageNum,
						pageSize:"10"
					}
				}else if(type == '2'){
					url = '/app/sale/totalAmountDetailByCustomer'
					data = {
						startDate:this.range[0],
						endDate:this.range[1],
						pageNum:this.pageNum,
						pageSize:"10"
					}
				}else if(type == '0'){
					url = '/app/sale/totalAmountDetail'
					data = {
						startDate:this.range[0],
						endDate:this.range[1],
						pageNum:this.pageNum,
						pageSize:"10"
					}
				}
				this.tui.request(url, "POST", data, false, false).then((res) => {
					this.status = 'nomore';
					if (res.code == 200) {
						if (this.pages == 1) {
							this.dataList = res.result.records
						} else {
							this.dataList = [...this.dataList, ...res.result.records]
						}
						this.pages = res.result.pages
					} else {
						this.tui.toast(res.message)
					}
							
				})
			},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
				if (e.currentIndex == 0) {
					this.dataList = []
					this.pageNum = 1
					this.pages = 1
					this.getDetailList('0')
				} else if (e.currentIndex == 1) {
					this.dataList = []
					this.pageNum = 1
					this.getDetailList('1')
				} else if (e.currentIndex == 2) {
					this.dataList = []
					this.pageNum = 1
					this.getDetailList('2')
				}
			},
			gotoDetail(id) {
				this.page = 1
				uni.setStorageSync('noLoad',true)
				if(this.dataType == 'jinhuo'){
					uni.navigateTo({
						url: '/package1/index/waitStorageDetail?id=' + id
					})
				}else{
					uni.navigateTo({
						url: '/package1/index/orderDetail?id=' + id
					})
				}
				
			},
		}
	}
</script>

<style lang="scss">
	.page1{
		background: #fff;
	}
</style>