<template>
	<view class="home-page" :style='scrollHeight'>
		<!-- <view class="status_bar">
		</view> -->
		<view class="banner">
			<!-- <view class="search-block">
				<image class="icon-search" src="../../static/image/search@3x.png" mode=""></image>
				<text>搜索</text>
			</view> -->
			<uni-swiper-dot :info="info" :current="current" field="content" :dotsStyles="dotsStyles" :mode="mode">
				<swiper class="swiper-box" @change="change" autoplay='true' interval='7000'>
					<swiper-item v-for="(item ,index) in info" :key="index">
						<view class="swiper-item">
							<image class="swiper-item-img" src="../../static/image/banner@3x.png" mode="widthFix">
							</image>
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<view class="content">
			<view class="todo-box block">
				<text class="title">常用查询</text>
				<view class="amount-block">
					<view class="order-amount" @click="entryHandle('purchase-plan')">
						<view class="order-amount--wrapper">
							<image class="entry-icon" src="@/static/image/home_report@3x.png" mode="aspectFit">
							</image>
							<text class="order-amount--num">销售计划</text>
						</view>
					</view>
					<view class="order-amount" @click="entryHandle('my-order')">
						<view class="order-amount--wrapper">
							<image class="entry-icon" src="@/static/image/home_marketclue@3x.png" mode="aspectFit">
							</image>
							<text class="order-amount--num">销售订单</text>
						</view>
					</view>
					<view class="order-amount" @click="entryHandle('billing-invoice')">
						<view class="order-amount--wrapper">
							<image class="entry-icon" src="@/static/image/home_breakmission@3x.png" mode="aspectFit">
							</image>
							<text class="order-amount--num">账票管理</text>
						</view>
					</view>
					<view class="order-amount" @click="entryHandle('ticket-order')">
						<view class="order-amount--wrapper">
							<image class="entry-icon" src="@/static/image/home_breakrule@3x.png" mode="aspectFit">
							</image>
							<text class="order-amount--num">制票通知单</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 	<view class="notice-block block">
				<image class="icon-notic" src="../../static/image/notice@3x.png" mode=""></image>
				<uni-notice-bar single="true" showGetMore="false" backgroundColor='#feebe6' color="#333"
					:text="noticeContent">
				</uni-notice-bar>
			</view> -->
			<!-- <view class="todo-box block">
			<text class="title">待办</text>
			<view class="cell-list">
				<view class="cell-list-block" @click="goFundsConfirm">
					<image class="icon-address" src="@/static/image/funds@3x.png" mode="aspectFit"></image>
					<view class="cell-list-block__content">
						<text class="cell-list-block__title">打款500,000.00 ，请确认用途</text>
						<text class="cell-list-block__desc">3月27日 14:00</text>
					</view>
					<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
				</view>
				<view class="cell-list-block">
					<image class="icon-address" src="@/static/image/funds@3x.png" mode="aspectFit"></image>
					<view class="cell-list-block__content">
						<text class="cell-list-block__title">打款500,000.00 ，请确认用途</text>
						<text class="cell-list-block__desc">3月27日 14:00</text>
					</view>
					<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
				</view>
			</view>
		</view> -->
			<!-- <view class="charts-box block">
				<view class="title">
					<text class="title-text">订单执行情况</text>
					<view class="summary">
						<text class="num">{{orderAmount}}</text>万
					</view>
				</view>
				<qiun-data-charts type="column" :chartData="chartData" :opts="uchartOpts" :ontouch="true"
					background="none" />
			</view> -->
		</view>
		<view class="check-block">
			<text class="check-date">选择产品:</text>
			<view class="check-date-des uni-list picker-year">
				<view class="uni-list-cell">
					<view class="uni-list-cell-db">
						<picker class="picker-block" @change="bindPickerTypesChange" range-key='name'
							mode="selector" :value="typesIndex" :range="selectedSalePlanTypes">
							<view class="uni-input">
								<text class="uni-input-text">{{selectedSalePlanTypes[typesIndex] && selectedSalePlanTypes[typesIndex].name || '请选择'}}</text>
							</view>
						</picker>
					</view>
				</view>
				<image class="icon-arrow" src="@/static/image/arrow_r@3x.png" mode="aspectFit"></image>
			</view>
		</view>
		<view class="uni-margin-wrap">
			<swiper class="swiper" circular indicator-dots="true" autoplay="true" interval="4000000">
				<swiper-item class="swiper-item">
					<view class="swiper-title">
						<text class="item-block__label">出入库合规累积量</text>
						<text class="item-block__label item-more" @click="entryHandle('storage-list')">查看更多 ></text>
					</view>
					<view class="text-block-box">
						<view class="text-block text-block-last">
							<text class="middle-date">{{scanDetail.scanin || 0}}件</text>
							<text class="middle-date-des">入库量</text>
						</view>
						<view class="text-block text-block-last">
							<text class="middle-date">{{scanDetail.scanout || 0}}件</text>
							<text class="middle-date-des">出库量</text>
						</view>
					</view>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<view class="swiper-title">
						<text class="item-block__label">下游合规累积量</text>
					</view>
					<view class="text-block-box">
						<view class="text-block text-block-last">
							<text class="middle-date">{{scanDetail.scanterminal || 0}}件</text>
							<text class="middle-date-des">终端入库量</text>
						</view>
						<view class="text-block text-block-last">
							<text class="middle-date">{{scanDetail.scanconsumer || 0}}瓶</text>
							<text class="middle-date-des">消费者扫码量</text>
						</view>
					</view>
				</swiper-item>
				<swiper-item class="swiper-item" v-if="!selectedSalePlanTypes[typesIndex].id">
					<view class="swiper-title">
						<text class="item-block__label">经销商分利</text>
					</view>
					<view class="text-block-box">
						<view class="text-block text-block-last">
							<text class="middle-date">{{pointBalance}}点</text>
							<text class="middle-date-des">剩余点数</text>
						</view>
						<view class="text-block text-block-last">
							<text class="middle-date">{{pointPositive}}点</text>
							<text class="middle-date-des">总点数</text>
						</view>
					</view>
				</swiper-item>
				
			</swiper>
		</view>
		<view class="uni-margin-wrap">
			<view class="swiper-title">
				<text class="item-block__label">资讯</text>
				<text class="item-block__label item-more" @click="entryHandle('information')">查看更多 ></text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import {
		apiGetHomeStatistics
	} from '@/servers/common.js'
	import {
		pointsAndCashSummary,
		queryUnique,
		queryProducts
	} from '@/servers/breakwords.js'
	import {
		apiFindUnreadMsg
	} from '@/servers/message.js'
	import timeFormat from '@/uview-ui/libs/function/timeFormat.js'
	export default {
		data() {
			return {
				info: [{
					content: '内容 A'
				}, {
					content: '内容 B'
				}, {
					content: '内容 C'
				}],
				current: 0,
				mode: 'round',
				noticeContent: '9.10日，21:00-24:00系统升级，系统停止服务',
				uchartOpts: {
					padding: [0, 0, 0, 0],
					color: ['#FDB95A', '#528FF4', '#FF7282', '#C38AFF', '#FFD600'],
					enableScroll: true,
					xAxis: {
						disableGrid: true,
						itemCount: 3,
						fontSize: 12
					},
					yAxis: {
						"dashLength": 20,
						disabled: true
					},
					legend: {
						"position": "bottom",
						"float": "left",
						margin: 20
					},
					extra: {
						column: {
							categoryGap: 7
						}
					},
					series: {
						show: false
					},
					padding: [50, 0, 20, 0]
				},
				chartData: {
					"categories": [
						"1618",
						"七代",
					],
					"series": [{
						"name": "制票总金额",
						"data": []
					}, {
						"name": "订单总金额",
						"data": []
					}, {
						"name": "订单总件数",
						"data": []
					}]
					// "series": [{
					// 		"name": "购买数量(件)",
					// 		"data": [
					// 			35,
					// 			36,
					// 			31,
					// 			33,
					// 			13,
					// 			34
					// 		]
					// 	},
					// ]
				},
				statisticsData: [],
				selectedSalePlanTypes: [{
					id: "",
					name: "全部产品"
				}],
				typesIndex: 0,
				scanDetail: {},
				pointBalance: 0,
				pointPositive: 0,
			}
		},
		computed: {
			dotsStyles() {
				return {
					width: 9,
					bottom: 8,
					color: '#FF1D32',
					backgroundColor: '#E6E6E6',
					border: 'none',
					selectedBackgroundColor: '#FF1D32',
					selectedBorder: 'none'
				}
			},
			orderAmount() {
				let num = 0
				this.statisticsData.forEach(item => num += item.INFO.PRICE_OD)
				return num / 10000
			},
			...mapState(['sysinfo', 'userInfo']),
			scrollHeight() {
				return {
					'height': `${this.sysinfo.safeArea.height - 15}px`
				}
			}
		},
		
		created() {
			// this.getHomeStatistics()
			this.queryProducts()
			this.queryUnique()
			this.pointsAndCashSummary()
			this.getMessageList()
		},
		methods: {
			async getMessageList() {
				let params = {
					customer: this.userInfo.code,
					msgType: 'app',
					page: 1,
					pagesize: 1,
					msgStatus: 0
				}
				let res = await apiFindUnreadMsg(params)
				if (res.status === 1 && res.data.Rows.length > 0) {
					uni.setTabBarBadge({
					  index:3,
					  text: res.data.Total.toString()
					})
				}
			
			},
			async pointsAndCashSummary() {
				try{
					const res = await pointsAndCashSummary({organizationId: this.userInfo.code})
					this.pointBalance = res.data.pointBalance
					this.pointPositive = res.data.pointPositive
				}catch(e){
					//TODO handle the exception
				}
			},
			async queryUnique() {
				try{
					const productId = this.selectedSalePlanTypes[this.typesIndex].id
					const params = {
						distributerId: this.userInfo.code,
						from: '1990-01-01',
						to: timeFormat(new Date(), 'yyyy-mm-dd'),
					}
					if(productId){
						params.productId = productId
					}
					const res = await queryUnique(params)
					this.scanDetail = {
						scanin: res.data.in,
						scanout: res.data.out,
						scanconsumer: res.data.customerScan,
						scanterminal: res.data.terminalIn,
					}
				}catch(e){
					//TODO handle the exception
				}
			},
			async queryProducts() {
				try{
					const res = await queryProducts({
						organizationId: this.userInfo.code
					})
					this.selectedSalePlanTypes = [
						{
							id: "",
							name: "全部产品"
						},
						...res.data
					]
				}catch(e){
					//TODO handle the exception
				}
			},
			bindPickerTypesChange(e) {
				this.typesIndex = e.detail.value
				this.queryUnique()
				if(!this.typesIndex){
					this.pointsAndCashSummary()
				}
			},
			async getHomeStatistics() {
				const res = await apiGetHomeStatistics({
					PARTNER: this.userInfo.code
				})
				let {
					DATA
				} = res
				this.statisticsData = DATA
				if (DATA.length > 0) {
					this.chartData.categories = DATA.map((item) => {
						return item.BRAND_BQ_NAME
					})
					let series = [{
						"name": "制票总金额",
						"data": []
					}, {
						"name": "订单总金额",
						"data": []
					}, {
						"name": "订单总件数",
						"data": []
					}]
					DATA.forEach((item, index) => {
						series[0].data[index] = item.INFO.PRICE_ZP
						series[1].data[index] = item.INFO.PRICE_OD
						series[2].data[index] = item.INFO.NUMBER_OD
					})
					this.chartData.series = series
				}
			},
			goFundsConfirm() {
				uni.navigateTo({
					url: `/pages/funds-confirm/funds-confirm`
				});
			},
			change(e) {
				this.current = e.detail.current;
			},
			entryHandle(path) {
				// uni.showToast({
				// 	title: '开发中...',
				// 	icon: 'none',
				// 	duration: 1500
				// });

				// return

				uni.navigateTo({
					url: `/pages/${path}/${path}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-PLUS */
	page {
		width: 100%;
		min-height: 100%;
		background: #fff;
	}
	
	/* #endif */
	.home-page {
		flex: 1;
		flex-direction: column;
		box-sizing: border-box;
		background: #f0f4f5;
	}

	.status_bar {
		height: var(--status-bar-height);
	}

	.banner {
		position: relative;
		width: 100%;
		height: 410rpx;
		overflow: hidden;
	}

	.search-block {
		display: flex;
		align-items: center;
		position: absolute;
		width: calc(100% - 32px);
		height: 30px;
		left: 16px;
		top: var(--status-bar-height);
		z-index: 10;
		background-color: rgba(255, 255, 255, 0.25);
		border-radius: 16px;
		padding-left: 33px;
		font-size: 13px;
		color: rgba(255, 255, 255, 0.8);

		.icon-search {
			position: absolute;
			left: 13px;
			width: 14px;
			height: 14px;
		}
	}

	.swiper-item {
		height: 480rpx;
	}

	/deep/ .uni-swiper__warp {
		height: 100%;

		.swiper-box {
			height: 100%;
		}

		.uni-swiper__dots-item {
			border-radius: 2px;
			width: 9px !important;
			height: 3px !important;
		}

		.uni-swiper__dots-item:first-child {
			border-radius: 2px;
			width: 3px !important;
			height: 3px !important;
		}

		.uni-swiper__dots-item:last-child {
			border-radius: 2px;
			width: 3px !important;
			height: 3px !important;
		}

		.uni-swiper__dots-long {
			width: 9px !important;
		}
	}

	.swiper-item-img {
		width: 100%;
		height: 100%;
	}

	.content {
		flex: 1;
		width: 100%;
		padding: 32rpx 36rpx 24px;
		box-sizing: border-box;
		background: #ffffff;
		border-radius: 32rpx 32rpx 0px 0px;
		z-index: 10;
	}

	.block {
		background: #ffffff;
		overflow: hidden;

		.title {
			display: inline-block;
			font-size: 14px;
			color: #333;
			font-weight: 600;
			margin-bottom: 24rpx;
		}
	}

	.amount-block {
		display: flex;
		flex-direction: row;
		margin-top: 8px;
		margin-bottom: 8px;
	}

	.order-amount {
		flex: 1;
		height: 70px;
	}

	.order-amount--wrapper {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.entry-icon {
		width: 58px;
		height: 58px;
	}

	.order-amount--num {
		font-size: 12px;
		color: #333;
	}

	.bill-amount {
		background: url('~@/static/image/home_bill_bg@3x.png') no-repeat;
		background-size: 100% 100%;
		margin-right: 0;

	}

	.notice-block {
		position: relative;
		height: 39px;
		margin-bottom: 16px;
		background: #feebe6;
		border-radius: 40rpx;
	}

	.icon-notic {
		position: absolute;
		left: 8px;
		top: 50%;
		margin-top: -10px;
		z-index: 10;
		width: 20px;
		height: 20px;
	}

	.uni-noticebar {
		margin-bottom: 0;
		height: 39px;
		padding-left: 36px;
	}

	.todo-box {
		margin-bottom: 10rpx;
	}

	.cell-list {
		flex-direction: column;

		.cell-list-block {
			display: flex;
			height: 77px;
			margin-bottom: 24rpx;
			background: #f8f9fa;
			border-radius: 8px;
			padding: 0 32rpx 0 24rpx;
			align-items: center;
		}

		.icon-address {
			width: 88rpx;
			height: 88rpx;
			margin-right: 24rpx;
		}

		.cell-list-block__content {
			display: flex;
			flex: 1;
			flex-direction: column;
		}

		.cell-list-block__title {
			color: #333;
			font-size: 28rpx;
			font-weight: 500;
			flex: 1;
			align-items: center;
		}

		.cell-list-block__desc {
			color: #acacac;
			font-size: 12px;
		}

		.icon-arrow {
			width: 12rpx;
			height: 20rpx;
		}
	}

	.charts-box {
		height: 380px;
		padding-bottom: 40px;

		.title {
			margin-bottom: 32rpx;
			display: flex;
			justify-content: space-between;

			.summary {
				color: #B2B2B2;
				font-size: 36rpx;

				.num {
					color: #333333;
					margin-right: 8rpx;
				}
			}
		}
	}
	
	.uni-margin-wrap {
		width: 100%;
		padding: 20rpx;
		margin-bottom: 20rpx;
		background: #fff;
		// box-shadow: 0 2px 4px rgba(0,0,0,0.1);
		// border-radius: 20rpx;
	}
	.swiper {
		width: 100%;
		height: 200rpx;
	}
	.swiper-item{
		flex: 1;
		display: flex;
		flex-direction: column;
		margin-bottom: 32rpx;
	}
	
	.item-block__label {
		flex: 1;
		text-align: left;
		font-size: 28rpx;
		color: #333;
		font-weight: 500;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	.text-block-box {
		display: flex;
		justify-content: space-between;
		padding: 40rpx 0;
	}
	.text-block {
		flex: 1;
		flex-direction: column;
		margin-bottom: 32rpx;
		display: flex;
		flex-direction: column;
	}
	
	.text-block-last {
		margin-bottom: 0;
	}
	.middle-date {
		font-size: 28rpx;
		color: $text-base-color;
		margin-bottom: 10rpx;
		text-align: center;
	}
	.middle-date-right {
		display: flex;
		align-items: center;
	}
	.middle-date-des {
		font-size: 24rpx;
		color: #999;
		text-align: center;
	}
	.check-block {
		overflow: hidden;
		padding: 20rpx;
		display: flex;
		flex-direction: row;
		margin-top: 20rpx;
		margin-bottom: 4rpx;
		background: #fff;
	}
	.check-date {
		flex: 2;
		font-size: 28rpx;
		line-height: normal;
		color: #333;
		align-items: center;
	}
	
	.check-date-des {
		flex: 3;
		font-size: 28rpx;
		line-height: normal;
		overflow: hidden;
		text-overflow: ellipsis;
		color: #888;
		justify-content: space-between;
		display: flex;
	}
	
	.picker-year {
		position: relative;
		border-radius: 4px;
		line-height: normal;
	}
	.uni-input-text {
		font-size: 14px;
		color: #888;
	}
	.picker-block {
		flex: 1;
	}
	.uni-list-cell {
		flex: 1;
	}
	
	.uni-list-cell-db {
		flex: 1;
	}
	.icon-arrow {
		width: 18rpx;
		height: 32rpx;
	}
	.swiper-title{
		justify-content: space-around;
		display: flex;
	}
	.item-more{
		text-align: right;
		color: #999;
		font-size: 24rpx;
	}
</style>
