#include "zdateedit.h"
#include "QWidgetAction"
#include "dateCommonInfo.h"
#include <QFile>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QMenu>

ZDateEdit::ZDateEdit(QWidget* parent)
    : QDateEdit(parent)
{
    m_popupButton = new QPushButton();
    m_popupButton->setObjectName("popupButton");
    //m_popupButton->setText("+");
    //m_popupButton->setCursor(Qt::ArrowCursor);
    QHBoxLayout* layout = new QHBoxLayout();
    m_popupButton->setCursor(QCursor(Qt::PointingHandCursor));
    layout->addStretch();
    layout->addWidget(m_popupButton);
    layout->setContentsMargins(0, 0, 20, 0);
    this->setLayout(layout);

    QFile file(":/component/ZDateEdit/qss/zDateTimeEdit.qss");
    if (file.open(QFile::ReadOnly)) {
        QString qss = file.readAll();
        setStyleSheet(qss);
        file.close();
    }

    m_pCldrWgt = new CalendarWidget(displayFormat(), this, QMenu().windowFlags() | Qt::FramelessWindowHint);
    m_dateTime = QDateTime::currentDateTime();
    setDateTime(m_dateTime);
    connect(m_popupButton, &QAbstractButton::clicked, this, &ZDateEdit::showCalendarWidget);
    connect(m_pCldrWgt, &CalendarWidget::dayClicked, this, &ZDateEdit::slotDayClicked);

    QString tmp = displayFormat();
}

ZDateEdit::~ZDateEdit()
{
    if (m_pCldrWgt) {
        delete m_pCldrWgt;
    }
}

void ZDateEdit::showCalendarWidget()
{
    QPoint pt(-17, height() - 10);
    pt = mapToGlobal(pt);

    m_pCldrWgt->jumpToDateTime(m_dateTime);
    m_pCldrWgt->move(pt);
    m_pCldrWgt->show();
}

void ZDateEdit::slotDayClicked(const QDateTime& dt)
{
    m_dateTime = dt;
    setDateTime(dt);
}

void ZDateEdit::setDisplayFormat(const QString& format)
{
    m_pCldrWgt->setDisplayFormat(format);
    QDateTimeEdit::setDisplayFormat(format);
}
