<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>
	import { openSqlite,closedb,getPageList,updateSql} from "@/utils/database";
	export default {
		data() {
			return {
				title: 'History',
				tableName:"scancode",
				options:{
				    current:1,  // 分页默认为第1页
				    size:10,
				},
				list:[],
				pages:0,
				total:0,
			}
		},
		onLoad() {

		},
		async onShow(){
		    await this.openSqlite()
		},
		methods: {
			
			// 打开数据库
			async openSqlite(){
			    try{
			        let b = await openSqlite()
			        uni.showToast({
			            title:"open db success",
			            icon:"none"
			        })
			    }catch(e){
			        console.error("open db error",e)
			    }
			},
			
			// 关闭数据库
			closedb(){
			    try{
			        closedb()
			    }catch(e){
			        console.error("close db error",e)
			    }
			
			},
			
			// 获取数据
			async getData(){
			    try{
			        let where = {}
			        let res = await getPageList(this.tableName,this.options,where,"fid desc")
			        console.log("加载数据",res)
			        this.list = res.data.data.records
			        this.pages = res.data.data.pages
			        this.total = res.data.data.total
			    }catch(e){
			        uni.showToast({
			            title:"报错，请查看控制台",
			            icon:"none"
			        });
			        console.error("报错",e)
			    }
			},
			
			// 修改数据
			async changeData(){
			    // fid 是自增id
			    try{
			        let res = await getPageList(this.tableName,{current:1,size:1},{},"fid desc")
			        console.log("最后一页数据",res.data.data)
			        await updateSql(this.tableName,{title:"updated data"},{fid:res.data.data.records[0].fid})
			        uni.showToast({
			            title:"update ok",
			            icon:"none"
			        });
			    }catch(e){
			        uni.showToast({
			            title:"update error",
			            icon:"none"
			        });
			        console.error("修改报错",e)
			    }
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
