<template>
  <view class="login-wrapper">
    <view class="whiteBg">
      <view class="list">
        <form @submit.prevent="submit">
          <view class="item">
            <view class="acea-row row-middle">
              <image
                  src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/phone_1.png"
                  style="width: 24rpx; height: 34rpx"
              ></image>
              <input
                type="text"
                class="texts"
                placeholder="填写手机号"
                v-model="phone"
                required
              />
            </view>
          </view>
          <view class="item">
            <view class="acea-row row-middle">
              <image
                src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
                style="width: 28rpx; height: 32rpx"
              ></image>
              <input
                type="text"
                placeholder="填写验证码"
                class="codeIput"
                v-model="captcha"
                style="width: 80%"
              />
              <button
                class="code"
                :disabled="disabled"
                :class="disabled === true ? 'on' : ''"
                @click="code"
              >
                {{ text }}
              </button>
            </view>
          </view>
          <view class="item">
            <view class="acea-row row-middle">
              <image
                src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
                style="width: 28rpx; height: 32rpx"
              ></image>
              <input
                  :password="true"
                class="texts"
                placeholder="填写新密码"
                v-model="password"
                required
              />
            </view>
          </view>
          <view class="item">
            <view class="acea-row row-middle">
              <image
                src="https://sb-admin.oss-cn-shenzhen.aliyuncs.com/crmebimage/public/maintain/2025/08/28/1cfaad5f37a64884b3eef985d254606eqzlswf1m9g.png"
                style="width: 28rpx; height: 32rpx"
              ></image>
              <input
                :password="true"
                class="texts"
                placeholder="重复新密码"
                v-model="passwordRP"
                required
              />
            </view>
          </view>
        </form>
      </view>
      <view class="logon" @click="submit">确认修改</view>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { useAppStore } from "@/stores/app";
import { registerVerify, registerReset } from "@/api/user";
import { useSendCode } from "@/hooks/useSendCode";
import { useToast } from "@/hooks/useToast.js";
import { telEncrypt } from "@/utils/util.js";

const appStore = useAppStore();
const { Toast } = useToast();

// Reactive state
const phone = ref("");
const password = ref("");
const passwordRP = ref("");
const captcha = ref("");

const { disabled, text, sendCode } = useSendCode();

onLoad(() => {
  phone.value = appStore.$userInfo?.phone || "";
});

// Send verification code
const code = async () => {
  if (!phone.value) return Toast({ title: "请填写手机号", icon: "none" });
  try {
    const res = await registerVerify(phone.value);
    Toast({ title: res.message, icon: "none" });
    sendCode();
  } catch (err) {
    Toast({ title: err.message, icon: "none" });
  }
};

// h5 login
const submit = async () => {
  if (!phone.value) return Toast({ title: "请填写手机号", icon: "none" });
  if (!captcha.value) return Toast({ title: "请填写验证码", icon: "none" });
  if (!password.value)
    return uni.showToast({ title: "请填写密码", icon: "none" });
  // if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/i.test(password.value))
  //   return uni.showToast({ title: "您输入的密码过于简单", icon: "none" });
  if (password.value !== passwordRP.value)
    return Toast({ title: "两次密码不一致" });

  try {
    const { data } = await registerReset({
      password: password.value,
      captcha: captcha.value,
      account: phone.value,
    });
    Toast({ title: "修改成功", icon: "none" });
    setTimeout(() => {
      uni.navigateTo({
        url: "/pages/users/login/index",
      });
    }, 1000);
  } catch (err) {
    console.log("submit error", err);
    Toast({ title: err, icon: "none" });
  }
};
</script>

<style lang="scss" scoped>
page {
  background: #fff;
}
.appLogin {
  margin-top: 60rpx;
}

.code img {
  width: 100%;
  height: 100%;
}
.acea-row.row-middle {
  input {
    margin-left: 20rpx;
    display: block;
  }
}
.login-wrapper {
  padding: 30rpx;
  .whiteBg {
    margin-top: 100rpx;

    .list {
      border-radius: 16rpx;
      overflow: hidden;

      .item {
        border-bottom: 1px solid #f0f0f0;
        background: #fff;

        .row-middle {
          position: relative;
          padding: 16rpx 45rpx;

          .texts {
            flex: 1;
            font-size: 28rpx;
            height: 80rpx;
            line-height: 80rpx;
            //display: flex;
            //justify-content: center;
            //align-items: center;
          }

          input {
            flex: 1;
            font-size: 28rpx;
            height: 80rpx;
            line-height: 80rpx;
            //display: flex;
            //justify-content: center;
            //align-items: center;
          }

          .code {
            position: absolute;
            right: 30rpx;
            top: 50%;
            color: $theme-color;
            font-size: 26rpx;
            transform: translateY(-50%);
            border: 1px solid $theme-color;
            padding: 6rpx 20rpx;
            border-radius: 12rpx;
            z-index: 99;
          }
        }
      }
    }
    .logon {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 86rpx;
      margin-top: 80rpx;
      background-color: $theme-color;
      border-radius: 16rpx;
      color: #ffffff;
      font-size: 30rpx;
    }
  }
}
</style>
