import { Modal, Button, Checkbox, Form, Input } from 'antd';

import _service from '@netuno/service-client';

function LoginModal({isLoginModalOpen, setIsLoginModalOpen}) {
  const handleLoginOk = () => {
    setIsLoginModalOpen(false);
  };
  const handleLoginCancel = () => {
    setIsLoginModalOpen(false);
  };

  const onFinish = values => {
    console.log('Success:', values);
    _service({
      url: "/services/_auth",
      method: "POST",
      data: {...values, jwt: true },
      success: (response) => {
        if (response.json) {
          console.log("Service Response", response.json);
          localStorage.setItem('access_token', response.json.access_token);
        }
      },
      fail: (e) => {
        console.log("Service Error", e);
      }
    });
  };

  const onFinishFailed = errorInfo => {
    console.log('Failed:', errorInfo);
  };

  return (
    <>

      <Modal
        title="Fazer login"
        closable={{ 'aria-label': 'Custom Close Button' }}
        open={isLoginModalOpen}
        onOk={handleLoginOk}
        onCancel={handleLoginCancel}
        okButtonProps={{ style: { display: 'none' } }}
      >

        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          style={{ maxWidth: 600 }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >

          <Form.Item
            label="Username"
            name="username"
            rules={[{ required: true, message: 'Please input your username!' }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="Password"
            name="password"
            rules={[{ required: true, message: 'Please input your password!' }]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item label={null}>
            <Button type="primary" htmlType="submit">
              Submit
            </Button>
          </Form.Item>

        </Form>

      </Modal>
    </>
  );
}

export default LoginModal; 
