<template>
  <MDialog :visible.sync="show" title="创建备份" @submit="handleSubmit">
    <MForm ref="form" :items="formItems" v-model="formValues" v-loading="loading"></MForm>
  </MDialog>
</template>

<script>
import MForm from '@/components/MForm'
import MDialog from '@/components/Dialog'
import {
  addPgBackup
} from '@/api/dataChart'
export default {
  name: 'backupCreate',
  components: { MDialog, MForm },
  data () {
    return {
      loading: false,
      show: false,
      formValues: {
        db_connection: null
      },
      formItems: [
        {
          label: 'PostgreSQL连接字符串',
          key: 'db_connection',
          type: 'text',
          placeholder: '请输入PostgreSQL连接字符串，不提供则从config.py获取'
        }
      ],
      itemData: null
    }
  },
  methods: {
    open (item) {
      this.itemData = item ?? null
      this.show = true
      this.formValues = {
        db_connection: null
      }
    },
    async handleSubmit () {
      this.loading = true
      try {
        await addPgBackup({
          ...this.formValues,
          task_id: this.itemData?.task_id
        })
        this.$snackbar.success('创建备份成功')
        this.$emit('success')
        this.show = false
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
