<template>
  <view class="submitres-page">
	<!-- 状态栏占位 -->
	<!-- 顶部导航栏 -->
    <custom-navbar 
      title="" 
	  backgroundColor="transparent"
      :show-back="true"
    ></custom-navbar>
	
	
	<!-- 入驻成功 -->
	<view class="success-page" v-if="applyState == 1">
		<!-- 内容区域 -->
		<view class="content">
		  <!-- 成功图标 -->
		  <view class="success-icon">
		    <image src="/static/images/submit-success.png" mode="aspectFit" class="icon-image"></image>
		  </view>
		  
		  <!-- 成功标题 -->
		  <text class="success-title">已提交成功</text>
		  
		  <!-- 成功提示 -->
		  <text class="success-tip">您的入驻申请已提交成功3-7个工作日为您反馈审核结果审核通过您即可正常使用</text>
		  
		  <!-- 返回按钮 -->
		  <view class="btn-back" @click="goBack">
		    <text class="btn-text">返回</text>
		  </view>
		</view>
	</view>
    <!-- 入驻失败 -->
	<view class="fail-page" v-if="applyState == 3">
		<!-- 内容区域 -->
		<view class="content">
		  <!-- 成功图标 -->
		  <view class="fail-icon">
		    <image src="/static/images/submit-fail.png" mode="aspectFit" class="icon-image"></image>
		  </view>
		  
		  <!-- 成功标题 -->
		  <text class="success-title">申请驳回</text>
		  
		  <!-- 成功提示 -->
		  <text class="success-tip">
			您的入驻申请被驳回
			驳回原因:{{backMag}}
		  </text>
		  
		  <!-- 按钮 -->
		  <view class="btn-back" @click="goRuzhu">
		    <text class="btn-text">重新申请</text>
		  </view>
		  <view class="fail-back-btn" @click="goBack">
		    <text class="btn-text">返回</text>
		  </view>
		</view>
	</view>
  </view>
</template>

<script>
import request from '../../utils/request';
export default {
  data() {
    return {
      statusBarHeight: 0,
      applyState: 1, //1：申请中 2：审核通过 3：审核失败
      backMag: '',  // 驳回原因
      identity: null, // 添加 identity 到 data 中
    }
  },
  onLoad(options) {
    console.log('页面加载，options:', options);
    
    const systemInfo = uni.getSystemInfoSync()
    this.statusBarHeight = systemInfo.statusBarHeight;
    
    if (options.applyState) {
      this.applyState = Number(options.applyState); // 转为数字
    }
    
    console.log('applyState:', this.applyState, '类型:', typeof this.applyState);
    
    // 优化身份获取逻辑
    const app = getApp();
    
    if (options.identity) {
      this.identity = Number(options.identity);
      console.log('从options获取identity:', this.identity);
    } 
    else if (app.globalData && app.globalData.artisanType) {
      this.identity = app.globalData.artisanType;
      console.log('从globalData获取identity:', this.identity);
    }
    else {
      try {
        const localIdentity = uni.getStorageSync('artisanType');
        if (localIdentity) {
          this.identity = localIdentity;
          console.log('从localStorage获取identity:', this.identity);
        }
      } catch (e) {
        console.log('获取本地存储失败:', e);
      }
    }
    
    // 申请被驳回时获取驳回原因
    if (this.applyState == 3) {
      this.getUserInfo();
    }
  },
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    getUserInfo() {
      console.log('开始执行getUserInfo方法');
      console.log('this.applyState:', this.applyState);
      console.log('this.identity:', this.identity);
      
      // 只有在申请被驳回的情况下才调用接口
      if (this.applyState !== 3) {
        console.log('applyState不是3，跳过');
        return;
      }
      
      // 检查identity是否存在
      if (!this.identity) {
        console.error('identity未定义，无法获取驳回信息');
        this.backMag = '无法获取用户信息，请重新登录';
        return;
      }
      
      request.post('/sj/userSjAuth/details', {
        type: this.identity // 1=手艺人，2=商家
      }).then(result => {
        console.log('申请详情接口返回数据:', result);
        if (result.code === 200) {
          if (result.data) {
            if (result.data.back_text) {
              this.backMag = result.data.back_text;
              console.log('设置backMag为:', this.backMag);
            } else {
              this.backMag = '未知原因，请联系客服';
              console.log('接口返回无back_text字段');
            }
          } else {
            // 接口返回成功但数据为空，可能没有申请记录
            console.log('接口返回成功但data为空');
            this.backMag = '暂无驳回原因信息';
          }
        } else {
          this.backMag = '获取驳回原因失败，请联系客服';
          console.log('接口返回错误:', result?.msg || '未知错误');
        }
      }).catch(err => {
        console.error('获取申请详情失败:', err);
        this.backMag = '获取驳回原因失败，请联系客服';
      }).finally(() => {
        console.log('getUserInfo执行完成');
      });
    },
    
    goRuzhu() {
      uni.redirectTo({
        url: '/pages/ruzhu/ruzhu'
      })
    }
  }
}
</script>

<style>
.status-bar {
  width: 100%;
}

.nav-back {
  position: fixed;
  left: 32rpx;
  top: 0;
  height: 88rpx;
  display: flex;
  align-items: center;
  z-index: 100;
  /* #ifdef H5 */
  top: 0;
  /* #endif */
  /* #ifdef APP-PLUS || MP-WEIXIN */
  top: var(--status-bar-height);
  /* #endif */
}

.back-icon {
  font-size: 36rpx;
  color: #333333;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 178rpx;
}

.success-icon {
  margin-bottom: 80rpx;
  position: relative;
}
.fail-icon {
  margin-bottom: 80rpx;
  position: relative;
}

.success-icon .icon-image {
  width: 392rpx;
  height: 392rpx;
  border-radius: 196rpx;
  box-shadow: 0 8rpx 60rpx 0 rgba(252, 67, 124, 0.30);
}
.fail-icon .icon-image {
  width: 392rpx;
  height: 392rpx;
  border-radius: 196rpx 156rpx 196rpx 196rpx;
  box-shadow: 0 8rpx 60rpx 0 rgba(255, 0, 0, 0.30);
}

.success-title {
  font-size: 40rpx;
  color: #3D3D3D;
  font-weight: 500;
  margin-bottom: 24rpx;
}

.success-tip {
  font-size: 32rpx;
  color: #3D3D3D;
  text-align: center;
  padding: 0 48rpx;
  margin-bottom: 24rpx;
}

.btn-back {
  width: 630rpx;
  height: 96rpx;
  background: #FF4767;
  border-radius: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-text {
  font-size: 40rpx;
  color: #FFFFFF;
  font-weight: 350;
}
.fail-back-btn {
  width: 630rpx;
  height: 96rpx;
  background: #FFFFFF;
  border-radius: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1rpx solid #FF4767;
  margin-top: 32rpx;
}
.fail-back-btn .btn-text {
	color: #FF4767;
}

.submitres-page /deep/ .back-btn {
  background: transparent !important; 
  background-color: transparent !important;
}

/* 平台适配 */
/* #ifdef MP-WEIXIN */
.nav-back {
  top: calc(var(--status-bar-height) + constant(safe-area-inset-top));
  top: calc(var(--status-bar-height) + env(safe-area-inset-top));
}
/* #endif */
</style> 