<script setup lang="ts">
import { message } from 'ant-design-vue'
import config from '@/api/config'
import use2FAModal from '@/components/TwoFA/use2FAModal'
import NodeSelector from '@/components/NodeSelector/NodeSelector.vue'

const emit = defineEmits(['renamed'])
const visible = ref(false)
const isDirFlag = ref(false)

const data = ref({
  basePath: '',
  orig_name: '',
  new_name: '',
  sync_node_ids: [] as number[],
})

const refForm = ref()

function open(basePath: string, origName: string, isDir: boolean) {
  visible.value = true
  data.value.orig_name = origName
  data.value.new_name = origName
  data.value.basePath = basePath
  isDirFlag.value = isDir
}

defineExpose({
  open,
})

function ok() {
  refForm.value.validate().then(() => {
    const { basePath, orig_name, new_name, sync_node_ids } = data.value

    const otpModal = use2FAModal()

    otpModal.open().then(() => {
      config.rename(basePath, orig_name, new_name, sync_node_ids).then(() => {
        visible.value = false
        message.success($gettext('Rename successfully'))
        emit('renamed')
      }).catch(e => {
        message.error(`${$gettext('Server error')} ${e?.message}`)
      })
    })
  })
}
</script>

<template>
  <AModal
    v-model:open="visible"
    :mask="false"
    :title="$gettext('Rename')"
    @ok="ok"
  >
    <AForm
      ref="refForm"
      layout="vertical"
      :model="data"
      :rules="{
        new_name: [
          { required: true, message: $gettext('Please input a filename') },
          { pattern: /^[^\\/]+$/, message: $gettext('Invalid filename') },
        ],
      }"
    >
      <AFormItem :label="$gettext('Original name')">
        <p>{{ data.orig_name }}</p>
      </AFormItem>
      <AFormItem
        :label="$gettext('New name')"
        name="new_name"
      >
        <AInput v-model:value="data.new_name" />
      </AFormItem>
      <AFormItem
        v-if="isDirFlag"
        :label="$gettext('Sync')"
      >
        <NodeSelector
          v-model:target="data.sync_node_ids"
          hidden-local
        />
      </AFormItem>
    </AForm>
  </AModal>
</template>

<style scoped lang="less">

</style>
