<template>
	<view class="flex search">
		<view class="search-item flex justify-center" :search="searchFlag">
			<view class="search-item-conent">
				<uni-easyinput prefixIcon="search" type="text" v-model="query.thingDesc" placeholder="搜索" :inputBorder="false" @focus="searchFlag = true" />
				
			</view>
		</view>
		<view class="search-item flex justify-center" v-if="!searchFlag" @tap="showPopup">
			<view :class="filterFlag ? 'search-item-conent active': 'search-item-conent'">
				<uni-icons customPrefix="customicons" type="filter" size="20" color="#666" />
				<view>筛选</view>
			</view>
		</view>
		<view v-else>
			<view class="cancel-btn" @tap="searchFlag = false">取消</view>
		</view>
	</view>
	<view class="negative-container">
		<view class="card negative-item" v-for="item in problems" @tap="open(item)">
			<view class="row">
				<view class="col col-24">
					<view class="label">录入时间：</view>
					<view class="content">{{ item.createTime }}</view>
				</view>
				<view class="col col-12">
					<view class="label">涉及单位：</view>
					<view class="content">{{ item.departName }}</view>
				</view>
				<view class="col col-12">
					<view class="label">涉及人员：</view>
					<view class="content">{{ item.peoples || '/' }}</view>
				</view>
				<view class="col col-24">
					<view class="label">问题类型：</view>
					<view class="content">{{ item.problemType || '/' }}</view>
				</view>
				<view class="col col-24">
					<view class="label">问题内容：</view>
					<view class="content">{{ item.thingDesc }}</view>
				</view>
			</view>
			<view class="negative-bottom">
				<view v-if="item.distributionState === '0'" style="color: var(--danger-color)">未下发</view>
				<view v-if="item.distributionState === '1'" style="color: #ff5722">已下发</view>
			</view>
		</view>
		<empty v-if="problems.length === 0" />
	</view>
	
	<uni-popup ref="filterPopupRef" type="top">
		<view class="popup-container">
			<view class="popup-header">
				<view>全部筛选</view>
				<uni-icons type="closeempty" class="close-btn" @tap="closePopup"></uni-icons>
			</view>
			<view class="popup-body">
				<view class="filter-container">
					
					<view class="filter-label mt-10">涉及单位</view>
					<view class="mb-10">
						<uni-data-picker :localdata="departs" placeholder="请选择涉及单位" :border="false"
							:map="{text:'shortName', value: 'id'}" v-model="query.departId" @change="search" />
					</view>
					
				</view>
			</view>
			<view class="footer col-24 flex gap-8">
				<button class="col-12" @tap="reset">重置</button>
				<button type="primary" @tap="handleSearch" class="col-12">完成</button>
			</view>
		</view>
	</uni-popup>
</template>
<script>
	import store from '@/store'
	import { getDictOptions } from '@/common/dict'
	import { listProblem } from '@/api/taskProblem.js'
	import {
		departTree
	} from '@/api/depart'
	import { getDictLabel } from '@/common/util'
	
	
	let oldList;
	let _this;
	
	export default {
		data() {
			return {
				problems: [],
				query: {
					size: 100,
					current: 1,
					taskType: 'problem_shooting'
				},
				departs: [],
				searchFlag: false,
				filterFlag: false,
			}
		},
		setup(props, context) {
			const taskType = getDictOptions('taskType');
			return {
				taskType,
				getDictLabel
			}
		},
		watch: {
			searchFlag(val) {
				if (val) {
					oldList = this.problems;
					this.problems = []
				} else {
					this.problems = oldList
				}
			},
			'query.thingDesc': function(val) {
				if (_this.searchFlag) {
					if (val) {
						_this.getProblems()
					} else {
						this.problems = []
					}
					
				}
			}
		},
		onLoad() {
			_this = this;
			this.getProblems();
			departTree().then(data => {
				this.departs = data[0].children
			})
		},
		methods: {
			open(item) {
				uni.navigateTo({
				    url: '/pages/task/problem/index?id=' + item.id
				});
			},
			getProblems() {
				uni.showLoading({
					title: '数据加载中'
				});
				listProblem(this.query).then(data => {
					this.problems = data.records
					uni.hideLoading();
				});
			},
			showPopup() {
				this.$refs.filterPopupRef.open()
			},
			closePopup() {
				this.$refs.filterPopupRef.close()
			},
			search() {
				if (this.query.taskType || this.query.departId) {
					this.filterFlag = true
				} else {
					this.filterFlag = false
				}
				this.getProblems()
			},
			handleSearch() {
				this.search()
				this.closePopup()
			},
			reset() {
				this.filterFlag = false
				this.query = {
					size: 100,
					current: 1,
					taskType: 'problem_shooting'
				}
				this.getProblems()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.negative-container {
		max-height: calc(100vh - 98rpx);
		overflow: auto;
	}
	.negative-title {
		display: flex;
		justify-content: space-between;
		margin-bottom: 12rpx;
		font-weight: 500;
		font-size: 16px;
	}
	.negative-bottom {
		display: flex;
		justify-content: space-between;
		.negative-tag {
			color: #FF0000;
		}
	}
</style>