<template>
	<view class="sales-warp">
		<view id="tjCons">
			<!--销售数据统计 -->
			<view class="content-data" v-if="$hasPermissions('M_salesTj_mobile')">
				<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')" v-if="$hasPermissions('M_productPrice_mobile')">
					<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/#!/')" v-if="$hasPermissions('M_queryProduct_mobile')">
					<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, openMessagePage } from '@/libs/tools'
	import { bizData } from '@/api/sales'
	import { listLookUp, getLookUpDatas } from '@/api/data.js'
	import { setReadNotice } from "@/api/user.js"
	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)
			}).exec();
		},
		onLoad(opts) {
			const _this = this
			this.$nextTick(function(){
				// 监听整改完成后刷新事件
				uni.$on('refreshSalesHomeBL', function(data){
					_this.$refs.salesList.refash()
				})
			})
			// 点击消息打开
			const msg = this.$store.state.vuex_tempData
			console.log('toMessage',msg)
			if(msg){
				setReadNotice({'noticeId': msg.extras.noticeId}).then(res => {
					openMessagePage(msg)
				})
			}
			// 底部tab
			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",
			  "visible": this.$hasPermissions('M_stockManage_mobile')
			})
			uni.setTabBarItem({
			  "index": 2,
			  "iconPath": "static/"+theme+"/tabbar/shelf.png",
			  "selectedIconPath": "static/"+theme+"/tabbar/shelf-active.png",
			  "visible": this.$hasPermissions('M_shelfManage_mobile')
			})
			uni.setTabBarItem({
			  "index": 3,
			  "iconPath": "static/"+theme+"/tabbar/statistics.png",
			  "selectedIconPath": "static/"+theme+"/tabbar/statistics-active.png",
			  "visible": this.$hasPermissions('M_statistics_mobile')
			})
			uni.setTabBarItem({
			  "index": 4,
			  "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)
			  }
			})
			
			// 获取数据字典
			this.getListLookUp();
			// 获取支付，收款方式
			this.getLookUpList('SETTLE_STYLE', 'vuex_paymentTypeList');
			// 获取门店审核状态
			this.getLookUpList('XPRH_STORE_APPLY_STATUS', 'vuex_storeAuthState');
			// 门店审核结算方式
			this.getLookUpList('SHELF_SETTLE_TYPE', 'vuex_shelfSettleType');
		},
		onUnload() {
			uni.$off('refreshSalesHomeBL')
			uni.$off('toMessage')
		},
		onShow() {
			this.navInd = 0
			this.getBizData()
		},
		methods:{
			//  获取数据字典
			getListLookUp() {
				const _this = this;
				listLookUp().then(res => {
					if (res.status == 200) {
						_this.$store.state.vuex_allLookUp = res.data;
					}
				});
			},
			// 或某一项字典列表，参数code
			getLookUpList(code, vuexKey) {
				getLookUpDatas({ pageNo: 1,pageSize: 1000, lookupCode: code}).then(res => {
					console.log(res)
					if (res.status == 200) {
						const list = res.data.list;
						list.map(item => {
							item.count = 0
						})
						this.$store.state[vuexKey] = list || [];
					}
				});
			},
			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%;
		height: 100vh;
		padding: 0 20upx;
		> view{
			&:first-child{
				padding-top: 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{
				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>
