<template>
	<div>
		<div class="head">
			<div class="head">
				<el-row>
					<el-form-item>
						<el-date-picker style="margin-left: 8px; width: 300px" v-model="searchInfo.dateRange"
							type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
							value-format="YYYY-MM-DD" />
					</el-form-item>
					<!-- <el-form-item>
						<el-input v-model="searchInfo.name" placeholder="请输入类型名称" style="margin-left: 10px;" />
					</el-form-item>
					<el-button type="primary" @click="GetList()" style="margin-left: 10px;">查询</el-button> -->
					<el-button type="success" @click="Add()" style="margin-left: 10px;">添加</el-button>
				</el-row>
			</div>
		</div>
		<el-table :data="tableData" style="width: 100%;" row-key="id" v-loading="loading">
			<el-table-column prop="id" label="Id" width="100" v-if="false" />
			<el-table-column prop="name" label="名称" />
			<el-table-column prop="" label="操作" width="150">
				<template #default="scope">
					<el-button type="primary" @click="Edit(scope.row)" size="small">修改</el-button>
					<el-button type="danger" @click="Del(scope.row.id)" size="small">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-dialog v-model="dialogVisible" title="适应人群设置">
			<div class="chuansuokuang" v-loading="loading">
				<el-form :model="Info" label-width="80" style="max-width: 600px">
					<el-form-item label="名称">
						<el-input v-model="Info.name" />
					</el-form-item>
					 
				</el-form>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="Save()">
						确定
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'
	import {
		GetMainList,
		GetServiceDateTime
	} from '@/api/api.js'
	let loading = ref(false)
	let searchInfo = ref({})
	let tableData = ref([])
	let currentPage = ref(1) //当前页码
	let pageSize = ref(15) //每页数量
	let total = 0 //总数量
	const PageSizeChange = (e) => { // 修改每页数量
		pageSize.value = e
		GetList()
	}
	const PageCurrentChange = (e) => { //切换页码
		currentPage.value = e
		GetList()
	}
 
	const GetList = () => {
		loading.value = true
		GetMainList({
			searchInfo: searchInfo.value,
			page: currentPage.value,
			pageSize: pageSize.value
		}).then(res => {
			loading.value = false
			if (res.status) {
				tableData.value = res.data.list
				total = res.data.count
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	let Info = ref({});
	let dialogVisible = ref(false);
	const Add = () => {
		Info.value = {}
		Info.value.id = 0
		Info.value.status = 1
		dialogVisible.value = true
		
	}
	const Save = () => {
		loading.value = true
		ComboCrowdSave({
			Info: Info.value
		}).then(res => {
			loading.value = false
			if (res.status) {
				dialogVisible.value = false
				GetList()
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	const Edit=(row)=>{
		dialogVisible.value = true
		Info.value.name =row.name
		Info.value.id = row.id
	}
	//获取详情
	const GetDetailFunc=(id)=>{
		loading.value = true
		PlanTypeGetDetail({
			id: id
		}).then(res => {
			loading.value = false
			if (res.status) {
			  Info.value=res.data
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	const Del=(id)=>{
		loading.value = true
		ComboCrowdDel({
			id: id
		}).then(res => {
			loading.value = false
			if (res.status) {
			  GetList()
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	//获取服务器时间
	const GetServiceDate = () => {
		GetServiceDateTime().then(res => {
			if (res.status) {
				let datetime = res.data.datetime.substr(0, 10)
				searchInfo.value.dateRange = [datetime, datetime]
				GetList()
			}
		})
	}
	onMounted(() => {
		GetList()
		 GetServiceDate()
	})
</script>

<style scoped>
	.head{
		margin: 10px auto;
	}
</style>