import React from 'react'
import styled from 'styled-components'
import { Form, Input, Button, message } from 'antd'
import { UserOutlined, LockOutlined } from '@ant-design/icons'
import { useNavigate } from 'react-router-dom'
import { loginUser } from '../services'
import Base64 from 'base-64'

const LoginWrapper = styled.div`
  width:100%;
  height: 100vh;
  background-image: url(${require('../style/img/login_bg.jpeg')});
  background-size: cover;
  display:flex;
  justify-content: center;
  align-items: center;
  .form_wrapper {
    width: 600px;
    height: 640px;
    background: #FFFFFF;
    box-shadow: 0px 8px 32px 0px rgba(0,0,0,0.32);
    border-radius: 10px;
  }
  .welcome {
    justify-content: center;
    display: flex;
    font-size: 40px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    line-height: 56px;
    letter-spacing: 4px;
    margin-top:80px;
  }
  .line_wrapper {
    width:100%;
    justify-content: center;
    display: flex;
  }
  .line {
    width: 100px;
    height: 10px;
    border-bottom: 4px solid #4883fb;
  }
  .login_form {
    margin: 80px 70px 80px;
  }
  .form_item {
    margin-bottom:40px;
  }
  .form_input {
    height:52px;
    background: #FFFFFF;
    box-shadow: inset 2px 2px 12px 0px rgba(43,67,141,0.08);
    border-radius: 10px;
  }
  .btns {
    display:flex;
    justify-content: center;
  }
  .btn {
    width: 460px;
    height: 48px;
    background: #4883FB;
    box-shadow: 4px 4px 16px 0px rgba(1,71,170,0.24);
    border-radius: 9px;
    font-size: 16px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #FFFFFF;
    line-height: 30px;
    letter-spacing: 2px;
  }
  .register {
    display: flex;
    justify-content: flex-end;
    color: #1890ff;
    margin: 30px;
    span {
      cursor: pointer;
    }
  }
}`

const FormItem = Form.Item
const { Password } = Input

const LoginView = () => {
  const navigate = useNavigate()
  const [loginForm] = Form.useForm()
  const login = () => {
    loginForm
      .validateFields()
      .then(async () => {
        const { account, password } = loginForm.getFieldValue()
        const { data } = await loginUser({
          username: account,
          password: Base64.encode(password),
        })
        if (data.code === 200) {
          sessionStorage.setItem('user_token', data.data.auth_token)
          sessionStorage.setItem('user_id', data.data.id)
          navigate('/datasource')
        } else {
          message.error(data.msg)
        }
      })
      .catch(err => {
        message.error('请检查表单数据是否完整')
      })
  }
  return (
    <LoginWrapper>
      <div className="form_wrapper">
        <div className="welcome">欢迎登陆</div>
        <div className="line_wrapper">
          <div className="line" />
        </div>
        <Form className="login_form" form={loginForm}>
          <FormItem
            name="account"
            className="form_item"
            rules={[
              {
                required: true,
                message: '请输入账号!',
              },
            ]}>
            <Input prefix={<UserOutlined />} className="form_input" />
          </FormItem>
          <FormItem
            name="password"
            rules={[
              {
                required: true,
                message: '请输入密码!',
              },
            ]}>
            <Password prefix={<LockOutlined />} className="form_input" />
          </FormItem>
        </Form>
        <div className="btns">
          <Button className="btn" type="primary" onClick={() => login()}>
            登陆
          </Button>
        </div>
        <div className="register">
          <span
            onClick={() => {
              navigate('/register')
            }}>
            前往注册
          </span>
        </div>
      </div>
    </LoginWrapper>
  )
}

export default LoginView
