<script setup>
import { ref, onMounted } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { useUserStore } from "@/store/user";
import { updateMyInfo } from "@/api/user";
import { uploadImage } from "@/api/spot";
import { resolveImageUrl } from "@/utils/image";
import { checkLogin } from "@/utils/auth";
import cityData from "@/utils/city-data";

const userStore = useUserStore();
const loading = ref(false);

const form = ref({
  nickname: "",
  avatar_url: "",
  city: "",
  bio: "",
  identity: "both",
});

const identityOptions = [
  { label: "摄影师", value: "photographer" },
  { label: "Coser", value: "cosplayer" },
  { label: "都是", value: "both" },
];

const provinces = cityData.map((p) => p.province);
const cityColumns = ref([provinces, cityData[0].cities]);
const cityPickerIndex = ref([0, 0]);

const initForm = () => {
  const u = userStore.userInfo;
  if (!u) return;
  form.value.nickname = u.nickname || "";
  form.value.avatar_url = u.avatar_url || "";
  form.value.bio = u.bio || "";
  form.value.identity = u.identity || "both";
  form.value.city = u.city || "";

  if (u.city) {
    const parts = u.city.split(" ");
    if (parts.length === 2) {
      const pi = provinces.indexOf(parts[0]);
      if (pi >= 0) {
        const ci = cityData[pi].cities.indexOf(parts[1]);
        cityPickerIndex.value = [pi, ci >= 0 ? ci : 0];
        cityColumns.value = [provinces, cityData[pi].cities];
      }
    }
  }
};

onShow(() => {
  if (!checkLogin()) return;
});

onMounted(async () => {
  await userStore.fetchUserInfo();
  initForm();
});

const onCityColumnChange = (e) => {
  const { column, value } = e.detail;
  if (column === 0) {
    cityColumns.value = [provinces, cityData[value].cities];
    cityPickerIndex.value = [value, 0];
  } else {
    cityPickerIndex.value = [cityPickerIndex.value[0], value];
  }
};

const onCityChange = (e) => {
  const [pi, ci] = e.detail.value;
  const province = provinces[pi];
  const city = cityData[pi].cities[ci];
  form.value.city = `${province} ${city}`;
};

const chooseAvatar = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ["compressed"],
    success: async (res) => {
      const tempPath = res.tempFilePaths[0];
      try {
        uni.showLoading({ title: "上传中..." });
        const data = await uploadImage(tempPath);
        form.value.avatar_url = data.url;
        uni.hideLoading();
      } catch (e) {
        uni.hideLoading();
        uni.showToast({ title: "头像上传失败", icon: "none" });
      }
    },
  });
};

const handleSave = async () => {
  if (!form.value.nickname.trim()) {
    uni.showToast({ title: "昵称不能为空", icon: "none" });
    return;
  }
  loading.value = true;
  try {
    await updateMyInfo({
      nickname: form.value.nickname.trim(),
      avatar_url: form.value.avatar_url || null,
      city: form.value.city || null,
      bio: form.value.bio.trim() || null,
      identity: form.value.identity,
    });
    await userStore.fetchUserInfo();
    uni.showToast({ title: "保存成功", icon: "success" });
    setTimeout(() => uni.navigateBack(), 800);
  } catch (e) {
    console.error(e);
  } finally {
    loading.value = false;
  }
};
</script>

<template>
  <view class="profile-page">
    <view class="avatar-section" @tap="chooseAvatar">
      <image
        v-if="form.avatar_url"
        class="avatar-img"
        :src="resolveImageUrl(form.avatar_url)"
        mode="aspectFill"
      />
      <view v-else class="avatar-img avatar-placeholder">
        <uni-icons type="camera-filled" size="32" color="#94a3b8" />
      </view>
      <text class="avatar-tip">点击更换头像</text>
    </view>

    <view class="form-card">
      <view class="field">
        <text class="label">昵称</text>
        <input
          v-model="form.nickname"
          class="input"
          maxlength="20"
          placeholder="请输入昵称"
          placeholder-class="placeholder"
        />
      </view>

      <view class="field">
        <text class="label">个人简介</text>
        <textarea
          v-model="form.bio"
          class="textarea"
          maxlength="120"
          placeholder="一句话介绍自己"
          placeholder-class="placeholder"
          :auto-height="false"
        />
      </view>

      <view class="field">
        <text class="label">所在城市</text>
        <picker
          mode="multiSelector"
          :value="cityPickerIndex"
          :range="cityColumns"
          @columnchange="onCityColumnChange"
          @change="onCityChange"
        >
          <view class="picker-value">
            <text :class="form.city ? '' : 'placeholder'">{{ form.city || '请选择城市' }}</text>
            <uni-icons type="right" size="16" color="#cbd5e1" />
          </view>
        </picker>
      </view>

      <view class="field">
        <text class="label">身份</text>
        <view class="identity-toggle">
          <view
            v-for="opt in identityOptions"
            :key="opt.value"
            class="toggle-btn"
            :class="{ active: form.identity === opt.value }"
            @tap="form.identity = opt.value"
          >
            <text class="toggle-text">{{ opt.label }}</text>
          </view>
        </view>
      </view>
    </view>

    <button class="save-btn" :loading="loading" :disabled="loading" @tap="handleSave">
      保存
    </button>
  </view>
</template>

<style scoped>
.profile-page {
  min-height: 100vh;
  background: #f5f6fa;
  padding: 32rpx;
}

.avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 32rpx;
}

.avatar-img {
  width: 160rpx;
  height: 160rpx;
  border-radius: 80rpx;
  border: 4rpx solid #e2e8f0;
}

.avatar-placeholder {
  background: #f1f5f9;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-tip {
  font-size: 24rpx;
  color: #6366f1;
  margin-top: 12rpx;
}

.form-card {
  background: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  margin-bottom: 40rpx;
}

.field {
  margin-bottom: 28rpx;
}

.field:last-child {
  margin-bottom: 0;
}

.label {
  display: block;
  font-size: 26rpx;
  color: #64748b;
  margin-bottom: 12rpx;
  font-weight: 500;
}

.input {
  width: 100%;
  height: 80rpx;
  background: #f8fafc;
  border: 2rpx solid #e2e8f0;
  border-radius: 12rpx;
  padding: 0 24rpx;
  font-size: 28rpx;
  color: #1e293b;
  box-sizing: border-box;
}

.textarea {
  width: 100%;
  height: 160rpx;
  background: #f8fafc;
  border: 2rpx solid #e2e8f0;
  border-radius: 12rpx;
  padding: 20rpx 24rpx;
  font-size: 28rpx;
  color: #1e293b;
  box-sizing: border-box;
}

.placeholder {
  color: #94a3b8;
}

.picker-value {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80rpx;
  background: #f8fafc;
  border: 2rpx solid #e2e8f0;
  border-radius: 12rpx;
  padding: 0 24rpx;
  font-size: 28rpx;
  color: #1e293b;
}

.identity-toggle {
  display: flex;
  gap: 16rpx;
}

.toggle-btn {
  flex: 1;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f6fa;
  border-radius: 12rpx;
  border: 2rpx solid #e2e8f0;
}

.toggle-btn.active {
  background: rgba(99, 102, 241, 0.12);
  border-color: #6366f1;
}

.toggle-text {
  font-size: 28rpx;
  color: #64748b;
}

.toggle-btn.active .toggle-text {
  color: #6366f1;
  font-weight: 600;
}

.save-btn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  background: #6366f1;
  color: #ffffff;
  font-size: 32rpx;
  font-weight: 600;
  border-radius: 16rpx;
  border: none;
}

.save-btn::after {
  border: none;
}

.save-btn[disabled] {
  opacity: 0.6;
}
</style>
