<template>
	<div class="trade-operation-setting-anchor">
		<el-card shadow="never" class="search-card">
			<el-form :inline="true" :model="searchForm" class="mb20">
				<el-form-item label="主播姓名">
					<el-input v-model="searchForm.name" placeholder="请输入主播姓名" clearable />
				</el-form-item>
				<el-form-item label="联系电话">
					<el-input v-model="searchForm.phone" placeholder="请输入联系电话" clearable />
				</el-form-item>
				<el-form-item label="工号">
					<el-input v-model="searchForm.code" placeholder="请输入工号" clearable />
				</el-form-item>
				<el-form-item label="状态">
					<el-select v-model="searchForm.status" placeholder="请选择状态" clearable size="default" style="width: 160px">
						<el-option label="正常" :value="1" />
						<el-option label="停用" :value="0" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleSearch" :loading="tableData.loading">
						<el-icon><Search /></el-icon>
						查询
					</el-button>
					<el-button @click="handleReset" :disabled="tableData.loading">
						<el-icon><Refresh /></el-icon>
						重置
					</el-button>
					<el-button type="success" @click="onOpenAdd">
						<el-icon><Plus /></el-icon>
						新增
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card shadow="never" class="mt20">
			<el-table v-loading="tableData.loading" :data="tableData.data" style="width: 100%">
				<el-table-column prop="id" label="ID" width="200" />
				<el-table-column prop="name" label="主播姓名" />
				<el-table-column prop="phone" label="联系电话" />
				<el-table-column prop="code" label="工号" />
				<el-table-column prop="statusName" label="状态" width="100" />
				<el-table-column prop="remark" label="备注" show-overflow-tooltip />
				<el-table-column prop="createdAt" label="创建时间" width="180">
					<template #default="{ row }">
						{{ formatTimestamp(row.createdAt) }}
					</template>
				</el-table-column>
				<el-table-column prop="updatedAt" label="更新时间" width="180">
					<template #default="{ row }">
						{{ formatTimestamp(row.updatedAt) }}
					</template>
				</el-table-column>
				<el-table-column label="操作" width="150" fixed="right">
					<template #default="{ row }">
						<el-button size="small" type="primary" text @click="onOpenEdit(row)">
							<el-icon><EditPen /></el-icon>修改
						</el-button>
						<el-button size="small" type="danger" text @click="handleDelete(row)">
							<el-icon><Delete /></el-icon>删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>

			<el-pagination
				v-model:current-page="tableData.param.pageNum"
				v-model:page-size="tableData.param.pageSize"
				:page-sizes="[10, 20, 30, 50]"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				class="mt20"
			/>
		</el-card>

		<EditAnchor ref="editAnchorRef" @refresh="getList" />
	</div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Search, Refresh, Plus, EditPen, Delete } from '@element-plus/icons-vue';
import { getAnchorList, deleteAnchor } from '/@/api/trade/operation/setting/anchor';
import EditAnchor from './component/editAnchor.vue';

interface TableDataItem {
	id: string;
	name: string;
	phone: string;
	code: string;
	status: number;
	statusName: string;
	remark: string;
	createdAt: number;
	updatedAt: number;
}

const searchForm = reactive({
	name: '',
	phone: '',
	code: '',
	status: undefined as number | undefined,
});

const tableData = reactive({
	loading: false,
	data: [] as TableDataItem[],
	total: 0,
	param: {
		pageNum: 1,
		pageSize: 10,
	},
});

const editAnchorRef = ref();

const handleSearch = () => {
	tableData.param.pageNum = 1;
	getList();
};

const handleReset = () => {
	searchForm.name = '';
	searchForm.phone = '';
	searchForm.code = '';
	searchForm.status = undefined;
	tableData.param.pageNum = 1;
	getList();
};

const handleSizeChange = (size: number) => {
	tableData.param.pageSize = size;
	getList();
};

const handleCurrentChange = (current: number) => {
	tableData.param.pageNum = current;
	getList();
};

const formatTimestamp = (timestamp: number) => {
	if (!timestamp) return '';
	const date = new Date(timestamp * 1000);
	return date.toLocaleString('zh-CN');
};

const getList = async () => {
	try {
		tableData.loading = true;
		const res = await getAnchorList({
			...tableData.param,
			name: searchForm.name || undefined,
			phone: searchForm.phone || undefined,
			code: searchForm.code || undefined,
			status: searchForm.status,
		});
		if (res && res.data) {
			tableData.data = (res.data.list || []).map((item: any) => ({
				...item,
				id: String(item.id),
			}));
			tableData.total = res.data.total || 0;
		}
	} catch (error) {
		// 错误已由全局拦截器处理
	} finally {
		tableData.loading = false;
	}
};

const onOpenAdd = () => {
	editAnchorRef.value?.openDialog();
};

const onOpenEdit = (row: TableDataItem) => {
	editAnchorRef.value?.openDialog(row);
};

const handleDelete = async (row: TableDataItem) => {
	try {
		await ElMessageBox.confirm(`确定要删除主播 "${row.name}" 吗？`, '提示', {
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			type: 'warning',
		});
		await deleteAnchor({ id: String(row.id) });
		ElMessage.success('删除成功');
		getList();
	} catch (error) {
		if (error !== 'cancel') {
			// 错误已由全局拦截器处理
		}
	}
};

onMounted(() => {
	getList();
});
</script>

<style scoped lang="scss">
.trade-operation-setting-anchor {
	padding: 20px;
}

.mb20 {
	margin-bottom: 20px;
}

.mt20 {
	margin-top: 20px;
}
</style>
