<template>
	<div>
		<el-button type="primary" round @click="openAddDialog" size="small">添加</el-button>
		<el-row>
			<el-col :span="24">
				<el-form :inline="true" :model="state.query" class="demo-form-inline">
					<el-form-item label="图书名称：">
						<el-input v-model="state.query.title" placeholder="请输入图书名称" clearable @input="init" />
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>
		<!--表格-->
		<el-row>
			<el-col :span="24">
				<el-tabs v-model="state.query.status" class="demo-tabs" @tab-click="handleClick">
					<el-tab-pane v-for="item in state.getStatus" :label="item.name" :name="item.status">
						<el-table v-loading="loading"  :data="state.getList">
							<!--					列表结开始-->
							<el-table-column prop="id" label="编号" align="center" />
							<el-table-column prop="title" label="名称" align="center" />
							<el-table-column prop="categoryName" label="类别" align="center" />
							<el-table-column prop="largePic" label="图片" align="center" >
								<template #default="{ row }">
									<!-- 点击图片后显示弹框预览 -->
									<el-image
										style="width: 50px;height: 50px"
										:src="row.image"
										fit="cover"
										:preview-src-list="[row.image]"
										:preview-teleported="true"
									/>
								</template>
							</el-table-column>
							<el-table-column prop="price" label="价格" align="center" />

							<el-table-column prop="stockQuantity" label="库存数量" align="center" />
							<el-table-column prop="view" label="预览" align="center" />
							<el-table-column prop="tag" label="作者" align="center" />
							<el-table-column prop="sort" label="排序" align="center" />

							<!--列表结束-->
							<el-table-column label="操作" align="center" >
								<template #default="scope">
									<el-button @click="edit(scope.row)" size="small">编辑</el-button>
									<el-button @click="del(scope.row.id)" type="danger" size="small">删除</el-button>
								</template>
							</el-table-column>
							<template v-slot:empty>
								<el-empty description="数据去外太空了~" />
							</template>
						</el-table>
					</el-tab-pane>
				</el-tabs>
				<!-- 分页控件 -->
				<el-pagination
					v-if="state.query.total > 0"
					:current-page="state.query.page"
					:page-size="state.query.limit"
					:total="state.query.total"
					background layout="prev, pager, next"
					@current-change="handlePageChange"
				/>
			</el-col>
		</el-row>
		<!-- 新增/编辑对话框 -->
		<el-dialog v-model="state.dialogVisible" title="新增" width="50%">
			<el-form
				:model="state.formData"
				ref="formRef"
				label-width="100px"
			>

				<!--				表单开始===============================================================================================================================-->
				<el-form-item label="图片" prop="image" :rules="[{ required: true, message: '请插入图片', trigger: 'blur' }]">
						<image-upload @update:imageUrl="handleImageUrl"  :image-url="state.formData.image"></image-upload>
				</el-form-item>
				<el-form-item label="图书名称" prop="title" :rules="[{ required: true, message: '请输入图书名称', trigger: 'blur' }]">
					<el-input v-model="state.formData.title"/>
				</el-form-item>
				<el-form-item label="作者" prop="tag" :rules="[{ required: true, message: '请输入作者', trigger: 'blur' }]">
					<el-input v-model="state.formData.tag"/>
				</el-form-item>

				<el-form-item label="类别" prop="categoryId" :rules="[{ required: true, message: '请输入类别编号', trigger: 'blur' }]">
					<el-select v-model="state.formData.categoryId" placeholder="请选择" style="width: 240px">
						<el-option
							v-for="item in state.cateList"
							:key="item.id"
							:label="item.name"
							:value="item.id"
						/>
					</el-select>
				</el-form-item>



				<el-form-item label="价格" prop="price" :rules="[{ required: true, message: '请输入价格', trigger: 'blur' }]">
					<el-input v-model.number="state.formData.price"/>
				</el-form-item>

				<el-form-item label="库存数量" prop="stockQuantity" :rules="[{ required: true, message: '请输入商品库存数量', trigger: 'blur' }]">
					<el-input v-model="state.formData.stockQuantity"/>
				</el-form-item>


				<el-form-item label="排序" prop="sort" :rules="[{ required: true, message: '请输入排序', trigger: 'blur' }]">
					<el-input v-model="state.formData.sort"/>
				</el-form-item>

					<el-form-item label="图书状态" prop="categoryId" :rules="[{ required: true, message: '请输入图书状态', trigger: 'blur' }]">
                					<el-select v-model="state.formData.status" placeholder="请选择" style="width: 240px">
                						<el-option
                							v-for="item in state.getStatus"
                							:key="item.name"
                							:label="item.name"
                							:value="item.status"
                						/>
                					</el-select>
                				</el-form-item>


				<el-form-item label="描述" prop="description" :rules="[{ required: true, message: '请输入描述', trigger: 'blur' }]">
					<e-editor  :content="state.formData.description" @update:content="handleUrl" />
				</el-form-item>
				<!--表单结束===============================================================================================================================-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="state.dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="saveTransaction(formRef)">保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script setup lang="ts">
// 记录数据
import type {FormInstance, TabsPaneContext} from 'element-plus'
const formRef = ref<FormInstance>()

const loading = ref(true)
const state = reactive(<any>{
	route:"sys/item",
	getStatus:[
		{name:"上架",status:0},
		{name:"下架",status:1},
		{name:"库存警告",status:2},
		{name:"售空",status:3},
	],
	dialogVisible:false,
	getList: [],//获取数据
	query:{
		total: 0, // 总记录数
		page: 1, // 当前页码
		limit: 5, // 每页显示的记录数
		title:"",
		status:0,
	},
	formData:{},
	cateList:[]
})

// 接收子组件传递的路径
function handleImageUrl(path: string) {
	state.formData.image = path

}
// 接收子组件传递的路径
function handleUrl(content: string) {
	state.formData.description = content
}
// 获取交易记录
const init = () => {
	adminRequest.get(`${state.route}/page`, {
		params: state.query
	}).then((res:any) => {
		state.getList = res.data.list
		state.query.total = res.data.total
	}).finally(() => {
		loading.value = false
	})

	adminRequest.get(`sys/categories/page`, {
		params: state.query
	}).then((res:any) => {
		state.cateList = res.data.list
	})


}
// 新增交易记录
const openAddDialog = () => {
	state.formData = {
		sort:1
	}
	state.dialogVisible = true
}
// 编辑交易记录
const edit = (row: any) => {
	state.formData = row
	state.dialogVisible = true
}
// 保存记录
const saveTransaction = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	formEl.validate((valid) => {
		if (valid) {
			if (state.formData.id) {
				// 更新交易记录
				  adminRequest.put(`${state.route}`, state.formData).then(() =>{
						init()
						state.dialogVisible = false
						ElMessage.success("提交成功~")
					})
			} else {
				// 新增交易记录
				adminRequest.post(`${state.route}`, state.formData).then(() =>{
					init()
					state.dialogVisible = false
					ElMessage.success("提交成功~")
				})
			}
		}
	})
}
// 删除交易记录
const del = async (id: number) => {
	try {
		await adminRequest.delete(`${state.route}/${id}`)
		init()
		ElMessage.success("删除成功~")
	} catch (error) {
		ElMessage.error("删除失败~")
	}
}

// 页码改变时的处理函数
const handlePageChange = (page: number) => {
	state.query.page = page
	init()
}

const handleClick = (tab: TabsPaneContext, event: Event) => {
	// 根据 tab 的 name 属性更新状态
	const selectedStatus = state.getStatus[tab.index].status;
	state.query.status = selectedStatus;
	init();
};

// 页面加载时获取交易记录
onMounted(() => {
	init()
})
</script>
<style scoped>
.dialog-footer {
	text-align: right;
}
:deep(.el-upload) {
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}

:deep(.el-upload:hover) {
	border-color: var(--el-color-primary);
}

</style>
<route lang="json">
{
"meta": {
"layout": "admin"
}
}
</route>
