<template>
	<view class="dl-notice-box-index">
		<!-- 标题 -->
		<view class="dl-title">
			<view class="left-search" @click="goSeach()">
				<view v-show="!showSearch" style="position: relative;">
					<image class="dl-image" src="@/static/index/search.png" mode="aspectFit"></image>
					<image class="dl-vip" src="@/static/index/vip.png" mode="aspectFit"></image>
				</view>
				<view v-show="showSearch" style="width: 90%;">
					<uni-search-bar radius="5" style="width: 100%;" placeholder="请输入关键词" clearButton="auto"
						cancelButton="none" @confirm="search" @clear="clear">
					</uni-search-bar>
				</view>
			</view>
			<view class="right-text" :style="{width: showSearch ? '40%':'56%'}">
				<text class="dl-text">通告</text>
			</view>
		</view>
		<!-- 菜单 -->
		<view class="dl-menu-box">
			<view v-for="(item,index) in menus" @click="itemClick(index,item)" class="dl-menu"
				:class="index==menuIndex?'dl-menu click':'dl-menu'">{{item}}</view>
		</view>
		<!-- 筛选条件 -->
		<view class="dl-menu-box" style="padding-bottom: 20rpx;">
			<view :class="[0==chooseCondition?'dl-focus dl-search-box':'dl-search-box']" @click="choosePlat(0)"
				:key="chooseCondition">
				<text>{{queryParams.platformName}}</text>
				<view class="image-box">
					<image v-if="0==chooseCondition" class="dl-icon" src="@/static/index/shang.png" mode="aspectFit">
					</image>
					<image v-else class="dl-icon" src="@/static/index/sanjiao.png" mode="aspectFit"></image>
				</view>
			</view>
			<view :class="[1==chooseCondition?'dl-focus dl-search-box':'dl-search-box']" @click="choosePlat(1)"
				:key="chooseCondition">
				<text>{{queryParams.bloggerTypeName}}</text>
				<view class="image-box">
					<image v-if="1==chooseCondition" class="dl-icon" src="@/static/index/shang.png" mode="aspectFit">
					</image>
					<image v-else class="dl-icon" src="@/static/index/sanjiao.png" mode="aspectFit"></image>
				</view>
			</view>
			<view :class="[2==chooseCondition?'dl-focus dl-search-box':'dl-search-box']" @click="choosePlat(2)"
				:key="chooseCondition">
				<text>筛选</text>
				<view class="image-box">
					<image v-if="2==chooseCondition" class="dl-icon" src="@/static/index/shang.png" mode="aspectFit">
					</image>
					<image v-else class="dl-icon" src="@/static/index/sanjiao.png" mode="aspectFit"></image>
					<image class="dl-vip" src="@/static/index/vip.png" mode="aspectFit"></image>
				</view>
			</view>
		</view>
		<!-- 主体内容区域 -->
		<view class="dl-body">
			<!-- 会员开通 -->
			<view class="dl-member-box">
				<view class="dl-left">
					<image class="dl-icon" src="@/static/index/zuanshi.png" mode="aspectFit"></image>
					<text>开通会员 解锁更多权益</text>
				</view>
				<view class="dl-right">
					<image class="dl-go-view" src="@/static/index/go-view.png" mode="aspectFit"></image>
				</view>
			</view>
			<!-- 通告列表 -->
			<view class="dl-item-box">
				<scroll-view style="height: 100%;" scroll-y="true" @scrolltolower="onReachBottomCus" refresher-enabled
					@refresherrefresh="onRefresherrefresh" :refresher-triggered="isTriggered">
					<notice-item v-if="dataList.length>0" :dataList="dataList" @goDetail="goDetail()"></notice-item>
					<notice-item v-if="dataList.length>0" :dataList="dataList" @goDetail="goDetail()"></notice-item>
					<notice-item v-if="dataList.length>0" :dataList="dataList" @goDetail="goDetail()"></notice-item>
					<notice-item v-if="dataList.length>0" :dataList="dataList" @goDetail="goDetail()"></notice-item>
					<notice-item v-if="dataList.length>0" :dataList="dataList" @goDetail="goDetail()"></notice-item>
					<view style="text-align: center" v-if="dataList.length==0">
						<image class="" src="@/static/images/nothing.png"></image>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 右侧抽屉 -->
		<uni-drawer ref="showRight" mode="right" :mask-click="true" @change="change($event)" width="160">
			<view class="scroll-view">
				<scroll-view class="scroll-view-box" scroll-y="true">
					<view class="info-content" @click="confirmItem(null)">
						<image v-show="0==chooseCondition" class="image" src="@/static/platform/quanbu.png"
							mode="aspectFit"></image>
						<view class="title">不限</view>
					</view>
					<view v-show="0==chooseCondition" class="info-content" v-for="(item,index) in platformList"
						@click="confirmItem(item)" :key="index">
						<image class="image" :src="'/static/platform/'+item.code+'.png'" mode="aspectFit"></image>
						<view class="title">{{item.title}}</view>
					</view>
					<view v-show="1==chooseCondition" class="info-content" v-for="(item,index) in bloggerTypeList"
						@click="confirmItem(item)" :key="index">
						<view class="title">{{item.title}}</view>
					</view>
				</scroll-view>
			</view>
		</uni-drawer>
		<!-- 右侧抽屉 -->
		<uni-drawer ref="showRight2" mode="right" :mask-click="true" @change="change($event)" width="260">
			<view class="scroll-view">
				<scroll-view class="scroll-view-box" scroll-y="true">
					<view style="padding: 20rpx 30rpx;">
						<view class="uni-common-mt">
							<view class="uni-form-item uni-column">
								<view class="title">粉丝量要求</view>
								<view class="dl-drawer-item">
									<input class="uni-input" v-model="queryParams.fansDown" type="digit"
										placeholder="最低" />
									<view>—</view>
									<input class="uni-input" v-model="queryParams.fansUp" type="digit"
										placeholder="最高" />
								</view>
							</view>
							<view class="uni-form-item uni-column">
								<view class="title">奖励类型</view>
								<view class="dl-drawer-item">
									<view
										:class="[''==queryParams.rewardType?'dl-focus dl-reward-type':'dl-reward-type']"
										@click="chooseRewardType('')">不限</view>
									<view
										:class="['money'==queryParams.rewardType?'dl-focus dl-reward-type':'dl-reward-type']"
										@click="chooseRewardType('money')">稿费
									</view>
									<view
										:class="['gift'==queryParams.rewardType?'dl-focus dl-reward-type':'dl-reward-type']"
										@click="chooseRewardType('gift')">赠品
									</view>
								</view>
							</view>
							<view class="uni-form-item uni-column">
								<view class="title">稿费要求</view>
								<view class="dl-drawer-item">
									<input class="uni-input" v-model="queryParams.feeDown" type="digit"
										placeholder="最低" />
									<view>—</view>
									<input class="uni-input" v-model="queryParams.feeUp" type="digit"
										placeholder="最高" />
								</view>
							</view>
							<view style="display: flex;margin-top: 10rpx;">
								<button class="mini-btn" type="default" size="mini" @click="reset()">重置</button>
								<button class="mini-btn" type="warn" size="mini"
									@click="onRefresherrefresh()">应用</button>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-drawer>
		<!-- 普通弹窗 -->
		<uni-popup ref="popup" background-color="#fff" @change="change">
			<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"><text
					class="text">popup 内容</text></view>
		</uni-popup>
	</view>
</template>

<script>
	import noticeItem from '@/pages/components/notice-item.vue'
	import rightsCode from '@/utils/rightsCode'
	import {
		getCatgByCode
	} from '@/api/system/config.js'
	import {
		toast,
		hasRights
	} from '@/utils/common.js'
	import {
		getNoticeList
	} from '@/api/business/notice.js'

	export default {
		components: {
			noticeItem
		},
		data() {
			return {
				menus: ['全部', '最新', '高奖励', '品牌置换'],
				menuIndex: 0,
				dataList: [],
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					//排序方式:new-最新；money-高奖励；
					sortBy: "",
					//品牌置换--只要有值代表查品牌置换 
					gift: "",
					platformCode: "",
					platformName: "平台",
					bloggerType: "",
					bloggerTypeName: "领域",
					//搜索的文字
					searchValue: "",
					//奖励类型选中值:""-不限 | money-稿费 | gift-赠品
					rewardType: "",
					//粉丝量要求
					fansUp: null,
					fansDown: null,
					//稿费要求
					feeUp: null,
					feeDown: null,
				},
				//是否显示搜索框
				showSearch: false,
				//当前焦点
				chooseCondition: 9,
				total: 0,
				//下来刷新状态
				isTriggered: false,
				//平台列表
				platformList: [],
				//博主分类
				bloggerTypeList: [],
			}
		},
		mounted() {
			this.initData("dl_platform", "platformList")
			this.initData("dl_blogger_type", "bloggerTypeList")
			this.selectDataList()
		},
		onLoad: function() {

		},
		methods: {
			/**
			 * 初始化数据
			 * @param {Object} code
			 * @param {Object} dataObj
			 */
			initData(code, dataObj) {
				let that = this
				getCatgByCode({
					code: code
				}).then(res => {
					if (res.code == 200) {
						this[dataObj] = res.data
					}
				}).catch((e) => {
					uni.showToast({
						icon: 'error',
						duration: 2000,
						title: e
					});
				})
			},
			/**
			 * 确认选择的条目
			 * @param {Object} item
			 */
			confirmItem(item) {
				if (0 == this.chooseCondition) {
					//选择平台
					if (null == item) {
						this.queryParams.platformCode = ""
						this.queryParams.platformName = "平台"
					} else {
						this.queryParams.platformCode = item.code
						this.queryParams.platformName = item.title
					}
				} else if (1 == this.chooseCondition) {
					//选择领域
					if (null == item) {
						this.queryParams.bloggerType = ""
						this.queryParams.bloggerTypeName = "领域"
					} else {
						this.queryParams.bloggerType = item.code
						this.queryParams.bloggerTypeName = item.title
					}
				}
				this.onRefresherrefresh()
				this.closeDrawer()
			},
			toggle(type) {
				this.type = type
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open(type)
			},
			/**
			 * 选择平台
			 */
			choosePlat(index) {
				if (index == 2) {
					//鉴权
					if (!this.checkIfHasRights(rightsCode.subscribeSelect)) {
						return
					}
					this.$refs.showRight2.open()
				} else {
					this.$refs.showRight.open()
				}
				this.chooseCondition = index
			},
			/**
			 * 选择奖励类型
			 * @param {Object} value
			 */
			chooseRewardType(value) {
				this.queryParams.rewardType = value
			},
			/**
			 * 重置筛选条件
			 */
			reset() {
				this.queryParams.rewardType = ""
				this.queryParams.feeUp = null
				this.queryParams.feeDown = null
				this.queryParams.fansDown = null
				this.queryParams.fansUp = null
			},
			// 关闭窗口
			closeDrawer(e) {
				this.chooseCondition = 9
				this.$refs.showRight.close()
				this.$refs.showRight2.close()
			},
			// 抽屉状态发生变化触发
			change(e) {
				if (!e) {
					//抽屉关闭
					this.chooseCondition = 9
				}
			},
			/**
			 * 菜单点击
			 * @param {Object} index
			 * @param {Object} item
			 */
			itemClick(index, item) {
				if ('全部' == item) {
					//全部
					this.queryParams.sortBy = ""
					this.queryParams.gift = ""
				} else if ('最新' == item) {
					//最新
					//鉴权
					if (!this.checkIfHasRights(rightsCode.highNewNotice)) {
						return
					}
					this.queryParams.sortBy = "new"
				} else if ('高奖励' == item) {
					//高奖励
					if (!this.checkIfHasRights(rightsCode.highNewNotice)) {
						return
					}
					this.queryParams.sortBy = "money"
				} else if ('品牌置换' == item) {
					//品牌置换
					this.queryParams.gift = "gift"
				}
				this.menuIndex = index
				this.onRefresherrefresh()
			},
			/**
			 * 搜索
			 */
			goSeach() {
				//鉴权
				if (!this.checkIfHasRights(rightsCode.searchNotice)) {
					return
				}
				this.showSearch = true
			},
			/**
			 * 鉴权通用方法
			 * @param {Object} rigthsCode
			 */
			checkIfHasRights(rigthsCode) {
				if (!hasRights(rigthsCode)) {
					toast("请开通会员")
					return false
				}
				return true
			},
			/**
			 * 上滑加载数据
			 */
			onReachBottomCus() {
				//判断 如果页码*页容量大于等于总条数，提示该页数据加载完毕
				if (this.queryParams.pageNum * this.queryParams.pageSize >= this.total) {
					toast("没有更多数据了")
					return
				}
				//页码+1,调用获取数据的方法获取第二页数据
				this.queryParams.pageNum++
				this.selectDataList()
			},
			/**
			 * 下拉刷新数据
			 */
			onRefresherrefresh() {
				this.isTriggered = true
				this.queryParams.pageNum = 1
				this.total = 0
				this.closeDrawer()
				this.selectDataList()
			},
			/**
			 * 文本搜索
			 * @param {Object} res
			 */
			search(res) {
				this.queryParams.searchValue = res.value
				this.onRefresherrefresh()
			},
			/**
			 * 清空搜索
			 */
			clear() {
				this.queryParams.searchValue = ""
				this.onRefresherrefresh()
			},
			/**
			 * 查询数据
			 */
			selectDataList() {
				getNoticeList(this.queryParams).then(res => {
					this.isTriggered = false
					if (res.code == 200) {
						if (this.queryParams.pageNum == 1) {
							this.dataList = res.data.records
						} else {
							this.dataList = this.dataList.concat(res.data.records)
						}
					}
				}).catch((e) => {
					this.isTriggered = false
					uni.showToast({
						icon: 'error',
						duration: 2000,
						title: e
					});
				})
			},
			/**
			 * 查看通告详情
			 * @param {Object} item
			 */
			goDetail(item) {
				this.$tab.navigateTo(`/pages/notice/detail?id=${item.id}`)
			}
		}
	}
</script>

<style lang="scss">
	@mixin flex {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.dl-notice-box-index {
		padding-top: var(--status-bar-height);
		width: 100%;
		color: #363636;
		background-color: white;
		font-size: 38rpx;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;

		.dl-title {
			width: 100%;
			display: flex;
			position: relative;
			z-index: 10;
			padding: 55rpx 10rpx 0 15rpx;
			border-bottom: 1px solid #F4F4F4;


			.left-search {
				flex: 1;
				display: flex;
				align-items: center;
				justify-content: left;
				position: relative;

				.dl-image {
					width: 50rpx;
					height: 50rpx;
				}

				.dl-vip {
					position: absolute;
					height: 25rpx;
					top: -10rpx;
					right: -22px;
				}
			}

			.right-text {
				transition: 1s;

				.dl-text {
					font-size: 32rpx;
					line-height: 78rpx;
					font-weight: bold;
				}
			}
		}

		.dl-menu-box {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			padding-top: 20rpx;

			.dl-menu {
				flex-grow: 1;
				font-size: 30rpx;
				margin: 0 20rpx;
				padding-bottom: 20rpx;
				text-align: center;
			}

			.click {
				color: #FF434E;
				font-weight: bold;
				border-bottom: 2px solid #FF434E;
			}

			.dl-search-box {
				border: 1rpx solid rgba(0, 0, 0, 0);
				position: relative;
				flex-grow: 1;
				font-size: 30rpx;
				margin: 0 15rpx;
				padding: 15rpx 25rpx;
				background-color: #F7F7F7;
				border-radius: 30rpx;
				align-items: center;
				justify-content: left;
				display: flex;

				.image-box {
					flex: 1;
				}

				.dl-icon {
					width: 20rpx;
					height: 20rpx;
					float: right;
				}

				.dl-vip {
					position: absolute;
					width: 50rpx;
					height: 25rpx;
					top: -16rpx;
					right: -5px;
				}
			}
		}

		.dl-body {
			flex: 1;
			width: 100%;
			height: 100%;
			background-color: #EEEEEE;
			padding: 20rpx 20rpx 0 20rpx;

			.dl-member-box {
				display: flex;
				align-items: center;
				border-radius: 35rpx;
				justify-content: center;
				height: 100rpx;
				width: 100%;
				background: url('/static/index/member-bg.png');
				background-size: 100% 100%;
				background-position: center;
				/* 图片居中显示 */
				background-repeat: no-repeat;
				/* 不重复背景图片 */

				.dl-left {
					display: flex;
					flex: 1;
					padding-left: 30rpx;
					align-items: center;
					font-size: 30rpx;
					color: #623109;
					justify-content: left;

					.dl-icon {
						width: 60rpx;
						height: 56rpx;

					}
				}

				.dl-right {
					.dl-go-view {
						width: 190rpx;
						float: right;
						height: 76rpx;
					}
				}
			}

			.dl-item-box {
				height: calc(100vh - var(--status-bar-height) - var(--window-bottom) - 586rpx);
				isplay: flex;
				flex-direction: column;
			}
		}
	}

	.scroll-view {
		/* #ifndef APP-NVUE */
		width: 100%;
		height: 100%;
		/* #endif */
		flex: 1
	}

	// 处理抽屉内容滚动
	.scroll-view-box {
		flex: 1;
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		padding-top: calc(var(--status-bar-height) + 100rpx);
		padding-bottom: 100rpx;
		font-size: 30rpx;
	}

	.info-content {
		padding: 20rpx 30rpx;
		display: flex;
		align-items: center;
		border-bottom: 1px solid #F4F4F4;
		justify-content: center;

		.image {
			flex: none;
			width: 40rpx;
			height: 40rpx;
		}

		.title {
			flex: 1;
			padding-left: 15rpx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
	}

	.dl-focus {
		border: 1rpx solid RGBA(255, 175, 179, 1) !important;
		background-color: RGBA(255, 175, 179, 0.3) !important;
		color: #FF434E !important;
	}

	.popup-content {
		@include flex;
		align-items: center;
		justify-content: center;
		padding: 15px;
		height: 50px;
		background-color: #fff;
	}

	.uni-popup {
		top: 370rpx !important;
	}

	.vue-ref {
		top: 370rpx !important;
	}

	.dl-drawer-item {
		display: flex;
		color: #929292;
		align-items: center;
		justify-content: center;
		border-bottom: 1px solid #F4F4F4;
		margin-bottom: 20rpx;

		input {
			border: 1rpx solid #F4F4F4;
			border-radius: 15rpx;
			color: #929292;
			padding: 5rpx 10rpx;
			margin: 10rpx 0;
			text-align: center;
			height: 60rpx;
			line-height: 60rpx;
		}

		.dl-reward-type {
			flex: 1;
			text-align: center;
			border: 1rpx solid #F4F4F4;
			color: #929292;
			border-radius: 15rpx;
			padding: 10rpx 25rpx;
			margin: 10rpx;
		}
	}
</style>