<template>
	<view class="container">
		<!-- 搜索框 -->
		<view class="search">
			<search tips="搜索商品" @event="$navTo('pages/search/index')" />
		</view>

		<!-- 一级分类 -->
		<primary
			v-if="setting.style == PageCategoryStyleEnum.ONE_LEVEL_BIG.value || setting.style == PageCategoryStyleEnum.ONE_LEVEL_SMALL.value"
			:display="setting.style" :list="list" />

		<!-- 二级分类 -->
		<secondary v-if="setting.style == PageCategoryStyleEnum.TWO_LEVEL.value" :list="list" />

		<!-- 分类+商品 -->
		<commodity v-if="setting.style == PageCategoryStyleEnum.COMMODITY.value" ref="mescrollItem" :list="list"
			:setting="setting" />
	</view>
</template>

<script>
	import MescrollCompMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-comp'
	import {
		setCartTabBadge
	} from '@/core/app'
	import SettingKeyEnum from '@/common/enum/setting/Key'
	import {
		PageCategoryStyleEnum
	} from '@/common/enum/store/page/category'
	import SettingModel from '@/common/model/Setting'
	import * as CategoryApi from '@/api/category'
	import Search from '@/components/search'
	import Primary from './components/primary'
	import Secondary from './components/secondary'
	import Commodity from './components/commodity'

	// 最后一次刷新时间
	let lastRefreshTime;

	export default {
		components: {
			Search,
			Primary,
			Secondary,
			Commodity
		},
		mixins: [MescrollCompMixin],
		data() {
			return {
				// 枚举类
				PageCategoryStyleEnum,
				// 分类列表
				list: [],
				// 分类模板设置
				setting: {},
				// 正在加载中
				isLoading: true
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad() {
			// 加载页面数据
			this.onRefreshPage()
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			// 每间隔5分钟自动刷新一次页面数据
			const curTime = new Date().getTime()
			if ((curTime - lastRefreshTime) > 5 * 60 * 1000) {
				this.onRefreshPage()
			}
		},

		methods: {

			// 刷新页面
			onRefreshPage() {
				// 记录刷新时间
				lastRefreshTime = new Date().getTime()
				// 获取页面数据
				this.getPageData()
				// 更新购物车角标
				setCartTabBadge()
			},

			// 获取页面数据
			getPageData() {
				console.log('获取分类页面数据', )
				const app = this
				app.isLoading = true

				const result = [{
						"category_id": 10001,
						"name": "手机数码",
						"parent_id": 0,
						"image_id": 10185,
						"status": 1,
						"sort": 50,
						"children": [{
								"category_id": 10010,
								"name": "小米",
								"parent_id": 10001,
								"image_id": 10001,
								"status": 1,
								"sort": 90,
								"image": {
									"file_id": 10001,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/201807171021031720f9679.png",
									"external_url": "https:\/\/static.yoshop.xany6.com\/201807171021031720f9679.png"
								}
							},
							{
								"category_id": 10011,
								"name": "华为",
								"parent_id": 10001,
								"image_id": 10002,
								"status": 1,
								"sort": 92,
								"image": {
									"file_id": 10002,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/20180717102129b4e214214.jpg",
									"external_url": "https:\/\/static.yoshop.xany6.com\/20180717102129b4e214214.jpg"
								}
							},
							{
								"category_id": 10013,
								"name": "VIVO",
								"parent_id": 10001,
								"image_id": 10004,
								"status": 1,
								"sort": 94,
								"image": {
									"file_id": 10004,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103633c69c94138.png",
									"external_url": "https:\/\/static.yoshop.xany6.com\/20180717103633c69c94138.png"
								}
							},
							{
								"category_id": 10012,
								"name": "iphone",
								"parent_id": 10001,
								"image_id": 10003,
								"status": 1,
								"sort": 96,
								"image": {
									"file_id": 10003,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103600edf9f5038.jpg",
									"external_url": "https:\/\/static.yoshop.xany6.com\/20180717103600edf9f5038.jpg"
								}
							},
							{
								"category_id": 10016,
								"name": "三星",
								"parent_id": 10001,
								"image_id": 10007,
								"status": 1,
								"sort": 100,
								"image": {
									"file_id": 10007,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103817b50602761.png",
									"external_url": "https:\/\/static.yoshop.xany6.com\/20180717103817b50602761.png"
								}
							},
							{
								"category_id": 10015,
								"name": "魅族",
								"parent_id": 10001,
								"image_id": 10006,
								"status": 1,
								"sort": 100,
								"image": {
									"file_id": 10006,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/201807171037574f1163513.jpg",
									"external_url": "https:\/\/static.yoshop.xany6.com\/201807171037574f1163513.jpg"
								}
							},
							{
								"category_id": 10014,
								"name": "OPPO",
								"parent_id": 10001,
								"image_id": 10005,
								"status": 1,
								"sort": 100,
								"image": {
									"file_id": 10005,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/2018071710372269a633020.png",
									"external_url": "https:\/\/static.yoshop.xany6.com\/2018071710372269a633020.png"
								}
							},
							{
								"category_id": 10017,
								"name": "女性手机",
								"parent_id": 10001,
								"image_id": 10008,
								"status": 1,
								"sort": 105,
								"image": {
									"file_id": 10008,
									"file_type": 10,
									"preview_url": "https:\/\/static.yoshop.xany6.com\/20180717103858579b29364.jpg",
									"external_url": "https:\/\/static.yoshop.xany6.com\/20180717103858579b29364.jpg"
								}
							}
						],
						"image": {
							"file_id": 10185,
							"file_type": 10,
							"preview_url": "https:\/\/static.yoshop.xany6.com\/201809281707400fb788198.png",
							"external_url": "https:\/\/static.yoshop.xany6.com\/201809281707400fb788198.png"
						}
					},

				]

				app.initSetting()
				app.initCategory(result)
				// Promise.all([
				// 		// 获取分类模板设置
				// 		// 优化建议: 可以将此处的false改为true 启用缓存
				// 		SettingModel.data(false),
				// 		// 获取分类列表
				// 		CategoryApi.list()
				// 	])
				// 	.then(result => {
				// 		// 初始化分类模板设置
				// 		app.initSetting(result[0])
				// 		// 初始化分类列表数据
				// 		app.initCategory(result[1])
				// 	})
				// 	.finally(() => app.isLoading = false)
			},

			/**
			 * 初始化分类模板设置
			 * @param {Object} result
			 */
			initSetting(setting) {
				this.setting = {
					"style": 20,
					"shareTitle": "全部分类",
					"showAddCart": true,
					"cartStyle": 1
				}
			},

			/**
			 * 初始化分类列表数据
			 * @param {Object} result
			 */
			initCategory(result) {
				this.list = result
			},

		},

		/**
		 * 设置分享内容
		 */
		onShareAppMessage() {
			const app = this
			return {
				title: _this.templet.shareTitle,
				path: '/pages/category/index?' + app.$getShareUrlParams()
			}
		},

		/**
		 * 分享到朋友圈
		 * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
		 * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
		 */
		onShareTimeline() {
			const app = this
			return {
				title: _this.templet.shareTitle,
				path: '/pages/category/index?' + app.$getShareUrlParams()
			}
		}

	}
</script>

<style>
	page {
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	// 搜索框
	.search {
		position: fixed;
		top: var(--window-top);
		left: var(--window-left);
		right: var(--window-right);
		z-index: 9;
	}
</style>