#pragma once
#include <string>
#include "xml.hpp"
#include "../db/pool.hpp"

namespace delivery {

inline void deliver_picklists(OdbcPool::Connection* c, int kBenutzer, int kSessionId,
                               int kAuftrag, int kVersandArt) {
    std::string pakete = xml::element("Paket",
        xml::tag("kBestellung", int64_t(kAuftrag)) +
        xml::tag("kVersandart", int64_t(kVersandArt)) +
        xml::tag("fGewicht", 0.0));

    const int AUSLIEFERN_OPTIONS = 0x002;

    const char* sql =
        "DECLARE @xHinweise XML = NULL;"
        "DECLARE @xPakete XML = CONVERT(XML, ?);"
        "DECLARE @xResult XML;"
        "EXEC Auslieferung.spPicklistenAusliefern"
        " @xHinweise = @xHinweise,"
        " @Pakete = @xPakete,"
        " @nOptions = ?,"
        " @kBenutzer = ?,"
        " @kSessionId = ?,"
        " @xResult = @xResult OUTPUT;"
        "SELECT @xResult AS xResult";

    std::vector<Param> ps = {
        {ParamType::NVarChar, pakete, 0},
        {ParamType::Int, "", AUSLIEFERN_OPTIONS},
        {ParamType::Int, "", kBenutzer},
        {ParamType::Int, "", kSessionId}
    };
    ResultSet rs;
    if (!get_pool().execute(c, sql, ps, rs)) {
        throw std::runtime_error("deliver_picklists failed");
    }
}

} // namespace delivery
