<template>
	<div>
		<el-drawer v-model="state.showDrawer" size="35%" title="" :show-close="false" :modal="props.modal">
			<template #header="{ close }">
				<div class="comments-header">
					<div>
						<el-button link @click="state.active = 'default'" :class="{ active: state.active === 'default' }"> 常用意见 </el-button>
						<el-button link @click="state.active = 'add'" :class="{ active: state.active === 'add' }"> 新增意见 </el-button>
						<span></span>
					</div>
				</div>
				<div class="flex-center-align">
					<el-button link @click="state.manage = true" v-if="!state.manage && state.active === 'default' && state.commentsList.length"
						>管理</el-button
					>
					<el-button link @click="state.manage = false" type="primary" v-if="state.manage && state.active === 'default'">返回</el-button>
					<el-button link @click="close">
						<SvgIcon name="ele-Close" class="mr5" @click="close" size="16px" />
					</el-button>
				</div>
			</template>
			<div class="comments-container" v-loading="state.loading">
				<template v-if="state.commentsList.length">
					<!-- 默认状态 -->
					<template v-if="state.active === 'default' && !state.manage">
						<ul class="comments-box">
							<li class="comments-item" v-for="(item, index) in state.commentsList" :key="index" @click="chooseComment(item)">
								<p>{{ item.content }}</p>
							</li>
						</ul>
					</template>
					<!-- 管理 -->
					<template v-if="state.active === 'default' && state.manage">
						<ul class="comments-box">
							<li
								class="comments-item"
								v-for="(item, index) in state.commentsList"
								:class="[item.ischeck === true ? 'choosed' : '']"
								:key="index"
								@click="handelRepeat(item, index)"
							>
								<p>{{ item.content }}</p>
								<el-checkbox v-model="item.ischeck" class="check-icon" label="" v-if="item.ischeck" size="large" />
							</li>
						</ul>
					</template>
				</template>
				<template v-if="!state.commentsList.length && state.active !== 'add'">
					<Empty description="暂无常用语" />
				</template>
				<!-- 新增意见 -->
				<template v-if="state.active === 'add'">
					<div>
						<el-form :model="state.commentsForm" ref="commentsFormRef">
							<el-form-item label="" prop="content" :rules="[{ required: true, message: '请填写新增常用意见', trigger: 'blur' }]">
								<el-input
									v-model="state.commentsForm.content"
									type="textarea"
									:autosize="{ minRows: 10, maxRows: 10 }"
									placeholder="请填写新增常用意见"
									clearable
								>
								</el-input>
							</el-form-item>
						</el-form>
					</div>
				</template>
			</div>
			<template #footer v-if="state.active === 'add' || state.manage">
				<div style="flex: auto">
					<el-button @click="deleteComment" class="default-button" v-if="state.active === 'default' && state.manage">删 除</el-button>
					<el-button type="primary" @click="commentSave" v-if="state.active === 'add'">保 存</el-button>
				</div>
			</template>
		</el-drawer>
	</div>
</template>

<script setup lang="ts" name="orderCrculationRecord">
import { reactive, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { commonList, addCommon, deleteCommon } from '/@/api/business/commonP';
const emit = defineEmits(['chooseComment']);
const props = defineProps({
	modal: {
		type: Boolean,
		default: false,
	},
});
// 定义变量内容
const state = reactive<any>({
	showDrawer: false,
	commentsList: [],
	commentsForm: {
		content: '',
	},
	chooseCommentList: [],
	active: 'default',
	manage: false,
	loading: false,
	typecode: '',
});
const commentsFormRef = ref();
// 打开弹窗
const openDialog = async (val: any) => {
	state.typecode = val;
	state.active = 'default';
	commentsFormRef.value?.resetFields();
	state.manage = false;
	getList(val);
	state.showDrawer = true;
};
const getList = async (val: any) => {
	try {
		state.loading = true;
		const res: any = await commonList({ typecode: val });
		state.commentsList = res.result;
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
};
// 选中常用意见
const handelRepeat = (item: any, index: number) => {
	const repeatData = [...state.commentsList];
	const repeatSelarr = [...state.chooseCommentList];
	if (!repeatData[index].ischeck) {
		repeatData[index].ischeck = true;
		repeatSelarr.push(item);
	} else {
		repeatData[index].ischeck = false;
		if (!repeatSelarr || repeatSelarr.length == 0) {
			return '';
		}
		repeatSelarr.splice(index, 1);
	}
	state.chooseCommentList = repeatSelarr.filter((item: any) => item.ischeck);
};
const resetState = () => {
	state.active = 'default';
	state.manage = false;
	state.chooseCommentList = [];
	state.commentsForm.content = '';
	getList(state.typecode);
};
// 删除常用意见
const deleteComment = () => {
	if (!state.chooseCommentList.length) {
		ElMessage.warning('请选择要删除的常用意见');
		return;
	}
	ElMessageBox.confirm(`确定要删除选中的常用意见，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		draggable: true,
		cancelButtonClass: 'default-button',
		type: 'warning',
	})
		.then(() => {
			const chooseCommentList = state.chooseCommentList.map((item: any) => {
				return item.id;
			});
			deleteCommon({ ids: chooseCommentList }).then(() => {
				ElMessage.success('操作成功');
				resetState();
				state.loading = false;
			});
		})
		.catch(() => {});
};
// 添加常用意见 保存
const commentSave = () => {
	commentsFormRef.value.validate((valid: boolean) => {
		if (valid) {
			state.loading = true;
			addCommon({
				typeCode: state.typecode,
				content: state.commentsForm.content,
			}).then(() => {
				ElMessage.success('操作成功');
				resetState();
				state.loading = false;
			});
		} else {
			return false;
		}
	});
};
// 选择常用意见 填入填写框
const chooseComment = (item: any) => {
	emit('chooseComment', item);
};
// 关闭弹窗
const closeDialog = () => {
	state.showDrawer = false;
};
// 暴露变量
defineExpose({
	openDialog,
	closeDialog,
});
</script>
<style lang="scss" scoped>
.comments-container {
	.comments-box {
		.comments-item {
			border: var(--el-border);
			border-radius: var(--el-border-radius-base);
			margin-bottom: 10px;
			padding: 8px 15px;
			cursor: pointer;
			position: relative;
			&:hover {
				box-shadow: 0 0 0 1px var(--el-color-primary) inset;
				background-color: var(--hotline-bg-main-color);
			}
			p {
				display: -webkit-box;
				text-overflow: ellipsis;
				overflow: hidden;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
			}
			.check-icon {
				position: absolute;
				right: 0;
				top: -13px;
			}
		}
		.choosed {
			box-shadow: 0 0 0 1px var(--el-color-primary) inset;
		}
	}
}
.comments-header {
	.active {
		color: var(--el-color-primary);
		font-size: var(--el-font-size-medium);
	}
}
:deep(.el-drawer__footer) {
	box-shadow: 0px -4px 10px 0px rgb(0 0 0 / 10%);
	border-radius: 0px 0px 8px 0px;
	padding: 10px 20px;
}
</style>
