<template>
  <view class="change-pwd-page">
    <view class="form-container">
      <!-- 原密码 -->
      <view class="form-item">
        <view class="form-label">原密码</view>
        <view class="input-wrapper">
          <input 
            v-model="formData.oldPassword"
            :password="!showOldPwd"
            placeholder="请输入原密码"
            class="form-input"
          />
          <view class="eye-icon" @click="showOldPwd = !showOldPwd">
            <text>{{ showOldPwd ? '👁️' : '👁️‍🗨️' }}</text>
          </view>
        </view>
      </view>
      
      <!-- 验证码 -->
      <view class="form-item">
        <view class="form-label">验证码</view>
        <view class="input-wrapper">
          <input 
            v-model="formData.code"
            type="number"
            maxlength="6"
            placeholder="请输入验证码"
            class="form-input code-input"
          />
          <button 
            class="code-btn"
            :disabled="countdown > 0"
            @click="sendCode"
          >
            {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
          </button>
        </view>
      </view>
      
      <!-- 新密码 -->
      <view class="form-item">
        <view class="form-label">新密码</view>
        <view class="input-wrapper">
          <input 
            v-model="formData.newPassword"
            :password="!showNewPwd"
            placeholder="请输入新密码（6-20位）"
            class="form-input"
          />
          <view class="eye-icon" @click="showNewPwd = !showNewPwd">
            <text>{{ showNewPwd ? '👁️' : '👁️‍🗨️' }}</text>
          </view>
        </view>
      </view>
      
      <!-- 确认新密码 -->
      <view class="form-item">
        <view class="form-label">确认新密码</view>
        <view class="input-wrapper">
          <input 
            v-model="formData.confirmPassword"
            :password="!showConfirmPwd"
            placeholder="请再次输入新密码"
            class="form-input"
          />
          <view class="eye-icon" @click="showConfirmPwd = !showConfirmPwd">
            <text>{{ showConfirmPwd ? '👁️' : '👁️‍🗨️' }}</text>
          </view>
        </view>
      </view>
      
      <!-- 提交按钮 -->
      <button 
        class="submit-btn"
        :disabled="!canSubmit"
        :loading="submitting"
        @click="handleSubmit"
      >
        确定修改
      </button>
    </view>
  </view>
</template>

<script>
import { changePassword, sendSms } from '@/api/miao.js';

export default {
  data() {
    return {
      formData: {
        oldPassword: '',
        code: '',
        newPassword: '',
        confirmPassword: ''
      },
      showOldPwd: false,
      showNewPwd: false,
      showConfirmPwd: false,
      countdown: 0,
      submitting: false,
      timer: null,
      userInfo: null
    }
  },
  
  computed: {
    canSubmit() {
      return this.formData.oldPassword.length >= 6 &&
             this.formData.code.length === 6 &&
             this.formData.newPassword.length >= 6 &&
             this.formData.confirmPassword === this.formData.newPassword;
    }
  },
  
  onLoad() {
    this.userInfo = uni.getStorageSync('userInfo');
  },
  
  onUnload() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  
  methods: {
    // 发送验证码
    async sendCode() {
      if (!this.userInfo || !this.userInfo.mobile) {
        uni.showToast({
          title: '请先登录',
          icon: 'none'
        });
        return;
      }
      
      try {
        const res = await sendSms({
          mobile: this.userInfo.mobile,
          event: 'changepwd'
        });
        
        if (res.code === 0) {
          uni.showToast({
            title: '验证码已发送',
            icon: 'success'
          });
          
          this.countdown = 60;
          this.timer = setInterval(() => {
            this.countdown--;
            if (this.countdown <= 0) {
              clearInterval(this.timer);
            }
          }, 1000);
        }
      } catch (error) {
        uni.showToast({
          title: error.msg || '发送失败',
          icon: 'none'
        });
      }
    },
    
    // 提交
    async handleSubmit() {
      if (this.formData.newPassword !== this.formData.confirmPassword) {
        uni.showToast({
          title: '两次密码不一致',
          icon: 'none'
        });
        return;
      }
      
      if (this.formData.newPassword === this.formData.oldPassword) {
        uni.showToast({
          title: '新密码不能与原密码相同',
          icon: 'none'
        });
        return;
      }
      
      this.submitting = true;
      
      try {
        const res = await changePassword({
          old_password: this.formData.oldPassword,
          new_password: this.formData.newPassword,
          code: this.formData.code
        });
        
        if (res.code === 0) {
          uni.showToast({
            title: '修改成功，请重新登录',
            icon: 'success'
          });
          
          // 清除登录信息
          uni.removeStorageSync('token');
          uni.removeStorageSync('userInfo');
          
          setTimeout(() => {
            uni.reLaunch({
              url: '/pages/sub-pages/login/index'
            });
          }, 1500);
        }
      } catch (error) {
        uni.showToast({
          title: error.msg || '修改失败',
          icon: 'none'
        });
      } finally {
        this.submitting = false;
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.change-pwd-page {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx 30rpx;
}

.form-container {
  background-color: #fff;
  border-radius: 20rpx;
  padding: 40rpx 30rpx;
  
  .form-item {
    margin-bottom: 40rpx;
    
    .form-label {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 15rpx;
      font-weight: 500;
    }
    
    .input-wrapper {
      position: relative;
      display: flex;
      align-items: center;
      
      .form-input {
        flex: 1;
        height: 90rpx;
        padding: 0 20rpx;
        background-color: #f5f5f5;
        border-radius: 45rpx;
        font-size: 28rpx;
        
        &.code-input {
          margin-right: 15rpx;
        }
      }
      
      .code-btn {
        width: 180rpx;
        height: 90rpx;
        line-height: 90rpx;
        background-color: #FF4757;
        color: #fff;
        border-radius: 45rpx;
        font-size: 24rpx;
        border: none;
        
        &[disabled] {
          background-color: #ccc;
        }
      }
      
      .eye-icon {
        position: absolute;
        right: 30rpx;
        font-size: 32rpx;
      }
    }
  }
  
  .submit-btn {
    width: 100%;
    height: 90rpx;
    line-height: 90rpx;
    background: linear-gradient(90deg, #FF6B6B, #FF4757);
    color: #fff;
    border-radius: 45rpx;
    font-size: 32rpx;
    font-weight: bold;
    border: none;
    margin-top: 20rpx;
    
    &[disabled] {
      opacity: 0.6;
    }
  }
}
</style>

