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

import com.zhangmeng.online.exam.ui.module.QuestionType;
import com.zhangmeng.online.exam.ui.module.SubjectType;
import com.zhangmeng.online.exam.ui.service.QuestionService;
import com.zhangmeng.online.exam.ui.service.SubjectService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.util.Callback;
import javafx.util.StringConverter;

import java.util.List;

/**
 * @author zm
 * @date 2025/4/1 12:00
 * @version: 1.0
 */
public class QuestionEditController {

    @FXML
    public TextField scoreField;

    @FXML
    public TextField explanationField;

    @FXML
    public ComboBox<QuestionType> typeComboBox;

    public ComboBox<SubjectType> subjectComboBox;

    @FXML
    private TextField questionField;

    @FXML
    public void initialize() {
        typeComboBox.getItems().addAll(QuestionService.getALLQuestionType());
        typeComboBox.getSelectionModel().selectFirst();
        typeComboBox.setConverter(new StringConverter<QuestionType>() {
            @Override
            public String toString(QuestionType object) {
                return object.getDesc();
            }

            @Override
            public QuestionType fromString(String string) {
                return null;
            }
        });


        subjectComboBox.getItems().addAll(SubjectService.getALLSubjectType());
        subjectComboBox.getSelectionModel().selectFirst();
        subjectComboBox.setConverter(new StringConverter<SubjectType>() {
            @Override
            public String toString(SubjectType object) {
                return object.getName();
            }

            @Override
            public SubjectType fromString(String string) {
                return null;
            }
        });
    }

    public void handleSave(ActionEvent actionEvent) {
        QuestionType questionType = typeComboBox.getSelectionModel().getSelectedItem();
        SubjectType subjectType = subjectComboBox.getSelectionModel().getSelectedItem();
        String question = questionField.getText();
        String score = scoreField.getText();
        String explanation = explanationField.getText();
        QuestionService.saveQuestion(questionType.getType(), subjectType.getId(), question, score, explanation);



    }

    public void handleCancel(ActionEvent actionEvent) {

    }
}
