<script lang="ts" setup>
import { reactive, ref, watch } from 'vue';

import { changeBdLeader } from '#/api/legacy/team';

import { Form, FormItem, Input, Modal, message } from 'antdv-next';

const props = defineProps<{
  open: boolean;
  row: null | Record<string, any>;
}>();

const emit = defineEmits<{
  close: [];
  success: [];
}>();

const saving = ref(false);
const form = reactive({
  bdId: '',
  newAccount: '',
});

watch(
  () => props.open,
  (open) => {
    if (!open) {
      return;
    }
    form.bdId = String(props.row?.id || '');
    form.newAccount = '';
  },
  { immediate: true },
);

async function handleSubmit() {
  if (!form.newAccount) {
    message.warning('请输入BD Leader账号');
    return;
  }
  saving.value = true;
  try {
    await changeBdLeader({ ...form });
    message.success('保存成功');
    emit('success');
    emit('close');
  } finally {
    saving.value = false;
  }
}
</script>

<template>
  <Modal
    :confirm-loading="saving"
    :open="open"
    destroy-on-close
    title="换绑BD Leader"
    @cancel="emit('close')"
    @ok="handleSubmit"
  >
    <Form layout="vertical">
      <FormItem label="BD Leader">
        <Input
          v-model:value="form.newAccount"
          placeholder="请输入BD Leader账号"
        />
      </FormItem>
    </Form>
  </Modal>
</template>
