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

import { addBd, updateBd } from '#/api/legacy/team';
import AccountInput from '#/components/account-input.vue';

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

import SystemRegionSelect from './system-region-select.vue';

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

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

const saving = ref(false);
const form = reactive({
  contact: '',
  id: '',
  region: '',
  sysOrigin: '',
  userId: '',
});

const isUpdate = ref(false);
const title = ref('新增BD');

watch(
  () => props.open,
  (open) => {
    if (!open) {
      return;
    }
    isUpdate.value = !!props.row?.id;
    title.value = isUpdate.value ? '编辑BD' : '新增BD';
    form.id = String(props.row?.id || '');
    form.userId = String(props.row?.userId || props.row?.userProfile?.id || '');
    form.region = String(props.row?.region || '');
    form.contact = String(props.row?.contact || '');
    form.sysOrigin = String(props.row?.sysOrigin || props.sysOrigin || '');
  },
  { immediate: true },
);

watch(
  () => props.sysOrigin,
  (value) => {
    if (!isUpdate.value) {
      form.sysOrigin = String(value || '');
    }
  },
  { immediate: true },
);

async function handleSubmit() {
  if (!form.userId) {
    message.warning('请输入BD用户ID');
    return;
  }
  if (!form.region) {
    message.warning('请选择区域');
    return;
  }
  saving.value = true;
  try {
    const payload = { ...form, sysOrigin: form.sysOrigin || props.sysOrigin };
    if (isUpdate.value) {
      await updateBd(payload);
    } else {
      await addBd(payload);
    }
    message.success('保存成功');
    emit('success');
    emit('close');
  } finally {
    saving.value = false;
  }
}
</script>

<template>
  <Drawer
    :open="open"
    :title="title"
    destroy-on-close
    width="520px"
    @close="emit('close')"
  >
    <Form layout="vertical">
      <FormItem label="BD">
        <AccountInput
          v-model:value="form.userId"
          :disabled="isUpdate"
          :sys-origin="form.sysOrigin || props.sysOrigin"
          placeholder="请输入BD用户ID"
        />
      </FormItem>
      <FormItem label="区域">
        <SystemRegionSelect
          v-model:value="form.region"
          :sys-origin="form.sysOrigin || props.sysOrigin"
          clearable
          placeholder="请选择区域"
        />
      </FormItem>
      <FormItem label="联系方式">
        <Input
          v-model:value="form.contact"
          placeholder="请输入联系方式"
        />
      </FormItem>
    </Form>

    <template #footer>
      <div class="drawer-footer">
        <Button @click="emit('close')">取消</Button>
        <Button :loading="saving" type="primary" @click="handleSubmit">
          保存
        </Button>
      </div>
    </template>
  </Drawer>
</template>

<style scoped>
.drawer-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
