#include "ExpertInfoWidget.h"

#include <Widgets/LineEdit.h>
#include <Widgets/Button.h>

#include <QBoxLayout>
#include <QGridLayout>
#include <QLabel>

ExpertInfoWidget::ExpertInfoWidget(QWidget *parent) : QWidget(parent)
{
    initWindow();
    initialize();
    initLayout();
    connectSignalsAndSlots();
    updateState();
}

void ExpertInfoWidget::setMode(ExpertInfoWidget::Mode mode)
{
    m_mode = mode;
    updateState();
}

void ExpertInfoWidget::clearInputs()
{
    m_idLineEdit->clear();
    m_passwordLineEdit->clear();
    m_nameLineEdit->clear();
    m_companyLineEdit->clear();
    m_jobLineEdit->clear();
    m_majorLineEdit->clear();
    m_contactInfoLineEdit->clear();
    m_noteTextEdit->clear();
    updateState();
}

void ExpertInfoWidget::initWindow()
{
    setWindowFlags(Qt::Window);
    setWindowFlag(Qt::WindowMinMaxButtonsHint, false);
    resize(400, 450);
}

void ExpertInfoWidget::initialize()
{
    int editWidth = 300;

    m_vBoxLayout = new QVBoxLayout(this);
    m_gridLayout = new QGridLayout();

    m_idLabel = new QLabel(this);
    m_idLabel->setText("账号ID:");
    m_idLineEdit = new LineEdit(this);
    m_idLineEdit->setFixedWidth(editWidth);
    m_idLineEdit->setPlaceholderText("请输入a-z/A-Z/0-9数字字母组合");
    m_passwordLabel = new QLabel(this);
    m_passwordLabel->setText("用户密码:");
    m_passwordLineEdit = new PasswordLineEdit(this);
    m_passwordLineEdit->setFixedWidth(editWidth);
    m_passwordLineEdit->setPlaceholderText("请输入密码");
    m_nameLabel = new QLabel(this);
    m_nameLabel->setText("专家名:");
    m_nameLineEdit = new LineEdit(this);
    m_nameLineEdit->setFixedWidth(editWidth);
    m_nameLineEdit->setPlaceholderText("请输入专家名");
    m_companyLabel = new QLabel(this);
    m_companyLabel->setText("单位:");
    m_companyLineEdit = new LineEdit(this);
    m_companyLineEdit->setFixedWidth(editWidth);
    m_jobLabel = new QLabel(this);
    m_jobLabel->setText("职务:");
    m_jobLineEdit = new LineEdit(this);
    m_jobLineEdit->setFixedWidth(editWidth);
    m_majorLabel = new QLabel(this);
    m_majorLabel->setText("专业:");
    m_majorLineEdit = new LineEdit(this);
    m_majorLineEdit->setFixedWidth(editWidth);
    m_contactInfoLabel = new QLabel(this);
    m_contactInfoLabel->setText("联系方式:");
    m_contactInfoLineEdit = new LineEdit(this);
    m_contactInfoLineEdit->setFixedWidth(editWidth);
    m_noteLabel = new QLabel(this);
    m_noteLabel->setText("备注:");
    m_noteTextEdit = new TextEdit(this);
    m_noteTextEdit->setFixedWidth(editWidth);
    m_noteTextEdit->setFixedHeight(100);

    m_hBoxLayout    = new QHBoxLayout();
    m_confirmButton = new PushButton("确认", this);
    m_cancelButton  = new PushButton("取消", this);
}

void ExpertInfoWidget::initLayout()
{
    m_vBoxLayout->setContentsMargins(20, 10, 20, 10);
    m_vBoxLayout->addLayout(m_gridLayout);

    m_gridLayout->addWidget(m_idLabel, 0, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_idLineEdit, 0, 1, 1, 2, Qt::AlignLeft);
    m_gridLayout->addWidget(m_passwordLabel, 1, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_passwordLineEdit, 1, 1, Qt::AlignLeft);
    m_gridLayout->addWidget(m_nameLabel, 2, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_nameLineEdit, 2, 1, 1, 1, Qt::AlignLeft);
    m_gridLayout->addWidget(m_companyLabel, 3, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_companyLineEdit, 3, 1, 1, 1, Qt::AlignLeft);
    m_gridLayout->addWidget(m_jobLabel, 4, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_jobLineEdit, 4, 1, 1, 1, Qt::AlignLeft);
    m_gridLayout->addWidget(m_majorLabel, 5, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_majorLineEdit, 5, 1, 1, 1, Qt::AlignLeft);
    m_gridLayout->addWidget(m_contactInfoLabel, 6, 0, 1, 1, Qt::AlignRight);
    m_gridLayout->addWidget(m_contactInfoLineEdit, 6, 1, 1, 1, Qt::AlignLeft);
    m_gridLayout->addWidget(m_noteLabel, 7, 0, 1, 1, Qt::AlignRight | Qt::AlignTop);
    m_gridLayout->addWidget(m_noteTextEdit, 7, 1, 1, 1, Qt::AlignLeft);

    m_vBoxLayout->addStretch();
    m_vBoxLayout->addLayout(m_hBoxLayout);

    m_hBoxLayout->addStretch();
    m_hBoxLayout->addWidget(m_confirmButton);
    m_hBoxLayout->addWidget(m_cancelButton);
}

void ExpertInfoWidget::connectSignalsAndSlots()
{
    connect(m_confirmButton, &PushButton::clicked, this, &ExpertInfoWidget::slotConfirm);
    connect(m_cancelButton, &PushButton::clicked, this, &ExpertInfoWidget::slotCancel);
}

void ExpertInfoWidget::updateState()
{
    switch (m_mode) {
    case Add: {
        setWindowTitle("添加用户");
        break;
    }
    case Update: {
        setWindowTitle("修改用户信息");
    }
    case Read: {
        setWindowTitle("用户信息");
    }
    }

    bool editEnabled = m_mode != Read;
    m_idLineEdit->setEnabled(editEnabled);
    m_passwordLineEdit->setEnabled(editEnabled);
    m_nameLineEdit->setEnabled(editEnabled);
    m_companyLineEdit->setEnabled(editEnabled);
    m_jobLineEdit->setEnabled(editEnabled);
    m_majorLineEdit->setEnabled(editEnabled);
    m_idLineEdit->setEnabled(editEnabled);
    m_contactInfoLineEdit->setEnabled(editEnabled);
    m_noteTextEdit->setEnabled(editEnabled);
    m_confirmButton->setHidden(!editEnabled);
    m_cancelButton->setHidden(!editEnabled);
}

void ExpertInfoWidget::slotConfirm()
{
    close();
}

void ExpertInfoWidget::slotCancel()
{
    close();
}