<template>
	<view class="page">
		<!-- <view class="status_bar">  
            <view class="top_view"></view>  
        </view>   -->

		<view>
			<button class="ant-btn ant-btn-lg" @click="open">打开数据库</button>
		</view>

		<view class="btn-group">
			<button class="ant-btn ant-btn-lg" @click="getTable">所有数据表</button>
			<button class="ant-btn ant-btn-lg" @click="isTable">用户表是否存在</button>
		</view>
		<view>
			<button class="ant-btn ant-btn-lg" @click="isOpen">数据库是否打开</button>
		</view>
		<view>
			<button class="ant-btn ant-btn-lg" @click="addUserTable">添加User表</button>
			<button class="ant-btn ant-btn-lg" @click="addUser">添加User数据</button>
			<button class="ant-btn ant-btn-lg" @click="getUser">读取User数据</button>			
		</view>
		<view>
			<button class="ant-btn ant-btn-lg" @click="addLogTable">添加Log表</button>
			<button class="ant-btn ant-btn-lg" @click="addLog">添加Log数据</button>
			<button class="ant-btn ant-btn-lg" @click="getLog">读取Log数据</button>			
		</view>
		<view>
			<button class="ant-btn ant-btn-lg" @click="addScopeTable">添加镜柜表</button>			
			<button class="ant-btn ant-btn-lg" @click="updateScope">更新镜柜表</button>
			<button class="ant-btn ant-btn-lg" @click="getScope">读取镜柜数据</button>
		</view>
		<view>
			<button class="ant-btn ant-btn-lg" @click="addEndoTable">添加镜柜信息表</button>			
			<button class="ant-btn ant-btn-lg" @click="addEndo">镜柜信息数据</button>
			<button class="ant-btn ant-btn-lg" @click="getEndo">读取镜柜信息数据</button>
		</view>
		<view style="margin-bottom: 40px;">
			<button class="ant-btn ant-btn-lg" @click="dropUser">删除用户表</button>			
			<button class="ant-btn ant-btn-lg" @click="dropLog">删除LOG表</button>
			<button class="ant-btn ant-btn-lg" @click="dropScope">删除镜柜表</button>
			<button class="ant-btn ant-btn-lg" @click="dropEndo">删除镜柜信息表</button>
		</view>

		<view class="result">
			{{ result }}
		</view>
	</view>
</template>

<script>
import * as sqlite from '@/db/sqlite.js'
import { formatDateTime } from "@/utils/common.js"
export default {
	data() {
		return {
			result: ''
		}
	},
	methods: {
		async open() {
			let db = await sqlite.openDb()
			console.log(db)
			this.result = db
		},
		async getTable() {
			let table = await sqlite.getTable('user')
			console.log('getTable', table)
			this.result = 'getTable:' + JSON.stringify(table)
		},
		async isTable() {
			let is = await sqlite.isTable('user')
			console.log('isTable', is)
			this.result = 'isTable:' + is
		},
		async isOpen() {
			let is = await sqlite.isOpen()
			console.log('isOpen', is)
			this.result = 'isOpen:' + is
		},
		async addUserTable() {
			let add = await sqlite.addUserTable()
			console.log('addUserTable', add)
			this.result = 'addUserTable:' + JSON.stringify(add)
		},
		async addUser() {
			let add = await sqlite.addTabItem('user', {
				name: 'test',
			})
			console.log('addUser', add)
			this.result = 'addUser:' + JSON.stringify(add)
		},
		async getUser() {
			let res = await sqlite.getDataList('user', 1)
			console.log('getUser', res)
			this.result = 'getUser:' + JSON.stringify(res)
		},

		async addLogTable() {
			let add = await sqlite.addLogTable()
			console.log('addLogTable', add)
			this.result = 'addLogTable:' + JSON.stringify(add)
		},
		async addLog() {
			let add = await sqlite.addTabItem('log', {
				name: '用户A',
				action: '刷卡开门',
			})
			console.log('addLog', add)
			this.result = 'addLog:' + JSON.stringify(add)
		},
		async getLog() {
			let res = await sqlite.getDataList('log', 1)
			console.log('getLog', res)
			this.result = 'getLog:' + JSON.stringify(res)
		},
		// 增加内镜信息表
		async addEndoTable() {
			let add = await sqlite.addEndoTable()
			console.log('addEndoTable', add)
			this.result = 'addEndoTable:' + JSON.stringify(add)
		},
		async addEndo() {
			let add = await sqlite.addTabItem('endo', {
				rfid: '123456',
				ic: '23456'
			})
			console.log('addEndo', add)
			this.result = 'addEndo:' + JSON.stringify(add)
		},
		async getEndo() {
			let res = await sqlite.getDataList('endo', 1)
			console.log('getEndo', res)
			this.result = 'getEndo:' + JSON.stringify(res)
		},
		async dropUser() {
			let res = await sqlite.dropTable('user')
			console.log('dropUser', res)
			this.result = 'dropUser:' + JSON.stringify(res)
		},
		async dropLog() {
			let res = await sqlite.dropTable('log')
			console.log('dropLog', res)
			this.result = 'dropLog:' + JSON.stringify(res)
		},
		async dropScope() {
			let res = await sqlite.dropTable('scope')
			console.log('dropScope', res)
			this.result = 'dropScope:' + JSON.stringify(res)
		},
		async dropEndo() {
			let res = await sqlite.dropTable('endo')
			console.log('dropEnode', res)
			this.result = 'dropEndo:' + JSON.stringify(res)
		},
		async addScopeTable() { 
			let res = await sqlite.addScopeTable()
			console.log('addScopeTable', res)
			for (let i = 0; i < 16; i++) {
				await sqlite.addTabItem('scope', {
					name: '', ic: '', key: i,
				})
			}
			this.result = 'addScopeTable:' + JSON.stringify(res)
		},
		async updateScope() {
			let ic = Math.floor(Math.random() * 90000000) + 10000000
			let key = Math.floor(Math.random() * 16)
			let res = await sqlite.updateSQL('scope', {
				name: 'in',
				ic: 'ic' + ic.toString(),
				time: formatDateTime(),
			}, 'key', key)
			console.log('updateScope', res)
			this.result = 'updateScope:' + JSON.stringify(res)
		},
		async getScope() {
			let res = await sqlite.getDataList('scope', 1, 16, 'time', 'desc')
			console.log('getScope', res)
			this.result = 'getScope:' + JSON.stringify(res)
		}
	}
}
</script>

<style scoped>

.ant-btn{
	margin: 0 8px;
	margin-top: 16px;
}

.result{
	display: block;
	margin-top: 40rpx;
	text-align: center;
	width: 500px;
	min-height: 300px;
	margin: 0 auto;
	background-color: #333333;
	color: #fff;
	padding: 16px;
	word-break: break-all;
}
</style>
