<template>
  <view class="container-login" :style="appThemeStyle">
    <!-- 表单 -->
    <view class="submit-form">
      <!-- 页面头部 -->
      <view class="header">
        <view class="title">
          <text>企业账户注册</text>
        </view>
        <view class="sub-title">
          <text>未注册的手机号登录后将自动注册</text>
        </view>
      </view>

      <!-- 手机号 -->
      <view class="form-item">
        <input class="form-item--input" type="number" v-model="agentstel" maxlength="11" placeholder="请输入手机号码" />
      </view>
      <!-- 请填写商户名称 -->
      <view class="form-item">
        <input class="form-item--input" type="text" v-model="agentsname" placeholder="请填写企业名称" />
      </view>
      <!-- 代理商联系地址 -->
      <view class="form-item">
        <select-region class="form-dizhi" ref="sRegion" v-model="address" />
      </view>
      <!-- 代理商联系地址 -->
      <view class="form-item">
        <input class="form-item--input" type="text" v-model="agentsadress" placeholder="请填写详细地址" />
      </view>
      <!-- 图形验证码 -->
      <!-- <view class="form-item">
        <input class="form-item--input" type="text" v-model="captchaCode" maxlength="5" placeholder="请输入图形验证码" />
        <view class="form-item--parts">
          <view class="captcha" @click="getCaptcha()">
            <image class="image" :src="captcha.base64"></image>
          </view>
        </view>
      </view> -->
      <!-- 短信验证码 -->
      <view class="form-item">
        <input class="form-item--input" type="number" v-model="smsCode" maxlength="6" placeholder="请输入短信验证码" />
        <view class="form-item--parts">
          <view class="captcha-sms" @click="handelSmsCaptcha()">
            <text v-if="!smsState" class="activate">获取验证码</text>
            <text v-else class="un-activate">重新发送({{ times }})秒</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 确认绑定 -->
    <view class="submit-button" @click="handleSubmit()">
      <text>登 录</text>
    </view>
  </view>
</template>

<script>
import store from '@/store'
import * as UserApi from '@/api/user'
import * as CaptchaApi from '@/api/captcha'
import * as Verify from '@/utils/verify'
import * as AppApi from '@/api/app'
import storage from '@/utils/storage'
import SelectRegion from '@/components/select-region/select-region'

// 倒计时时长(秒)
const times = 60

// 表单验证场景
const GET_CAPTCHA = 10
const FORM_SUBMIT = 20

export default {
  data() {
    return {
      // 正在加载
      isLoading: false,
      // 图形验证码信息
      captcha: {},
      // 短信验证码发送状态
      smsState: false,
      // 倒计时
      times,
      // 手机号
      agentstel: '',
      agentsname: '',
      agentsadress: '',
      address: '',
      // 图形验证码
      captchaCode: '',
      // 短信验证码
      smsCode: '',
      wx_userid: storage.get('wx_userid'),
      wx_openid: storage.get('wx_openid')
    }
  },
  components: {
    SelectRegion
  },
  /**
   * 生命周期函数--监听页面加载
   */
  created() {
    // 获取图形验证码
    // this.getCaptcha()
    if (storage.get('wx_userinfo').customertype == '代理商') {
      // 如果已经是代理商则直接登录
      this.$navTo('pages/firm/home/index')
    }
  },

  methods: {
    // 获取图形验证码
    getCaptcha() {
      const app = this
      CaptchaApi.image().then((result) => (app.captcha = result.data))
    },

    // 点击发送短信验证码
    handelSmsCaptcha() {
      const app = this
      if (!app.isLoading && !app.smsState && app.formValidation(GET_CAPTCHA)) {
        app.sendSmsCaptcha()
      }
    },

    // 表单验证
    formValidation(scene = GET_CAPTCHA) {
      const app = this
      // 验证获取短信验证码
      if (scene === GET_CAPTCHA) {
        if (!app.validteMobile(app.agentstel)) {
          return false
        }
      }
      // 验证表单提交
      if (scene === FORM_SUBMIT) {
        if (!app.validteMobile(app.agentstel) || !app.validteSmsCode(app.smsCode)) {
          return false
        }
      }
      return true
    },

    // 验证手机号
    validteMobile(str) {
      if (Verify.isEmpty(str)) {
        this.$toast('请先输入手机号')
        return false
      }
      if (!Verify.isMobile(str)) {
        this.$toast('请输入正确格式的手机号')
        return false
      }
      return true
    },

    // 验证图形验证码
    validteCaptchaCode(str) {
      if (Verify.isEmpty(str)) {
        this.$toast('请先输入图形验证码')
        return false
      }
      return true
    },

    // 验证短信验证码
    validteSmsCode(str) {
      if (Verify.isEmpty(str)) {
        this.$toast('请先输入短信验证码')
        return false
      }
      return true
    },

    // 请求发送短信验证码接口
    sendSmsCaptcha() {
      const app = this
      app.isLoading = true
      AppApi.getSmsCode({mobile: app.agentstel})
        .then((result) => {
          // 显示发送成功
          app.$toast('发送成功')
          // 执行定时器
          app.timer()
        })
        .finally(() => {
          app.isLoading = false
        })
    },

    // 执行定时器
    timer() {
      const app = this
      app.smsState = true
      const inter = setInterval(() => {
        app.times = app.times - 1
        if (app.times <= 0) {
          app.smsState = false
          app.times = times
          clearInterval(inter)
        }
      }, 1000)
    },

    // 点击提交
    handleSubmit() {
      const app = this
      if (!app.isLoading && app.formValidation(FORM_SUBMIT)) {
        app.onSubmitEvent()
      }
    },

    // 确认提交事件
    onSubmitEvent() {
      const app = this
      if (Verify.isEmpty(app.address)) {
        this.$toast('请选择地区')
        return false
      }
      if (Verify.isEmpty(app.agentsname)) {
        this.$toast('请填写商户名称')
        return false
      }
      app.isLoading = true
      let platform = {
        agentscustomerid: storage.get('wx_userid'),
        agentsname: app.agentsname,
        agentsadress: app.agentsadress,
        agentstel: app.agentstel,
        agentsprovince: app.address[0].label,
        agentscity: app.address[1].label,
        agentsarea: app.address[2].label,
        smscode: app.smsCode
      }
      AppApi.companyLogin(platform)
        .then((res) => {
          // 显示操作成功
          app.$toast('登录成功，正在跳转')
          // 登录成功存储企业信息
          storage.set('wx_qyid', res.Data.id)
          storage.set('wx_qym', res.Data.qym)
          setTimeout(() => {
            this.$navTo('pages/firm/home/index')
          }, 1000)
        })
        .finally(() => (app.isLoading = false))
    },

    /**
     * 提交成功-跳转回原页面
     */
    onNavigateBack(delta) {
      uni.navigateBack({
        delta: Number(delta || 1)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container-login {
  padding: 30rpx 60rpx;
  padding-top: 200rpx;
  min-height: 100vh;
  background-color: #fff;
}

// 页面头部
.header {
  margin-bottom: 50rpx;

  .title {
    color: #0081ff;
    font-size: 50rpx;
    font-weight: 900;
  }

  .sub-title {
    margin-top: 20rpx;
    color: #0080ffa9;
    font-size: 25rpx;
  }
}

.shiming {
  margin-top: 90rpx;
}

// 输入框元素
.form-item {
  display: flex;
  padding: 18rpx;
  border-bottom: 1rpx solid #f3f1f2;
  margin-bottom: 25rpx;
  height: 96rpx;
  font-size: 30rpx;
  color: #333;

  &--input {
    font-size: 30rpx;
    letter-spacing: 1rpx;
    flex: 1;
    height: 100%;
    color: #333;
  }

  &--parts {
    min-width: 100rpx;
    height: 100%;
  }

  // 图形验证码
  .captcha {
    height: 100%;

    .image {
      display: block;
      width: 192rpx;
      height: 100%;
    }
  }

  // 短信验证码
  .captcha-sms {
    font-size: 30rpx;
    line-height: 50rpx;
    padding-right: 20rpx;

    .activate {
      color: #0081ff;
    }

    .un-activate {
      color: #9e9e9e;
    }
  }
}

// 提交按钮
.submit-button {
  width: 100%;
  height: 86rpx;
  margin-top: 70rpx;
  background: linear-gradient(to right, $main-bg, $main-bg2);
  color: $main-text;
  border-radius: 80rpx;
  box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.1);
  letter-spacing: 5rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(-20deg, #b721ff 0%, #21d4fd 100%);
}

.form-dizhi {
  color: gray;
}
</style>
