<template>
	<view>
		<view class="search">
			<u-search placeholder="搜索标题" v-model="param.title" bgColor="white" :showAction="false" @search="(current = 0), (param.type = ''), refresh()" @clear="(current = 0), (param.type = ''), (param.title = ''), refresh()"></u-search>
		</view>
		<view class="tab">
			<u-tabs :list="tab" :current="current" keyName="dictLabel" @click="click"></u-tabs>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="go('/pages/knowledge/doctor/add?id=' + item.id)">
				<view class="title omit">
					<text class="icon" v-if="item.top === 1">&#xe61f;</text>
					<text>{{ item.title }}</text>
				</view>
				<view class="desc">
					<text>{{ item.type }}</text>
					<text>{{ item.createTime }}</text>
					<text class="del" @click.stop="del(item, index)">删除</text>
					<text class="icon state" :style="{ color: item.state == 0 ? '#4CAF50' : '#b5b5b5' }">&#xe830;{{ item.state == 0 ? '启用' : '停用' }}</text>
				</view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
		<view class="mfooter">
			<button class="btn" @click="go('/pages/knowledge/doctor/add')">
				<text class="icon">&#xe8d5;</text>
				<text>新增</text>
			</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			tab: [],
			current: 0,
			list: [],
			param: { pageNum: 1, pageSize: 10, orderByColumn: 'k.id', isAsc: 'desc' },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getType();
		this.getData();
		uni.$on('knowledge', (res) => {
			this.refresh();
		});
	},
	methods: {
		click(e) {
			this.current = e.index;
			this.param.type = e.dictValue;
			this.refresh();
		},
		getData() {
			this.http.request({
				url: '/work/knowledge/list',
				data: this.param,
				loading: 'false',
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach((item) => {
						item.createTime = uni.$u.timeFrom(Date.parse(item.createTime));
						this.list.push(item);
					});
				}
			});
		},
		getType() {
			this.http.request({
				url: '/app/common/type/knowledge_type',
				loading: 'false',
				success: (res) => {
					this.tab = res.data.data;
					this.tab.unshift({ dictLabel: '全部类型', dictValue: '' });
				}
			});
		},
		del(item, index) {
			uni.showModal({
				title: '提示',
				content: '确定删除该知识?',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: '/work/knowledge/remove/' + item.id,
							success: (res) => {
								uni.showToast({ title: '删除成功' });
								this.list.splice(index, 1);
							}
						});
					}
				}
			});
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.list {
	padding: 5px 12px 5px 10px;
	background-color: white;
	margin: 10px;
	border-radius: 10px;
	.item {
		border-radius: 5px;
		padding: 13px 8px 13px 8px;
		margin-bottom: 10px;
		overflow: hidden;
		border-bottom: 1px solid $line;
		&:last-child {
			border: 0px;
		}
		.title {
			font-size: 15px;
			font-weight: bold;
			.icon {
				color: orangered;
				padding-right: 3px;
			}
		}
		.desc {
			font-size: 14px;
			padding-top: 10px;
			color: $font-c;
			text {
				padding-right: 20px;
			}
			.state {
				float: right;
			}
			.del {
				color: red;
				float: right;
				padding-right: 0px;
			}
		}
	}
}
</style>
