<template>
	<view class="sales-warp">
		<view id="tjCons">
			<!--销售数据统计 -->
			<view class="content-data">
				<view class="date-type">
					<text v-for="(item, index) in tabNav" :key="index" :class="[index == navInd ? 'active_day' : '']" @click="seleteTime(index)">{{item.name}}</text>
				</view>
				<view class="total">
					<view class="item-box">
						<u-count-to v-if="navInd==0" :end-val="bizData&&bizData.sales&&(bizData.sales.dayTotalQty || bizData.sales.dayTotalQty==0) ? bizData.sales.dayTotalQty : 0" separator=","></u-count-to>
						<u-count-to v-if="navInd==1" :end-val="bizData&&bizData.sales&&(bizData.sales.weekSalesQty || bizData.sales.weekSalesQty==0) ? bizData.sales.weekSalesQty : 0" separator=","></u-count-to>
						<u-count-to v-if="navInd==2" :end-val="bizData&&bizData.sales&&(bizData.sales.monthSalesQty || bizData.sales.monthSalesQty==0) ? bizData.sales.monthSalesQty : 0" separator=","></u-count-to>
						<view>销售总单量(个)</view>
					</view>
					<view class="item-box">
						<u-count-to v-if="navInd==0" :end-val="bizData&&bizData.sales&&(bizData.sales.dayTotalAmount || bizData.sales.dayTotalAmount==0) ? bizData.sales.dayTotalAmount : 0" separator="," :decimals="2"></u-count-to>
						<u-count-to v-if="navInd==1" :end-val="bizData&&bizData.sales&&(bizData.sales.weekSalesAmount || bizData.sales.weekSalesAmount==0) ? bizData.sales.weekSalesAmount : 0" separator="," :decimals="2"></u-count-to>
						<u-count-to v-if="navInd==2" :end-val="bizData&&bizData.sales&&(bizData.sales.monthSalesAmount || bizData.sales.monthSalesAmount==0) ? bizData.sales.monthSalesAmount : 0" separator="," :decimals="2"></u-count-to>
						<view>销售总金额(￥)</view>
					</view>
				</view>
			</view>
			<!-- 产品报价及新增销售单 -->
			<view class="nav-cont flex align_center justify_between">
				<view class="nav-item" @click="toPage('/pages/sales/productPricing')">
					<u-image class="nav-pic" style="margin-top: 5upx;" :src="`../../static/${theme}/navIcon/price_icon.png`" width="60" height="60"></u-image>
					<text class="nav-txt" style="margin-top: 5upx;">产品报价</text>
				</view>
				<!-- <view class="nav-item" @click="toPage('/pages/sales/chooseCustomer')">
					<u-image class="nav-pic" :src="`../../static/${theme}/navIcon/sale_icon.png`" width="70" height="70"></u-image>
					<text class="nav-txt">新增销售单</text>
				</view> -->
				<view class="nav-item" @click="toPage('/pages/webviews/openview?title=产品查询&url=https://jgqp.51cjml.com/h5/#!/')">
					<u-image class="nav-pic" :src="`../../static/${theme}/navIcon/pfind.png`" width="64" height="64"></u-image>
					<text class="nav-txt">产品查询</text>
				</view>
			</view>
			<!-- 销售单 -->
			<view class="sales-list">
				<view class="title">
					<text class="title-name" @click="$refs.salesList.refash()">
						销售单 
						<u-icon name="reload" color="#2979ff" size="28"></u-icon>
					</text>
					<view class="title-all" @click="toPage('/pages/sales/list')">查看全部<u-icon name="arrow-right" color="rgb(144, 147, 153)" size="28"></u-icon></view>
				</view>
			</view>
		</view>
		<!-- 销售单列表 -->
		<listComponent ref="salesList" :height="listHeight" />
	</view>
</template>

<script>
	import ListComponent from './listComponent.vue'
	import { toThousands } from '@/libs/tools'
	import { bizData } from '@/api/sales'
	export default{
		components: { ListComponent },
		data(){
			return{
				tabNav: [
					{ name: '今日', ind: 0 },
					{ name: '本周', ind: 1 },
					{ name: '本月', ind: 2 }
				],
				navInd: 0,
				bizData: null, // 销售数据统计
				theme: '',
				listHeight: 400
			}
		},
		onReady() {
			const query = uni.createSelectorQuery().in(this);
			query.select('#tjCons').boundingClientRect(data => {
			  this.listHeight = Math.floor(data.height + 18)
			}).exec();
		},
		onLoad() {
			const _this = this
			this.$nextTick(function(){
				// 监听整改完成后刷新事件
				uni.$on('refreshSalesHomeBL', function(data){
					_this.$refs.salesList.refash()
				})
			})
			
			const theme = getApp().globalData.theme
			this.theme = theme
			uni.setTabBarItem({
			  "index": 0,
			  "iconPath": "static/"+theme+"/tabbar/home.png",
			  "selectedIconPath": "static/"+theme+"/tabbar/home-active.png",
			})
			uni.setTabBarItem({
			  "index": 1,
			  "iconPath": "static/"+theme+"/tabbar/stock.png",
			  "selectedIconPath": "static/"+theme+"/tabbar/stock-active.png",
			})
			uni.setTabBarItem({
			  "index": 2,
			  "iconPath": "static/"+theme+"/tabbar/shelf.png",
			  "selectedIconPath": "static/"+theme+"/tabbar/shelf-active.png",
			})
			uni.setTabBarItem({
			  "index": 3,
			  "iconPath": "static/"+theme+"/tabbar/user.png",
			  "selectedIconPath": "static/"+theme+"/tabbar/user-active.png",
			})
			uni.setTabBarStyle({
			  color: this.$config("topBarTitleColors"),
			  selectedColor: this.$config("primaryColor"),
			  borderStyle: 'white',
			  complete: function(res){
				console.log(res)
			  }
			})
		},
		onUnload() {
			uni.$off('refreshSalesHomeBL')
		},
		onShow() {
			this.navInd = 0
			this.getBizData()
		},
		methods:{
			seleteTime(val){
				this.navInd = val
			},
			toPage(url){
				uni.navigateTo({ url: url })
			},
			getBizData () {
				bizData({}).then(res => {
					if (res.status == 200) {
						this.bizData = res.data || null
					} else {
						this.bizData = null
					}
					uni.stopPullDownRefresh();
				})
			}
		}
	}
</script>

<style lang="scss">
	.sales-warp{
		width: 100%;
		padding: 20rpx 20upx;
		.color_6{
			color: #666;
		}
		.font_13{
			font-size: 26upx;
		}
		.content-data{
			background-color: #fff;
			padding: 20upx 20upx;
			border-radius: 20upx;
			box-shadow: 3upx 2upx 6upx #eee;
			margin-bottom: 20upx;
			.date-type{
				margin: 0 120upx 20upx;
				border-radius: 50upx;
				border: 1upx solid #eee;
				text{
					display: inline-block;
					padding: 10upx 0;
					width: 33%;
					text-align: center;
				}
				text:first-child{
					border-top-left-radius: 50upx;
					border-bottom-left-radius: 50upx;
				}
				text:last-child{
					border-top-right-radius: 50upx;
					border-bottom-right-radius: 50upx;
				}
				text:not(:last-child){
					border-right: 1upx solid #eee;
				}
				.active_day {
				  background: rgb(0, 122, 255);
				  color: #fff;
				}
			}
			.total{
				display: flex;
				.item-box{
					text-align: center;
					width: 50%;
					color:#999;
				}
				.item-box:first-child{
					border-right: 1px solid #e4e7ed;
				}
			}
		}
		.nav-cont{
			.nav-item{
				width: 49%;
				border-radius: 25upx;
				background-color: #ffffff;
				padding: 14upx 0;
				box-shadow: 3upx 2upx 6upx #eee;
				margin-bottom: 20upx;
				text-align: center;
				.nav-pic{
					margin: 0 auto;
				}
				.nav-txt{
					font-size: 28upx;
					color: #666666;
				}
			}
		}
		.sales-list{
			.title{
				margin-bottom: 14upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.title-name{
					font-size: 32upx;
					color: #333;
					font-weight: 1000;
				}
				.title-all{
					color: rgb(144, 147, 153);
				}
			}
		}
	}
</style>
