<template>
  <MDialog :visible.sync="show" title="恢复确认" @submit="handleSubmit">
    <MForm ref="form" :items="formItems" v-model="formValues" v-loading="loading"></MForm>
  </MDialog>
</template>

<script>
import MDialog from '@/components/Dialog'
import MForm from '@/components/MForm'
import {
  pgBackupRestore
} from '@/api/dataChart'
export default {
  name: 'backupDetailsConfirm',
  components: {
    MDialog,
    MForm
  },
  data () {
    return {
      loading: false,
      show: false,
      formValues: {
        tables: ['langchain_pg_collection', 'langchain_pg_embedding'],
        db_connection: null,
        truncate_before_restore: false
      },
      formItems: [
        {
          label: '恢复表名',
          type: 'autocomplete',
          key: 'tables',
          multiple: true,
          noAttach: true,
          items: ['langchain_pg_collection', 'langchain_pg_embedding']
        },
        {
          label: 'PostgreSQL连接字符串',
          type: 'text',
          key: 'db_connection'
        },
        {
          label: '清空pgvecror目标表',
          type: 'ifRadio',
          key: 'truncate_before_restore',
          width: '140',
          items: [
            { label: '是', value: true },
            { label: '否', value: false }
          ]
        }
      ],
      itemData: {},
      origin: {}
    }
  },
  methods: {
    open (item, origin) {
      this.itemData = item
      this.origin = origin
      this.show = true
      this.formValues = {
        tables: ['langchain_pg_collection', 'langchain_pg_embedding'],
        db_connection: null,
        truncate_before_restore: false
      }
    },
    async handleSubmit () {
      this.loading = true
      try {
        await pgBackupRestore({
          backup_path: this.origin.relative_path,
          timestamp: this.itemData.timestamp,
          ...this.formValues
        })
        this.$snackbar.success('恢复成功')
        this.show = false
        this.$emit('success')
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
