<template>
	<view class="main">
		<view class="search">
			<u-search placeholder="搜索医生" :focus="true" bgColor="white" v-model="nickName" :showAction="false" @search="search()" @clear="refresh()"></u-search>
		</view>
		<view class="search_list">
			<view class="history" v-if="!focus">
				<view class="title">搜索历史</view>
				<view class="del" v-if="history.length > 0" @click="del()">
					<text class="icon">&#xe8b6;</text>
					<text>清除</text>
				</view>
				<view class="tag" v-for="(item, index) in history" :key="item" @click="(nickName = item), search()">{{ item }}</view>
			</view>
			<view class="item" v-for="(item, index) in list" :key="item.deptId" @click="go('/pages/doctor/detail?id=' + item.id)">
				<view class="title omit">{{ item.name }}</view>
				<view class="icon">&#xe62b;</view>
			</view>
			<u-empty v-if="list.length == 0"></u-empty>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			list: [],
			focus: false,
			history: uni.getStorageSync('history2') || [],
			nickName: ''
		};
	},
	onLoad(e) {},
	methods: {
		getData() {
			this.http.request({
				url: '/app/doctor/list',
				data: { nickName: this.nickName },
				success: (res) => {
					this.list = res.data.data;
				}
			});
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		search() {
			if (this.nickName.trim()) {
				this.focus = true;
				if (this.history.filter((item) => item == this.nickName.trim()) == 0) {
					this.history.unshift(this.nickName);
					uni.setStorageSync('history2', this.history);
				}
				this.getData();
			}
		},
		refresh() {
			this.nickName = '';
			this.list = [];
			this.search();
		},
		del() {
			uni.removeStorageSync('history2');
			this.history = [];
		},
		clear() {
			this.focus = false;
			this.nickName = '';
			this.list = [];
		}
	}
};
</script>

<style lang="scss"></style>
