<template>

	<div class="flex-container">
		<!-- 菜单区域 -->
		<div class="menu-area">
			<el-button type="primary" plain @click="createType" style="width: 90;font-size: 12px;">
				<Icon icon="ep:plus" class="mr-5px" /> 新增分类
			</el-button>
			<el-menu :default-active="targetMenuId" style="width:183px">
				<el-menu-item v-for="item in huashuType" :index="item.value" :key="item.value"
					@click="clickMenu(item.value)">{{item.label}}</el-menu-item>
			</el-menu>
		</div>

		<!-- 内容区域 -->
		<div class="content-wrap">


			<ContentWrap>
				<!-- 搜索工作栏 -->
				<el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
					<!-- <el-form-item label="分类" prop="type">	
						<el-select v-model="queryParams.type" placeholder="请选择分类" clearable class="!w-240px">
							<el-option v-for="dict in getIntDictOptions(DICT_TYPE.KEFU_VERBAL_TRICK_TYPE)"
								:key="dict.value" :label="dict.label" :value="dict.value" />
						</el-select>
					</el-form-item> -->
					<el-form-item label="标题" prop="title">
						<el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter="handleQuery"
							class="!w-240px" />
					</el-form-item>
					<el-form-item label="创建时间" prop="createTime">
						<el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss"
							type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
							:default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-240px" />
					</el-form-item>
					<el-form-item>
						<el-button @click="handleQuery">
							<Icon icon="ep:search" class="mr-5px" /> 搜索
						</el-button>
						<el-button @click="resetQuery">
							<Icon icon="ep:refresh" class="mr-5px" /> 重置
						</el-button>
						<el-button type="primary" plain @click="openForm('create')"
							v-hasPermi="['promotion:verbal-trick:create']">
							<Icon icon="ep:plus" class="mr-5px" /> 新增
						</el-button>
						<el-button type="success" plain @click="handleExport" :loading="exportLoading"
							v-hasPermi="['promotion:verbal-trick:export']">
							<Icon icon="ep:download" class="mr-5px" /> 导出
						</el-button>
					</el-form-item>
				</el-form>
			</ContentWrap>

			<!-- 列表 -->
			<ContentWrap>
				<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
					<el-table-column label="ID" align="center" prop="id" />
					<el-table-column label="分类" align="center" prop="type">
						<template #default="scope">
							<dict-tag :type="DICT_TYPE.KEFU_VERBAL_TRICK_TYPE" :value="scope.row.type" />
						</template>
					</el-table-column>
					<el-table-column label="标题" align="center" prop="title" />
					<el-table-column label="详情" align="center" prop="details" />
					<el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter"
						width="180px" />
					<el-table-column label="操作" align="center">
						<template #default="scope">
							<el-button link type="primary" @click="openForm('update', scope.row.id)"
								v-hasPermi="['promotion:verbal-trick:update']">
								编辑
							</el-button>
							<el-button link type="danger" @click="handleDelete(scope.row.id)"
								v-hasPermi="['promotion:verbal-trick:delete']">
								删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>

			</ContentWrap>
			<!-- 分页 -->
			<Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
				@pagination="getList" />
		</div>
	</div>



	<!-- 表单弹窗：添加/修改 -->
	<VerbalTrickForm ref="formRef" @success="getList" />


	<Dialog v-model="dialogVisible" :title="dialogTitle">
		<el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="80px">

			<el-form-item label="数据标签" prop="label">
				<el-input v-model="formData.label" placeholder="请输入数据标签" />
			</el-form-item>
			<el-form-item label="显示排序" prop="sort">
				<el-input-number v-model="formData.sort" :min="0" controls-position="right" />
			</el-form-item>

		</el-form>
		<template #footer>
			<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
			<el-button @click="dialogVisible = false">取 消</el-button>
		</template>
	</Dialog>


</template>

<script setup lang="ts">
	import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
	import { dateFormatter } from '@/utils/formatTime'
	import download from '@/utils/download'
	import { VerbalTrickApi, VerbalTrickVO } from '@/api/mall/promotion/verbaltrick'
	import VerbalTrickForm from './VerbalTrickForm.vue'
	import * as DictDataApi from '@/api/system/dict/dict.data'
	const huashuType = ref<DictDataApi.DictDataVO[]>([]) // 
	const targetMenuId = ref('0')

	const dialogVisible = ref(false) // 弹窗的是否展示
	const dialogTitle = ref('新增分类') // 弹窗的标题
	const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
	const formData = ref({
		id: undefined,
		sort: undefined,
		label: '',
		value: '',
		dictType: '',
		// status: CommonStatusEnum.ENABLE,
		colorType: '',
		cssClass: '',
		remark: ''
	})
	const formRules = reactive({
		label: [{ required: true, message: '数据标签不能为空', trigger: 'blur' }],
		sort: [{ required: true, message: '数据顺序不能为空', trigger: 'blur' }],
	})

	/** 客服话术 列表 */
	defineOptions({ name: 'VerbalTrick' })

	const message = useMessage() // 消息弹窗
	const { t } = useI18n() // 国际化

	const loading = ref(true) // 列表的加载中
	const list = ref<VerbalTrickVO[]>([]) // 列表的数据
	const total = ref(0) // 列表的总页数
	const queryParams = reactive({
		pageNo: 1,
		pageSize: 10,
		type: '',
		title: undefined,
		details: undefined,
		createTime: []
	})
	const queryFormRef = ref() // 搜索的表单
	const exportLoading = ref(false) // 导出的加载中

	/** 查询列表 */
	const getList = async () => {
		loading.value = true
		try {
			const data = await VerbalTrickApi.getVerbalTrickPage(queryParams)
			list.value = data.list
			total.value = data.total
		} finally {
			loading.value = false
		}
	}

	//新增分类
	const createType = () => {
		dialogVisible.value = true
	}

	/** 提交表单 */
	const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
	const submitForm = async () => {
		// 校验表单
		if (!formRef) return
		const valid = await formRef.value.validate()
		if (!valid) return
		// 提交请求
		formLoading.value = true
		try {
			const data = formData.value as DictDataApi.DictDataVO
			await DictDataApi.createType(data)
			message.success(t('common.createSuccess'))
			dialogVisible.value = false
			// 发送操作成功的事件
			emit('success')
			queryParams.type = targetMenuId.value
			getList()
			getHuaShuTypeList()
		} finally {
			formLoading.value = false
		}
	}


	const clickMenu = (id : string) => {
		console.log('1111111111', id)
		queryParams.type = id
		getList()
	}

	//话术分类菜单
	const getHuaShuTypeList = async () => {
		const data = await DictDataApi.getHuaShuTypeList()
		huashuType.value = data
	}

	/** 搜索按钮操作 */
	const handleQuery = () => {
		queryParams.pageNo = 1
		getList()
	}

	/** 重置按钮操作 */
	const resetQuery = () => {
		queryFormRef.value.resetFields()
		handleQuery()
	}

	/** 添加/修改操作 */
	const formRef = ref()
	const openForm = (type : string, id ?: number) => {
		formRef.value.open(type, id)
	}

	/** 删除按钮操作 */
	const handleDelete = async (id : number) => {
		try {
			// 删除的二次确认
			await message.delConfirm()
			// 发起删除
			await VerbalTrickApi.deleteVerbalTrick(id)
			message.success(t('common.delSuccess'))
			// 刷新列表
			await getList()
		} catch { }
	}

	/** 导出按钮操作 */
	const handleExport = async () => {
		try {
			// 导出的二次确认
			await message.exportConfirm()
			// 发起导出
			exportLoading.value = true
			const data = await VerbalTrickApi.exportVerbalTrick(queryParams)
			download.excel(data, '客服话术.xls')
		} catch {
		} finally {
			exportLoading.value = false
		}
	}

	/** 初始化 **/
	onMounted(() => {
		queryParams.type = targetMenuId.value
		getList()
		getHuaShuTypeList()
	})
</script>

<style scoped>
	.flex-container {
		height: 100vh;
		/* 使容器填满视口高度 */
		display: flex;
	}

	.menu-area {
		padding: 25px;
		background-color: white;
		width: 180px;
		height: 1000px;
		/* 固定高度 */
		overflow-y: hidden;
		/* 禁止滚动 */
		border-right: 1px solid #e0e0e0;
		/* 可选：添加分隔线 */
	}

	.content-wrap {
		flex-grow: 1;
		/* 使内容区域占据剩余空间 */
		overflow-y: auto;
		/* 允许内容区域滚动 */
	}
</style>