<template>
    <AppLayout :title="title" :show-bg-image="false" >
		<view class="page-container" >
			<scroll-view :scroll-y="true" class="nearby-scroll" @scrolltolower="scrollBottom" lower-threshold="50">
				<view class="main-list" :style="getBackgroundStyle('k.png')">
					<view class="list-top">
						<view class="list-title">
							<text>筛选条件</text>
							<view class="title-line"></view>
						</view>
						<view class="title-right button-sp-area">
							<button class="mini-btn search-box-btn" type="primary" size="mini" @click="handleSearch">查询</button>
							<button class="mini-btn reset-box-btn" type="default" size="mini" @click="handleReset">重置</button>
						</view>
					</view>
					<view class="search-container">
					    <view class="search-item">
					      <text class="label">姓名：</text>
						  <uni-easyinput v-model="formData.name" placeholder="请输入任务名称"></uni-easyinput>
					    </view>
						<view class="search-item">
						  <text class="label">身份证号：</text>
						  <uni-easyinput v-model="formData.idCard" placeholder="请输入身份证号"></uni-easyinput>
						</view>
						<view class="search-item">
						  <text class="label">人员标签：</text>
						  <uni-data-select v-model="formData.tagId" :localdata="personTagOptions" placeholder="请选择人员标签" @change="personTagChange"></uni-data-select>
						</view>
						<view class="search-item">
						  <text class="label">所属区域：</text>
						  <uni-data-select v-model="formData.deptTags" :localdata="taskTypeOptions" placeholder="请选择所属区域" @change="onTaskTypeChange"></uni-data-select>
						</view>	
					</view>
				</view>
			
				<view class="main-list" :style="getBackgroundStyle('k.png')">
					<view class="list-top">
						<view class="list-title">
							<text>目标人员列表</text>
							<view class="title-line" style="left: 70rpx;"></view>
						</view>
						<view class="title-total">
							目标人数<text class="total-num">{{totalNum}}</text>人
						</view>
					</view>
					<view class="list-box" v-if="dataList.length>0">
						<view class="con-box" v-for="(item,index) in dataList" :key="index">
							<view class="form-title">
								<checkbox :value="item.id" :checked="item.checked" @click="selectItem(item)"/>
								<view class="form-name">
									{{item.name}}
								</view>
							</view>
							<view class="form-item" v-if="item.idCard">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/person.png'" mode=""></image>
									<view class="item-label">
										身份证号：
									</view>
								</view>
								<view class="item-right">
									{{item.idCard}}
								</view>
							</view>
							<view class="form-item" v-if="item.phone">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/tele.png'" mode=""></image>
									<view class="item-label">
										联系电话：
									</view>
								</view>
								<view class="item-right">
									{{item.phone}}
								</view>
							</view>
							<view class="form-item" v-if="item.address">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/base.png'" mode=""></image>
									<view class="item-label">
										住址：
									</view>
								</view>
								<view class="item-right">
									{{item.address}}
								</view>
							</view>
							<view class="form-item" v-if="item.skills">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/num.png'" mode=""></image>
									<view class="item-label">
										技能特长：
									</view>
								</view>
								<view class="item-right">
									{{item.skills}}
								</view>
							</view>
							<view class="form-item" v-if="item.tags.length>0">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/date.png'" mode=""></image>
									<view class="item-label">
										人员标签：
									</view>
								</view>
								<view class="item-right">
									<view class="" v-for="(val,num) in item.tags" :key="num">
										{{val}}
									</view>
								</view>
							</view>
							<view class="form-item" v-if="item.datasource">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/next.png'" mode=""></image>
									<view class="item-label">
										数据来源：
									</view>
								</view>
								<view class="item-right">
									{{item.datasource=== '1' ? '自动识别' : '手动添加'}}
								</view>
							</view>
							<view class="form-item" v-if="item.assistStatus">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/help.png'" mode=""></image>
									<view class="item-label">
										帮扶状态：
									</view>
								</view>
								<view class="item-right">
									{{item.assistStatus}}
								</view>
							</view>
							<view class="form-item" v-if="item.employmentStatus">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/num.png'" mode=""></image>
									<view class="item-label">
										就业状态：
									</view>
								</view>
								<view class="item-right">
									{{item.employmentStatus}}
								</view>
							</view>
							<view class="form-item" v-if="item.unemploymentDate">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/date.png'" mode=""></image>
									<view class="item-label">
										失业时间：
									</view>
								</view>
								<view class="item-right">
									{{item.unemploymentDate}}
								</view>
							</view>
							<view class="form-item" v-if="item.unemploymentRegisterDate">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/date.png'" mode=""></image>
									<view class="item-label">
										失业登记时间：
									</view>
								</view>
								<view class="item-right">
									{{item.unemploymentRegisterDate}}
								</view>
							</view>
							<view class="form-item" v-if="item.graduateDate">
								<view class="item-left">
									<image class="item-img" :src="baseUrl+'/dispatch/next.png'" mode=""></image>
									<view class="item-label">
										毕业时间：
									</view>
								</view>
								<view class="item-right">
									{{item.graduateDate}}
								</view>
							</view>
						</view>
					</view>
				    <empty v-else pdTop="200"></empty>
				</view>
				
			</scroll-view>
			<view class="button-group">
				<view class="btns">
					<button class="btn submit-btn" @click="handlesubmit">确认选择</button>
					<button class="btn reset-btn" @click="handleCancel">关闭</button>
				</view>
			  
			</view>
		</view>
    </AppLayout>
</template>

<script setup>
import { inject, ref, reactive, onMounted } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
const { $api, navTo, navBack } = inject('globalFunction');
import config from "@/config.js"

const props = defineProps({
  allocationId:{
    type: String,
    required: false,
	default: ''
  },
})
const title = ref('');
const formData = reactive({
	allocationId:'',
    name: '',
	idCard: '',
	tagId: '',
	deptTags: '',
	pageNum: 1,
	pageSize: 10
})
const totalNum=ref(0)
const dataList=ref([])
const personDatabaseStatusOptions=ref([])
const employmentStatusOptions=ref([])
const personTagOptions=ref([])
const selectedPersonIds=ref([])
const baseUrl = config.imgBaseUrl
const getBackgroundStyle = (imageName) => ({
    backgroundImage: `url(${baseUrl}/dispatch/${imageName})`,
    backgroundSize: 'cover',      // 覆盖整个容器
    backgroundPosition: 'center', // 居中
    backgroundRepeat: 'no-repeat'
});
const emit = defineEmits(['update:showVue'])
function getDictionary(){
	$api.myRequest('/system/public/dict/data/type/person_database_status').then((resData) => {
		if(resData && resData.code == 200){
			resData.data.forEach(item=>{
				const obj = {
				    value: item.dictValue,
				    text: item.dictLabel
				}
				personDatabaseStatusOptions.value.push(obj)
			})
		}
	});
	$api.myRequest('/system/public/dict/data/type/assist_employment_status').then((resData) => {
		if(resData && resData.code == 200){
			resData.data.forEach(item=>{
				const obj = {
				    value: item.dictValue,
				    text: item.dictLabel
				}
				employmentStatusOptions.value.push(obj)
			})
		}
	});
	let header={
		'Authorization':uni.getStorageSync('Padmin-Token'),
		'Content-Type': "application/x-www-form-urlencoded"
	}
	let params={}
	$api.myRequest('/dispatch/assist/person/tag/list',params,'get',9100,header).then((resData) => {
		console.log("res",resData)
		if(resData && resData.code == 200){
			resData.data.forEach(item=>{
				const obj = {
				    value: item.id,
				    text: item.name
				}
				personTagOptions.value.push(obj)
			})
		}
	});
}
function getabelByValue(value,arr) {
	if (!Array.isArray(arr.value)) {
	    return ''
	}
	const item = arr.value.find(item => item.value === String(value))
	return item ? item.text : '暂无'
}
const handleCancel = () => {
	emit('update:showVue', {
	    showVue: 'main',
	    selectedPersonIds: []
	});
	// emit('update:showVue', 'main')
}
function handleSearch(){
	dataList.value=[]
	getDataList()
}
function handleReset(){
	formData.allocationId=''
	formData.name=''
	formData.idCard=''
	formData.tagId=''
	formData.deptTags=''
	formData.pageNum=1
	formData.pageSize=10
	dataList.value=[]
	getDataList()
}
function getDataList(){
	let url=''
	if(props.allocationId){
		formData.allocationId=props.allocationId
		url='/dispatch/assist/person/database/pageAllocatePersonList'
	}else{
		url='/dispatch/assist/person/database/pageList'
	}
	
	
	let header={
		'Authorization':uni.getStorageSync('Padmin-Token'),
		'Content-Type': "application/x-www-form-urlencoded"
	}
	$api.myRequest(url,formData,'get',9100,header).then((resData) => {
		resData.rows.forEach(item=>{
			item.tags=item.tagName.split(',')
			item.assistStatus=getabelByValue(item.assistStatus,personDatabaseStatusOptions)
			item.employmentStatus=getabelByValue(item.employmentStatus,employmentStatusOptions)
			item.checked = false; // 添加这一行
		})
		totalNum.value=resData.total
		dataList.value=dataList.value.concat(resData.rows)
		
	});
}
function selectItem(item) {
	const id = item.id; // 假设每条数据有唯一 id 字段
    item.checked = !item.checked;
  
    if (item.checked) {
      // 如果选中，加入数组（去重）
      if (!selectedPersonIds.value.includes(id)) {
        selectedPersonIds.value.push(id);
      }
    } else {
      // 如果取消选中，从数组中移除
      selectedPersonIds.value = selectedPersonIds.value.filter(v => v !== id);
    }
}
// 获取选中的数据
function handlesubmit() {
	emit('update:showVue', {
	    showVue: 'main',
	    selectedPersonIds: selectedPersonIds.value
	});
}
function personTagChange(e){
	formData.tagId=e
}
function scrollBottom(){
	if(formData.pageNum<totalNum.value/formData.pageSize){
		formData.pageNum++
		getDataList()
	}
}
onMounted(() => {
	runAsyncTasks()
});
const runAsyncTasks = async () => {
  await getDictionary()
  await getDataList()
}

</script>

<style lang="stylus" scoped>
image
  height: 100%
  width: 100%
.page-container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--window-top)); /* 如果 AppLayout 有 header，需减去 */
  padding-bottom: 140rpx
  /* 或者简单用：height: 100vh; */
}
.nearby-scroll
    // 使用flex布局让scroll-view自适应高度，占据剩余空间
  flex: 1
  // overflow: hidden;
.task-box
  display: flex
  align-items: center
.task-label
  font-size: 30rpx
  color: #6E7E9B
.task-name
  font-weight: bold
  font-size: 30rpx
  color: #3D61AC
.main-list
  background-color: #ffffff
  padding: 20rpx 20rpx 28rpx 20rpx
  margin: 30rpx 30rpx
  box-shadow: 0px 3px 20px 0px rgba(0,105,234,0.1)
  border-radius: 12px
.list-top
  display: flex
  align-items: center
  justify-content: space-between
.list-title
  font-weight: bold
  font-size: 36rpx
  color: #404040
  position: relative

.title-line
  position: absolute
  bottom: -10rpx
  left: 70rpx
  width: 70rpx
  height: 8rpx
  background: linear-gradient(90deg, #FFAD58 0%, #FF7A5B 100%)
  border-radius: 4rpx
.title-total
  font-size: 24rpx
  color: #999999
.total-num
  color: #3088FF
  margin-left: 4rpx
  margin-right: 4rpx
  font-weight: bold
  font-size: 26rpx

.list-box
  margin-top: 40rpx

:deep(.uni-forms-item__label)
  width: 194rpx !important
  font-size: 28rpx;
  color: #404040;
  
.search-container
  padding: 20rpx 0rpx 0rpx 0rpx
.title-total
  font-size: 24rpx
  color: #999999
.total-num
  color: #3088FF
  margin-left: 4rpx
  margin-right: 4rpx
  font-weight: bold
  font-size: 26rpx
.search-item
  display: flex
  align-items: center
  margin-bottom: 30rpx

.label
  width: 160rpx
  font-size: 28rpx
  color: #404040
  flex-shrink: 0

.input,
.picker
  background: #FFFFFF
  flex: 1
  min-width: 0
.search-box-btn
  border-radius: 32rpx !important
  background: #3088FF !important
  margin-right: 16rpx
.reset-box-btn
  border-radius: 32rpx !important
  background: #02B44D
  color: #fff
.con-box
  background: #fff
  padding: 20rpx
  box-shadow: 0px 0px 6rpx 0px rgba(0,71,200,0.16)
  border-radius: 24rpx
  border: 1rpx solid #EDF5FF
  margin-top: 30rpx
.form-title
  display: flex
  align-items: center

.form-name
  font-weight: bold
  font-size: 30rpx
  color: #595959
  margin-right:16rpx
.form-type
  border-radius: 8rpx;
  border: 2rpx solid #FF7D26;
  font-size: 24rpx
  color: #F1690E
  padding: 4rpx 10rpx
.form-item
  display: flex
  align-items: center
  justify-content: space-between
  margin-top: 30rpx
.item-left
  display: flex
  align-items: center
.item-img
  width: 26rpx
  height: 26rpx
  margin-right: 10rpx
.item-label
  font-size: 26rpx
  color: #B3B3B3
  width: 190rpx
.item-right
  font-size: 26rpx
  color: #737373
  // overflow: hidden
  // text-overflow: ellipsis
  // white-space: nowrap
  word-wrap: break-word
  overflow-wrap: break-word
  white-space: normal
.form-btns
  margin-top:30rpx
.form-box-btn
  border-radius: 50rpx !important
  margin-right: 24rpx
  padding: 0rpx 40rpx
.detail-btn
  background: #EDF5FF
  border: 1px solid #3088FF
  font-size: 28rpx
  color: #3088FF
.follow-btn
  background: #EEF9F3
  border: 1px solid #00933E
  font-size: 28rpx
  color: #00933E
.recommend-btn
  background: linear-gradient(92deg, #0DCCFF 0%, #4760FF 100%)
  font-size: 28rpx
  color: #FFFFFF
.button-group
	position: fixed
	bottom: 0
	left: 0
	width: 100%
	height: 120rpx
	background: #fff
.btns
  height: 120rpx
  width: 100%
  line-height: 120px
  display: flex
  align-items: center
  justify-content: space-between
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1)
.btn {
  width: 45%;
  height: 80rpx;
  font-size: 30rpx;
  border-radius: 8rpx;
}

.reset-btn {
  background-color: #D8E9FF;
  color: #1176FF;
}

.submit-btn {
  background-color: #368BFF;
  color: white;
}
</style>
