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

namespace delivery {

inline void commit_picklists(OdbcPool::Connection* c, int kBenutzer, int kSessionId, int kAuftrag) {
    std::string bestellungen = xml::element("Bestellung", xml::tag("kBestellung", int64_t(kAuftrag)));

    const char* sql =
        "DECLARE @xBestellungen XML = CONVERT(XML, ?);"
        "DECLARE @xResult XML;"
        "EXEC Auslieferung.spPicklistenUebernehmen"
        " @Bestellungen = @xBestellungen,"
        " @kBenutzer = ?,"
        " @nTeillieferung = 0,"
        " @kSessionId = ?,"
        " @xResult = @xResult OUTPUT";

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

} // namespace delivery
