<template>
  <view class="add-status-page">
    <!-- 主体内容 -->
    <scroll-view class="content" scroll-y>
      <!-- 实况描述输入框 -->
      <view class="form-item">
        <text class="label">实况描述</text>
        <textarea
          class="textarea"
          placeholder="请输入活动实况内容，也可打卡签到"
          v-model="statusDesc"
          maxlength="500"
          @input="handleTextInput"
        ></textarea>
        <text class="count">{{ currentLength }}/500</text>
      </view>

      <!-- 活动照片上传 -->
      <view class="form-item">
        <text class="label">活动照片</text>
        <view class="upload-area">
          <!-- 已上传图片 -->
          <view class="uploaded-img" v-for="(img, index) in imgList" :key="index">
            <image :src="img" mode="aspectFill"></image>
            <text class="del-img" @click="deleteImg(index)">×</text>
          </view>
          <!-- 上传按钮（最多3张） -->
          <view class="upload-btn" @click="chooseImg" v-if="imgList.length < 3">
            <text class="plus-icon">+</text>
            <text class="tips">最多3张图片</text>
          </view>
        </view>
      </view>

      <!-- 签到地点 -->
      <view class="form-item">
        <text class="label">签到地点</text>
        <view class="location-area">
          <text class="location-text">{{ location }}</text>
          <view class="re-locate" @click="reLocate">
			  <uni-icons type="refreshempty" size="14" color="#2196f3" class="re-icon"></uni-icons>
			  重新定位
			  </view>
        </view>
      </view>
    </scroll-view>

    <!-- 底部提交按钮 -->
    <view class="submit-btn-area">
      <button class="submit-btn" @click="submitStatus">提交</button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
// import { chooseImage, uploadFile, getLocation } from '@dcloudio/uni-app';

// 实况描述内容
const statusDesc = ref('');
// 输入字符长度
const currentLength = ref(0);
// 图片列表
const imgList = ref([]);
// 定位信息
const location = ref('山东省日照市东港区学院路');

// 监听输入框字数变化
const handleTextInput = (e) => {
  currentLength.value = e.detail.value.length;
};

// 选择图片
const chooseImg = () => {
  uni.chooseImage({
    count: 3 - imgList.value.length, // 最多可选择的数量
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      // 临时文件路径转存到图片列表
      imgList.value = [...imgList.value, ...res.tempFilePaths];
    },
    fail: (err) => {
      uni.showToast({ title: '选择图片失败', icon: 'none' });
      console.error('选择图片失败：', err);
    }
  });
};

// 删除图片
const deleteImg = (index) => {
  imgList.value.splice(index, 1);
};

// 重新定位
const reLocate = () => {
  uni.showLoading({ title: '定位中...' });
  uni.getLocation({
    type: 'gcj02',
    success: (res) => {
      uni.hideLoading();
      // 实际项目中可调用逆地理编码接口转换为具体地址，这里模拟
      location.value = '山东省日照市东港区学院路（新定位）';
      uni.showToast({ title: '定位成功', icon: 'success' });
    },
    fail: (err) => {
      uni.hideLoading();
      uni.showToast({ title: '定位失败，请检查权限', icon: 'none' });
      console.error('定位失败：', err);
    }
  });
};

// 提交实况
const submitStatus = () => {
  // 简单校验
  if (!statusDesc.value.trim()) {
    uni.showToast({ title: '请输入实况描述', icon: 'none' });
    return;
  }

  // 模拟提交逻辑（实际项目中可上传图片+提交表单）
  uni.showLoading({ title: '提交中...' });
  
  // 如需上传图片，可循环调用uploadFile
  // 示例：
  // const uploadPromises = imgList.value.map(img => {
  //   return uploadFile({
  //     url: '你的上传接口',
  //     filePath: img,
  //     name: 'file'
  //   });
  // });
  // Promise.all(uploadPromises).then(res => { /* 处理上传结果 */ });

  setTimeout(() => {
    uni.hideLoading();
    uni.showToast({ title: '添加成功', icon: 'success' });
    // 提交成功后可返回上一页
    uni.navigateBack();
  }, 1000);
};
</script>

<style scoped>
/* 页面整体样式 */
.add-status-page {
  width: 100%;
  height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

/* 主体内容 */
.content {
  flex: 1;
  padding: 15px 0px;
}

/* 表单项通用样式 */
.form-item {
  background-color: #fff;
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 15px;
}
.label {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 10px;
  display: block;
}

/* 输入框样式 */
.textarea {
  width: 100%;
  min-height: 120px;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
}
.count {
  text-align: right;
  font-size: 12px;
  color: #999;
  margin-top: 5px;
  display: block;
}

/* 图片上传区域 */
.upload-area {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.uploaded-img {
  width: 80px;
  height: 80px;
  position: relative;
}
.uploaded-img image {
  width: 100%;
  height: 100%;
  border-radius: 4px;
}
.del-img {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  background-color: #ff4444;
  color: #fff;
  border-radius: 50%;
  font-size: 16px;
}
.upload-btn {
  width: 80px;
  height: 80px;
  border: 1px dashed #ddd;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.plus-icon {
  font-size: 24px;
  color: #999;
  margin-bottom: 5px;
}
.tips {
  font-size: 12px;
  color: #999;
}

/* 定位区域 */
.location-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}
.location-text {
  font-size: 14px;
  color: #333;
}
.re-locate {
  font-size: 14px;
  color: #007aff;
  display: flex;
  align-items: center;
}
.re-icon{
	color: #007aff;
}
/* .re-locate::before {
  content: '';
  width: 16px;
  height: 16px;
  background: url(/static/logo.png) no-repeat center;
  background-size: 100%;
  margin-right: 5px;
} */

/* 提交按钮 */
.submit-btn-area {
  padding: 15px;
}
.submit-btn {
  width: 100%;
  height: 48px;
  line-height: 48px;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
}
</style>