#include "medicalrecordwidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QFontMetrics>
#include <QDebug>
#include <QGroupBox>
#include "datamanager.h"
#include <QButtonGroup>
MedicalRecordWidget::MedicalRecordWidget(QWidget * parent   ):QWidget(parent)
{
    init();
    initLay();
    initConnect();
}
MedicalRecordWidget::~MedicalRecordWidget()
{

}
void  MedicalRecordWidget::init()
{
    setObjectName("MedicalRecordWidget");
    this->setStyleSheet("QWidget \
   {\
       background-color:white;\
        border-radius:10px;\
    }"\
   );

   // setFixedSize(600,600);
    m_labStar.setText(tr("*"));
    m_labStar.setObjectName("labStar");
    m_labStar2.setText(tr("*"));
    m_labStar2.setObjectName("labStar");

#if 0

#else
     m_labTitle.setText("病历资料");
     m_labTitle.setStyleSheet("width: 136px;font-family: 思源黑体;color: #0D9DDB; font-size: 32px;font-weight: 500;height: 46px;");
    //检查号
    m_labCheckNum.setText(tr("检 查 号:"));
    m_labCheckNum.setObjectName("labCheckNum");
    //住院 hospitalized
    m_labType.setText(tr("类    型:"));
    m_labType.setObjectName("labCheckNum");
    m_chHospitalized.setText(tr("住院:"));
    m_chHospitalized.setObjectName("labCheckNum");
    //门诊
    m_chOutpatient.setText(tr("门诊"));
m_chOutpatient.setObjectName("labCheckNum");
    //姓名
    m_labName.setText(tr("姓    名:"));
    m_labName.setObjectName("labCheckNum");
    //性别
     m_labSex.setText(tr("性    别:"));;
     m_labSex.setObjectName("labCheckNum");
     m_chMan.setText(tr("男"));;
     m_chMan.setObjectName("labCheckNum");
     m_chWan.setText(tr("女"));;
 m_chWan.setObjectName("labCheckNum");
     QButtonGroup *SexpButtonGroup = new QButtonGroup(this);
     // 设置互斥
     SexpButtonGroup->setExclusive(true);
     SexpButtonGroup->addButton(&m_chMan);
     SexpButtonGroup->addButton(&m_chWan);
    // bool ok = connect(pButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(onButtonClicked(QAbstractButton*)));


    //左右利
    m_labLaterality.setText(tr("左 右 利:"));
    m_labLaterality.setObjectName("labCheckNum");
    m_chLeft.setText(tr("左"));;
     m_chLeft.setObjectName("labCheckNum");
    m_chRfight.setText(tr("右"));;
     m_chRfight.setObjectName("labCheckNum");
    QButtonGroup *LabpButtonGroup = new QButtonGroup(this);
    // 设置互斥
    LabpButtonGroup->setExclusive(true);
    LabpButtonGroup->addButton(&m_chLeft);
    LabpButtonGroup->addButton(&m_chRfight);


    //检查日期
    m_labDate_Of_inspection.setText(tr("检查日期:"));
       m_labDate_Of_inspection.setObjectName("labCheckNum");
    m_labAge.setText(tr("年    龄:"));
     m_labAge.setObjectName("labCheckNum");
    m_labBirthDay.setText(tr("出生日期:"));
    m_labBirthDay.setObjectName("labCheckNum");
    //诊断病历
    m_labDiagnosticRecord.setText(tr("其他信息:"));
    m_labDiagnosticRecord.setObjectName("labCheckNum");
    m_labDiagnosticRecord.setFixedHeight(30);
    m_btnOk.setText(tr("保存"));
    m_btnCancel.setText(tr("取消"));
    m_btnOk.setObjectName("ok");
    m_btnCancel.setObjectName("cancel");
   int iwidth =  QFontMetrics(this->font()).width("Laterality");

#endif
}
void  MedicalRecordWidget::initLay()
{
#if 0
    QHBoxLayout * hlayStart1  = new QHBoxLayout;
    hlayStart1->addWidget(&m_labStar);
    hlayStart1->addWidget(&m_labCheckNum);
    hlayStart1->addWidget(&m_editCheckNum);


    hlayStart1->addWidget(&m_labType);
    hlayStart1->addWidget(&m_chHospitalized);
    hlayStart1->addWidget(&m_chOutpatient);
    hlayStart1->addStretch();

    QHBoxLayout * hlayStart2  = new QHBoxLayout;
    hlayStart2->addWidget(&m_labStar2);
    hlayStart2->addWidget(&m_labName);
    hlayStart2->addWidget(&m_editName);


    hlayStart2->addWidget(&m_labSex);
    hlayStart2->addWidget(&m_chMan);
    hlayStart2->addWidget(&m_chWan);
    hlayStart2->addStretch();

    QHBoxLayout  * hlay3 = new QHBoxLayout;
    hlay3->addWidget(&m_labBirthDay);
    hlay3->addWidget(&m_dateBirthDay);


    hlay3->addWidget(&m_labAge);
    hlay3->addWidget(&m_editAge);
     hlay3->addStretch();

    QHBoxLayout  * hlay4 = new QHBoxLayout;
    hlay4->addWidget(&m_labLaterality);
    hlay4->addWidget(&m_chLeft);
    hlay4->addWidget(&m_chRfight);
    hlay4->addWidget(&m_labDate_Of_inspection);
    hlay4->addWidget(&m_dateInspection);
    hlay4->addStretch();

    QHBoxLayout  * hlay5 = new QHBoxLayout;
    hlay5->addWidget(&m_labDiagnosticRecord);
    hlay5->addWidget(&m_textDiagnosticRecord);

    QHBoxLayout  * hlay6 = new QHBoxLayout;
    hlay6->addStretch();
    hlay6->addWidget(&m_btnCancel);
    hlay6->addWidget(&m_btnOk);
     hlay6->addStretch();


    QVBoxLayout *vlay = new QVBoxLayout;
    vlay->addLayout(hlayStart1);
    vlay->addLayout(hlayStart2);
    vlay->addLayout(hlay3);
    vlay->addLayout(hlay4);
    vlay->addLayout(hlay5);
    vlay->addLayout(hlay6);

    //vlay->addStretch();



    m_gbx.setLayout(vlay);
    QHBoxLayout * vlayall = new QHBoxLayout;

    vlayall->addWidget(&m_gbx);
    int widthi =50;
    vlay->setContentsMargins(widthi,widthi,widthi,widthi);
    setLayout(vlayall);

    int iwidth = 200;
   m_editCheckNum.setFixedWidth(iwidth);
   m_editName.setFixedWidth(iwidth);
   m_dateBirthDay.setFixedWidth(iwidth);
   m_chLeft.setFixedWidth(iwidth);
   m_editAge.setFixedWidth(iwidth);
   m_chLeft.setFixedWidth(iwidth/2);
   m_chRfight.setFixedWidth(iwidth/2);

#else
    QGridLayout * gridlay = new QGridLayout;
    int col = 0;
    int row = 0;
    gridlay->addWidget(&m_labStar,row,col,Qt::AlignRight);
    gridlay->addWidget(&m_labCheckNum,row,++col);
    gridlay->addWidget(&m_editCheckNum,row,++col);


    gridlay->addWidget(&m_labType,row,++col,Qt::AlignRight);
    QHBoxLayout * hlay_hostOut = new QHBoxLayout;
    hlay_hostOut->addWidget(&m_chHospitalized);
    hlay_hostOut->addWidget(&m_chOutpatient);
    hlay_hostOut->addStretch();
    gridlay->addLayout(hlay_hostOut,row,++col) ;

    row ++;
    col = 0;
    gridlay->addWidget(&m_labStar2,row,col,Qt::AlignRight);
    gridlay->addWidget(&m_labName,row,++col);
    gridlay->addWidget(&m_editName,row,++col);


    gridlay->addWidget(&m_labSex,row,++col,Qt::AlignRight);
    QHBoxLayout * hlay_ManWan = new QHBoxLayout;
    hlay_ManWan->addWidget(&m_chMan);
    hlay_ManWan->addWidget(&m_chWan);
    hlay_ManWan->addStretch();
    gridlay->addLayout(hlay_ManWan,row,++col);



    row ++;
    col = 1;
    gridlay->addWidget(&m_labBirthDay,row,col );
    gridlay->addWidget(&m_dateBirthDay,row,++col);

    gridlay->addWidget(&m_labAge,row,++col,Qt::AlignRight);
    gridlay->addWidget(&m_editAge,row,++col);

    row ++;
    col = 1;
    gridlay->addWidget(&m_labLaterality,row,col);
    QHBoxLayout * Hlay_leftRight = new QHBoxLayout;
    Hlay_leftRight->addWidget(&m_chLeft);
    Hlay_leftRight->addWidget(&m_chRfight);
    Hlay_leftRight->addStretch();
    gridlay->addLayout(Hlay_leftRight,row,++col);


    gridlay->addWidget(&m_labDate_Of_inspection,row,++col,Qt::AlignRight);
    gridlay->addWidget(&m_dateInspection,row,++col);

    row ++;
    col = 1;
    gridlay->addWidget(&m_labDiagnosticRecord,row,col);
    gridlay->addWidget(&m_textDiagnosticRecord,row,++col,3,3);


    QHBoxLayout * hlay_title = new QHBoxLayout;
    hlay_title->addWidget(&m_labTitle);

    QHBoxLayout * hlay_grid = new QHBoxLayout;
    //hlay_grid->addSpacing(30);
    hlay_grid->addLayout(gridlay);
    int iwidth = 30;
    gridlay->setContentsMargins(50,30,30,50);
    //hlay_grid->addSpacing(30);

    QVBoxLayout * vlay = new QVBoxLayout;


    vlay->addLayout(hlay_title,1);
    vlay->addLayout(hlay_grid,15);

    QHBoxLayout * hlay_okCancel = new QHBoxLayout;
    hlay_okCancel->addStretch();
    hlay_okCancel->addWidget(&m_btnOk);
    hlay_okCancel->addWidget(&m_btnCancel);
    hlay_okCancel->addStretch();

    QWidget   * w = new QWidget;
    w->setLayout(vlay);

    QVBoxLayout * vlayAll = new QVBoxLayout;
    vlayAll->addWidget(w);
    vlayAll->addLayout(hlay_okCancel);
    setLayout(vlayAll);
#endif


}
bool  MedicalRecordWidget::initConnect()
{
    bool reok;
   reok = connect(&m_btnOk,SIGNAL(clicked()),this,SLOT(slotSave()));
   if(!reok)
   {
       qDebug()<<"save connect failed"<<endl;
   }
   reok = connect(&m_btnCancel,SIGNAL(clicked()),this,SLOT(slotCancel()));
   if(!reok)
   {
       qDebug()<<"save connect failed"<<endl;
   }
}
void  MedicalRecordWidget::slotSave()
{

    QString   CheckNum =  m_editCheckNum.text();
    //住院 hospitalized
    QString Type ;
    if(m_chHospitalized.isChecked())
    {
         Type = m_chHospitalized.text();
    }
    else
    {
         Type = m_chOutpatient.text();
    }
    qDebug()<<Type<<endl;
    //姓名
     QString Name  =  m_editName.text();
    //性别
      QString Sex;
     if(m_chMan.isChecked())
     {
          Sex = m_chMan.text();
     }
     else
     {
          Sex = m_chWan.text();
     }
    //年龄

    QString Year =  m_editAge.text();
    //左右利
   QString  Laterality;
    if(m_chLeft.isChecked())
    {
         Laterality = m_chLeft.text();
    }
    else
    {
         Laterality = m_chRfight.text();
    }

    //出生日期
     QString Birthday =  m_dateBirthDay.dateTime().toString("yyyy/MM/dd hh:mm:ss");

    //检查日期

     QString inspectDate  =  m_dateInspection.dateTime().toString("yyyy/MM/dd hh:mm:ss");;

    //诊断病历

    QString  Other =  m_textDiagnosticRecord.toPlainText();
    DataManager::instance().updateMedicRecord(
            CheckNum ,
            Type ,
            Name ,
            Sex,
            Birthday,
            Year,
            Laterality,
            inspectDate,
            Other);


}
void MedicalRecordWidget::slotCancel()
{

}
