<template>
  <m-dialog title="合并训练数据" :visible.sync="show" @submit="handleSubmit">
    <MForm ref="form" :items="formItems" v-model="formValues" v-loading="loading"></MForm>
  </m-dialog>
</template>

<script>
import MDialog from '@/components/Dialog'
import MForm from '@/components/MForm'
import {
  combineTrainData
} from '@/api/dataChart'
export default {
  name: 'modelTrainCombine',
  components: {
    MDialog,
    MForm
  },
  data () {
    return {
      loading: false,
      show: false,
      formItems: [
        {
          label: '数据集合名称',
          key: 'collections_names',
          type: 'autocomplete',
          rules: [v => v.length > 0 || '请输入数据集合名称'],
          multiple: true,
          noAttach: true,
          items: [
            { label: 'sql', value: 'sql' },
            { label: 'ddl', value: 'ddl' },
            { label: 'documentation', value: 'documentation' },
            { label: 'error_sql', value: 'error_sql' }
          ]
        },
        {
          label: '保留策略',
          key: 'keep_strategy',
          type: 'autocomplete',
          noAttach: true,
          items: [
            { label: '保留第一条记录', value: 'first' },
            { label: '保留最后一条记录', value: 'last' },
            { label: '保留最新记录', value: 'by_metadata_time' }
          ]
        },
        {
          label: '预览模式',
          key: 'dry_run',
          type: 'ifRadio',
          items: [
            { label: '是', value: true },
            { label: '否', value: false }
          ]
        }
      ],
      formValues: {
        collections_names: [],
        keep_strategy: 'first',
        dry_run: true
      }
    }
  },
  methods: {
    open () {
      this.show = true
      this.formValues = {
        collections_names: [],
        keep_strategy: 'first',
        dry_run: true
      }
      this.$nextTick(() => {
        this.$refs.form.resetValidation()
      })
    },
    async handleSubmit () {
      if (!this.$refs.form.validate()) return
      this.loading = true
      try {
        const { data } = await combineTrainData(this.formValues)
        this.$snackbar.success(data.response)
        this.show = false
        this.$emit('success')
      } catch (error) {
        this.$snackbar.error(error.message)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
