#ifndef NEWMAINFORM_H
#define NEWMAINFORM_H

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QSettings>
#include <QSet>
#include <QString>
#include <QMap>
#include <QVector>
#include <QVariantMap>

#include "preDefine.h"
#include "model/posorderpool.h"
#include "view/mainbtn.h"

#define MAIN_ORDER  "order"
#define MAIN_REFUND "refund"
#define MAIN_SET    "set"

#define SET_PRT_DV  "dv"
#define SET_PRT_COM  "com"
#define SET_PRT_LPT  "lpt"
#define SET_PRT_NET  "net"

#define ORDER_TYPE_AUTO     "auto"
#define ORDER_TYPE_SELF     "self"
#define ORDER_TYPE_TIMEOUT  "timeout"

#define TYPE_NEEDREFUND     0
#define TYPE_REFUNDED       1
#define TYPE_ALL            2
#define TYPE_NEW            3
#define TYPE_MAKE           4
#define TYPE_SEND           5
#define TYPE_NAKEOVER       6
#define TYPE_OVER           7
#define TYPE_FUTURE         8

typedef struct
{
    int order_status;
    int refund_status;
} Order_Index;

namespace Ui {
class NewMainForm;
}

class NewMainForm : public QWidget
{
    Q_OBJECT

public:
    explicit NewMainForm(QWidget *parent = 0);
    ~NewMainForm();

    bool event(QEvent *e);

    void FullShow();
private slots:
    void on_newmainbtn_clicked();

    void on_classifybtn_clicked();

    void on_PrtSet_toggled(bool checked);

    void on_newmaincombx_prttype_currentIndexChanged(int index);

    void on_newmaintbwgt_tabBarClicked(int index);

    void on_newmainbtn_prtsave_clicked();

    void on_newmainbtn_prtadd_clicked();

    void on_newmainbtn_prtdel_clicked();

    void on_newmaincombx_name_currentIndexChanged(int index);

    void on_newmainvtn_ordersave_clicked();

    void on_newmainbtn_down_clicked();

    void on_newmainbtn_up_clicked();

    void on_newmainbtn_close_clicked();

    void on_newmainbtn_jump_clicked();

private:
    Ui::NewMainForm *ui;
    //
    QSet<MainBtn *> _main_btn;
    //打印界面集合
    QSet<QWidget *> _prtsetting_widget;
    //分类器集合
    QSet<QPushButton *> _classify_set;
    //选中的分类
    QPushButton * _curr_check_btn;
    //后厨打印配置容器
    QVector< QMap< QString, QString > > _other_prt_configs;
    //小票打印机配置
    QMap<QString,QString> _main_prt_configs;
    //杯贴打印机配置
    QMap<QString,QString> _label_prt_configs;
    //其他打印机数量
    int _other_prt_num;
    //订单索引
    QMap<QString, Order_Index> _order_indexs;
    //
    QVariantMap _storeinfo;
    //当前总页数
    int _curr_page_num;
    //当前页数
    int _curr_page;


private:
    void InitOrderWidget(QString type);
    void InitSetWidget();
    void InitWidget(QString type);
    void InitClassify();
    void SetChecked(QPushButton *btn);
    void UpdateTableWidget(OrderObject &orderObject);
    void DeleteTableWidget(QString orderid);
    //初始化设置界面
    void InitSettingWidget(QString type);
    //初始化订单界面
    void InitOrderSetting();
    //初始化打印机界面
    void InitPrtSetting();
    //初始化用户设置界面
    void InitUserSetting();
    void InitPrtSettingWidget();
    void LoadMainPrtSetting();
    void InitPrtConfig();
    void LoadLabelPrtSetting();
    void SettingWidgetWithConf(int index, QMap<QString, QString> &map);
    void LoadOtherPrtSetting(int index);
    void InitPrtList();
    void ShowSetErrorMsg(QString str);
    bool GetMapAndSaveSet(int index, QMap<QString, QString> &map);
    void InsertTableWidget(OrderObject &orderObject);
    void GetOrderStatus(OrderObject &order, int &order_status);
    void InitTableWidget();
    bool ClassifyOrder(QString type);
    void ClassifyOrderWithOrderStatus(int order_status);
    void InitPageNum();
    void InitMainBtn();
    void SetMainBtnCheck(MainBtn *wgt);
    void ClassifyOrderWithOrderType();
};

class NewMainFormDataProcess
{
public:
    friend class NewMainForm;

    static bool GetOrderInde(OrderObject &order, Order_Index &order_index)
    {
        order_index.order_status = order.status;
        order_index.refund_status = order.refund_status;
    }

private:
    static void SetOrderConfig(QString autoconf, QString timeout)
    {
        QString path = QString("%1/config.ini").arg(qApp->applicationDirPath());
        QSettings config(path, QSettings::IniFormat);
        config.setValue(INI_AUTOCONFIRM, autoconf);
        config.setValue(INI_DELEORDERTIMER, timeout);
    }

    static void SetOtherPrintNum(int prt_num)
    {
        QString path = QString("%1/needprt.ini").arg(qApp->applicationDirPath());
        QSettings config(path, QSettings::IniFormat);
        config.setValue("all/needothernum", prt_num);
    }

    static void SetPrintIniWithInde(int index, QMap<QString, QString> &map)
    {
        QString section = "Printer";
        QString comsection = "COMDEF";

        if(index == 2)
        {
             section = "OtherPrinter";
             comsection = "OTHERCOMDEF";
        }

        if(index > 2)
        {
            section = QString("OtherPrinter") + QString::number(index - 2 + 1);
            comsection = QString("OTHERCOMDEF") + QString::number(index - 2 + 1);
        }

        SetPrintIni(section, comsection, map);

    }

    static void GetPrintIniWithInde(int index, QMap<QString, QString> &map)
    {
        QString section = "Printer";
        QString comsection = "COMDEF";

        if(index == 2)
        {
             section = "OtherPrinter";
             comsection = "OTHERCOMDEF";
        }

        if(index > 2)
        {
            section = QString("OtherPrinter") + QString::number(index - 2 + 1);
            comsection = QString("OTHERCOMDEF") + QString::number(index - 2 + 1);
        }

        GetPrintIni(section, comsection, map);
    }

    static void SetPrintIni(QString section, QString comsection, QMap<QString, QString> &map)
    {  
        QString path = QString("%1/printer.ini").arg(qApp->applicationDirPath());
        QSettings config(path, QSettings::IniFormat);

        QMap<QString, QString>::iterator it;

        for (it = map.begin();it != map.end(); ++it)
        {
            if(map["type"].compare("0") == 0 || it.key().compare("type",Qt::CaseInsensitive) != 0)
                config.setValue(QString(comsection) + "/" + it.key(), it.value());
            config.setValue(QString(section) + "/" + it.key(), it.value());
        }
    }

    static void GetPrintIni(QString section, QString comsection, QMap<QString, QString> &map)
    {
        QString path = QString("%1/printer.ini").arg(qApp->applicationDirPath());
        QSettings config(path, QSettings::IniFormat);

        if(!config.value(section+"/Name").toString().isEmpty())
            map.insert("Name",config.value(section+"/Name").toString());
        if(!config.value(section+"/printType").toString().isEmpty())
            map.insert("printType",config.value(section+"/printType").toString());
        if(!config.value(section+"/PaperWidth").toString().isEmpty())
            map.insert("PaperWidth",config.value(section+"/PaperWidth").toString());
//        if(!config.value(section+"/levlel").toString().isEmpty())
//            map.insert(section+"/levlel",config.value(section+"/levlel").toString());
        if(!config.value(section+"/parallel").toString().isEmpty())
            map.insert("parallel",config.value(section+"/parallel").toString());
        if(!config.value(section+"/type").toString().isEmpty())
            map.insert("type",config.value(section+"/type").toString());
        if(!config.value(section+"/usb").toString().isEmpty())
            map.insert("usb",config.value(section+"/usb").toString());
        if(!config.value(section+"/IP").toString().isEmpty())
            map.insert("IP",config.value(section+"/IP").toString());
        if(!config.value(section+"/Printlabel").toString().isEmpty())
            map.insert("Printlabel",config.value(section+"/Printlabel").toString());
//        if(!config.value(section+"/LogDays").toString().isEmpty())
//            map.insert(section+"/LogDays",config.value(section+"/LogDays").toString());
        if(!config.value(section+"/wigth").toString().isEmpty())
            map.insert("wigth",config.value(section+"/wigth").toString());
        if(!config.value(section+"/high").toString().isEmpty())
            map.insert("high",config.value(section+"/high").toString());
//        if(!config.value(section+"/speed").toString().isEmpty())
//            map.insert(section+"/speed",config.value(section+"/speed").toString());
//        if(!config.value(section+"/crow").toString().isEmpty())
//            map.insert(section+"/crow",config.value(section+"/crow").toString());
        if(!config.value(section+"/labeltype").toString().isEmpty())
            map.insert("labeltype",config.value(section+"/labeltype").toString());
        if(!config.value(section+"/labelparall").toString().isEmpty())
            map.insert("labelparall",config.value(section+"/labelparall").toString());
        if(!config.value(section+"/labelusb").toString().isEmpty())
            map.insert("labelusb",config.value(section+"/labelusb").toString());
        if(!config.value(section+"/labelip").toString().isEmpty())
            map.insert("labelip",config.value(section+"/labelip").toString());
        if(!config.value(section+"/labelprttype").toString().isEmpty())
            map.insert("labelprttype",config.value(section+"/labelprttype").toString());

        //-----------------------------------------------------------------------------------
        if(!config.value(comsection+"/com").toString().isEmpty())
            map.insert("com", config.value(comsection+"/com").toString());
        if(!config.value(comsection+"/Baudrate").toString().isEmpty())
            map.insert("Baudrate", config.value(comsection+"/Baudrate").toString());
        if(!config.value(comsection+"/DataBits").toString().isEmpty())
            map.insert("DataBits",config.value(comsection+"/DataBits").toString());
        if(!config.value(comsection+"/StopBits").toString().isEmpty())
            map.insert("StopBits",config.value(comsection+"/StopBits").toString());
        if(!config.value(comsection+"/Parity").toString().isEmpty())
            map.insert("Parity",config.value(comsection+"/Parity").toString());
        if(!config.value(comsection+"/FlowControl").toString().isEmpty())
            map.insert("FlowControl",config.value(comsection+"/FlowControl").toString());
    }
};

#endif // NEWMAINFORM_H
