<template>
  <view class="login">
    <view class="login-form">
      <uv-form
        labelPosition="top"
        labelWidth="80"
        :labelStyle="labelStyle"
        :model="formData"
        ref="formRef"
        :rules="rules"
      >
        <uv-form-item label="手机号" prop="userName" borderBottom>
          <view class="pbone-input">
            <input
              class="login-input"
              clearable
              maxlength="11"
              v-model="formData.userName"
              border="none"
              type="number"
              placeholder="请输入手机号"
            />
          </view>
        </uv-form-item>
        <uv-form-item label="验证码" prop="code" borderBottom>
          <view class="pbone-input">
            <input
              class="login-input"
              clearable
              v-model="formData.code"
              border="none"
              type="number"
              placeholder="请输入验证码"
            />
            <view class="code">
              <uv-code
                ref="codeRef"
                @change="codeChange"
                @start="codeDisabledChange(true)"
                @end="codeDisabledChange(false)"
                changeText="Xs"
              ></uv-code>
              <uv-button
                @click="getCode"
                :disabled="codeDisabled || !codeDisabledType"
                :text="code"
                :custom-style="customStyle"
              />
            </view>
          </view>
        </uv-form-item>
        <uv-form-item label="密码" prop="p1" borderBottom>
          <view class="pbone-input">
            <input
              class="login-input"
              clearable
              v-model="formData.p1"
              border="none"
              type="password"
              placeholder="请输入密码"
            />
          </view>
        </uv-form-item>
        <uv-form-item label="确认密码" prop="p2" borderBottom>
          <view class="pbone-input">
            <input
              class="login-input"
              clearable
              v-model="formData.p2"
              border="none"
              type="password"
              placeholder="请输入密码"
            />
          </view>
        </uv-form-item>
      </uv-form>
      <view class="tips">
        <text
          >密码由8~16位含有大小写字母、数字、!#$%^&*@符号且不能有三位连续的数字组成（如123）</text
        >
      </view>

      <view class="login-btn">
        <button class="btn" @click="submit">提交</button>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { reactive, ref, computed } from 'vue'
import { getMobileUpdApi, postUserUpdApi } from '@/service/modules/login'
import { isValidPhoneNumber } from '@/utils'

const labelStyle = {
  fontSize: '32rpx',
  fontWeight: 600,
  color: '#333'
}
let validatePassword = (rule: any, value: any, callback: any) => {
  var regA = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*@])[\da-zA-Z!#$%^&*@]{8,16}$/ //密码必须是8位以上、必须含有字母、数字、特殊符号
  var regB = /(123|234|345|456|567|678|789|012)/ //不能有3个连续数字
  if (!value) {
    callback(new Error('密码不能为空'))
  }
  if (!regA.test(value)) {
    callback(new Error('密码必须是8~16位、必须含有大小写字母、数字、特殊符号'))
  } else if (regB.test(value)) {
    callback(new Error('不能有3个连续数字'))
  } else {
    callback()
  }
}
var validateCheckPass = (rule: any, value: any, callback: any) => {
  if (value !== formData.p1) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = reactive({
  userName: {
    type: 'string',
    required: true,
    trigger: ['blur']
  },
  code: {
    type: 'string',
    required: true,
    trigger: ['blur']
  },

  p1: {
    type: 'string',
    required: true,
    trigger: ['blur'],
    validator: validatePassword
  },
  p2: {
    type: 'string',
    required: true,
    validator: validateCheckPass,
    trigger: ['blur']
  }
})

const formRef = ref()
const formData = reactive({
  userName: '',
  p1: '',
  p2: '',
  code: ''
})

// 验证码
const code = ref('获取验证码')
const codeRef = ref()
const codeDisabled = ref(false)
function codeChange(text: string) {
  code.value = text
}
const codeDisabledChange = (flag: boolean) => {
  codeDisabled.value = flag
}

let codeDisabledType = computed(() => {
  return formData.userName && !codeDisabled.value
})
// 获取验证码按钮样式
const customStyle = {
  height: '64rpx',
  width: '200rpx',
  background: 'rgba(110,182,87, .1)',
  borderRadius: '16rpx',
  color: '#6eb657',
  fontWeight: 500,
  lineHeight: '64rpx',
  fontSize: '28rpx'
}
async function getCode() {
  if (!isValidPhoneNumber(formData.userName)) {
    return uni.showToast({
      title: '请检查手机号码格式',
      icon: 'none'
    })
  }

  // 获取验证码
  uni.showLoading({
    title: '正在获取验证码'
  })
  const res = await getMobileUpdApi(formData.userName)
  uni.hideLoading()
  if (!res.data) {
    uni.showToast({
      title: res.msg,
      icon: 'none'
    })
  } else {
    uni.showToast({
      title: res.msg,
      icon: 'none'
    })
  }
  codeRef.value.start()
}

const submit = () => {
  if (!isValidPhoneNumber(formData.userName)) {
    return uni.showToast({
      title: '请检查手机号码格式',
      icon: 'none'
    })
  }

  formRef.value.validate().then(async () => {
    const res = await postUserUpdApi(formData)

    if (res.code === 0) {
      uni.showToast({
        icon: 'none',
        title: '修改成功'
      })
      setTimeout(() => {
        uni.reLaunch({
          url: '/pages/login/index'
        })
      }, 1000)
    }
  })
}
</script>

<style lang="scss" scoped>
.login {
  padding: 0 50rpx;
}
.login-bg {
  position: absolute;
  z-index: -333;
  top: 0;
  left: 0;
  right: 0;
  height: 800rpx;
  background: linear-gradient(#f8f8f0 0%, rgba(255, 255, 255, 0) 100%);
}
.login-title {
  margin-top: 300rpx;
  color: #333;
  font-size: 56rpx;
  font-weight: 500;
}
.login-form {
  margin-top: 80rpx;
  .pbone-input {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .forgot-password {
    color: #999;
  }
  .login-input {
    margin: 20rpx 0 0 10rpx;
  }
  :deep(.uv-form-item) {
    margin-bottom: 40rpx;
  }
  .agreement {
    display: flex;
    font-size: 26rpx;
    font-weight: 400;
    line-height: 40rpx;
    .read-content {
      .primarily-text {
        color: #6eb657;
      }
    }
  }
  .login-btn {
    margin-top: 70rpx;
    .btn {
      width: 640rpx;
      height: 96rpx;
      border-radius: 200rpx;
      background-color: #6eb657;
      text-align: center;
      color: #fff;
      font-size: 40rpx;
      font-weight: 600;
    }
  }
  .login-password {
    margin-top: 50rpx;
    color: #999;
    font-size: 32rpx;
    text-align: center;
    font-weight: 400;
    line-height: 60rpx;
  }
}
.tips {
  color: #666;
}
</style>
