<script setup lang="ts">
import { ref } from 'vue'

// 在unibest框架中，使用definePage宏配置页面属性
definePage({
  // 页面样式配置
  style: {
    // 导航栏标题文本 - 这就是设置头部导航栏标题的属性
    navigationBarTitleText: '%me.feedback%',
    // 导航栏样式：default(默认)或custom(自定义)
    navigationStyle: 'default',
    backgroundColor: '#ffffff',
    // 可以添加其他配置如背景色、文字颜色等
    navigationBarBackgroundColor: '#ffffff', // 导航栏背景色
    // navigationBarTextStyle: 'black', // 导航栏标题颜色，仅支持black/white
  },
})

const feedbackContent = ref('')
const phoneOrEmail = ref('')
const feedbackImages = ref([])

function uploadImage() {
  if (feedbackImages.value.length >= 6) {
    uni.showToast({
      title: '最多上传6张照片',
      icon: 'none',
    })
    return
  }
  uni.chooseImage({
    count: 6 - feedbackImages.value.length,
    sourceType: ['album'],
    success: (res) => {
      feedbackImages.value = [...feedbackImages.value, ...(res.tempFilePaths as string[])]
    },
  })
}

function previewImage(index: number) {
  uni.previewImage({
    urls: feedbackImages.value,
    current: feedbackImages.value[index],
  })
}

function removeImage(index: number) {
  feedbackImages.value.splice(index, 1)
}
</script>

<template>
  <view class="feedback">
    <view class="feedback-section">
      <view class="section-title required">
        意见或建议
      </view>
      <wd-textarea v-model="feedbackContent" class="feedback-text" clear-trigger="focus" :maxlength="200" clearable show-word-limit />
    </view>
    <view class="feedback-section">
      <view class="section-title">
        上传照片({{ feedbackImages.length }}/6)
      </view>
      <div class="upload-area">
        <view v-for="(item, index) in feedbackImages" :key="index" class="upload-box uploaded">
          <image class="upload-image" :src="item" mode="scaleToFill" @click="previewImage(index)" />
          <image
            class="remove-icon"
            src="/static/images/me/remove.png"
            mode="scaleToFill"
            @click="removeImage(index)"
          />
        </view>
        <view v-if="feedbackImages.length < 6" class="upload-box" @click="uploadImage">
          <image class="upload-icon" src="/static/images/me/upload.png" mode="scaleToFill" />
        </view>
      </div>
    </view>
    <view class="feedback-section">
      <view class="section-title">
        邮箱/电话
      </view>
      <div class="phone-area">
        <input
          v-model="phoneOrEmail"
          placeholder="请留下您的电话或邮箱，方便我们联系您"
          placeholder-class="input-placeholder"
        >
      </div>
    </view>
    <view class="feedback-btn">
      <ff-button :disabled="feedbackContent.length === 0">
        提交
      </ff-button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.feedback {
  height: 100%;
  background-color: #fff;

  .feedback-section {
    padding: 0 28rpx 32rpx 28rpx;
    .section-title {
      font-size: 32rpx;
      color: #24272c;
      padding: 32rpx 0;

      &.required {
        &::after {
          content: '*';
          font-size: 32rpx;
          line-height: 32rpx;
          color: #ff4d4f;
        }
      }
    }

    .upload-area {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      column-gap: 24rpx;
      row-gap: 38rpx;
      padding: 23rpx;
      background-color: #f8fafb;
      border-radius: 12rpx;
      .upload-box {
        &.uploaded {
          border: none;
        }
        width: 100%;
        aspect-ratio: 1/1;
        background-color: #f8fafb;
        border-radius: 15rpx;
        border: 1px dashed #b6c3cc;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        .upload-icon {
          width: 45rpx;
          height: 45rpx;
        }
        .upload-image {
          border-radius: 15rpx;
          width: 100%;
          height: 100%;
        }
        .remove-icon {
          width: 40rpx;
          height: 40rpx;
          right: 0;
          top: 0;
          transform: translate(50%, -50%);
          position: absolute;
        }
      }
    }

    .phone-area {
      background-color: #f8fafb;
      border-radius: 12rpx;
      padding: 19rpx 16rpx;
      input {
        font-size: 28rpx;
        line-height: 1.2;
      }
      .input-placeholder {
        color: #999999;
        font-size: 28rpx;
        line-height: 1.2;
      }
    }

    :deep(.feedback-text) {
      background-color: #f8fafb;
      border-radius: 12rpx;
      .wd-textarea__value {
        background-color: #f8fafb;
      }
      .wd-textarea__count {
        background-color: #f8fafb;
      }
    }
  }

  .feedback-btn {
    position: absolute;
    bottom: calc(32rpx + env(safe-area-inset-bottom));
    padding: 0 32rpx;
    width: 100%;
    box-sizing: border-box;
  }
}
</style>
