{%
-- 接收两个参数：str 和 delimiter
local function string_split(str, delimiter)
    -- 如果传入的字符串为空，则返回空表
    if str == nil or str == '' then
        return {}
    end
    if delimiter == nil then
        delimiter = ','
    end
    -- 定义一个表 res 用于存储拆分结果
    local res = {}
    -- 使用 Lua 的 string.gmatch 函数进行字符串的拆分
    for match in (str .. delimiter):gmatch("(.-)" .. delimiter) do
        table.insert(res, match)
    end
    return res
end

if option == 'suggestion' then
    if type(Id) ~= 'string' then
%}
Usage: ipmcget -d sel -v <option>
option:
 list              list sel record.
 info              get sel information.
 suggestion ID     get sel suggestion based on event number.
{%
    echo('\b')
    elseif GetIdValid ~= true then
        echo('Please check ID.')
    elseif IdLowerThanCurEventCount ~= true then
        echo('Get event suggestion failed\nPlease check ID.')
    else
        local event = {}
        for _, v in pairs(Event) do
            event[v.MappingTable[1].Key] = v.MappingTable[1].Value
        end
        local local_time = os.date('%Y-%m-%d %H:%M:%S', event.Timestamp)
        local description = string.len(event.Description) == 0 and 'N/A' or event.Description
        local state = event.State
        local suggestion = (state == 'Deasserted' or string.len(event.Suggestion) == 0) and {'N/A'} or string_split(event.Suggestion, '@#AB;')
%}
ID                  :  {* event.RecordId *}
Generation Time     :  {* local_time *}
Severity            :  {* event.Severity *}
Event Code          :  {* event.EventCode *}
Status              :  {* state *}
Event Description   :  {* description *}
{%
        for i, desc in pairs(suggestion) do
            if i ~= 1 then
                echo('\n                       ' .. desc)
            else
                echo('Suggestion          :  ' .. desc)
            end
        end
    end
elseif option == 'list' then
    echo('ID       | Generation Time      | Severity     | Event Code   | Status       | Event Description\n')
    if #EventList == 0 then
        goto out
    end
    -- 计算结构体参数个数
    local num = 0
    local end_flag = EventList[1].MappingTable[1].Key
    for key, value in pairs(EventList) do
        if value.MappingTable[1].Key == end_flag and key ~= 1 then
            break
        end
        num = num + 1
    end
    -- 格式化结构体,num个结构体构成一条Sel
    local count = 0
    local res = {}
    local event = {}
    for _, v in pairs(EventList) do
        event[v.MappingTable[1].Key] = v.MappingTable[1].Value
        count = count + 1
        if count % num == 0 then
            local temp = event
            event = {}
            res[#res + 1] = temp
        end
    end
    for i, event in ipairs(res) do
        local local_time = os.date('%Y-%m-%d %H:%M:%S', event.Timestamp)
        local line = string.format('%-8s | %-20s | %-12s | %-12s | %-12s | %s\n',
            event.RecordId, local_time, event.Severity, event.EventCode, event.State, event.Description
        )
        echo(line)
        if event.RecordId == '1' then
            echo('\b') -- 消除最后一行的换行符
        end
    end
    ::out::
elseif option == 'info' then
%}
SEL Information
Version               :  {* Version *}
Current Event Number  :  {* CurEventCount *}
Max Event Number      :  {* MaxEventCount *}
{%
echo('\b')
end
%}