#include "hostwidget.h"
#include "ui_hostwidget.h"
#include "QKeyEvent"
#include "QsLog.h"
#include "fmtool.h"
#include "labelstimer.h"

#include <QMovie>
#include <QDateTime>
#include <QJsonArray>
#include <QButtonGroup>

HostWidget::HostWidget(QWidget *parent) :
    FMPWindow(parent),
    ui(new Ui::HostWidget),
    _queryPaymentMethod(""),
    _queryConditionsIsChanged(true)
{
    ui->setupUi(this);
    _need_exit = false;
    _runing = false;

    _widgetlist.append(ui->pay);
    _widgetlist.append(ui->find);
    _widgetlist.append(ui->Manager);
    _widgetlist.append(ui->sign);
    _widgetlist.append(ui->wait);

    _curr_show_widget = NULL;

    InitWidget();

    connect(&_intertime, &QTimer::timeout, this, [this](){
        if(!_need_exit)
        {
            ui->btn_pay_exitint->show();
        }
    });

    _label_pay_timer = new LabelsTimer(ui->label_pay_time, 60, QString::fromLocal8Bit("%1 秒"));
    _label_find_timer = new LabelsTimer(ui->label_check_timer, 60, QString::fromLocal8Bit("%1 秒"));
    _label_wait_timer = new LabelsTimer(ui->label_wait_time, 60, QString::fromLocal8Bit("%1 秒"));

    connect(_label_pay_timer, &LabelsTimer::timeout, this, &HostWidget::on_TimeOut);
    connect(_label_find_timer, &LabelsTimer::timeout, this, &HostWidget::on_TimeOut);
    connect(_label_wait_timer, &LabelsTimer::timeout, this, &HostWidget::on_TimeOut);

    _label_pay_movie = new QMovie(":res/loading.gif");
    _label_pay_movie->start();
    ui->label_pay_time_load->setMovie(_label_pay_movie);
    ui->label_logo->setMovie(_label_pay_movie);
    ui->label_wait_time_lod->setMovie(_label_pay_movie);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(45);

    for(int r=0; r<10; r++) {
        ui->tableWidget->insertRow(r);
    }

}

void HostWidget::ShowWidget(QWidget *showwidget)
{
    if(showwidget == ui->pay) {
        ui->lineEdit_pay_code->clear();
        ui->btn_pay_exitint->hide();
        ui->lineEdit_pay_code->setWindowOpacity(0);
        ui->lineEdit_pay_code->setFocus();
        ui->label_pay_msg_time->setVisible(false);

        _label_pay_timer->start(60, QString::fromLocal8Bit("%1 秒"));
    }

    for(int i = 0; i < _widgetlist.size(); ++i) {
        if(showwidget == _widgetlist.at(i)) {
            if(showwidget->isHidden())
                showwidget->show();

            continue;
        }
        _widgetlist.at(i)->hide();
    }

    //解决POS收银屏幕多屏时出现程序界面跑到非主屏BUG; 需要将此处代码转移到父类中测试;
    //QDesktopWidget qDW; int primaryScreenIndex = qDW.primaryScreen();//程序获取主屏幕索引号;
    //QRect tempRect = qDW.screenGeometry(primaryScreenIndex);//根据屏幕索引号获取屏幕尺寸
    //this->move(tempRect.left(),tempRect.top()); //移动到主屏并在主屏上显示全屏
    this->show();
    //this->showFullScreen(); //出现双屏POS之后就在无全屏展示了;
}

void HostWidget::InitWidget()
{
    for(int i = 0; i < _widgetlist.size(); ++i) {
        _widgetlist.at(i)->hide();
    }

    // Init manage page.
    int btnNums = 3;

    ui->btn_mananger_print_shift->setVisible(false);
    ui->btn_mananger_print_shift->setMaximumSize(0,0);
    ui->btn_mananger_print_shift->setMinimumSize(0,0);

    // Simphony不显示流水查询，但显示卡券流水查询和第三方流水查询
    ui->btn_mananger_check_coupon->setVisible(false);
    ui->btn_mananger_check_coupon->setMaximumSize(0,0);
    ui->btn_mananger_check_coupon->setMinimumSize(0,0);
    ui->btn_mananger_check_third->setVisible(false);
    ui->btn_mananger_check_third->setMaximumSize(0,0);
    ui->btn_mananger_check_third->setMinimumSize(0,0);

    ui->btn_mananger_print->setVisible(false);
    ui->btn_mananger_print->setMaximumSize(0,0);
    ui->btn_mananger_print->setMinimumSize(0,0);

    int spacing = (qApp->desktop()->availableGeometry().width()-ui->btn_mananger_register->width()*btnNums)/(btnNums+2);
    ui->horizontalLayout_manager->setSpacing(spacing);

    QStringList payCodes = _setting.GetAllKeys(CONFIG_PAYMENT_METHODS_THIRD);

    foreach (QString payCode, payCodes) {
        QString payStr = _setting.GetValue(QString("%1/%2").arg(CONFIG_PAYMENT_METHODS_THIRD).arg(payCode), "").toString();
        _paymentMethodsMapThird.insert(payStr, payCode);
        ui->comBox_payment_method->addItem(payStr);
    }

    payCodes = _setting.GetAllKeys(CONFIG_PAYMENT_METHODS_COUPON);

   foreach (QString payCode, payCodes) {
       QString payStr = _setting.GetValue(QString("%1/%2").arg(CONFIG_PAYMENT_METHODS_COUPON).arg(payCode), "").toString();
       _paymentMethodsMapCoupon.insert(payStr, payCode);
       ui->comBox_payment_method->addItem(payStr);
   }

}

void HostWidget::setRuning(bool runing)
{
    _runing = runing;
}

HostWidget::~HostWidget()
{
    delete ui;
    if(_label_pay_timer != NULL) {
        delete _label_pay_timer;
        _label_pay_timer = NULL;
    }
    if(_label_wait_timer != NULL) {
        delete _label_wait_timer;
        _label_wait_timer = NULL;
    }
    if(_label_find_timer != NULL) {
        delete _label_find_timer;
        _label_find_timer = NULL;
    }
    if(_label_pay_movie  != NULL) {
        delete _label_pay_movie;
        _label_pay_movie = NULL;
    }
}

void HostWidget::keyPressEvent(QKeyEvent *ke)
{
    if (ke->key() == Qt::Key_Return && _curr_show_widget == ui->pay)
    {
        ShowPayWidget();
    } else {
        FMPWindow::keyPressEvent(ke);
    }
}

void HostWidget::ShowPayWidget()
{
    if(!_need_exit && _runing == false) {
    	_runing = true;
        ui->btn_pay_exit->hide();
        ui->label_pay_money->hide();
        _label_pay_timer->start(60, QString::fromLocal8Bit("%1 秒"));

        QStringList list;
        list.append(ui->lineEdit_pay_code->text());
        QLOG_INFO() << list;

        if(_type == PAY) {
            ui->label_pay_title->setText(QString::fromLocal8Bit("支付中/等待用户输入密码"));
            _intertime.start(2*1000);
            emit RequestWithType(PAY, list);
        }
    }

}


void HostWidget::ShowQueryRespJson(bool flag, QJsonObject object, QString error)
{
#ifdef FMTEST_DATA
    flag = true;
    object[JSON_KEY_STATUSCODE] = 100;
    object[JSON_KEY_TOTALNUM] = 22;
    object[JSON_KEY_TOTALAMOUNT] = 998;
    QJsonArray queryInfo;
    QJsonObject queryObj;
    if(ui->comBox_payment_method->currentIndex() < 3) {
        queryObj[JSON_KEY_FMID] = "13131712271000005006";
        queryObj[JSON_KEY_FMID] = "13131712271000005006";
    } else {
        queryObj[JSON_KEY_CODE] = "7310180806223610\n2432342342\n2434223423\n1231231231";
        queryObj[JSON_KEY_AUTHCODE] = "856875";
        queryObj[JSON_KEY_SALENO] = "500389";
    }
    queryObj[JSON_KEY_TRANSTIME] = "2017-12-26 10:00:00";
    queryObj[JSON_KEY_TRANSAMOUNT] = 100;
    queryObj[JSON_KEY_STATE] = 1;
    queryObj[JSON_KEY_STATEMSG] = QString::fromLocal8Bit("成功");
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    queryInfo.append(queryObj);
    object[JSON_KEY_QUERYINFO] = queryInfo;
#endif
    ui->widget_check_load->hide();
    ui->btn_find_check->show();
    _label_find_timer->stop();
    _need_exit = true;
    QLOG_INFO() << object;

    if(!flag) {
        ui->label_find_msg->setText(QString::fromLocal8Bit("%1").arg(error));
    } else {
        if((object.contains(JSON_KEY_TOTALNUM) && object[JSON_KEY_TOTALNUM].toInt() == 0) ||
                !object.contains(JSON_KEY_QUERYINFO))
        {
            ui->label_find_msg->setText(QString::fromLocal8Bit("无交易记录"));
        } else {
            ui->frame->show();
            ui->label_find_msg->setText(QString::fromLocal8Bit("%1的账单流水").arg(_queryPaymentDate.toString("yyyy-MM-dd")));
            ui->label_total_amount->setText(QString::fromLocal8Bit("总金额：%1").arg(object[JSON_KEY_TOTALAMOUNT].toInt(0) / 100.0));

            if(ui->label_find_page->text().compare("1") == 0) {
                _line = object[JSON_KEY_TOTALNUM].toInt()/10;

                if(object[JSON_KEY_TOTALNUM].toInt()%10 != 0)
                    _line += 1;
            }

            QJsonArray array = object[JSON_KEY_QUERYINFO].toArray();

            int row = 0, col = 0;
            foreach(auto val , array) {
                QJsonObject tmp = val.toObject();

                QStringList itemDataList;
                if(_paymentMethodsMapThird.contains(_queryPaymentMethod)) {
                    itemDataList << tmp[JSON_KEY_FMID].toString();
                } else {
                    itemDataList << tmp[JSON_KEY_CODE].toString()
                                 << tmp[JSON_KEY_SALENO].toString()
                                 << tmp[JSON_KEY_AUTHCODE].toString();
                }
                itemDataList << QString::number(tmp[JSON_KEY_TRANSAMOUNT].toInt()/100.0)
                             << tmp[JSON_KEY_TRANSTIME].toString()
                             << tmp[JSON_KEY_STATEMSG].toString();

                for(col=0; col<itemDataList.length(); col++) {
                    QTableWidgetItem *item = new QTableWidgetItem(itemDataList.at(col));
                    if(col == 0) {
                        item->setTextAlignment(Qt::AlignLeft);
                    } else {
                        item->setTextAlignment(Qt::AlignCenter);
                    }
                    item->setFont(QFont("Microsoft YaHei UI Light", 15));
                    ui->tableWidget->setItem(row, col, item);
                }

                //NOTE 显示支付渠道
                if( _queryPaymentMethod==QString::fromLocal8Bit("微信支付") || _queryPaymentMethod==QString::fromLocal8Bit("支付宝支付") ) {
                    QWidget *pWdg = new QWidget(ui->tableWidget);
                    QHBoxLayout *hLayout = new QHBoxLayout(pWdg);
                    QPushButton * pBtn = new QPushButton(pWdg);
                    pBtn->setFixedSize(70, 30);
                    pBtn->setFocusPolicy(Qt::NoFocus);
                    pBtn->setProperty("orderId", tmp[JSON_KEY_FMID].toString());

                    pBtn->setText(QString::fromLocal8Bit("打印"));
                    hLayout->addWidget(pBtn);
                    hLayout->setMargin(0);
                    pWdg->setLayout(hLayout);
                    ui->tableWidget->setCellWidget(row, ui->tableWidget->columnCount()-1, pWdg);   // 操作按钮
                    pBtn->setStyleSheet("border:0px;border-radius:2px;background-color: rgb(0, 179, 238);font: 75 20px \"Microsoft YaHei UI Light\";");

                    qDebug() << "********************-----------*******************" << tmp;

                    if(tmp[JSON_KEYT_STATE].toString().toInt() != 1) {
                        pBtn->setEnabled(false);
                        pBtn->setStyleSheet("border:0px;border-radius:2px;background-color: rgb(208, 208, 208);font: 75 20px \"Microsoft YaHei UI Light\";");
                    }

                    connect(pBtn, &QPushButton::clicked, this, [this, pBtn]()
                    {
                        QStringList list;
                        list.append(pBtn->property("orderId").toString());
                        on_btn_printagain_clicked();
                        emit RequestWithType(AGAIN_PRINT, list);
                    });

                }
                row ++;
            }

            // 根据时间所在列排序
            int sortColumn = 2;
            if(ui->tableWidget->columnCount() == 6) {
                sortColumn = 4;
            }
            ui->tableWidget->sortByColumn(sortColumn, Qt::DescendingOrder);

            ui->tableWidget->resizeRowsToContents();
            ui->tableWidget->show();
        }
    }
}


void HostWidget::ShowUiWithRequest(QJsonObject requestObj)
{
    _type = (ReqType)FMTool::GetJsonValue(requestObj, JSON_KEY_REQTYPE).toInt();
    _posReqJsonObj = requestObj;

    switch (_type) {
    case MANAGE:
    {
        ShowWidget(ui->Manager);
        _curr_show_widget = ui->Manager;
        break;
    }
    case SIGN:
    {
        ShowWidget(ui->Manager);
        _curr_show_widget = ui->Manager;
        on_btn_mananger_register_clicked();
        break;
    }
    case PAY:
    {
        ShowWidget(ui->pay);
        _curr_show_widget = ui->pay;

        QString reqCode = requestObj[JSON_KEY_CODE].toString("");
        if(reqCode.isEmpty()) {
            double Amount = FMTool::GetJsonValue(requestObj, JSON_KEY_TRANSAMOUNT).toInt()/100.0;
            ui->label_pay_title->setText(QString::fromLocal8Bit("请扫描支付二维码"));
            ui->btn_pay_exit->setText(QString::fromLocal8Bit("取消支付"));
            ui->label_pay_money->setText(QString::fromLocal8Bit("需收取 ￥ %1").arg(Amount));
        } else {
            ui->btn_pay_exit->hide();
            ui->label_pay_money->hide();
            _runing = true;   // 避免多次扫码
            _label_pay_timer->start(60, QString::fromLocal8Bit("%1 秒"));
            QStringList list;
            list.append(reqCode);
            ui->label_pay_title->setText(QString::fromLocal8Bit("支付中/等待用户输入密码"));
            _intertime.start(5*1000);
            emit RequestWithType(PAY, list);
        }
        break;
    }
    case REFUND:
    {
        ShowWidget(ui->pay);
        _curr_show_widget = ui->pay;
        ui->btn_pay_exit->hide();
        ui->label_pay_money->hide();
        _label_pay_timer->start(60, QString::fromLocal8Bit("%1 秒"));

        QStringList list;
        list.append(FMTool::GetJsonValue(requestObj, JSON_KEY_FMID).toString());
        ui->label_pay_title->setText(QString::fromLocal8Bit("退款中..."));
        emit RequestWithType(REFUND, list);
        break;
    }
    default:
        break;
    }
}

void HostWidget::ShowResponseMGS(ReqType type, bool flag, QString message)
{
    if(type == SIGN) {
        _need_exit = true;
        if(flag)
            ui->label_sign_title->setText(QString::fromLocal8Bit("获取签名成功"));
        else
            ui->label_sign_title->setText(QString::fromLocal8Bit("获取签名失败"));

        ui->label_sign_msg->setText(message);

        ui->btn_sign_sign->show();
        ShowWidget(ui->sign);
        return ;
    }

    if(type == PAY && _curr_show_widget == ui->pay) {
        _need_exit = true;
        _label_pay_timer->stop();
        ui->btn_pay_exitint->hide();
        ui->label_pay_time_load->setVisible(false);
        ui->label_pay_money->setVisible(true);
        ui->label_pay_money->setText(message);

        //星巴克业务需求->支付成功无需显示确认按钮，减少店员点击动作;
        ui->btn_pay_exit->setText(QString::fromLocal8Bit("确认"));
        ui->btn_pay_exit->setVisible(false);

        if(flag) {
            ui->label_pay_msg_time->setVisible(true);
            _label_pay_timer->setLabel(ui->label_pay_msg_time);
            _label_pay_timer->start(3);
            ui->label_pay_title->setText(QString::fromLocal8Bit("支付成功"));
            ui->label_pay_time->setPixmap(QPixmap(":res/success.png"));
        } else {
            Exits();
        }
        return ;
    }

    if(type == REFUND && _curr_show_widget == ui->pay) {
        _need_exit = true;
        _label_pay_timer->stop();
        ui->label_pay_time_load->setVisible(false);
        ui->label_pay_money->setVisible(true);
        ui->label_pay_money->setText(message);
        //星巴克业务需求->支付成功无需显示确认按钮，减少店员点击动作;
        ui->btn_pay_exit->setText(QString::fromLocal8Bit("确认"));
        ui->btn_pay_exit->setVisible(false);

        if(flag) {
            ui->label_pay_msg_time->setVisible(true);
            _label_pay_timer->setLabel(ui->label_pay_msg_time);
            _label_pay_timer->start(3);
            ui->label_pay_title->setText(QString::fromLocal8Bit("退款成功"));
            ui->label_pay_time->setPixmap(QPixmap(":res/success.png"));
        } else {
            Exits();
        }
        return ;
    }

    if(type == ENDDAY) {
            ui->label_wait_msg->hide();
            _need_exit = true;
            _label_wait_timer->stop();
            ui->label_wait_time_lod->setVisible(false);
            ui->label_wait_title->show();
            ui->btn_wait_quit->show();
            ui->btn_wait_quit->setText(QString::fromLocal8Bit("确认"));
            ui->label_wait_msg->show();
            ui->label_wait_msg->setText(message);

            if(flag) {
                ui->label_wait_msg_time->setVisible(true);
                _label_wait_timer->setLabel(ui->label_wait_msg_time);
                _label_wait_timer->start(3);
                ui->label_wait_title->setText(QString::fromLocal8Bit("打印日结单成功"));
                ui->label_wait_time->setPixmap(QPixmap(":res/success.png"));
            } else {
                ui->label_wait_title->setText(QString::fromLocal8Bit("打印日结单失败"));
                ui->label_wait_time->setPixmap(QPixmap(":res/warning.png"));
            }

        return ;
    }

    if(type == AGAIN_PRINT) {
        _need_exit = true;
        _label_pay_timer->stop();
        ui->label_pay_time_load->setVisible(false);
        ui->label_pay_money->setVisible(true);
        ui->label_pay_money->setText(message);
        ui->label_pay_title->show();
        ui->label_pay_msg_time->show();
        ui->btn_pay_exit->setVisible(true);
        ui->btn_pay_exit->setText(QString::fromLocal8Bit("确认"));

        if(flag) {
            ui->label_pay_msg_time->setVisible(true);
            _label_pay_timer->setLabel(ui->label_pay_msg_time);
            _label_pay_timer->start(3);
            ui->label_pay_title->setText(QString::fromLocal8Bit("重印小票成功"));
            ui->label_pay_time->setPixmap(QPixmap(":res/success.png"));
        } else {
            ui->label_pay_msg_time->hide();
            ui->label_pay_title->setText(QString::fromLocal8Bit("重印小票失败"));
            ui->label_pay_time->setPixmap(QPixmap(":res/warning.png"));
        }
        return ;
    }
}

void HostWidget::on_btn_printagain_clicked()
{
    _type = AGAIN_PRINT;
    ui->label_pay_title->hide();
    ui->label_pay_money->hide();
    ui->btn_pay_exit->hide();
    ShowWidget(ui->pay);
    _curr_show_widget = ui->pay;
}

void HostWidget::on_btn_mananger_register_clicked()
{
    QStringList list;
    ShowWidget(ui->sign);
    ui->btn_sign_sign->hide();
    ui->label_sign_title->setText(QString::fromLocal8Bit("签到中..."));
    ui->label_sign_msg->setText("");
    ui->label_sign_storeid->setText(FMTool::GetJsonValue(_posReqJsonObj, JSON_KEY_STOREID).toString());
    emit RequestWithType(SIGN, list);
}

void HostWidget::on_btn_mananger_check_clicked()
{
    _type = FINDS;
    ui->label_find_title->setText(QString::fromLocal8Bit("门店%1交易流水查询").arg(FMTool::GetJsonValue(_posReqJsonObj, JSON_KEY_STOREID).toString()));
    ui->label_find_msg->clear();
    ui->dateEdit_find_begin->setDisplayFormat("yyyy-MM-dd");
    ui->dateEdit_find_begin->setDate(QDate::currentDate());
    ui->dateEdit_find_begin->setMinimumDate(QDate::currentDate().addDays(-35));
    ui->dateEdit_find_begin->setMaximumDate(QDate::currentDate());
    ui->tableWidget->hide();
    ui->frame->hide();
    ui->label_total_amount->setText("");
    ui->widget_check_load->hide();
    ShowWidget(ui->find);
    _curr_show_widget = ui->find;
    ui->label_find_page->setText("1");
}

void HostWidget::on_btn_mananger_print_clicked()
{
    QStringList list;
    this->hide();
    ui->label_wait_msg_time->setVisible(false);
    ui->btn_wait_quit->hide();
    ui->label_wait_time_lod->setVisible(true);
    ui->label_wait_title->setText(QString::fromLocal8Bit("正在打印日结单.."));
    ShowWidget(ui->wait);
    _curr_show_widget = ui->wait;
    _label_wait_timer->start();
    emit RequestWithType(ENDDAY, list);
}

void HostWidget::on_TimeOut()
{
    this->hide();
    if(_need_exit)
        emit Exits();
    else
        emit ExitWithMSG("01", QString::fromLocal8Bit("操作超时"));
}

void HostWidget::on_btn_mananger_exit_clicked()
{
    this->hide();
    emit ExitWithMSG("02", QString::fromLocal8Bit("收银员取消"));
}

void HostWidget::on_btn_pay_exit_clicked()
{
    this->hide();
    if(_need_exit)
        emit Exits();
    else
        emit ExitWithMSG("02", QString::fromLocal8Bit("收银员取消"));
}

void HostWidget::on_btn_find_exit_clicked()
{
    this->hide();
    if(_need_exit)
        emit Exits();
    else
        emit ExitWithMSG("02", QString::fromLocal8Bit("收银员取消"));
}

void HostWidget::on_btn_sign_exit_clicked()
{
    this->hide();
    if(_need_exit)
        emit Exits();
    else
        emit ExitWithMSG("01", QString::fromLocal8Bit("收银员取消"));
}

void HostWidget::on_btn_sign_sign_clicked()
{
    QStringList list;
    ui->btn_sign_sign->hide();
    emit RequestWithType(SIGN, list);
}


void HostWidget::on_btn_find_check_clicked()
{
    QStringList list;
    if(_queryConditionsIsChanged) {
        _queryPaymentMethod = ui->comBox_payment_method->currentText();
        _queryPaymentDate = ui->dateEdit_find_begin->date();
        ui->label_find_page->setText("1");
        _queryConditionsIsChanged = false;
    }

    ui->tableWidget->clear();
    SetTableWidgetHeader();
    ui->frame->hide();
    ui->label_total_amount->setText("");
    ui->widget_check_load->show();
    ui->btn_find_check->hide();
    list.append(_queryPaymentDate.toString("yyyy-MM-dd 00:00:00"));
    list.append(_queryPaymentDate.addDays(1).toString("yyyy-MM-dd 00:00:00"));
    list.append(ui->label_find_page->text());

    // 支付方式code
    QString payCode;
    if(_paymentMethodsMapThird.contains(_queryPaymentMethod)) {
        payCode = _paymentMethodsMapThird[_queryPaymentMethod];
    } else {
        payCode = _paymentMethodsMapCoupon[_queryPaymentMethod];
    }
    list.append(payCode);

    qDebug() << "Find list: " << list;
    _label_find_timer->start();
    emit RequestWithType(FINDS, list);
}

void HostWidget::on_btn_find_next_clicked()
{
    if(ui->label_find_page->text().toInt() == _line)
        return ;

    int page = ui->label_find_page->text().toInt() + 1;
    ui->label_find_page->setText(QString::number(page));

    on_btn_find_check_clicked();
}

void HostWidget::on_btn_find_up_clicked()
{
    if(ui->label_find_page->text().compare("1") != 0)
    {
        int page = ui->label_find_page->text().toInt() - 1;
        ui->label_find_page->setText(QString::number(page));
    }
    on_btn_find_check_clicked();
}

void HostWidget::HideExit()
{
    ui->btn_pay_exitint->hide();
}

void HostWidget::on_btn_pay_exitint_clicked()
{
    if(_intertime.isActive()) {
        _intertime.stop();
    }
    ui->btn_pay_exitint->hide();
    ui->btn_pay_exit->hide();
    _label_pay_timer->start(60);
    ui->label_pay_title->setText(QString::fromLocal8Bit("交易取消中"));
    emit Interrupt();
}

void HostWidget::on_btn_wait_quit_clicked()
{
    _label_wait_timer->stop();
    this->hide();
    if(_need_exit)
        emit Exits();
    else
        emit ExitWithMSG("02", QString::fromLocal8Bit("收银员取消"));
}

void HostWidget::StopPay()
{
    ui->label_pay_title->setText(QString::fromLocal8Bit("正在取消退款..."));
    _intertime.stop();
    ui->btn_pay_exitint->hide();
    _label_pay_timer->start(60, QString::fromLocal8Bit("%1 秒"));
}

void HostWidget::onPaymentMethodBtnsToggled(QAbstractButton *button, bool checked)
{
    static QString style_checked("#%1{border:0px;border-radius:2px;background-color:rgb(0,179,238);font:75 20px\"MicrosoftYaHeiUILight\";max-height:35px;max-width:100px;min-height:35px;min-width:100px;}");
    static QString style_unchecked("#%2{border:0px;border-radius:2px;background-color:rgb(150, 150, 150);font:75 20px\"MicrosoftYaHeiUILight\";max-height:35px;max-width:100px;min-height:35px;min-width:100px;}");

    if(checked) {
        button->setStyleSheet(style_checked.arg(button->objectName()));
    } else {
        button->setStyleSheet(style_unchecked.arg(button->objectName()));
    }
}

void HostWidget::SetTableWidgetHeader()
{
    QStringList headerString;
    QList<double> widthScale;

    if(_paymentMethodsMapThird.contains(_queryPaymentMethod)) {
        headerString<<QString::fromLocal8Bit("订单编号")<<QString::fromLocal8Bit("订单金额")<<QString::fromLocal8Bit("订单时间")<<QString::fromLocal8Bit("支付状态");
        // 只有微信支付和支付宝支付有操作列
        if( _queryPaymentMethod==QString::fromLocal8Bit("支付宝支付")||_queryPaymentMethod==QString::fromLocal8Bit("微信支付") ) {
            headerString << QString::fromLocal8Bit("操作");
        }
    } else {
        headerString<<QString::fromLocal8Bit("卡券号码")<<QString::fromLocal8Bit("销售单号")<<QString::fromLocal8Bit("授权码")<<QString::fromLocal8Bit("交易金额")<<QString::fromLocal8Bit("交易时间")<<QString::fromLocal8Bit("状态");
    }

    switch (headerString.length()) {
    case 4:
        widthScale << 0.39 << 0.18 << 0.25 << 0.18;
        break;
    case 5:
        widthScale << 0.35 << 0.15 << 0.21 << 0.15 << 0.14;
        break;
    case 6:
        widthScale << 0.27 << 0.13 << 0.13 << 0.13 << 0.20 << 0.12;
        break;
    default:
        break;
    }

    int width  = ui->widget_find_main->width() - 4;

    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{border: 0px;font: 75 18px \"Microsoft YaHei UI Light\";}");
    ui->tableWidget->horizontalHeader()->setFixedHeight(25); //设置表头的高度

    ui->tableWidget->setColumnCount(headerString.length());

    for(int i=0; i<headerString.length(); i++) {
        ui->tableWidget->setColumnWidth(i, widthScale[i]*width);
    }

    ui->tableWidget->setHorizontalHeaderLabels(headerString);

}

void HostWidget::on_dateEdit_find_begin_dateChanged()
{
    _queryConditionsIsChanged = true;
}

void HostWidget::on_comBox_payment_method_currentTextChanged()
{
    _queryConditionsIsChanged = true;
}
