﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Text.Json;

namespace WinFormsApp2
{
    public partial class Form2 : Form
    {
        private static readonly HttpClient client;
        static Form2()
        {
            var handler = new HttpClientHandler
            {
                UseProxy = false
            };

            client = new HttpClient(handler);
        }
        public Form2()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var payload = new
                {
                    email = textBox1.Text,
                    password = textBox2.Text
                };

                var json = JsonSerializer.Serialize(payload);

                using var content = new StringContent(
                    json,
                    Encoding.UTF8,
                    "application/json"
                );

                var response = await client.PostAsync(
                    "http://127.0.0.1:8000/login",
                    content
                );

                var body = await response.Content.ReadAsStringAsync();

                MessageBox.Show(
                    $"Status: {response.StatusCode}\n\n{body}",
                    "RESULT"
                );
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {

                    Form1 form1 = new Form1();
                    form1.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Ошибка");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ERROR");
            }
        }
    }
}
