#ifndef REMOTEPACKCONFIG_H
#define REMOTEPACKCONFIG_H

#include <QObject>
#include "httprequest.h"
#include <QJsonArray>
#include <QPair>
#include <functional>

struct DIYPackConfig
{
    int type;//包装类型
    int danDianXiaXian;//單点下限
    int danDianShangXian;//單点上限
    int jingZhongShangXian;//净重上限
    int jingZhongXiaXian;//净重下限
};

struct POYPackConfig
{
    int type;//包装类型
    int jingZhongShangXian;//净重上限
    int jingZhongXiaXian;//净重下限
};

struct DaBaoConfig
{
    //基础配置
    QStringList jitai;//机台
    QStringList dangchegongbanci;//挡车工班次
    QStringList daBaoGongBanCi;//打包工班次
    QList<double> xiangzhong;//箱重
    QList<double> tongZhong;// 桶重
    QStringList guanse;//管色
    QStringList nianxiang;//捻向
    QStringList zhixiang;//纸箱
    QList<int> mashu;//码数
    QStringList cheZhong;//车重
    QStringList gangHao;//缸号
    QStringList pinZhong;//品种
    int xiangHaoXianShiGuiZe;//箱号显示规则
    int daBaoRiQiXianShiFangShi;//打包日期时间显示方式
    bool daBaoRuku;//打包即入库
    //打包配置
    bool ziDongDushu; //自动读数开启
    bool buDingZhongDaBaoXianzhi;//不定重打包筒数限制开启
    bool dingZhongDaBaoXianZhiKaiQi;//定重打包筒数限制开启
    bool jiTaiBangDingMaDan; //机台绑定码单
    bool morenTongZhongGUDing ;// 默认筒重固定
    bool morenXiandhiMaoZhong;//默认显示毛重
    bool morenDaYinRuKuMadan;//默认打印入库码单
    bool morenBudingZhongMaoZhongXianZhi;//默认不定重毛重限制
    bool daBaoZhuangLeiXingGuiZeKaiQi;//大包装类型规则开启
    bool daBaoZhuangMaoZhongXianZhiKaiQi;//大包装毛重限制开启
    bool pingBanZiDongTongBuShuJu;// 平板自动同步数据
    bool ziDongDaBaoKaiQi;//自动打包开启
    int chengZhongJIngDu;//称重精度
    int chengZhongJingDuMode; // 0 四舍五入，1 二舍三入，2 一舍二入
    QPair<int,int> buDingZhongTongShuFanWei;//不定重筒数范围
    QPair<int,int> dingZhongTongShuFanwei;//定重筒数范围
    int morenRuKuMaDanFenShu;//默认入库码单份数
    QPair<int,int> buDingZhongXianZhiFanWei;//不定重毛重限制范围
    int daBaoZhuangDaYinXiangDanFenShu;//默认大包装打印箱单份数
    int daBaoZhuangMaoZhongFanWei;//大包装毛重范围
    int ziDongDaBaoXiangZhongFanWei;//自动打包箱重范围
    int zidongDaBaoMaoZhongFanWei;//自动打包毛重范围
//出库配置
    bool morenDaYinHuaMaDan;//默认打印划码单
    bool morenXiaoShouXianShiJinE;//默认销售单显示金额
    bool morenDayinXiaoShouMinXidan;//默认打印销售明细单
    bool morenDaYinXIaoShouDan;//默认打印销售单
    bool morenXianShiQianKuan;//默认销售单显示欠款
    bool xiaoShouEJingDu;//销售额精度


//DIY配置
    QList<DIYPackConfig> diys;
    //POY配置
    QList<POYPackConfig> poys;
};

struct DengJiConfigItem
{
    int id;
    QString value;
    bool isEnable;
};

struct WarehouseConfig
{
    int warehouse_id;
    QString serial_number;
    QString name;
    int factory_id;
    QString factory_name;
    QString remark;
    int is_disable;
    int create_time;
    int modified_time;
};

class RemotePackConfig : public QObject
{
    Q_OBJECT
public:
    explicit RemotePackConfig(QObject *parent = nullptr);

    inline HttpRequest & request() {return  _request;}

    typedef std::function<void (const QJsonObject & obj)> ParseFun;
signals:
    void configUpdate(const DaBaoConfig & config);
    void dengJIConfigUp(const QList<DengJiConfigItem> & dengJis);
    void wareConfigUp(const QList<WarehouseConfig> & ware);
    void configUpdateError(int code,const QString & msg);

public slots:
    void refConfig();
    void refWarehouse();
private slots:
    void result(int code, const QJsonObject & body);
    void WareResult(int code,const QJsonObject & body);

    void refDengJiList();
private:
    QStringList getConfigStringList(const QJsonObject & obj);
    QList<double> getConfigStringDouble(const QJsonObject & data);
    QList<int> getConfigStringInt(const QJsonObject & data);
    bool getConfigBool(const QJsonObject & data);
    int getConfigInt(const QJsonObject & data);
    QPair<int,int> daBaoJingDu(const QJsonObject & data);
    QPair<int,int> getConfigpAIR(const QJsonObject & data);

    QList<DIYPackConfig> getDiys(const QJsonObject & data);
    QList<POYPackConfig> getPoys(const QJsonObject & data);
private:
    HttpRequest _request;
    HttpRequest _WareRequest;
    DaBaoConfig  _config;
    QList<DengJiConfigItem> dengji;
    QList<WarehouseConfig> Warehouse;
    QMap<QString,ParseFun> parsefUNS;
    bool getDdengji;
};

#endif // REMOTEPACKCONFIG_H
