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

import { saveOrUpdateSpecialId } from '#/api/legacy/app-system';
import { getUserBaseInfoBySysOriginAccount } from '#/api/legacy/user';
import AccountInput from '#/components/account-input.vue';
import { getDisplaySysOriginText } from '#/views/system/shared';

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

import SpecialIdCustomizeModal from './special-id-customize-modal.vue';

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

const emit = defineEmits<{
  close: [];
  success: [payload: { account: string; sysOrigin: string; userId: string }];
}>();

const loading = ref(false);
const customizeOpen = ref(false);

const form = reactive<Record<string, any>>({
  account: '',
  customizeField: {},
  expiredDays: '',
  id: '',
  remark: '',
  sysOrigin: '',
  userId: '',
  userSearch: '',
});

const title = computed(() => (props.initialRecord?.id ? '修改' : '添加'));

function syncForm() {
  const record = props.initialRecord || {};
  form.account = record.account ?? '';
  form.customizeField = record.customizeField || {};
  form.expiredDays = record.expiredDays ?? '';
  form.id = record.id ?? '';
  form.remark = record.remark ?? '';
  form.sysOrigin = record.sysOrigin ?? props.sysOrigin;
  form.userId = record.userId ?? '';
  form.userSearch = record.userId ?? '';
}

watch(
  () => [props.open, props.initialRecord],
  ([open]) => {
    if (!open) {
      return;
    }
    syncForm();
  },
  { immediate: true },
);

function validateForm() {
  if (!String(form.userSearch || '').trim()) {
    message.warning('请输入用户ID');
    return false;
  }
  if (!String(form.account || '').trim()) {
    message.warning('请输入新账号');
    return false;
  }
  if (form.expiredDays === '' || form.expiredDays === null || form.expiredDays === undefined) {
    message.warning('请输入有效天数');
    return false;
  }
  return true;
}

function parseEncodedValue(value: unknown) {
  const raw = String(value ?? '');
  if (!raw) {
    return {
      accountType: '',
      content: '',
      sysOrigin: '',
    };
  }
  const match = raw.match(/^\*(.*)\*$/);
  if (!match) {
    return {
      accountType: '',
      content: raw,
      sysOrigin: '',
    };
  }
  const body = match[1] || '';
  return {
    accountType: body.match(/accountType:([^,]*)/)?.[1] || '',
    content: body.match(/content:(.*)$/)?.[1] || '',
    sysOrigin: body.match(/sysOrigin:([^,]*)/)?.[1] || '',
  };
}

async function resolveUserId() {
  const parsed = parseEncodedValue(form.userSearch);
  const content = String(parsed.content || '').trim();
  if (!content) {
    message.warning('请输入用户ID');
    return '';
  }
  if (!parsed.accountType || parsed.accountType === 'LONG') {
    return content.replace(/[^\d]/g, '');
  }
  const sysOrigin = parsed.sysOrigin || form.sysOrigin || props.sysOrigin;
  if (!sysOrigin) {
    message.warning('请选择系统');
    return '';
  }
  const result = await getUserBaseInfoBySysOriginAccount(sysOrigin, content);
  const userId = result?.id || result?.userId;
  if (!userId) {
    message.warning('未找到用户');
    return '';
  }
  return String(userId);
}

async function handleSubmit() {
  if (!validateForm()) {
    return;
  }
  const resolvedUserId = await resolveUserId();
  if (!resolvedUserId) {
    return;
  }
  const payload = {
    account: String(form.account || '').trim(),
    sysOrigin: form.sysOrigin || props.sysOrigin,
    userId: resolvedUserId,
  };
  loading.value = true;
  try {
    await saveOrUpdateSpecialId({
      ...form,
      account: payload.account,
      remark: String(form.remark || '').trim(),
      sysOrigin: payload.sysOrigin,
      userId: payload.userId,
    });
    message.success('操作成功');
    emit('success', payload);
    emit('close');
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <Drawer
    :open="open"
    destroy-on-close
    :title="title"
    width="520"
    @close="emit('close')"
  >
    <Form :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" layout="horizontal">
      <FormItem label="系统">
        <div class="readonly-value">{{ getDisplaySysOriginText(sysOrigin) }}</div>
      </FormItem>
      <FormItem label="用户">
        <AccountInput
          v-model:value="form.userSearch"
          :disabled="loading"
          :sys-origin="form.sysOrigin || props.sysOrigin"
          placeholder="请输入用户ID"
        />
      </FormItem>
      <FormItem label="新账号">
        <Input
          v-model:value="form.account"
          :disabled="loading"
          :maxlength="10"
          placeholder="请输入新账号"
        />
      </FormItem>
      <FormItem label="有效天数">
        <Input
          v-model:value="form.expiredDays"
          :disabled="loading"
          :maxlength="4"
          placeholder="0代表永久，其他范围1~3650"
        />
      </FormItem>
      <FormItem label="备注">
        <Input
          v-model:value="form.remark"
          :disabled="loading"
          :maxlength="100"
          placeholder="请输入备注"
        />
      </FormItem>
      <FormItem label="自定义属性">
        <Button type="link" @click="customizeOpen = true">点击查看/设置</Button>
      </FormItem>
    </Form>

    <template #footer>
      <Space>
        <Button @click="emit('close')">取消</Button>
        <Button :loading="loading" type="primary" @click="handleSubmit">
          保存
        </Button>
      </Space>
    </template>

    <SpecialIdCustomizeModal
      :customize-field="form.customizeField"
      :open="customizeOpen"
      @close="customizeOpen = false"
      @submit="(value) => { form.customizeField = value; customizeOpen = false; }"
    />
  </Drawer>
</template>

<style scoped>
.readonly-value {
  color: #0f172a;
  font-weight: 500;
}
</style>
