package com.zhangmeng.online.exam.ui.controller;

import com.zhangmeng.online.exam.ui.module.Subject;
import com.zhangmeng.online.exam.ui.service.SubjectService;
import com.zhangmeng.online.exam.ui.utils.AlertUtils;
import com.zhangmeng.online.exam.ui.utils.FxUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import static com.zhangmeng.online.exam.ui.utils.FxUtils.Subject_DynamicTableComponent;

/**
 * @author zm
 * @date 2025/4/11 9:26
 * @version: 1.0
 */
public class SubjectEditController {

    @FXML
    public TextField nameField;

    @FXML
    public TextField codeField;

    @FXML
    public TextField descField;

    private Subject subject;

    public void setSubject(Subject subject) {
        this.subject = subject;
        this.nameField.setText(subject.getName());
        this.codeField.setText(subject.getCode());
        this.descField.setText(subject.getDesc());
        nameField.textProperty().bindBidirectional(subject.nameProperty());
        codeField.textProperty().bindBidirectional(subject.codeProperty());
        descField.textProperty().bindBidirectional(subject.descProperty());
    }

    @FXML
    public void initialize() {
        nameField.setPromptText("请输入科目名称");
        codeField.setPromptText("请输入考试代码");
        descField.setPromptText("请输入科目描述");
        subject = new Subject();
        nameField.textProperty().bindBidirectional(subject.nameProperty());
        codeField.textProperty().bindBidirectional(subject.codeProperty());
        descField.textProperty().bindBidirectional(subject.descProperty());
    }

    @FXML
    public void handleSave(ActionEvent actionEvent) {

        if (nameField.getText().isEmpty()){
            AlertUtils.alert_msg("科目名称不能为空！");
            return;
        }

        if (codeField.getText().isEmpty()){
            AlertUtils.alert_msg("科目代码不能为空！");
            return;
        }

        if (descField.getText().isEmpty()){
            AlertUtils.alert_msg("科目描述不能为空！");
            return;
        }

        System.out.println("科目名称：" + subject.getName()
                + " 科目代码：" + subject.getCode()
                + " 科目描述：" + subject.getDesc()
        );

        boolean isSuccess = SubjectService.saveSubject(subject);
        if (isSuccess){
            if (subject.getId()!= null){
                AlertUtils.alert_msg("保存成功！");
            }else {
                AlertUtils.alert_msg("更新成功！");
            }
            closeDialog();
            FxUtils.FX_BEANS.get(Subject_DynamicTableComponent).flushData();

        }else {
            AlertUtils.alert_msg("操作失败！");
        }
    }

    public void handleCancel(ActionEvent actionEvent) {
        closeDialog();
    }

    public Subject getSubject() {
        return subject;
    }

    private void closeDialog() {
        Stage stage = (Stage) nameField.getScene().getWindow();
        stage.close();
    }
}
