<template>
	<div>
		<div class="head">
			<el-row>
				<el-form-item>
					<el-button type="success" style="margin-left: 10px;"  @click="AddClick()">新建文章</el-button>
				</el-form-item>
			</el-row>
		 <el-row>
		 	<el-form-item>
		 		文章类型
		 		<el-radio-group style="margin-left: 8px;" v-model="searchInfo.ArticleType"
		 			@change="ArticleTypeChange()">
		 			<el-radio-button :label="1">健康指导</el-radio-button>
		 			<el-radio-button :label="2">注意事项</el-radio-button>
		 		</el-radio-group>
		 	</el-form-item>
		 </el-row>
		</div>
		<el-table :data="tableData" style="width: 100%;" row-key="id" v-loading="loading">
			<el-table-column prop="title" label="题目" />
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					<el-tag v-if="scope.row.status==1" class="ml-2" type="success">正常</el-tag>
					<el-tag v-if="scope.row.status==0" class="ml-2" type="danger">关闭</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="author" label="作者" />
			<el-table-column prop="order" label="排序" />
			<el-table-column prop="show_date" label="显示时间" />
			<el-table-column label="操作">
				<template #default="scope">
					<el-button type="primary" @click="EditClick(scope.row)" :icon="Edit" circle />
					<el-button type="danger" @click="Del(scope.row)" :icon="Delete" circle />
				</template>
			</el-table-column>
		</el-table>
		<div class="page">
			<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"
				:page-sizes="[15, 50, 100, 200]" layout="total,sizes, prev, pager, next" :total="total"
				@size-change="PageSizeChange" @current-change="PageCurrentChange" />
		</div>
		<el-dialog v-model="dialogVisible" title="套餐信息" width="60%">
			<el-form :model="Info" label-width="100px" v-loading="loading">
				<el-form-item label="名称">
					{{Info.name}}
				</el-form-item>
				<el-form-item label="Logo">
					<el-upload class="avatar-uploader" :action="upfileurl" :headers="headerObj"
						:show-file-list="false" :on-success="upLoadSuccess">
						<img v-if="logo" :src="logo" class="avatar" style="width: 60px;" />
						<el-icon v-else class="avatar-uploader-icon">
							<Plus />
						</el-icon>
					</el-upload>
				</el-form-item>
				<el-form-item label="体检类型">
					<el-checkbox-group v-model="Info.checkup_type_id">
						<el-select :filterable="true" clearable v-model="Info.checkup_type_id" placeholder="选择体检类型">
							<el-option v-for="(item,index) in CheckUpTypeEnableList" :key="index" :label="item.name"
								:value="item.id" />
						</el-select>
					</el-checkbox-group>
				</el-form-item>
				<el-form-item label="套餐类型">
					<el-select :filterable="true" clearable v-model="Info.type_id" placeholder="选择套餐类型">
						<el-option v-for="(item,index) in ComboTypeList" :key="index" :label="item.name"
							:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="适应人群">
					<el-select :filterable="true" clearable v-model="Info.crowd_id" placeholder="选择适应人群">
						<el-option v-for="(item,index) in ComboCrowdList" :key="index" :label="item.name"
							:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="标签">
					<el-input v-model="Info.tags" type="textarea" />
				</el-form-item>
				<el-form-item label="标签2">
					<el-input v-model="Info.tags2" type="textarea" />
				</el-form-item>
				<el-form-item label="简介">
					<el-input v-model="Info.intro" type="textarea" />
				</el-form-item>
				<el-form-item label="服务承诺">
					<el-input v-model="Info.sub_intro" type="textarea" />
				</el-form-item>
				<el-form-item label="详情">
					<div style="border: 1px solid #ccc">
						<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef"
							:defaultConfig="toolbarConfig" :mode="mode" />
						<Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml"
							:defaultConfig="editorConfig" :mode="mode" @onCreated="handleCreated" />
					</div>
				</el-form-item>
			</el-form>
			<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 '@wangeditor/editor/dist/css/style.css' // 引入 css
	import {
		onBeforeUnmount,
		ref,
		shallowRef,
		onMounted
	} from 'vue'
	import {
		Editor,
		Toolbar
	} from '@wangeditor/editor-for-vue'
	import {
		ElMessage
	} from 'element-plus'
	import {
		UpFileUrl,
		ArticleGetList
	} from '@/api/api.js'
	import {
		Edit,
		Top,
		Bottom,Delete
	} from '@element-plus/icons-vue'
	let loading = ref(false)
	let tableData = ref([])
	let currentPage = ref(1) //当前页码
	let pageSize = ref(15) //每页数量
	let total = 0 //总数量
	let dialogVisible = ref(false)
	const PageSizeChange = (e) => { // 修改每页数量
		pageSize.value = e
		GetList()
	}
	const PageCurrentChange = (e) => { //切换页码
		currentPage.value = e
		GetList()
	}
	let upfileurl = UpFileUrl()
	let headerObj = {
		Authorization: 'Bearer ' + sessionStorage.getItem("token")
	}
	let logo = ref('');
	//上传成功钩子
	let BaseUrl = ref(import.meta.env.VITE_APP_FILE)
	const upLoadSuccess = (res) => {
		logo.value = BaseUrl.value + res.data
		Info.value.cover = res.data
	}
	let searchInfo = ref({});
	let Info = ref({});
	const GetList = () => {
		loading.value = true
		ArticleGetList({
			page: currentPage.value,
			pageSize: pageSize.value,
			searchInfo: searchInfo.value
			
		}).then(res => {
			loading.value = false
			if (res.status) {
				tableData.value = res.data.list
				total = res.data.count
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	
	
	//富文本编辑器
	// 编辑器实例，必须用 shallowRef
	const editorRef = shallowRef()
	
	// 内容 HTML
	const valueHtml = ref(null)
	const toolbarConfig = {}
	const editorConfig = {
		placeholder: '请输入内容...',
		MENU_CONF: {},
	}
	let mode = 'default' // 'simple\default'
	
	
	// 修改 uploadImage 菜单配置
	editorConfig.MENU_CONF['uploadImage'] = {
		server: upfileurl,
		fieldName: 'WangEditorImageFile',
		headers: headerObj,
		// 继续写其他配置...
	
		//【注意】不需要修改的不用写，wangEditor 会去 merge 当前其他配置
	}
	
	// 组件销毁时，也及时销毁编辑器
	onBeforeUnmount(() => {
		const editor = editorRef.value
		if (editor == null) return
		editor.destroy()
	})
	
	const handleCreated = (editor) => {
		editorRef.value = editor // 记录 editor 实例，重要！
	}
	const ArticleTypeChange = () => {
		GetList()
	}
	const AddClick=()=>{
		dialogVisible.value = true
	}
	const EditClick = () => {
		dialogVisible.value = true
	}
	onMounted(() => {
		searchInfo.value.ArticleType = 1
		GetList() 
	})
</script>

<style>
</style>