<template>
  <view class="transfer-container">
    <view class="content">
      <view class="tips">处理中...</view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { storeToRefs } from 'pinia';
import api from '@/utils/api';
import useAccountStore from '@/store/account';
import { confirm as uniConfirm, hideLoading, loading as showLoading, toast } from '@/utils/widget';

const CURRENT_TEAM_STORAGE_KEY = 'ykt_case_current_team';
const NEED_RELOAD_STORAGE_KEY = 'ykt_case_need_reload';
const BATCH_CUSTOMER_IDS_KEY = 'ykt_case_batch_customer_ids';

const accountStore = useAccountStore();
const { account, doctorInfo } = storeToRefs(accountStore);
const { getDoctorInfo } = accountStore;

const customerIds = ref([]);
const currentTeam = ref(null);
const teams = ref([]);

function getUserId() {
  const d = doctorInfo.value || {};
  const a = account.value || {};
  return String(d.userid || d.userId || d.corpUserId || a.userid || a.userId || '') || '';
}

function getCorpId() {
  const t = currentTeam.value || {};
  const a = account.value || {};
  const d = doctorInfo.value || {};
  return String(t.corpId || d.corpId || a.corpId || '') || '';
}

function getCurrentTeamId() {
  return String(currentTeam.value?.teamId || '') || '';
}

function showActionSheet(itemList = [], title = '') {
  return new Promise((resolve, reject) => {
    if (!Array.isArray(itemList) || itemList.length === 0) {
      reject(new Error('empty'));
      return;
    }
    uni.showActionSheet({
      title,
      itemList,
      success: ({ tapIndex }) => resolve(tapIndex),
      fail: () => reject(new Error('cancel')),
    });
  });
}

function normalizeTeam(raw) {
  if (!raw || typeof raw !== 'object') return null;
  const teamId = raw.teamId || raw.id || raw._id || '';
  const name = raw.name || raw.teamName || raw.team || '';
  const corpId = raw.corpId || raw.corpID || '';
  if (!teamId || !name) return null;
  return { teamId: String(teamId), name: String(name), corpId: corpId ? String(corpId) : '' };
}

async function ensureDoctor() {
  if (doctorInfo.value) return;
  if (!account.value?.openid) return;
  try {
    await getDoctorInfo();
  } catch (e) {
    // ignore
  }
}

async function loadTeams() {
  await ensureDoctor();
  const corpId = getCorpId();
  const userId = getUserId();
  if (!corpId || !userId) return [];
  const res = await api('getTeamBymember', { corpId, corpUserId: userId });
  if (!res?.success) {
    toast(res?.message || '获取团队失败');
    return [];
  }
  const list = Array.isArray(res?.data) ? res.data : Array.isArray(res?.data?.data) ? res.data.data : [];
  teams.value = list.map(normalizeTeam).filter(Boolean);
  return teams.value;
}

async function fetchTeamMembers(teamId) {
  const corpId = getCorpId();
  if (!teamId) return [];
  const res = await api('getTeamData', { corpId, teamId });
  if (!res?.success) {
    toast(res?.message || '获取团队成员失败');
    return [];
  }
  const t = res?.data && typeof res.data === 'object' ? res.data : {};
  return Array.isArray(t.memberList) ? t.memberList : [];
}

async function transferToCustomerPool() {
  try {
    await uniConfirm('客户将与本团队解除服务关系，本团队成员将没有权限查询到客户档案。');
  } catch (e) {
    return false;
  }

  const corpId = getCorpId();
  const currentTeamId = getCurrentTeamId();
  const creatorUserId = getUserId();
  if (!corpId || !currentTeamId || !creatorUserId) {
    return false;
  }

  showLoading('保存中...');
  try {
    const res = await api('transferCustomers', {
      corpId,
      customerIds: customerIds.value,
      currentTeamId,
      operationType: 'transferToCustomerPool',
      creatorUserId,
    });
    if (!res?.success) {
      toast(res?.message || '操作失败');
      return false;
    }
    toast('操作成功');
    return true;
  } catch (e) {
    toast('操作失败');
    return false;
  } finally {
    hideLoading();
  }
}

async function transferToOtherTeam() {
  const corpId = getCorpId();
  const currentTeamId = getCurrentTeamId();
  const creatorUserId = getUserId();
  if (!corpId || !currentTeamId || !creatorUserId) {
    return false;
  }

  if (!teams.value.length) await loadTeams();
  const candidates = teams.value.filter((t) => String(t?.teamId || '') !== String(currentTeamId));
  if (!candidates.length) {
    toast('暂无可选团队');
    return false;
  }

  let teamIndex;
  try {
    teamIndex = await showActionSheet(candidates.map((t) => t.name || ''), '选择新负责团队');
  } catch (e) {
    return false;
  }
  const pickedTeam = candidates[teamIndex];
  if (!pickedTeam?.teamId) {
    toast('团队信息异常');
    return false;
  }

  const members = await fetchTeamMembers(pickedTeam.teamId);
  if (!members.length) {
    toast('当前团队暂无可选成员');
    return false;
  }

  let userIndex;
  try {
    userIndex = await showActionSheet(
      members.map((m) => String(m?.anotherName || m?.name || m?.userid || '')),
      '选择责任人'
    );
  } catch (e) {
    return false;
  }

  const targetUserId = String(members[userIndex]?.userid || '') || '';
  if (!targetUserId) {
    toast('责任人信息异常');
    return false;
  }

  showLoading('保存中...');
  try {
    const res = await api('transferCustomers', {
      corpId,
      customerIds: customerIds.value,
      currentTeamId,
      targetTeamId: String(pickedTeam.teamId),
      targetUserId,
      operationType: 'transferToOtherTeam',
      creatorUserId,
    });
    if (!res?.success) {
      toast(res?.message || '操作失败');
      return false;
    }
    toast('操作成功');
    return true;
  } catch (e) {
    toast('操作失败');
    return false;
  } finally {
    hideLoading();
  }
}

async function runFlow(options = {}) {
  let mode = options?.mode === 'pool' ? 'pool' : options?.mode === 'team' ? 'team' : '';
  if (!mode) {
    let pick;
    try {
      pick = await showActionSheet(['转移给其他团队', '转移至客户公共池']);
    } catch (e) {
      return false;
    }
    mode = pick === 0 ? 'team' : 'pool';
  }
  return mode === 'team' ? await transferToOtherTeam() : await transferToCustomerPool();
}

onLoad(async (options = {}) => {
  customerIds.value = Array.isArray(uni.getStorageSync(BATCH_CUSTOMER_IDS_KEY))
    ? uni.getStorageSync(BATCH_CUSTOMER_IDS_KEY).map(String).filter(Boolean)
    : [];
  currentTeam.value = uni.getStorageSync(CURRENT_TEAM_STORAGE_KEY) || null;

  if (!customerIds.value.length) {
    toast('未选择客户');
    setTimeout(() => uni.navigateBack(), 200);
    return;
  }

  const ok = await runFlow(options);
  if (ok) {
    uni.removeStorageSync(BATCH_CUSTOMER_IDS_KEY);
    uni.setStorageSync(NEED_RELOAD_STORAGE_KEY, 1);
  }
  setTimeout(() => uni.navigateBack(), 50);
});
</script>

<style lang="scss" scoped>
.transfer-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f7f8fa;
}

.content {
  flex: 1;
  padding: 20px;

  .section-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-top: 20px;
    margin-bottom: 10px;

    &:first-child {
      margin-top: 0;
    }
  }

  .selector-item {
    background-color: #fff;
    height: 44px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;

    text {
      font-size: 14px;
      color: #333;

      &.placeholder {
        color: #999;
      }
    }
  }

  .tips {
    margin-top: 15px;
    font-size: 14px;
    color: #999;
    line-height: 1.5;
  }
}

.footer {
  background-color: #fff;
  padding: 15px 20px 30px;
  display: flex;
  gap: 15px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);

  .btn {
    flex: 1;
    height: 44px;
    line-height: 44px;
    font-size: 16px;
    border-radius: 4px;
    margin: 0;

    &.plain {
      background-color: #fff;
      color: #666;
      border: 1px solid #ddd;
    }

    &.primary {
      background-color: #0877F1;
      color: #fff;
      border: none;
    }

    &::after {
      border: none;
    }
  }
}
</style>

