import { Image, Input, Text, View } from '@tarojs/components'
import styles from './index.module.scss'
import useLogin from '@/use/useLogin'
import classnames from "classnames";
import { formatImgUrl } from '@/common/fotmat'
import { useEffect, useState } from 'react';
import { alert } from '@/common/common';
import CloseBtn from '@/components/closeBtn';
import SuccessBind from './component/successBind';
import Taro, { useDidShow } from "@tarojs/taro";
export default () => {
  useLogin()

  useDidShow(() => {
    onClipboardData()
  })

  const [submitData, setSubmitData] = useState({
    code: '',
  })
  
  const onSubmit = () => {
    if(!submitData.code) alert.error('请输入邀请码')
  }

  const onInputCode = (e) => {
    setSubmitData((val) => ({...val, code:e.detail.value}))
  }

  const oncloseEven = () => {
    setSubmitData((val) => ({...val, code:''}))
  }

  const onScanCode = () => {
    Taro.scanCode({
      success: (res) => {
        console.log(res)
      }
    })
  }

  //获取粘贴版内容
  const onClipboardData = () => {
    Taro.getClipboardData({
      success: function (res){
        console.log('res.data:::', res.data)
      }
    })
  }
 
  return (
    <View className={styles.bindSalesmanPage_main}>
      <View className={styles.header_image}>
        <Image src={formatImgUrl('/mall/invite_code_bg.png')} mode="aspectFill"/>
      </View>
      <View className={styles.inputCode}>
        <Input value={submitData.code} placeholder='请输入邀请码' onInput={(e) => onInputCode(e)}/>
        <View className={styles.close_btn} >
          {submitData.code&&<CloseBtn styleObj={{backgroundColor: '#ccc', color: '#fff'}} onClose={oncloseEven}/>}
        </View>
        <View className={styles.scan_code} onClick={() => onScanCode()}>
          <Text className={classnames('iconfont icon-saomazhifu', styles.miconfont)}></Text>
        </View>
      </View>
      <View className={styles.btns} onClick={onSubmit}>提交</View>
      <View className={styles.message}>
        <Text>温馨提示</Text>
        <Text>1. 新用户授权手机号码后，7天内可以输入邀请码，超过时间不能输入。</Text>
        <Text>2. 一个手机只能输入一次邀请码。输入邀请码。</Text>
      </View>
      <View className='common_safe_area_y'></View>
      <SuccessBind show={false}/>
    </View>
  )
}

