#include "detailForm.h"
#include "ui_detailForm.h"
#include "base/Arithmetic/util.h"
#include "preDefine.h"
#include "base/Config/configManger.h"
//#include "Control/flowControl.h"
//#include "fmPlugin.h"
//#include "DTools/orderstatus.h"
#include <QDateTime>

#include "event/fmapplication.h"
#include "event/posevent.h"

#include "QsLog.h"

DetailForm::DetailForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DetailForm)
{
    ui->setupUi(this);
    ui->detailBtn0->hide();
    //connect(this, &DetailForm::processOrder, &FlowControl::GetInstance(), &FlowControl::onProcessOrder);
    //connect(this, &DetailForm::processRejectOrder, &FlowControl::GetInstance(), &FlowControl::onProcessRejectOrder);
    //connect(this, &DetailForm::processReportOrder, &FlowControl::GetInstance(), &FlowControl::onreportDaysOrder);
     m_rejectForm =NULL;
    _Init();
}

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

void DetailForm::InitData(OrderObject *orderObject)
{
    m_orderObject = orderObject;

    _orderid = orderObject->order_id;
    // 恢复界面
    ui->detailTable0->clearContents();
    ui->detailTable0->setRowCount(0);
    ui->detailBtn2->show();
    ui->detailBtn3->show();
    //ui->detailBtn1->hide();

    // 初始化界面
    ui->detailLab0->setText(QString::fromLocal8Bit("[%1][%2][%3]").arg(orderObject->channel_name,
                                                        orderObject->status_desc,
                                                        orderObject->order_id));
    ui->detailLab1->setText(QString::fromLocal8Bit("[%1]%2").arg(orderObject->pay_type,
                                                  orderObject->remark));
    ui->detailLab2->setText(QString::fromLocal8Bit("[姓名]%1 [电话]%2").arg(orderObject->customer,
                                                            orderObject->phone));
    ui->detailLab3->setText(orderObject->address);
    ui->detailLab4->setText(QString::fromLocal8Bit("[配送方式]%1 [配送费]%2").arg(orderObject->delivery_party,
                                                                 Penny2Dollar(orderObject->send_fee)));
    ui->detailLab5->setText(orderObject->delivery_time==0? QString::fromLocal8Bit("立即送出") : QDateTime::fromTime_t(orderObject->delivery_time).toString("MM/dd hh:mm"));
//    ui->detailLab6->setText(orderObject->courier_name.isEmpty() ? QString::fromLocal8Bit("暂未指定") : QString::fromLocal8Bit("[姓名]%1 [电话]%2")
//                                                                            .arg(orderObject->courier_name, orderObject->courier_phone));
    if(orderObject->disList.size() > 0)
        ui->detailLab6->setText(QString::number((orderObject->discount_fee)/100.0, 'f' , 2).append(QString::fromLocal8Bit("(元)")));
    else
        ui->detailLab6->setText("0");

    ui->detailLab7->setText(!orderObject->pay_type.compare("在线支付") ? "0" : Penny2Dollar(orderObject->user_fee));
    ui->detailLab8->setText(orderObject->has_invoiced? QString::fromLocal8Bit("发票抬头:%1").arg(orderObject->invoice_title) : QString::fromLocal8Bit("不需要发票"));
    ui->detailLab8_3->setText(QString::fromLocal8Bit("%1").arg(QString::number(orderObject->package_fee / 100.0, 'f', 2).append(QString::fromLocal8Bit("(元)"))));


    for(int i=0; i<orderObject->proList.count(); i++)
    {
        ProductObject *product = orderObject->proList.at(i);
        ui->detailTable0->insertRow(0);
        QString name = product->name , property_tags=product->property_tags;
        if(!property_tags.isEmpty())
        {
            //<font color='#ff0000'>未登录</font>
            QTableWidgetItem *item = new QTableWidgetItem(property_tags);
            item->setTextColor(QColor(255,0,0));
            //name.append(QString::fromLocal8Bit("\r\n加料：")).append(QString(property_tags.replace(",", "\r\n")));
            ui->detailTable0->setItem(0, 0, item);  // 商品名
            //setTextColor(QColor(255,0,0)
            ui->detailTable0->item(0, 0)->setTextAlignment(Qt::AlignCenter);
            ui->detailTable0->setItem(0, 1, new QTableWidgetItem(QString()));  // 价格
            ui->detailTable0->item(0, 1)->setTextAlignment(Qt::AlignCenter);
            ui->detailTable0->setItem(0, 2, new QTableWidgetItem(QString()));  // 数量
            ui->detailTable0->item(0, 2)->setTextAlignment(Qt::AlignCenter);
            ui->detailTable0->insertRow(0);
        }
        ui->detailTable0->setItem(0, 0, new QTableWidgetItem(name));  // 商品名
        ui->detailTable0->item(0, 0)->setTextAlignment(Qt::AlignCenter);
        ui->detailTable0->setItem(0, 1, new QTableWidgetItem(QString::number(orderObject->proList.at(i)->price/100.0)));  // 价格
        ui->detailTable0->item(0, 1)->setTextAlignment(Qt::AlignCenter);
        ui->detailTable0->setItem(0, 2, new QTableWidgetItem(QString::number(orderObject->proList.at(i)->product_amount)));  // 数量
        ui->detailTable0->item(0, 2)->setTextAlignment(Qt::AlignCenter);
    }

    QString records;
    foreach(QString record, orderObject->records)
    {
        records.append(record+"\r\n");
    }
    ui->detailLabRecord->setText(records);

//    if(!Orderstatus::getInstance().isput(orderObject->order_id))
//    {
//        ui->detailBtn0->show();
//    }
//    else
//    {
//        ui->detailBtn0->hide();
//    }

    int tmpstatus = 0;

    if(orderObject->refund_status == 20 || orderObject->refund_status == 100 || orderObject->refund_status == 22)
        tmpstatus = orderObject->refund_status;
    else
        tmpstatus = orderObject->status;

    ui->detailBtn2->setText(GetOperNameByStatus(tmpstatus, true));
    ui->detailBtn2->setProperty("operation", GetOperByStatus(tmpstatus, true));
    ui->detailBtn2->setProperty("orderId", orderObject->order_id);

    if(ui->detailBtn2->text().compare(QString::fromLocal8Bit(OPERATIONNAME_REFUNDORDER)) == 0)
        ui->detailBtn2->hide();
    else
        ui->detailBtn2->show();

    if(/*orderObject->status != 1 && orderObject->status != 20&& orderObject->status != 2&&*/ orderObject->refund_status != 20 && orderObject->status != 4)
    {
        ui->detailBtn2->hide();
    }
//    if(orderObject->status==100||orderObject->status==3)
//    {
//        ui->detailBtn0->hide();
//    }

//    if(ui->detailBtn3->text().compare(QString::fromLocal8Bit(OPERATIONNAME_MAKEOVER)) == 0 &&
//            (orderObject->order_type == 1 || orderObject->order_type == 2))
//    {
//        ui->detailBtn3->hide();
//    }
//    else
//        ui->detailBtn3->show();

    ui->detailBtn3->setText(GetOperNameByStatus(tmpstatus));
    ui->detailBtn3->setProperty("operation", GetOperByStatus(tmpstatus));
    ui->detailBtn3->setProperty("orderId", orderObject->order_id);
    ui->detailBtn3->setProperty("ordertype", orderObject->order_type);

    QLOG_DEBUG() << "----------------------------------------" << orderObject->order_id << ";" << orderObject->refund_status << tmpstatus;

    if((orderObject->order_type == 1 || orderObject->order_type == 2) &&
            ui->detailBtn3->text().compare(QString::fromLocal8Bit(OPERATIONNAME_MAKEOVER)) == 0 &&
            orderObject->status == 2 &&
            orderObject->refund_status != 20)
    {
        QLOG_DEBUG() << "++++++++++++++++++++++++++++++++++" << orderObject->order_id << ";" << orderObject->refund_status << tmpstatus;

        ui->detailBtn3->setText(QString::fromLocal8Bit(OPERATIONNAME_SENDOUT));
        ui->detailBtn3->setProperty("operation", OPERATION_SENDOUT);
    }

    if(orderObject->status == 5 || orderObject->status == 4)
    {
        ui->detailBtn3->setText(QString::fromLocal8Bit(OPERATIONNAME_COMPLETE));
        ui->detailBtn3->setProperty("operation", OPERATION_COMPLETE);
    }

    if(!GetOperByStatus(tmpstatus).compare("NULL"))
    {
        ui->detailBtn3->hide();
    }
}

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);
}

void DetailForm::onOperaBtnClicked()
{
    hide();
    QPushButton *pBtn = (QPushButton*)sender();

//    if((pBtn->property("ordertype").toInt() == 1 || pBtn->property("ordertype").toInt() == 2) &&
//            pBtn->property("operation").toString().compare(OPERATION_MAKEOVER, Qt::CaseInsensitive) == 0)
//    {
//        QLOG_DEBUG() << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << "operation : " << pBtn->property("operation").toString() << "; ordertype : " << pBtn->property("ordertype").toInt();
//        return ;
//    }

    //QLOG_INFO() << "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" << "operation : " << pBtn->property("operation").toString() << "; ordertype : " << pBtn->property("ordertype").toInt();

    if(m_rejectForm==NULL){m_rejectForm = new RejectForm(this);}

//    if(pBtn->text()=="拒单")
//    {
//        m_rejectForm->InitUi(REJECTUITYPE_REFUSEORDER);
//        m_rejectForm->show();
//        if(m_rejectForm->exec()==QDialog::Accepted)
//        {
//            emit processRejectOrder( pBtn->property("orderId").toString(),m_rejectForm->GetReasonCode(),m_rejectForm->GetReason(),REJECTUITYPE_REFUSEORDER);
//        }
//    }
//    else if(pBtn->text()=="退单")
//    {
//        m_rejectForm->InitUi(REJECTUITYPE_CANCLEORDER);
//        m_rejectForm->show();
//        if(m_rejectForm->exec()==QDialog::Accepted)
//        {
//            emit processRejectOrder( pBtn->property("orderId").toString(),m_rejectForm->GetReasonCode(),m_rejectForm->GetReason(),REJECTUITYPE_CANCLEORDER);
//        }
//    }
//    else if(pBtn->text()=="拒绝")
//    {
//        m_rejectForm->InitUi(REJECTUITYPE_REFUSEREFUND);
//        m_rejectForm->show();
//        if(m_rejectForm->exec()==QDialog::Accepted)
//        {
//            emit processRejectOrder( pBtn->property("orderId").toString(),m_rejectForm->GetReasonCode(),m_rejectForm->GetReason(),REJECTUITYPE_REFUSEREFUND);
//        }
//    }
//    else
//    {
//       emit processOrder(pBtn->property("operation").toString(), pBtn->property("orderId").toString(), DeliverObject());
//    }

    QVariantMap value;

    if(pBtn->text()=="拒单")
    {
        m_rejectForm->InitUi(REJECTUITYPE_REFUSEORDER);
        m_rejectForm->show();
        if(m_rejectForm->exec()!=QDialog::Accepted)
            return ;
        value.insert(EVENT_KEY_MSG, m_rejectForm->GetReason());
    }

    if(pBtn->text()==QString::fromLocal8Bit("退单"))
    {
        m_rejectForm->InitUi(REJECTUITYPE_CANCLEORDER);
        m_rejectForm->show();
        if(m_rejectForm->exec()!=QDialog::Accepted)
            return ;
        value.insert(EVENT_KEY_MSG, m_rejectForm->GetReason());
    }



    if(pBtn->text()==QString::fromLocal8Bit("拒绝"))
    {
        m_rejectForm->InitUi(REJECTUITYPE_REFUSEREFUND);
        m_rejectForm->show();
        if(m_rejectForm->exec() != QDialog::Accepted)
            return ;
        value.insert(EVENT_KEY_MSG, m_rejectForm->GetReason());
    }



    value.insert(EVENT_KEY_ORDERID, pBtn->property("orderId").toString());
    value.insert(EVENT_KEY_STATUS, pBtn->property("operation").toString());


    POSTEVENTTYPE(PosEvent::s_opt_order,value,QVariantMap);
}

void DetailForm::on_detailBtn1_clicked()
{
    QVariantMap map;

    map.insert(EVENT_KEY_ORDERID, _orderid);
    map.insert(EVENT_KEY_MSG, DEFAULT_LOCATION_PRINT);

    POSTEVENTTYPE(PosEvent::s_order_location, map, QVariantMap);
}

void DetailForm::on_detailBtn0_clicked()
{
//    QString error,business;
//    if(FmPlugin::GetInstance().DoOrderEntry(m_orderObject,error,business))
//    {
//        Orderstatus::getInstance().putupdate(m_orderObject->order_id,1);
//        emit processReportOrder(m_orderObject->order_id,business,1,error,m_orderObject->product_fee-m_orderObject->package_fee);
//        emit showAlert(AlertForm::SUCCESS, QString("[重新补录销售单成功]."));
//    }
//    else{
//        emit showAlert(AlertForm::ERROR, QString("重新补录销售单失败[%1]").arg(error));
//    }
//    hide();
//    emit doReEntry(m_orderObject);
}
