#include "medicalrecordmanager.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>

MedicalRecordManager::MedicalRecordManager(QWidget * parent):QWidget(parent)
{
    init();
    initLay();
    initConnect();
    initTable();

m_btnOpen.setObjectName("open");
#if 0
    m_btnOpen.setStyleSheet("QPushButton{\
                                background: white;\
                                border-radius:10px;padding:7px 10px;\
                        }\
                        QPushButton:hover{\
                                background: rgb(85, 85, 85);\
                border-radius:10px;padding:7px 10px;\
                        }\
                        QPushButton:pressed{\
                                background: rgb(80, 80, 80);\
               border-radius:10px;padding:7px 10px;\
                        }\
                        QPushButton:checked{\
                                background: #0d9ddb;\
                 border-radius:10px;padding:7px 10px;\
                        }");
#endif
    m_btnEdit.setStyleSheet("QPushButton{\
                            background: white;\
                            color: green;\
                            border-radius:10px;padding:7px 10px;\
                    }\
                    QPushButton:hover{\
                            background: rgb(85, 85, 85);\
            border-radius:10px;padding:7px 10px;\
                    }\
                    QPushButton:pressed{\
                            background: rgb(80, 80, 80);\
           border-radius:10px;padding:7px 10px;\
                    }\
                    QPushButton:checked{\
                            background: #0d9ddb;\
             border-radius:10px;padding:7px 10px;\
                    }");
    m_btnExport.setStyleSheet("QPushButton{\
                              background: white;\
                              color: blue;\
                              border-radius:10px;padding:7px 10px;\
                      }\
                      QPushButton:hover{\
                              background: rgb(85, 85, 85);\
              border-radius:10px;padding:7px 10px;\
                      }\
                      QPushButton:pressed{\
                              background: rgb(80, 80, 80);\
             border-radius:10px;padding:7px 10px;\
                      }\
                      QPushButton:checked{\
                              background: #0d9ddb;\
               border-radius:10px;padding:7px 10px;\
                      }");
    m_btnDelete.setStyleSheet("QPushButton{\
                              background: red;\
                               color:white;\
                              border-radius:10px;padding:7px 10px;\
                      }\
                      QPushButton:hover{\
                              background: rgb(85, 85, 85);\
              border-radius:10px;padding:7px 10px;\
                      }\
                      QPushButton:pressed{\
                              background: rgb(80, 80, 80);\
             border-radius:10px;padding:7px 10px;\
                      }\
                      QPushButton:checked{\
                              background: #0d9ddb;\
               border-radius:10px;padding:7px 10px;\
                      }");
    //上一页
     m_btnUpPage.setStyleSheet("QPushButton{\
                               border-image:url(:/image/setting_btn_left.png);\
                               background: white;\
                               border-radius:10px;padding:7px 10px;\
                       }\
                       QPushButton:hover{\
                               background: rgb(85, 85, 85);\
               border-radius:10px;padding:7px 10px;\
                       }\
                       QPushButton:pressed{\
                               background: rgb(80, 80, 80);\
              border-radius:10px;padding:7px 10px;\
                       }\
                       QPushButton:checked{\
                               background: #0d9ddb;\
                border-radius:10px;padding:7px 10px;\
                       }");
    //下一页
    m_btnNetPage.setStyleSheet("QPushButton{\
                               border-image:url(:/image/setting_btn_right.png);\
                               background: white;\
                               border-radius:10px;padding:7px 10px;\
                       }\
                       QPushButton:hover{\
                               background: rgb(85, 85, 85);\
               border-radius:10px;padding:7px 10px;\
                       }\
                       QPushButton:pressed{\
                               background: rgb(80, 80, 80);\
              border-radius:10px;padding:7px 10px;\
                       }\
                       QPushButton:checked{\
                               background: #0d9ddb;\
                border-radius:10px;padding:7px 10px;\
                       }");
    //首页
    m_btnfirstPage.setStyleSheet("QPushButton{\
                                 border-image:url(:/image/setting_btn_firstPage.png);\
                                 background: white;\
                                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:hover{\
                                 background: rgb(85, 85, 85);\
                 border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:pressed{\
                                 background: rgb(80, 80, 80);\
                border-radius:10px;padding:7px 10px;\
                         }\
                         QPushButton:checked{\
                                 background: #0d9ddb;\
                  border-radius:10px;padding:7px 10px;\
                         }");
    //尾页
    m_btnlastPage.setStyleSheet("QPushButton{\
                                  border-image:url(:/image/setting_btn_lastPage.png);\
                                background: white;\
                                border-radius:10px;padding:7px 10px;\
                        }\
                        QPushButton:hover{\
                                background: rgb(85, 85, 85);\
                border-radius:10px;padding:7px 10px;\
                        }\
                        QPushButton:pressed{\
                                background: rgb(80, 80, 80);\
               border-radius:10px;padding:7px 10px;\
                        }\
                        QPushButton:checked{\
                                background: #0d9ddb;\
                 border-radius:10px;padding:7px 10px;\
                        }");


    m_tableWidget.horizontalHeader()->setStyleSheet(
    "QHeaderView::section{"
                "border-top:0px solid #E5E5E5;"
                "border-left:0px solid #E5E5E5;"
                "border-right:0.5px solid #E5E5E5;"
                "border-bottom: 0.5px solid #E5E5E5;"
                "background-color:white;"
                "padding:4px;"
            "}"
    );


}
MedicalRecordManager::~MedicalRecordManager()
{

}
void  MedicalRecordManager::init()
{
    //当前页
    m_currentPage = 0 ;

    m_labMedRecManager.setText("病例管理");
    m_labMedRecManager.setObjectName("labMedManager");
    m_labMedRecManager.setStyleSheet("width: 136px; height: 46px;font-family: 思源黑体;font-size: 32px;font-weight: 500; color: #0D9DDB;");
    //QAction
     m_actSearch = new QAction;
     m_actSearch->setToolTip("搜索");//设置鼠标悬浮的提示
     m_actSearch->setIcon(QIcon(":/image/EGG_icon_amplify.png"));//加载显示图片
     //第二个编辑框
     m_editSearch.setGeometry(100, 150, 400, 50);//设置编辑框位置大小
     m_editSearch.setPlaceholderText("请输入你想要搜索的内容");//设置编辑框没有输入内容时的友好提示

      //TrailingPosition:将action放置在右边
      m_editSearch.addAction(m_actSearch, QLineEdit::LeadingPosition);//QLineEdit::TrailingPosition);

      //打开
     m_btnOpen.setText(tr("打开"));
     //编辑
     m_btnEdit.setText(tr("编辑"));
     //导出
     m_btnExport.setText(tr("导出"));
     //删除
     m_btnDelete.setText(tr("删除"));
     //上一页
      //m_btnUpPage.setText(tr("上一页"));
     //下一页
    // m_btnNetPage.setText(tr("下一页"));
     m_labPageNumTxt.setText(tr("0/0"));
     //首页
    // m_btnfirstPage.setText(tr("首页"));
     //尾页
    // m_btnlastPage.setText(tr("尾页"));


}
void  MedicalRecordManager::initLay()
{

    QHBoxLayout *  hlay = new QHBoxLayout;
    m_editSearch.setStyleSheet(":/image/EGG_icon_amplify.png");
    hlay->addWidget(&m_labMedRecManager);
    hlay->addStretch();
    hlay->addWidget(&m_editSearch);

    QHBoxLayout * hlay2 = new QHBoxLayout;
    hlay2->addSpacing(20);
    hlay2->addWidget(&m_btnOpen);
    hlay2->addWidget(&m_btnEdit);
    hlay2->addWidget(&m_btnExport);
    hlay2->addWidget(&m_btnDelete);
    hlay2->addStretch();
    hlay2->addWidget(&m_btnfirstPage);
    hlay2->addWidget(&m_btnUpPage);//
    hlay2->addWidget(&m_labPageNumTxt);
    hlay2->addWidget(&m_btnNetPage);
    hlay2->addWidget(&m_btnlastPage);

    QVBoxLayout * vlay = new QVBoxLayout;
    QWidget * w = new QWidget;

    w->setObjectName("MedicalRecordManager");
    w->setStyleSheet("QWidget#MedicalRecordManager{background:white;}");
    vlay->addLayout(hlay);
    vlay->addWidget(&m_tableWidget);
    w->setLayout(vlay);
    QVBoxLayout * vlay2 = new QVBoxLayout;
    vlay2->addWidget(w);
    vlay2->addLayout(hlay2);
    setLayout(vlay2);
    QSize   tempSize(80,30);
    m_btnOpen.setFixedSize(tempSize);
    m_btnEdit.setFixedSize(tempSize);
    m_btnExport.setFixedSize(tempSize);
    m_btnDelete.setFixedSize(tempSize);

    QSize   tempSize2(60,30);
    m_btnfirstPage.setFixedSize(tempSize2);
    m_btnUpPage.setFixedSize(tempSize2);
    m_btnNetPage.setFixedSize(tempSize2);
    m_btnlastPage.setFixedSize(tempSize2);


    this->setStyleSheet("QWidget \
   {\
       background-color:white;\
        border-radius:10px;\
    }"\
   );
    m_tableWidget.setStyleSheet("QWidget \
   {\
       background-color:white;\
        border-radius:10px;\
    }"\
   );
}
bool  MedicalRecordManager::initConnect()
{
    connect(m_actSearch, SIGNAL(triggered(bool)), this, SLOT(slotSearch()));
    connect(&m_btnUpPage,SIGNAL(clicked()),this,SLOT(slotPreviousPage()));
    connect(&m_btnNetPage,SIGNAL(clicked()),this,SLOT(slotNextPage()));
     connect(&m_btnDelete,SIGNAL(clicked()),this,SLOT(slotDelete()));
    return true;
}
void MedicalRecordManager::slotSearch()
{
    qDebug()<<"search"<<endl;
    m_currentPage = 0;
    updateContext();

}
void MedicalRecordManager::initTable()
{
        m_tableWidget.setFocusPolicy(Qt::NoFocus);
        m_tableWidget.setColumnCount(9);
        m_tableWidget.setRowCount(100);
        //m_tableWidget.setShowGrid(false);
        QFont font;
        font.setFamily("SimSun");
        font.setPixelSize(12);
        font.setBold(false);
        m_tableWidget.horizontalHeader()->setFont(font);
        m_tableWidget.setFont(font);
        //设置全选按钮
        //connect(&m_tableWidget, SIGNAL(itemClicked(QTableWidgetItem *)), this, SLOT(slotCilclidItem(QTableWidgetItem *)), Qt::QueuedConnection);

        /* 设置 tableWidget */
        //m_tableWidget.verticalHeader()->setVisible(true);
        m_tableWidget.verticalHeader()->setVisible(false);
        //设置选择一个
        m_tableWidget.setSelectionMode(QAbstractItemView::SingleSelection);
        m_tableWidget.setSelectionBehavior(QAbstractItemView::SelectItems);
        m_tableWidget.setWordWrap(false);
        /*单个选中  QAbstractItemView::ExtendedSelection);   可以选中多个*/
        m_tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(5, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
        m_tableWidget.horizontalHeader()->setSectionResizeMode(7, QHeaderView::ResizeToContents);
        m_tableWidget.setEditTriggers(QAbstractItemView::NoEditTriggers);
        m_tableWidget.setShowGrid(false);
        for (int row = 0; row < m_tableWidget.rowCount(); row++)
        {
            QTableWidgetItem  *item_id = new QTableWidgetItem;
            QTableWidgetItem  *item_fileName = new QTableWidgetItem;
            QTableWidgetItem  *item_hash = new QTableWidgetItem;
            QTableWidgetItem  *item_butt = new QTableWidgetItem;
            QTableWidgetItem  *item_createDate = new QTableWidgetItem;
            QTableWidgetItem  *item_hash6 = new QTableWidgetItem;
            QTableWidgetItem  *item_butt7 = new QTableWidgetItem;
            QTableWidgetItem  *item_createDate8 = new QTableWidgetItem;
             QTableWidgetItem  *item_createDate9 = new QTableWidgetItem;
            item_id->setData(Qt::DisplayRole, "");
            item_fileName->setData(Qt::DisplayRole, "");

            item_hash->setData(Qt::DisplayRole, "");
            item_butt->setData(Qt::DisplayRole, "");
            item_createDate->setData(Qt::DisplayRole, "");

           item_hash6->setData(Qt::DisplayRole, "");
           item_butt7->setData(Qt::DisplayRole, "");
           item_createDate8->setData(Qt::DisplayRole, "");

            m_tableWidget.setItem(row, 0, item_id);
            m_tableWidget.setItem(row, 1, item_fileName);
            m_tableWidget.setItem(row, 2, item_hash);
            m_tableWidget.setItem(row, 3, item_butt);
            m_tableWidget.setItem(row, 4, item_createDate);

            m_tableWidget.setItem(row, 5, item_hash6);
            m_tableWidget.setItem(row, 6, item_butt7);
            m_tableWidget.setItem(row, 7, item_createDate8);
            m_tableWidget.setItem(row, 8, item_createDate9);

            //m_tableWidget.item(row, 4)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        }
        QStringList lstHHead;
        /*("序号")  "文件") << "修改日期"*/
        lstHHead << tr("序号") << tr("检查号") << tr("类别") << tr("姓名") <<tr("性别")
                 << tr("年龄") <<tr("左右利")<<tr("检查日期")<<tr("其他信息");
        m_tableWidget.setHorizontalHeaderLabels(lstHHead);//setWordWrap(true)
        //m_tableWidget.show();
}
void MedicalRecordManager::slotPreviousPage()
{
    if (m_currentPage > 1)
        {
            m_currentPage = m_currentPage - 2;
        }
        else
        {
            return;
        }
        updateContext();
}
void MedicalRecordManager::slotNextPage()
{
        updateContext();
}

void  MedicalRecordManager::updateContext()
{
    QString fileName = m_editSearch.text();
    fileName = fileName.simplified();
    QList<MedicRecord> lstInfo;
    int rowCounts = DataManager::instance().getMedicRecordCount(fileName);
    qDebug() << fileName << rowCounts << endl;
    double page = (double)rowCounts / (double)100;
    if (page > (int)page)
    {
        page = page + 1;
    }
    else if (page == 0)
    {
        page = 1;
    }
    if (m_currentPage == (int)page)
    {
        return;
    }
    if(m_currentPage!=0)
    {
         m_labPageNumTxt.setText(QString("%1/%2").arg(m_currentPage).arg(QString::number((int)page)));
    }
    else
    {
          m_labPageNumTxt.setText(QString("%1/%2").arg(1).arg(QString::number((int)page)));
    }

    DataManager::instance().getMedicRecordList(fileName, lstInfo, m_currentPage, m_tableWidget.rowCount());
    m_tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    m_tableWidget.verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
   QString::number((int)page);//设置一共有多少页
    int row = 0;
    for (; row < lstInfo.size(); row++)
    {
        int col=0;
        QTableWidgetItem  *item_id = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_m_CheckNum = m_tableWidget.item(row, col++);
        QTableWidgetItem  *item_type = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_Name = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_Sex = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_Age = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_Laterality = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_inspectDate = m_tableWidget.item(row,  col++);
        QTableWidgetItem  *item_other = m_tableWidget.item(row,  col++);
        item_id->setCheckState(Qt::Unchecked);
        item_id->setFlags(Qt::ItemIsEditable| Qt::ItemIsSelectable| Qt::ItemIsUserCheckable| Qt::ItemIsEnabled| Qt::ItemIsDragEnabled| Qt::ItemIsDropEnabled);
        int id_page = 0;
        if (m_currentPage == 0)
        {
            id_page = 1;
        }
        else
        {
            id_page = m_currentPage;
        }
        item_id->setData(Qt::DisplayRole, row + 1 + (id_page - 1) * 100);
        //设置数据
        item_m_CheckNum->setData(Qt::DisplayRole, lstInfo.at(row).m_CheckNum);
        item_type->setData(Qt::DisplayRole, lstInfo.at(row).m_Type);
        item_Name->setData(Qt::DisplayRole, lstInfo.at(row).m_Name);
        item_Sex->setData(Qt::DisplayRole, lstInfo.at(row).m_Sex);
        item_Age->setData(Qt::DisplayRole, lstInfo.at(row).m_Year);
        item_Laterality->setData(Qt::DisplayRole, lstInfo.at(row).m_Laterality);
        item_inspectDate->setData(Qt::DisplayRole, lstInfo.at(row).m_InspectDate);
        item_other->setData(Qt::DisplayRole, lstInfo.at(row).m_Other);

        item_m_CheckNum->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        item_type->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        item_Name->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        item_Sex->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        item_Age->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

        item_Laterality->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        item_inspectDate->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        item_other->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    }

    if (row < m_tableWidget.rowCount())
    {
        for (int i = row; i < m_tableWidget.rowCount(); i++)
        {
            int col=0;
            QTableWidgetItem  *item_id = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_m_CheckNum = m_tableWidget.item(i, col++);
            QTableWidgetItem  *item_type = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_Name = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_Sex = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_Age = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_Laterality = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_inspectDate = m_tableWidget.item(i,  col++);
            QTableWidgetItem  *item_other = m_tableWidget.item(i,  col++);
            item_id->setData(Qt::CheckStateRole, QVariant());
            item_id->setData(Qt::DisplayRole, "");
            //item_id->setFlags(Qt::NoItemFlags);
            item_m_CheckNum->setData(Qt::DisplayRole, "");
            item_type->setData(Qt::DisplayRole, "");
            item_Name->setData(Qt::DisplayRole, "");
            item_Sex->setData(Qt::DisplayRole, "");
            item_Age->setData(Qt::DisplayRole, "");
            item_Age->setData(Qt::DisplayRole, "");
            item_Laterality->setData(Qt::DisplayRole, "");
            item_inspectDate->setData(Qt::DisplayRole, "");
            item_other->setData(Qt::DisplayRole, "");

        }
    }
    if (m_currentPage == 0)
    {
        //m_labCurentPage.setText(QString::number(1));
    }
    else
    {
        //m_labCurentPage.setText(QString::number(m_currentPage));
    }
    m_tableWidget.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//单独使用--每一列宽度平均//Qt::4 //horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(5, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
    m_tableWidget.horizontalHeader()->setSectionResizeMode(7, QHeaderView::ResizeToContents);
  //  m_tableWidget.horizontalHeader()->setSectionResizeMode(8, QHeaderView::ResizeToContents);
    m_tableWidget.verticalHeader()->scrollToTop();
    m_tableWidget.scrollToTop();

}

void MedicalRecordManager::slotDelete()
{
    QStringList  CheckNumList;
    for (int i = 0; i < m_tableWidget.rowCount(); i++)
    {
        QTableWidgetItem  *item_id = m_tableWidget.item(i, 0);
        QTableWidgetItem  *item_fileName = m_tableWidget.item(i, 1);

        if (item_id->checkState() == Qt::CheckState::Checked)
        {
            //item_id->setCheckState(Qt::CheckState::Unchecked);
            //item_id->setFlags(Qt::ItemIsSelectable);
            int id = item_id->data(Qt::DisplayRole).toInt();
            QString checkNum = item_fileName->data(Qt::DisplayRole).toString();
            CheckNumList.append(checkNum);
            bool okret = DataManager::instance().deleteNumRecode(checkNum);
            if(!okret)
            {
                qDebug()<<"fails delete"<<endl;
            }
        }
    }
    m_currentPage =0 ;
    updateContext();
}
