#include "detailForm.h"
#include "ui_detailForm.h"
#include "DTools/util.h"
#include "preDefine.h"
#include "DTools/configManger.h"
#include "Control/flowControl.h"

DetailForm::DetailForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DetailForm)
{
    ui->setupUi(this);
    connect(this, &DetailForm::processOrder, &FlowControl::GetInstance(), &FlowControl::onProcessOrder);
    connect(this, &DetailForm::processRejectOrder, &FlowControl::GetInstance(), &FlowControl::onProcessRejectOrder);
    connect(this, &DetailForm::processRepealOrder, &FlowControl::GetInstance(), &FlowControl::onProcessRepealOrder);
    m_rejectForm=NULL;
    m_refuseForm=NULL;
    _Init();
}

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

void DetailForm::InitData(OrderObject *orderObject)
{
    m_orderObject = orderObject;
    // 恢复界面
    ui->detailTable0->clearContents();
    ui->detailTable0->setRowCount(0);
    ui->detailBtn2->show();
    ui->detailBtn3->show();
    ui->detailBtn1->hide();
    ui->detailBtn0->hide();
    // 初始化界面
    ui->detailLab0->setText(QString("[%1 # %2][%3][%4]").arg(QString::number(orderObject->orderIndex),orderObject->getChannelName(),
                                                        orderObject->getOrderStatusDec(),
                                                        orderObject->id));
    ui->detailLab1->setText(QString("%1:%2").arg(orderObject->customerName,orderObject->phone));
    ui->detailLab2->setText(orderObject->deliveryTime.isEmpty()? QString::fromLocal8Bit("立即送出") : orderObject->deliveryTime);
    ui->detailLab3->setText(orderObject->riderName.isEmpty() ? QString::fromLocal8Bit("暂未指定") : QString::fromLocal8Bit("[姓名]%1 [电话]%2")
                                                                            .arg(orderObject->riderName, orderObject->riderPhone));
//TODO
    ui->detailLab4->setText(Penny2Dollar(orderObject->productPrice));
    ui->detailLab6->setText(orderObject->needInvoice? orderObject->invoiceTitle : QString::fromLocal8Bit("不需要发票"));
    ui->detailLab5->setText(QString("[%1]").arg(orderObject->memo));
    for(int i=0; i<orderObject->proList.count(); i++)
    {
        ui->detailTable0->insertRow(i);
        ui->detailTable0->setItem(i, 0, new QTableWidgetItem(GetProductName(orderObject->proList.at(i))));  // 商品名
        ui->detailTable0->item(i, 0)->setTextAlignment(Qt::AlignCenter);
        ui->detailTable0->setItem(i, 1, new QTableWidgetItem(Penny2Dollar(orderObject->proList.at(i)->price)));  // 价格
        ui->detailTable0->item(i, 1)->setTextAlignment(Qt::AlignCenter);
        ui->detailTable0->setItem(i, 2, new QTableWidgetItem(QString::number(orderObject->proList.at(i)->qty)));  // 数量
        ui->detailTable0->item(i, 2)->setTextAlignment(Qt::AlignCenter);
    }
//TODO
//    QString records;
//    foreach(QString record, orderObject->records)
//    {
//        records.append(record+"\r\n");
//    }
//    ui->detailLabRecord->setText(records);

//    if(!ConfigManger::GetInstance().GetIsExistReEntryOrder())
//    {
//        ui->detailBtn0->hide();
//    }

    ui->detailBtn2->setText(GetOperNameByStatus(orderObject->orderStatus, true));
    ui->detailBtn2->setProperty("operation", GetOperByStatus(orderObject->orderStatus, true));
    ui->detailBtn2->setProperty("orderId", orderObject->id);
    if(orderObject->orderStatus != 1 && orderObject->orderStatus != 20&& orderObject->orderStatus != 2&& orderObject->orderStatus != 4)
    {
        ui->detailBtn2->hide();
    }

    ui->detailBtn3->setText(GetOperNameByStatus(orderObject->orderStatus));
    ui->detailBtn3->setProperty("operation", GetOperByStatus(orderObject->orderStatus));
    ui->detailBtn3->setProperty("orderId", orderObject->id);

    if(!GetOperByStatus(orderObject->orderStatus).compare("NULL"))
    {
        ui->detailBtn3->hide();
    }
    if(m_rejectForm==NULL)
        m_rejectForm = new RejectForm(this);
    if(m_refuseForm==NULL)
        m_refuseForm = new RefuseForm(this);
#ifdef TODO
    ui->detailBtn3->hide();
#endif
}

void DetailForm::_Init()
{
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);

    setGeometry(((QWidget*)parent())->geometry());

    int tableWidth = this->width()/2 - 65;
    ui->detailTable0->setColumnWidth(0, tableWidth*5/9);
    ui->detailTable0->setColumnWidth(1, tableWidth*2/9);
    ui->detailTable0->setColumnWidth(2, tableWidth*2/9);
    ui->detailTable0->horizontalHeader()->setFixedHeight(25);
    ui->detailTable0->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
}

QString DetailForm::GetProductName(dishesObject *dish)
{
    QString name;
    QString propertyTags;
    name.append(dish->name).append("\r\n");
    propertyTags=dish->specId;
    if(!propertyTags.isEmpty())
    {
        name.append(QString("(%1)").arg(propertyTags));
    }

    if(!dish->sub_products.isEmpty())
    {
        foreach(auto value,dish->sub_products)
        {
            name.append(value->name).append("|");
        }
    }
    name.remove((name.length()-1),1);
    return name;
}

void DetailForm::onOperaBtnClicked()
{
    hide();
    QPushButton *pBtn = (QPushButton*)sender();
    if(pBtn->text()==QString::fromLocal8Bit("拒单")||pBtn->text()==QString::fromLocal8Bit("退单"))
    {
        //        m_rejectForm->show();
        //        if(m_rejectForm->exec()==QDialog::Accepted)
        //            {
        //                emit processRejectOrder( pBtn->property("orderId").toString(),m_rejectForm->GetReasonCode());
        //            }
        //    }
        //    else if(pBtn->text()=="退单")
        //    {
        m_refuseForm->show();
        if(m_refuseForm->exec()==QDialog::Accepted)
        {
            emit processRepealOrder( pBtn->property("orderId").toString(),m_refuseForm->m_reasonCode,m_refuseForm->m_reasonStr);
        }
    }
    else
    {
        emit processOrder(pBtn->property("operation").toString(), pBtn->property("orderId").toString());
    }
}

void DetailForm::on_detailBtn1_clicked()
{
}
