package com.zhangmeng.online.exam.ui.api.form;

import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.api.DataLoad;
import com.zhangmeng.online.exam.ui.api.form.base.Form;
import com.zhangmeng.online.exam.ui.api.model.UserDataLoad;
import com.zhangmeng.online.exam.ui.components.DynamicTableComponent;
import com.zhangmeng.online.exam.ui.components.callBack.DynamicTableComponentCallBack;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zm
 * @date 2025/3/14 17:40
 * @version: 1.0
 */
public class UserForm extends Form {

    private TextField username_field;
    private PasswordField password_field;
    private TextField phone_field;
    private TextField email_field;

    public UserForm() {

        Label username_label = new Label("用户名:");
        username_label.setLayoutX(83.0);
        username_label.setLayoutY(64.0);

        username_field = new TextField();
        username_field.setLayoutX(162.0);
        username_field.setLayoutY(60.0);
        username_field.setPrefHeight(23.0);
        username_field.setPrefWidth(289.0);
        username_field.setPromptText("请输入用户名");

        password_field = new PasswordField();
        password_field.setLayoutX(162.0);
        password_field.setLayoutY(117.0);
        password_field.setPrefHeight(23.0);
        password_field.setPrefWidth(289.0);
        password_field.setPromptText("请输入密码");

        Label password_label = new Label("密码:");
        password_label.setLayoutX(87.0);
        password_label.setLayoutY(121.0);

        Label phone_label = new Label("手机号:");
        phone_label.setLayoutX(87.0);
        phone_label.setLayoutY(169.0);

        phone_field = new TextField();
        phone_field.setLayoutX(162.0);
        phone_field.setLayoutY(165.0);
        phone_field.setPrefHeight(23.0);
        phone_field.setPrefWidth(289.0);
        phone_field.setPromptText("请输入手机号");

        Label email_label = new Label("邮箱:");
        email_label.setLayoutX(87.0);
        email_label.setLayoutY(224.0);

        email_field = new TextField();
        email_field.setLayoutX(162.0);
        email_field.setLayoutY(220.0);
        email_field.setPrefHeight(23.0);
        email_field.setPrefWidth(289.0);
        email_field.setPromptText("请输入邮箱");

        Button submit_button = new Button("提交");
        submit_button.setLayoutX(263.0);
        submit_button.setLayoutY(288.0);

        submit_button.setOnAction(e -> {

            DataLoad formDataLoad = new UserDataLoad();
            Map<String, Object> map = new HashMap<>();
            map.put("username", username_field.getText());
            map.put("password", password_field.getText());
            map.put("phone", phone_field.getText());
            map.put("email", email_field.getText());
            formDataLoad.saveForm(map);

            Stage stage = (Stage) this.getScene().getWindow();
            stage.close();

            DynamicTableComponentCallBack callBack = getTableComponent().getCallBack();
            ObservableList<Map<String, SimpleStringProperty>> data = getTableComponent().getData();
            data.clear();
            data.addAll(callBack.flushData(1, ApiUtils.PAGE_SIZE).getDataList());

        });

        this.getChildren().addAll(username_label,
                username_field,
                password_field,
                phone_label,
                phone_field,
                email_label,
                email_field,
                submit_button,
                password_label);
    }


    public TextField getUsername_field() {
        return username_field;
    }

    public void setUsername_field(TextField username_field) {
        this.username_field = username_field;
    }

    public PasswordField getPassword_field() {
        return password_field;
    }

    public void setPassword_field(PasswordField password_field) {
        this.password_field = password_field;
    }

    public TextField getPhone_field() {
        return phone_field;
    }

    public void setPhone_field(TextField phone_field) {
        this.phone_field = phone_field;
    }

    public TextField getEmail_field() {
        return email_field;
    }

    public void setEmail_field(TextField email_field) {
        this.email_field = email_field;
    }
}
