<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" placeholder="搜索" :inputBorder="false" v-model="query.thingDesc" @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="cancleSearch">取消</view>
		</view>
	</view>
	<view>
		<view class="flex gap-16 task-item card" v-for="item in todos" @tap="open(item)" >
			<view>
				<image src="../../static/icon/ic_question.png" style="width: 44px; height: 44px"></image>
			</view>
			<view style="width: calc(100% - 60px)">
				<view class="flex justify-between header">
					<view class="title">{{ item.problemSources }}</view>
					<view class="date">{{ item.createTime }}</view>
				</view>
				<view>
					<view class="row">
						<view class="col col-24">
							<view class="label">业务类别：</view>
							<view class="content">{{ item.businessTypeName }}</view>
						</view>
						<view class="col col-24">
							<view class="label">涉及单位：</view>
							<view class="content">{{ item.involveDepartName }}</view>
						</view>
						<view class="col col-24">
							<view class="label">问题内容：</view>
							<view class="content">{{ item.thingDesc }}</view>
						</view>
						<view class="col col-24">
							<view class="label">涉嫌问题：</view>
							<view class="content">{{ getDictLabelByArray(suspectProblems, item.involveProblem) || '/' }}</view>
						</view>
					</view>
					<view style="color: #555;" v-if="item.flowKey === 'completed'">已办结</view>
					<view style="color: #FF0000;" v-else>{{ getFlowLable(item.flowKey) }}</view>
				</view>
			</view>
						
		</view>
		<empty v-if="todos.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>
					<filter-radio :data="businessTypes" v-model="query.businessTypeCode" :prop="{text: 'dictLabel', value: 'dictValue'}" @change="search" />
					<view class="filter-label mt-10">办理状态</view>
					<filter-radio :data="processingStatus" v-model="query.processingStatus" :prop="{text: 'dictLabel', value: 'dictValue'}" @change="search" />
					
				</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 { listNegativeTodo } from '@/api/negative'
	import { getDictLabelByArray } from '@/common/util'
	import { getDictOptions } from '@/common/dict'
	
	let _this;
	let oldTasks = []
	export default {
		inheritAttrs: false,
		data() {
			return {
				tabOptions: [
					{
						text: '我的待办（0）',
						value: 'todo'
					},
					{
						text: '我的已办（0）',
						value: 'done'
					}
				],
				query: {
					size: 100,
					current: 1
				},
				todos: [],
				searchFlag: false,
				filterFlag: false,
				loading: false
				
			}
		},
		setup() {
			const suspectProblems = getDictOptions('suspectProblem');
			const processingStatus = getDictOptions('processingStatus');
			const businessTypes = getDictOptions('businessType')
			return {
				suspectProblems,
				processingStatus,
				businessTypes,
				getDictLabelByArray
			}
		},
		watch: {
			searchFlag(val) {
				if (val) {
					oldTasks = this.todos;
					this.todos = []
				} else {
					this.todos = oldTasks
				}
			},
			'query.thingDesc': function(val) {
				if (_this.searchFlag && val) {
					_this.getList()
				}
			}
		},
		onLoad() {
			_this = this;
		},
		onShow() {
			this.getList()
		},
		methods: {
			open(item) {
				uni.navigateTo({
					url: `/pages/negative/action?id=${item.negativeId}&workId=${item.workId}`
				});
			},
			getFlowLable(flowKey) {
				const signFlows = ['second_sign']
				if (signFlows.indexOf(flowKey) > -1) {
					return '待签收'
				}
				const distributeFlows = ['first_distribute', 'second_distribute']
				if (distributeFlows.indexOf(flowKey) > -1) {
					return '待下发'
				}
				const verifyFlows = ['verify']
				if (verifyFlows.indexOf(flowKey) > -1) {
					return '核查办理'
				}
				const approveFlows = ['second_approve']
				if (approveFlows.indexOf(flowKey) > -1) {
					return '待审批'
				}
				return ''
			},
			async getList() {
				this.loading = true
				uni.showLoading({
					title: '数据加载中'
				});
				listNegativeTodo(this.query).then(data => {
					this.todos = data.records
					this.loading = false
					uni.hideLoading();
				})
			},
			cancleSearch() {
				this.searchFlag = false
				this.query.taskName = ''
			},
			showPopup() {
				this.$refs.filterPopupRef.open()
			},
			closePopup() {
				this.$refs.filterPopupRef.close()
			},
			search() {
				if (this.query.businessTypeCode || this.query.processingStatus) {
					this.filterFlag = true
				} else {
					this.filterFlag = false
				}
				this.getList()
			},
			handleSearch() {
				this.search()
				this.closePopup()
			},
			reset() {
				this.filterFlag = false
				this.query = {
					size: 100,
					current: 1
				}
				this.getList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.task-item {
		.header {
			margin-bottom: 12rpx;

			.title {
				font-size: 28rpx;
				line-height: 40rpx;
			}

			.date {
				font-size: 24rpx;
				color: #666;
			}
		}

	}
	
</style>