import Button from '@/components/button'
import message from '@/components/message'
import React, {useState} from 'react'

export const linkToPc = (p: any) => {
  const appid = p
  const redirect = window.location.href
  window.location.href = `https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect?appid=${appid}&usertype=member&redirect_uri=${redirect}`
}

export const getQueryString = (name: string) => {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i')
  const r = window.location.search.substr(1).match(reg)
  if (r !== null) return unescape(r[2])
  return ''
}

function Login() {
  const [value, setValue] = useState('')
  const linkToAuth = () => {
    if (!value) {
      message.info({content: '请输入appid！'})
      return
    }
    linkToPc(value)
  }
  const changeVal = (e: any) => {
    setValue(e.target.value)
  }
  const codes = getQueryString('auth_code')
  return (
    <div>
      <span>请输入appid:</span>
      <input
        onChange={changeVal}
        style={{
          padding: '8px 16px',
          outline: 'none',
          border: '1px solid #ccc',
          borderRadius: '2px',
        }}
      />
      <p />
      <Button onClick={linkToAuth}>点我开始跳转构造扫码连接</Button>
      <p>
        {codes ? '当前codes值=' : ''} {codes}
      </p>
    </div>
  )
}

export default Login
