<template>
  <div>
    <NSpace>
      <NButton
        type="primary"
        @click="showCommit = true"
      >
        提交修改
      </NButton>
      <NButton @click="onPush">
        Push 到远端
      </NButton>
    </NSpace>

    <CommitDialog
      v-if="showCommit"
      target="main"
      :target-id="ws.id"
      @close="showCommit = false"
      @committed="showCommit = false"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { NSpace, NButton, useMessage } from 'naive-ui'
import { useWorkspacesStore } from '@/stores/workspaces'
import CommitDialog from './CommitDialog.vue'
import type { Workspace } from '@/api/types'

const props = defineProps<{ ws: Workspace }>()
const store = useWorkspacesStore()
const msg = useMessage()
const showCommit = ref<boolean>(false)

async function onPush() {
  try {
    await store.pushOnMain(props.ws.id)
    msg.success('已 push')
  } catch (e: unknown) {
    msg.error('push 失败：' + (e instanceof Error ? e.message : String(e)))
  }
}
</script>
