// 回收站（平台视角：全量软删除记录；用户内容本人也能在自己的「我的」页恢复）
const { useState: RBS, useEffect: RBE } = React;
const {
	Card: RBCd, Table: RBT, Select: RBSl, Popconfirm: RBP,
	message: RBMg, Space: RBSp, Tag: RBTg,
} = antd;

NR.Pages.RecyclePage = function RecyclePage() {
	const [list, setList] = RBS([]);
	const [loading, setLoading] = RBS(false);
	const [entity, setEntity] = RBS("");

	const load = async () => {
		setLoading(true);
		try { const d = await NR.api("recycle_list"); setList(d.list || []); }
		catch (e) { RBMg.error(e.message); }
		finally { setLoading(false); }
	};
	RBE(() => { load(); }, []);

	const restore = async (r) => {
		try {
			await NR.api("recycle_restore", { bin_id: r.bin_id }, "POST");
			RBMg.success("已恢复");
			setList(ls => ls.filter(x => x.bin_id !== r.bin_id));
		} catch (e) { RBMg.error(e.message); }
	};
	const forget = async (r) => {
		try {
			await NR.api("recycle_forget", { bin_id: r.bin_id }, "POST");
			RBMg.success("已彻底删除");
			setList(ls => ls.filter(x => x.bin_id !== r.bin_id));
		} catch (e) { RBMg.error(e.message); }
	};

	const TYPES = { conv: "💬 会话", doc: "📚 资料", task: "🔁 复刻任务", viral: "📰 爆文", viral_img_group: "🖼 爆图组", skill: "🧩 技能" };
	const filtered = entity ? list.filter(x => x.entity === entity) : list;
	const columns = [
		{ title: "ID", dataIndex: "bin_id", width: 60 },
		{ title: "类型", dataIndex: "entity", width: 100, render: v => <RBTg>{TYPES[v] || v}</RBTg> },
		{ title: "内容", dataIndex: "title", ellipsis: true },
		{
			title: "归属", dataIndex: "user_id", width: 110, render: (v, r) =>
				Number(v) > 0
					? <RBTg>用户#{v}</RBTg>
					: <RBTg color="blue">平台内容</RBTg>,
		},
		{ title: "删除时间", dataIndex: "del_time", width: 150, render: v => dayjs(v * 1000).format("YYYY-MM-DD HH:mm") },
		{ title: "剩余", dataIndex: "del_time", width: 80, render: v => Math.max(0, 30 - Math.floor((Date.now() / 1000 - v) / 86400)) + " 天" },
		{
			title: "操作", width: 140, render: (_, r) => (
				<RBSp>
					<RBP title="恢复到原位置，确定？" okText="确定" cancelText="取消" onConfirm={() => restore(r)}><a>恢复</a></RBP>
					<RBP title="彻底删除将无法恢复，确定？" okText="确定" cancelText="取消" onConfirm={() => forget(r)}><a style={{ color: "#f5222d" }}>彻底删除</a></RBP>
				</RBSp>
			),
		},
	];

	return (
		<RBCd size="small" title="🗑 回收站（全平台软删除记录；删除满 30 天系统自动物理清除）"
			extra={
				<RBSl size="small" allowClear placeholder="全部类型" style={{ width: 130 }} value={entity || undefined} onChange={v => setEntity(v || "")}
					options={Object.keys(TYPES).map(k => ({ value: k, label: TYPES[k] }))} />
			}>
			<RBT rowKey="bin_id" size="small" columns={columns} dataSource={filtered} loading={loading}
				pagination={{ pageSize: 15, showTotal: t => "共 " + t + " 条" }} locale={{ emptyText: "回收站是空的" }} />
		</RBCd>
	);
};
