#include "dateTimeSelectionPanel.h"
#include "./ui_dateTimeSelectionPanel.h"
#include "customCalendarWidget.h"

DateTimeSelectionPanel::DateTimeSelectionPanel(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::dateTimeSelectionPanel)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setAttribute(Qt::WA_TranslucentBackground);

    m_pCalendar = new CustomCalendarWidget(this);
    m_pCalendar->setStyleSheet("background-color:transparent;");
    ui->vLayout_calendar->addWidget(m_pCalendar);
    connect(m_pCalendar, SIGNAL(currentPageChanged(int,int)), this, SLOT(onCalendarPageChanged(int, int)));

    connect(ui->btn_prevMonth, SIGNAL(clicked()), this, SLOT(onBtnClicked_YMControl()));
    connect(ui->btn_nextMonth, SIGNAL(clicked()), this, SLOT(onBtnClicked_YMControl()));
    connect(ui->btn_confirm, SIGNAL(clicked()), this, SLOT(onBtnClicked_confirm()));
    connect(ui->btn_cancle, SIGNAL(clicked()), this, SLOT(onBtnClicked_cancle()));
    connect(ui->btn_currentTime, SIGNAL(clicked()), this, SLOT(onBtnClicked_currentTime()));
}

DateTimeSelectionPanel::~DateTimeSelectionPanel()
{
    delete ui;
}

void DateTimeSelectionPanel::show_()
{
    QDate today = QDate::currentDate();
    m_pCalendar->setSelectedDate(today);
    m_pCalendar->showToday();

    ui->btn_year->setText(QString::number(m_pCalendar->selectedDate().year()));
    ui->btn_month->setText(QString::number(m_pCalendar->selectedDate().month()));

    show();
}

void DateTimeSelectionPanel::onBtnClicked_YMControl()
{
    QPushButton* senderBtn = qobject_cast<QPushButton*>(sender());
    if(senderBtn == ui->btn_prevMonth)
        m_pCalendar->showPreviousMonth();
    else if(senderBtn == ui->btn_nextMonth)
        m_pCalendar->showNextMonth();
}

void DateTimeSelectionPanel::onBtnClicked_currentTime()
{
    QDate today = QDate::currentDate();
    m_pCalendar->setSelectedDate(today);
    m_pCalendar->showToday();
}

void DateTimeSelectionPanel::onBtnClicked_confirm()
{
    QDateTime datetime;
    datetime.setDate(m_pCalendar->selectedDate());
    emit selectResults(datetime);
    close();
}

void DateTimeSelectionPanel::onBtnClicked_cancle()
{
    emit selectResults(QDateTime::currentDateTime());
    close();
}

void DateTimeSelectionPanel::onCalendarPageChanged(int year, int month)
{
    ui->btn_year->setText(QString::number(year));
    ui->btn_month->setText(QString::number(month));
}
