<template>
	<view class="main">
		<view class="search">
			<u-search placeholder="搜索科室" :focus="true" bgColor="white" v-model="deptName" :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="(deptName = item), search()">{{ item }}</view>
			</view>
			<view class="item" v-for="(item, index) in list" :key="item.deptId" @click="go('/pages/department/detail?id=' + item.deptId)">
				<view class="title omit">{{ item.deptName }}</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('history') || [],
			deptName: ''
		};
	},
	onLoad(e) {},
	methods: {
		getData() {
			this.http.request({
				url: '/app/department/selectDeptList',
				data: { deptName: this.deptName },
				success: (res) => {
					this.list = res.data.data;
				}
			});
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		search() {
			if (this.deptName.trim()) {
				this.focus = true;
				if (this.history.filter((item) => item == this.deptName.trim()) == 0) {
					this.history.unshift(this.deptName);
					uni.setStorageSync('history', this.history);
				}
				this.getData();
			}
		},
		refresh() {
			this.deptName = '';
			this.list = [];
			this.search();
		},
		del() {
			uni.removeStorageSync('history');
			this.history = [];
		},
		clear() {
			this.focus = false;
			this.deptName = '';
			this.list = [];
		}
	}
};
</script>

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