<!-- 账号密码登录 accountLogin  -->
<template>
  <view>
    <!-- 标题栏 -->
    <view class="head-box ss-m-b-60 ss-flex-col">
      <view class="head-title ss-m-r-40">登录</view>
      <view class="head-subtitle">登录后即可使用更多功能</view>
    </view>
	
    <!-- 表单项 -->
    <uni-forms
      ref="accountLoginRef"
      v-model="state.model"
      :rules="state.rules"
      validateTrigger="bind"
    >
      <uni-forms-item name="phone">
        <uni-easyinput placeholder="请输入手机号" v-model="state.model.phone" :inputBorder="false" :maxlength="11"></uni-easyinput>
      </uni-forms-item>

      <uni-forms-item name="password">
        <uni-easyinput
          type="password"
          placeholder="请输入密码"
          v-model="state.model.password"
          :inputBorder="false"
        >
        </uni-easyinput>
      </uni-forms-item>
	  <!-- <uni-forms-item name="captcha">
	    <uni-easyinput placeholder="请输入验证码" v-model="state.model.captcha" :inputBorder="false">
			<template v-slot:right>
			  <image class="captcha-btn" :src="state.captchaImg" mode="heightFix"></image>
			</template>
		</uni-easyinput>
	  </uni-forms-item> -->
    </uni-forms>
	<view class="forgot_box">
		<view class="forgot_btn">忘记密码</view>
	</view>
	<view class="submit_btn" @tap="accountLoginSubmit">登录</view>
  </view>
</template>

<script setup>
  import { ref, reactive, unref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app'
  import sheep from '@/common';
  import { phone, username, password, captcha } from '@/common/validate/form';
  import { closeAuthModal, closeLoginPage } from '@/common/hooks/useModal';
  import AuthUtil from '@/common/api/member/auth';
  import { conformsTo } from 'lodash';

  const accountLoginRef = ref(null);

  const emits = defineEmits(['onConfirm']);

  const props = defineProps({
    agreeStatus: {
      type: [Boolean, null],
      default: null,
    },
  })

  // 数据
  const state = reactive({
    model: {
		clientType: 0,
		// username: '', // 账号
		phone: '', 
		password: '', // 密码
		// captcha: '', //验证码
		// captchaId: '',
    },
    rules: {
		// username,
		phone,
		password,
		// captcha,
    },
	//captchaImg:''
  });
  onLoad(async (options) => {
    //getCaptcha()
  })
  //获取图形验证码
  async function getCaptcha(){
	  const { code, data } = await AuthUtil.getCaptcha()
	  if (code === 0) {
		  state.captchaImg = data.imgPath
		  state.model.captchaId = data.captchaId
	  }
  }

  // 账号登录
  async function accountLoginSubmit() {
    // 表单验证
    const validate = await unref(accountLoginRef).validate().catch((error) => {
        console.log('error: ', error)
    })
    if (!validate) return
	if(!props.agreeStatus){
		sheep.$helper.toast('请选择同意协议')
	}
    // 提交数据
    const { code, data } = await AuthUtil.login(state.model);
    if (code === 1) {
		sheep.$store('task').resetTaskData()
		//closeAuthModal()
		closeLoginPage()
    }else{
		//getCaptcha()
	}
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';
</style>
