<template>
	<u-popup class="jnpf-tree-select-popup" mode="right" v-model="showPopup" width="100%" @close="close">
		<view class="jnpf-tree-select-body">
			<view class="jnpf-tree-select-title">
				<text class="icon-ym icon-ym-report-icon-preview-pagePre backIcon" @tap="close()"></text>
				<view class="title">选择用户</view>
			</view>
			<view class="jnpf-tree-select-search">
				<u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
					:show-action="false" @change="handleSearch" bg-color="#f0f2f6" shape="square">
				</u-search>
			</view>
			<view class="jnpf-tree-selected">
				<view class="jnpf-tree-selected-head">
					<view>{{$t('component.jnpf.common.selected')}}({{selectedList.length||0}})</view>
					<view v-if="multiple" class="clear-btn" @click="setCheckAll">
						{{$t('component.jnpf.common.clearAll')}}
					</view>
				</view>
				<view class="jnpf-tree-selected-box">
					<scroll-view scroll-y="true" class="select-list">
						<u-tag closeable @close="delSelect(index)" v-for="(item,index) in selectedList" :key="index"
							:text="item.fullName" class="u-selectTag" />
					</scroll-view>
				</view>
			</view>
			<view class="jnpf-tree-selected-line"></view>
			<view class="jnpf-tree-selected-tabs">
				<view class="tab-item" :class="{'tab-item-active':activeKey==='user'}" @click="toggloActive('user')">
					用户
				</view>
				<block v-if="selectType === 'all'">
					<view class="tab-item" :class="{'tab-item-active':activeKey==='position'}"
						@click="toggloActive('position')">
						组织岗位
					</view>
					<view class="tab-item" :class="{'tab-item-active':activeKey==='role'}"
						@click="toggloActive('role')">
						角色
					</view>
					<view class="tab-item" :class="{'tab-item-active':activeKey==='current'}"
						@click="toggloActive('current')">
						当前用户
					</view>
				</block>
			</view>
			<view class="jnpf-tree-selected-breadcrumb" v-if="selectType === 'all' && activeKey!=='current'">
				<view class="breadcrumb-item" :class="{'breadcrumb-item-active':!currStep}" @click="handleToFirst()"
					v-if="activeKey==='user'">
					用户组
				</view>
				<view class="breadcrumb-item" :class="{'breadcrumb-item-active':!currStep}" @click="handleToFirst()"
					v-if="activeKey==='position'">
					组织岗位
				</view>
				<view class="breadcrumb-item" :class="{'breadcrumb-item-active':!currStep}" @click="handleToFirst()"
					v-if="activeKey==='role'">
					角色
				</view>
				<view class="icon-ym icon-ym-caret-right breadcrumb-item" v-if="currStep"></view>
				<view class="breadcrumb-item" :class="{'breadcrumb-item-active':currStep}" v-if="currStep">
					用户列表
				</view>
			</view>
			<view class="jnpf-tree-select-tree">
				<scroll-view :scroll-y="true" style="height: 100%" :scroll-top="scrollTop" @scroll="handleScroll"
					v-show="(!hasPage && !currStep) && selectType === 'all'  && activeKey!=='current'">
					<ly-tree ref="tree" :props="realProps" :node-key="realProps.value" :load="loadNode" lazy
						:tree-data="lazyOptions" show-node-icon :defaultExpandAll='false'
						@node-click="handleTreeNodeClick" :expandOnClickNode="false" :checkOnClickNode="false"
						v-if="activeKey==='position'" />
					<block v-else>
						<view class="jnpf-selcet-list" v-if="list.length">
							<view class="jnpf-selcet-cell" v-for="item in list" :key="item.id"
								@click.stop="handleNodeClick(item)">
								<view class="jnpf-selcet-cell-icon" :class='item.icon'></view>
								<view class="jnpf-selcet-cell-name">
									{{item.fullName}}
								</view>
							</view>
						</view>
						<Empty class="h-full" v-else />
					</block>
				</scroll-view>
				<scroll-view :scroll-y="true" style="height: 100%" :refresher-enabled="false" :refresher-threshold="100"
					:scroll-with-animation='true' :refresher-triggered="triggered" @scrolltolower="handleScrollToLower"
					v-if="hasPage||currStep||activeKey==='current'">
					<view class="jnpf-selcet-list" v-if="userList.length">
						<view class="jnpf-selcet-cell" v-for="item in userList" :key="item.id"
							@click.stop="handleUserNodeClick(item)">
							<view class="jnpf-selcet-cell-action">
								<lyCheckbox :type="multiple ? 'checkbox' : 'radio'"
									:checked="selectedIds.includes(item.id)" />
							</view>
							<u-avatar class="jnpf-selcet-cell-avatar" :src="baseURL+item.headIcon" mode="circle"
								size="44"></u-avatar>
							<view class="jnpf-selcet-cell-name">
								{{item.fullName}}
							</view>
						</view>
					</view>
					<Empty class="h-full" v-else />
				</scroll-view>
			</view>
			<!-- 底部按钮 -->
			<view class="jnpf-tree-select-actions">
				<u-button class="buttom-btn" @click="close()">{{$t('common.cancelText')}}</u-button>
				<u-button class="buttom-btn" type="primary"
					@click.stop="handleConfirm()">{{$t('common.okText')}}</u-button>
			</view>
		</view>
	</u-popup>
</template>
<script>
	import {
		getOrgAndPosSelector,
		getUserList,
		getSelectedUserList
	} from '@/api/common'
	import lyCheckbox from '@/components/ly-tree/components/ly-checkbox.vue';
	import {
		useBaseStore
	} from '@/store/modules/base'
	import Empty from '../Empty/index.vue'

	const baseStore = useBaseStore()
	const defaultProps = {
		label: 'fullName',
		value: 'id',
		icon: 'icon',
		children: 'children'
	}
	const defaultUserQuery = {
		enabledMark: 1,
		groupId: '',
		organizeId: '',
		positionId: '',
		roleId: '',
	};
	export default {
		props: {
			selectedData: {
				type: Array,
				default: () => []
			},
			ableIds: {
				type: Array,
				default: () => []
			},
			selectType: {
				type: String,
				default: 'all'
			},
			// 通过双向绑定控制组件的弹出与收起
			modelValue: {
				type: Boolean,
				default: false
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 0
			},
			props: {
				type: Object,
				default: () => ({
					label: 'fullName',
					value: 'id',
					icon: 'icon',
					children: 'children',
					isLeaf: 'isLeaf'
				})
			},
			multiple: {
				type: Boolean,
				default: false
			}
		},
		components: {
			lyCheckbox,
			Empty
		},
		data() {
			return {
				moving: false,
				selectedList: [],
				selectedIds: [],
				keyword: '',
				showPopup: false,
				lazyOptions: [],
				activeKey: 'user',
				hasPage: false,
				pagination: {
					hasPage: 1,
					currentPage: 1,
					pageSize: 20
				},
				triggered: false,
				finish: false,
				list: [],
				userList: [],
				scrollTop: 0,
				currStep: 0,
				userQuery: {},
			};
		},
		watch: {
			// 在select弹起的时候，重新初始化所有数据
			modelValue: {
				handler(val) {
					this.showPopup = val
					if (val) setTimeout(() => this.init(), 10);
				},
				immediate: true
			},
			selectedList: {
				handler(val) {
					this.selectedIds = val.map((o) => o.id);
					this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
				},
				deep: true
			},
		},
		computed: {
			baseURL() {
				return this.define.baseURL
			},
			uZIndex() {
				// 如果用户有传递z-index值，优先使用
				return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
			},
			realProps() {
				return {
					...defaultProps,
					...this.props
				}
			},
			getCurrList() {
				const userInfo = uni.getStorageSync('userInfo') || {}
				const current = {
					fullName: `${userInfo.userName}/${userInfo.userAccount}`,
					headIcon: userInfo.headIcon,
					id: userInfo.userId,
					realName: userInfo.userName,
				};
				return [current];
			}
		},
		methods: {
			init() {
				this.keyword = ""
				this.hasPage = 0
				this.currStep = 0
				this.activeKey = 'user'
				this.resetQuery()
				this.$nextTick(() => {
					this.triggered = true
				})
				this.selectedList = JSON.parse(JSON.stringify(this.selectedData))
				if (this.selectType === 'all') {
					this.getGroupList()
				} else {
					this.hasPage = 1
					this.getConditionOptions()
				}

			},
			handleToFirst() {
				if (!this.currStep) return
				this.currStep = 0
				this.hasPage = false
				this.keyword = ''
				this.resetQuery()
			},
			resetQuery() {
				this.userList = []
				this.finish = false
				this.pagination.currentPage = 1
			},
			getConditionOptions() {
				if (!this.ableIds.length) return
				const query = {
					keyword: this.keyword,
					ids: this.ableIds,
					...this.pagination
				}
				getSelectedUserList(query).then(res => {
					const list = res.data.list || []
					if (list.length < this.pagination.pageSize) this.finish = true;
					this.userList = this.userList.concat(list);
					this.pagination.currentPage++
				})
			},
			loadNode(node, resolve) {
				const id = node.key || '0'
				const type = node?.data?.type || 'organize'
				const data = {
					id,
					type
				}
				getOrgAndPosSelector(data).then(res => {
					const list = res.data?.list || []
					resolve(list)
				})
			},
			handleScroll(e) {
				this.scrollTop = e.detail.scrollTop
			},
			goTop() {
				this.scrollTop = 0
			},
			handleScrollToLower() {
				if (this.finish || this.activeKey === 'current') return
				this.getUserData()
			},
			getUserData() {
				this.selectType == 'all' ? this.getUserList() : this.getConditionOptions()
			},
			getUserList() {
				let data = {
					keyword: this.keyword,
					...this.pagination,
					...this.userQuery
				}
				getUserList(data).then(res => {
					const list = res.data.list || []
					if (list.length < this.pagination.pageSize) this.finish = true;
					this.userList = this.userList.concat(list);
					this.pagination.currentPage++
				})
			},
			handleTreeNodeClick(item) {
				const data = item.data
				this.handleNodeClick(data)
			},
			handleNodeClick(data) {
				let key = `${this.activeKey==='position'?(data.type||'position'):this.activeKey}Id`;
				if (this.activeKey === 'user') key = 'groupId'
				this.userQuery = {
					...defaultUserQuery,
					[key]: data.id
				};
				this.currStep = 1
				this.keyword = ''
				this.resetQuery()
				this.getUserList()
			},
			handleUserNodeClick(data) {
				const index = this.selectedList.findIndex((o) => o.id === data.id);
				if (index !== -1) return this.selectedList.splice(index, 1);
				this.multiple ? this.selectedList.push(data) : (this.selectedList = [data]);
			},
			delSelect(index) {
				this.selectedList.splice(index, 1);
			},
			setCheckAll() {
				this.selectedIds = []
				this.selectedList = []
			},
			handleConfirm() {
				this.$emit('confirm', this.selectedList, this.selectedIds);
				this.close();
			},
			close() {
				this.$emit('close', false);
			},
			toggloActive(key) {
				if (this.activeKey === key) return
				this.currStep = 0
				this.keyword = ''
				this.resetQuery()
				this.$nextTick(async () => {
					this.activeKey = key
					this.goTop()
					if (this.activeKey === 'user') return this.getGroupList()
					if (this.activeKey === 'role') return (this.list = await baseStore.getRoleList())
					if (this.activeKey === 'current') this.userList = this.getCurrList
				})
			},
			async getGroupList() {
				const list = await baseStore.getGroupList()
				this.list = [{
					fullName: '全部用户',
					icon: 'icon-ym icon-ym-generator-group1',
					id: ''
				}, ...list]
			},
			handleSearch(val) {
				this.keyword = val
				this.hasPage = this.selectType !== 'all' || !!val
				this.currStep = val ? 1 : 0
				this.goTop()
				if (this.activeKey != 'user') this.activeKey = 'user'
				this.$nextTick(() => {
					if (this.keyword || this.selectType !== 'all') {
						this.userQuery = {
							...defaultUserQuery
						};
						this.getGroupList()
						this.resetQuery()
						this.$u.debounce(this.getUserData, 300)
					}
				})
			},
		}
	};
</script>