<template>
	<div class="page">
		<BaseTable ref="baseTable" :action="action" :columns="columns" :operation="operation" :search="search">
			<template v-slot:operation>
				<a-button type="primary" v-if="checkPermission('备份')" @click="handleClickBackupData(0)"> 完整备份 </a-button>
				<a-button type="primary" v-if="checkPermission('备份')" @click="handleClickBackupData(1)"> 增量备份 </a-button>
				<a-button type="primary" v-if="checkPermission('备份')" @click="handleClickBackupData(2)"> 数据结构备份 </a-button>
				<BaseUpload
					:action="actionUpload"
					:btnType="'primary'"
					:textColor="'#fff'"
					:uploadAccept="'.sql,.log'"
					:uploadListType="'text'"
					@upload-success="handleUpload"
				>
				</BaseUpload>
			</template>
		</BaseTable>
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import BaseTable from '../../../components/table'
import BaseUpload from '../../../components/BaseUpload.vue'
import { BaseUtil } from '../../../utils/base-util'
import columns from './columns'
import search from './search'
import requestMethod from '../../../api/system-log/backup'
import { OperationProps } from '../../../components/table/table-helper'
import { confirmMethod, messageMethod } from '../../../utils/message-util'
import { LogBackup } from '../../../entity/system-log/log-backup'
import { manageMethod } from '../../../components/form/method'
import { checkPermission } from '../../../utils/permission-util'

// 列表查询信息的api
const action = ref(requestMethod.page)
const actionUpload = '/api/logBackup/importBackup'

const useManageMethod = manageMethod()

// 备份 （备份类型,0完整备份，1增量备份，2数据结构备份，3导入备份）
const handleClickBackupData = (backupType: 0 | 1 | 2 | 3 = 0) => {
	requestMethod.add({ backupType }).then(res => {
		messageMethod.success(res.message)
		useManageMethod.baseTable().pageRequest()
	})
}

// 上传成功的回调
const handleUpload = () => {
	useManageMethod.baseTable().pageRequest()
	messageMethod.success('上传成功')
}

// 列表操作信息
const operation = ref<OperationProps<LogBackup>[]>([
	{
		title: '下载',
		permission: true,
		action: (vo: LogBackup) => {
			BaseUtil.downBackupFile(vo.backupPath, vo.backupName)
		}
	},
	{
		title: '还原',
		permission: true,
		action: (vo: LogBackup) => {
			confirmMethod.info('确认还原？', () => {
				requestMethod.restore(vo.id).then(() => {
					messageMethod.success('还原成功')
				})
			})
		}
	}
])
</script>

<style lang="scss" scoped>
.page {
	height: 100%;
	display: flex;
	flex-direction: column;
}
</style>
