<template>
  <!-- Mobile 来源: ykt-management-mobile/src/pages/customer/new-followup-record/new-followup-record.vue（简化移植：去除 pinia/接口） -->
  <view class="page">
    <scroll-view scroll-y class="scroll" :style="{ height: scrollHeight + 'px' }">
      <view class="card">
        <picker mode="date" :value="form.plannedExecutionTime" @change="changeDate">
          <view class="row clickable">
            <view class="label">回访日期</view>
            <view class="right">
              <view class="value" :class="{ muted: !form.plannedExecutionTime }">{{ form.plannedExecutionTime || '请选择回访日期' }}</view>
              <uni-icons type="arrowright" size="16" color="#999" />
            </view>
          </view>
        </picker>

        <view class="block">
          <view class="block-title">回访方式</view>
          <view class="method-row">
            <view class="method" @click="selectMethod('phone')">
              <image class="radio" :src="`/static/circle${form.todoMethod === 'phone' ? 'd' : ''}.svg`" />
              <view class="m-label">电话</view>
              <view v-if="form.todoMethod === 'phone'" class="m-value" :class="{ muted: !form.phoneNumber }">{{ form.phoneNumber || '请选择电话号码' }}</view>
              <uni-icons v-if="form.todoMethod === 'phone'" type="arrowright" size="16" color="#999" />
            </view>
            <view class="method" @click="selectMethod('wechat')">
              <image class="radio" :src="`/static/circle${form.todoMethod === 'wechat' ? 'd' : ''}.svg`" />
              <view class="m-label">微信</view>
            </view>
          </view>
        </view>

        <view class="row clickable" @click="selectType">
          <view class="label">回访类型</view>
          <view class="right">
            <view class="value" :class="{ muted: !form.eventType }">{{ eventTypeLabel || '请选择回访类型' }}</view>
            <uni-icons type="arrowright" size="16" color="#999" />
          </view>
        </view>

        <view class="row clickable" @click="selectTeam">
          <view class="label">所在团队</view>
          <view class="right">
            <view class="value" :class="{ muted: !form.teamId }">{{ form.teamName || '请选择团队' }}</view>
            <uni-icons type="arrowright" size="16" color="#999" />
          </view>
        </view>

        <view class="block">
          <view class="block-title">回访结果</view>
          <view class="textarea-box">
            <textarea v-model="form.result" class="textarea tall" placeholder="请输入回访结果" maxlength="500" />
            <view class="counter">{{ (form.result || '').length }}/500</view>
          </view>
        </view>
      </view>

      <view class="scroll-spacer" />
    </scroll-view>

    <view class="footer">
      <button class="btn plain" @click="cancel">取消</button>
      <button class="btn primary" @click="save">保存</button>
    </view>

    <uni-popup ref="typePopup" type="bottom" :mask-click="true">
      <view class="picker-sheet">
        <view class="picker-title">选择回访类型</view>
        <scroll-view scroll-y class="picker-body">
          <view
            v-for="t in eventTypeList"
            :key="t.value"
            class="picker-item"
            :class="{ active: form.eventType === t.value }"
            @click="pickType(t.value)"
          >
            <view class="picker-item-text">{{ t.label }}</view>
          </view>
        </scroll-view>
        <view class="picker-actions">
          <button class="btn plain" @click="closeTypePicker">取消</button>
          <button class="btn primary" @click="closeTypePicker">确定</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script setup>
import { computed, reactive, ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import dayjs from 'dayjs';
import { storeToRefs } from 'pinia';
import api from '@/utils/api';
import useAccountStore from '@/store/account';
import { toast } from '@/utils/widget';
import { getTodoEventTypeLabel, getTodoEventTypeOptions } from '@/utils/todo-const';

const scrollHeight = ref(0);

const archiveId = ref('');
const archiveName = ref('');
const archiveMobile = ref('');
const customerData = ref({});

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

const teams = ref([]);

const form = reactive({
  plannedExecutionTime: '',
  todoMethod: '',
  phoneNumber: '',
  eventType: '',
  teamId: '',
  teamName: '',
  result: '',
});

const eventTypeList = getTodoEventTypeOptions();
const eventTypeLabel = computed(() => getTodoEventTypeLabel(form.eventType));

const mobiles = computed(() => {
  const arr = [];
  if (archiveMobile.value) arr.push(String(archiveMobile.value));
  if (!arr.includes('13800000000')) arr.push('13800000000');
  return arr;
});

onLoad((options) => {
  try {
    const { windowHeight } = uni.getSystemInfoSync();
    scrollHeight.value = windowHeight || 0;
  } catch {
    scrollHeight.value = 0;
  }

  archiveId.value = options?.archiveId ? String(options.archiveId) : '';
  const c = uni.getStorageSync('new-followup-record-customer');
  if (c && typeof c === 'object') {
    archiveId.value = archiveId.value || String(c._id || '');
    archiveName.value = String(c.name || '');
    customerData.value = c;
  }
  const cached = uni.getStorageSync('ykt_case_archive_detail');
  if (cached && typeof cached === 'object' && String(cached?._id || '') === archiveId.value) {
    archiveMobile.value = String(cached.mobile || '');
    customerData.value = cached;
    archiveName.value = archiveName.value || String(cached.name || '');
  }

  if (!archiveId.value) {
    uni.showToast({ title: '缺少 archiveId', icon: 'none' });
    setTimeout(() => uni.navigateBack(), 300);
    return;
  }
  form.plannedExecutionTime = dayjs().format('YYYY-MM-DD');
  initDefaultTeam();
});

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

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 = uni.getStorageSync('ykt_case_current_team') || {};
  const a = account.value || {};
  const d = doctorInfo.value || {};
  return String(t.corpId || a.corpId || d.corpId || '') || '';
}

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 loadTeams() {
  await ensureDoctor();
  const corpId = getCorpId();
  const userId = getUserId();
  if (!corpId || !userId) return;
  const res = await api('getTeamBymember', { corpId, corpUserId: userId });
  if (!res?.success) return;
  const list = Array.isArray(res?.data) ? res.data : Array.isArray(res?.data?.data) ? res.data.data : [];
  teams.value = list.map(normalizeTeam).filter(Boolean);
}

async function initDefaultTeam() {
  await ensureDoctor();
  const currentTeam = uni.getStorageSync('ykt_case_current_team') || {};
  const teamId = String(currentTeam.teamId || '');
  const teamName = String(currentTeam.name || '');
  if (teamId) {
    form.teamId = teamId;
    form.teamName = teamName;
  } else {
    await loadTeams();
    if (teams.value[0]) {
      form.teamId = teams.value[0].teamId;
      form.teamName = teams.value[0].name;
    }
  }
}

function changeDate(e) {
  form.plannedExecutionTime = e.detail.value || '';
}

function selectMethod(method) {
  if (method === 'phone') {
    if (mobiles.value.length === 0) {
      uni.showToast({ title: '暂无可用电话号码', icon: 'none' });
      return;
    }
    uni.showActionSheet({
      itemList: mobiles.value,
      success: ({ tapIndex }) => {
        form.todoMethod = 'phone';
        form.phoneNumber = mobiles.value[tapIndex] || '';
      },
    });
  } else {
    form.todoMethod = 'wechat';
    form.phoneNumber = '';
  }
}

function selectType() {
  typePopup.value?.open?.();
}

function selectTeam() {
  (async () => {
    if (!teams.value.length) await loadTeams();
    if (!teams.value.length) {
      toast('暂无可选团队');
      return;
    }
    uni.showActionSheet({
      itemList: teams.value.map((t) => t.name),
      success: ({ tapIndex }) => {
        const t = teams.value[tapIndex];
        if (!t) return;
        form.teamId = t.teamId;
        form.teamName = t.name;
      },
    });
  })();
}

function cancel() {
  uni.showModal({
    title: '确认取消',
    content: '确定要取消新建回访记录吗？',
    success: (res) => res.confirm && uni.navigateBack(),
  });
}

async function save() {
  if (!form.plannedExecutionTime) return uni.showToast({ title: '请选择回访日期', icon: 'none' });
  if (!form.todoMethod) return uni.showToast({ title: '请选择回访方式', icon: 'none' });
  if (!form.eventType) return uni.showToast({ title: '请选择回访类型', icon: 'none' });
  if (!form.teamId) return uni.showToast({ title: '请选择所在团队', icon: 'none' });
  if (!String(form.result || '').trim()) return uni.showToast({ title: '请输入回访结果', icon: 'none' });

  await ensureDoctor();
  const corpId = getCorpId();
  const userId = getUserId();
  if (!corpId || !userId) {
    toast('缺少用户/团队信息，请先完成登录与团队选择');
    return;
  }

  const customer = customerData.value && typeof customerData.value === 'object' ? customerData.value : {};
  const customerId = String(customer._id || archiveId.value || '');
  const customerName = String(customer.name || archiveName.value || '');
  const customerUserId = String(customer.externalUserId || customer.customerUserId || '') || '';

  const phoneValue = form.phoneNumber ? `phone:${form.phoneNumber}` : 'phone';
  const plannedExecutionTime = dayjs(form.plannedExecutionTime).valueOf();

  const params = {
    corpId,
    eventStatus: 'treated',
    customerId,
    customerName,
    customerUserId,
    executeTeamId: form.teamId,
    executeTeamName: form.teamName,
    executorUserId: userId,
    creatorUserId: userId,
    taskContent: '',
    result: String(form.result || ''),
    todoMethod: form.todoMethod === 'phone' ? phoneValue : form.todoMethod,
    plannedExecutionTime,
    endTime: plannedExecutionTime,
    eventType: form.eventType,
  };

  const res = await api('createEvents', { params });
  if (!res?.success) {
    toast(res?.message || '保存失败');
    return;
  }
  uni.$emit('archive-detail:followup-changed');
  toast('保存成功');
  setTimeout(() => uni.navigateBack(), 300);
}

const typePopup = ref(null);
function pickType(v) {
  form.eventType = String(v || '');
}
function closeTypePicker() {
  typePopup.value?.close?.();
}
</script>

<style scoped>
.page {
  height: 100vh;
  overflow: hidden;
  background: #f5f6f8;
}
.scroll {
  width: 100%;
}
.card {
  background: #fff;
  margin: 10px 14px 0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
}
.scroll-spacer {
  height: calc(76px + env(safe-area-inset-bottom));
}
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px;
  border-bottom: 1px solid #f2f2f2;
}
.row.clickable:active {
  background: #fafafa;
}
.label {
  font-size: 14px;
  color: #666;
}
.right {
  display: flex;
  align-items: center;
  gap: 10px;
}
.value {
  font-size: 14px;
  color: #333;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.value.muted {
  color: #999;
}
.block {
  padding: 14px 14px;
  border-bottom: 1px solid #f2f2f2;
}
.block:last-child {
  border-bottom: none;
}
.block-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}
.method-row {
  display: flex;
  align-items: center;
  gap: 18px;
}
.method {
  display: flex;
  align-items: center;
  gap: 10px;
}
.radio {
  width: 16px;
  height: 16px;
}
.m-label {
  font-size: 14px;
  color: #333;
}
.m-value {
  font-size: 14px;
  color: #333;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.m-value.muted {
  color: #999;
}
.textarea-box {
  border: 1px solid #e6e6e6;
  border-radius: 8px;
  padding: 10px;
}
.textarea {
  width: 100%;
  min-height: 140px;
  font-size: 14px;
  box-sizing: border-box;
}
.textarea.tall {
  min-height: 160px;
}
.counter {
  margin-top: 6px;
  text-align: right;
  font-size: 12px;
  color: #999;
}
.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  gap: 12px;
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.06);
}
.btn {
  flex: 1;
  height: 44px;
  line-height: 44px;
  border-radius: 6px;
  font-size: 15px;
}
.btn::after {
  border: none;
}
.btn.plain {
  background: #fff;
  color: #0877F1;
  border: 1px solid #0877F1;
}
.btn.primary {
  background: #0877F1;
  color: #fff;
}

.picker-sheet {
  background: #fff;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
  overflow: hidden;
}
.picker-title {
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  padding: 14px;
  border-bottom: 1px solid #f0f0f0;
}
.picker-body {
  max-height: 60vh;
}
.picker-item {
  padding: 14px;
  border-bottom: 1px solid #f2f2f2;
}
.picker-item.active {
  background: #f2f6ff;
}
.picker-item-text {
  font-size: 14px;
  color: #333;
}
.picker-actions {
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  gap: 12px;
  background: #fff;
}
</style>
