<template>
  <view class="page">
    <view class="card">
      <view class="title">首页输入（测试存储）</view>
      <textarea
        v-model="draftText"
        class="input"
        placeholder="请输入要保存的文本"
        maxlength="200"
      />
      <button type="primary" @click="saveText">保存文本</button>
	  
	  <text class="label">webView路径：</text>
	  <input class="input" v-model="webviewUrl" @input="onWebViewKeyInput" placeholder="webview路径" />
	  
	  <button type="default" @click="goWebView">测试 WebView</button>
      <view class="preview">
        <text class="label">当前已保存：</text>
        <text class="value">{{ savedText || '暂无' }}</text>
      </view>
    </view>
  </view>
</template>

<script>
import { STORAGE_KEYS } from '@/common/env';
import { get } from '@/common/request';
import { API } from '@/common/api';

const STORAGE_KEY = 'demo_saved_text';

export default {
  data() {
    return {
      draftText: '',
      savedText: '',
	  
	  webviewUrl: '',
	  
      loadingProfile: false
    };
  },
  onShow() {
    const token = uni.getStorageSync(STORAGE_KEYS.TOKEN);
    if (!token) {
      uni.navigateTo({ url: '/pages/login/login' });
      return;
    }
    this.fetchUserProfile();
    this.savedText = uni.getStorageSync(STORAGE_KEY) || '';
    if (!this.draftText) {
      this.draftText = this.savedText;
    }
	
	// 获取appConfig信息的webview地址
	const appConfig = uni.getStorageSync(STORAGE_KEYS.APP_CONFIG) || '';
	if (appConfig && appConfig.data && appConfig.data.webview) {
		this.webviewUrl = appConfig.data.webview.baseUrl;
	}
  },
  methods: {
    async fetchUserProfile() {
      if (this.loadingProfile) return;
      this.loadingProfile = true;
      try {
        const res = await get(API.USER_PROFILE, { platformType: 1 });
        const data = res && res.data ? res.data : res;
        if (data) {
          uni.setStorageSync(STORAGE_KEYS.USER_INFO, data);
        }
      } catch (err) {
        console.log('获取用户信息失败：', err && err.message ? err.message : err);
      } finally {
        this.loadingProfile = false;
      }
    },
    goWebView() {
		uni.navigateTo({ url: '/pages/wv-test/wv-test' });
	},
	onWebViewKeyInput: function(event) {
		this.webviewUrl = event.target.value;
		let appConfig = uni.getStorageSync(STORAGE_KEYS.APP_CONFIG) || '';
		if (appConfig && appConfig.data && appConfig.data.webview) {
			appConfig.data.webview.baseUrl = this.webviewUrl;
			uni.setStorageSync(STORAGE_KEYS.APP_CONFIG, appConfig);
		}
	},
	saveText() {
      const text = (this.draftText || '').trim();
      uni.setStorageSync(STORAGE_KEY, text);
      this.savedText = text;
      uni.showToast({ title: '已保存', icon: 'success' });
    }
  }
};
</script>

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

.card {
  background: #fff;
  border-radius: 16rpx;
  padding: 32rpx;
  box-shadow: 0 12rpx 32rpx rgba(22, 41, 88, 0.08);
}

.title {
  font-size: 34rpx;
  font-weight: 600;
  margin-bottom: 24rpx;
}

.input {
  width: 100%;
  min-height: 60rpx;
  padding: 10rpx;
  border-radius: 12rpx;
  background: #f3f5fa;
  margin-bottom: 24rpx;
  font-size: 28rpx;
}

.preview {
  margin-top: 24rpx;
  font-size: 26rpx;
  color: #5b6777;
}

.label {
  color: #8a97a8;
}

.value {
  color: #1f2d3d;
}
</style>
