local _, ZLAUTO = ...
local ZL = ZLAUTO.mainFrame
local pruneCrossRealm = ZL.pruneCrossRealm
local ZLCreateFrame = ZL.ZLCreateFrame
local CreateHeaderButton = ZL.CreateHeaderButton
local CreateButton = ZL.CreateButton
local CreateEditBox = ZL.CreateEditBox
local GameTooltipShow = ZL.GameTooltipShow
local cGetItemInfo = C_Item and C_Item.GetItemInfo or GetItemInfo
local cGetItemIcon = C_Item and C_Item.GetItemIconByID or GetItemIcon
local GetItemInfoInstant = C_Item and C_Item.GetItemInfoInstant or GetItemInfoInstant

local zhangDan
local itemStr = "|c.-|Hitem:.+|h%[.-%]|h|r"

local function defaultZhangDan(player, type)
    local Timer = ZL.ReconciliationFrame.Timer
    if Timer then
        if not Timer:IsCancelled() then Timer:Cancel() end
        ZL.ReconciliationFrame.Timer = nil
    end
    ZL.ReconciliationFrame.Timer = C_Timer.NewTimer(25, function ()
            if zhangDan then
                ZL.Print("获取对账单来源超时！", true, nil, 1, 0, 0)
                zhangDan = nil
            end
    end)
    return {
        player = player,
        type = type,
        time = date("%Y-%m-%d %H:%M:%S", time()),
        zhangDan = {},
    }
end
function ZL.Reconciliation()
    local msgStr
    if not ZL_PLAYERLIST.zhangDan or not ZL_PLAYERLIST.zhangDan.zhangDan then
        msgStr = "没有对账单来源！"
        ZL.Print(msgStr)
        return
    end
    if not ZL_PLAYERLIST.lootList then
        msgStr = "没有拾取记录！"
        ZL.Print(msgStr)
        return
    end
    --生成自己的账单
    local finalZhangdan = {}
    local myZhangdan = {}
    local mytotal = 0
    for bossName, bossNameData in pairs(ZL_PLAYERLIST.lootList) do
        if bossName ~= "拍卖合计" and type(bossNameData) == "table" then
            for itemLink, itemLinkData in pairs(bossNameData) do
                if not myZhangdan[itemLink] then
                    myZhangdan[itemLink] = {
                        mycounts = 0,
                        mytotalAmount = 0,
                        mytotalCounts = 0,
                    }
                end
                for playerName, playerNameData in pairs(itemLinkData) do
                    myZhangdan[itemLink].mytotalCounts = myZhangdan[itemLink].mytotalCounts + playerNameData["counts"]
                    if playerName ~= "拾取1" then
                        myZhangdan[itemLink].mycounts = myZhangdan[itemLink].mycounts + playerNameData["counts"]
                        myZhangdan[itemLink].mytotalAmount = myZhangdan[itemLink].mytotalAmount + (playerNameData.totalAmount or 0)
                        mytotal = mytotal + (playerNameData.totalAmount or 0)
                    end
                end
            end
        end
    end

    local otherZhangDan = CopyTable(ZL_PLAYERLIST.zhangDan.zhangDan)
     for itemLink, data in pairs(otherZhangDan) do
        if myZhangdan[itemLink] then
            if myZhangdan[itemLink]["mytotalAmount"] and myZhangdan[itemLink]["mytotalAmount"] ~= 0 then
                if (not data["totalAmount"]) or data["totalAmount"] == 0 then
                    if not finalZhangdan["|cffff0000对方未记账|r"] then
                        finalZhangdan["|cffff0000对方未记账|r"] = {}
                    end
                    finalZhangdan["|cffff0000对方未记账|r"][itemLink] = {
                        mycounts = myZhangdan[itemLink]["mycounts"],
                        mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                        mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                        othercounts = 0,
                        othertotalAmount = 0,
                        texture = false,
                    }
                elseif format("%.0f", data["totalAmount"]) == format("%.0f", myZhangdan[itemLink]["mytotalAmount"]) then
                    if not finalZhangdan["|cff00ff00记账相符|r"] then
                        finalZhangdan["|cff00ff00记账相符|r"] = {}
                    end
                    finalZhangdan["|cff00ff00记账相符|r"][itemLink] = {
                        mycounts = myZhangdan[itemLink]["mycounts"],
                        mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                        mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                        othercounts = data["counts"],
                        othertotalAmount = data["totalAmount"],
                        texture = true,
                    }
                else
                    if not finalZhangdan["|cffff6600记账不符|r"] then
                        finalZhangdan["|cffff6600记账不符|r"] = {}
                    end
                    finalZhangdan["|cffff6600记账不符|r"][itemLink] = {
                        mycounts = myZhangdan[itemLink]["mycounts"],
                        mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                        mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                        othercounts = data["counts"],
                        othertotalAmount = data["totalAmount"],
                        texture = false,
                    }
                end
            else
                if (not data["totalAmount"]) or data["totalAmount"] == 0 then
                    if not finalZhangdan["|cffffff00都未记账|r"] then
                        finalZhangdan["|cffffff00都未记账|r"] = {}
                    end
                    finalZhangdan["|cffffff00都未记账|r"][itemLink] = {
                        mycounts = myZhangdan[itemLink]["mycounts"],
                        mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                        mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                        othercounts = 0,
                        othertotalAmount = 0,
                        texture = nil,
                    }
                else
                    if not finalZhangdan["|cffff8899我未记账|r"] then
                        finalZhangdan["|cffff8899我未记账|r"] = {}
                    end
                    finalZhangdan["|cffff8899我未记账|r"][itemLink] = {
                        mycounts = myZhangdan[itemLink]["mycounts"],
                        mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                        mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                        othercounts = data["counts"],
                        othertotalAmount = data["totalAmount"],
                        texture = "?",
                    }
                end
            end
            myZhangdan[itemLink] = nil
        else
            if not finalZhangdan["|cffff0000我漏记的装备|r"] then
                finalZhangdan["|cffff0000我漏记的装备|r"] = {}
            end
            finalZhangdan["|cffff0000我漏记的装备|r"][itemLink] = {
                mycounts = 0,
                mytotalAmount = 0,
                mytotalCounts = data.counts,
                othercounts = data.counts,
                othertotalAmount = data.totalAmount,
                texture = "?",
            }
        end
    end
    for itemLink, data in pairs(myZhangdan) do
        if data["mytotalAmount"] and data["mytotalAmount"] ~= 0 then
            if not finalZhangdan["|cffff0000对方未记账|r"] then
                finalZhangdan["|cffff0000对方未记账|r"] = {}
            end
            finalZhangdan["|cffff0000对方未记账|r"][itemLink] = {
                mycounts = myZhangdan[itemLink]["mycounts"],
                mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                othercounts = 0,
                othertotalAmount = 0,
                texture = false,
            }
        else
            if not finalZhangdan["|cffffff00都未记账|r"] then
                finalZhangdan["|cffffff00都未记账|r"] = {}
            end
            finalZhangdan["|cffffff00都未记账|r"][itemLink] = {
                mycounts = myZhangdan[itemLink]["mycounts"],
                mytotalAmount = myZhangdan[itemLink]["mytotalAmount"],
                mytotalCounts = myZhangdan[itemLink]["mytotalCounts"],
                othercounts = 0,
                othertotalAmount = 0,
                texture = nil,
            }
        end
    end
    local groupMembers = GetNumGroupMembers(1) == 0 and 1 or GetNumGroupMembers(1)
    finalZhangdan["|cff00ff00总结|r"] = {
        ["|cff00ff00拍卖收入|r"] = {
            mycounts = 1,
            mytotalAmount = mytotal,
            mytotalCounts = 1,
            othercounts = 1,
            othertotalAmount = ZL_PLAYERLIST.zhangDan.totalAmount,
            texture = format("%.0f", mytotal) == format("%.0f", ZL_PLAYERLIST.zhangDan.totalAmount) and true or false,
        },
        ["|cff00ff00差额|r(我的-对方)"] = {
            mycounts = 1,
            mytotalAmount = format("%.0f", mytotal) - format("%.0f", ZL_PLAYERLIST.zhangDan.totalAmount),
            mytotalCounts = 1,
            othercounts = 1,
            othertotalAmount = 0
        },
        ["人均预估"] = {
            mycounts = 1,
            mytotalAmount = format("%.0f÷%d=%d", mytotal, groupMembers, floor(mytotal/groupMembers)),
            mytotalCounts = 1,
            othercounts = 1,
            othertotalAmount = 0
        },

    }

    -- ZL_PLAYERLIST.finalZhangdan = finalZhangdan
    -- ZL_PLAYERLIST.myZhangdan = myZhangdan
    myZhangdan = nil
    otherZhangDan = nil
    return finalZhangdan
end
function ZL:CreateReconciliationFrame()
    ZL.ReconciliationFrame = ZLCreateFrame("ZLReconciliationFrame", ZL, nil, nil, "DIALOG", false, true, "对账单")
    local ReconciliationFrame = ZL.ReconciliationFrame

    ReconciliationFrame.Background:SetColorTexture(0, 0, 0, 0.95)
    ReconciliationFrame:SetPoint("CENTER")
    ReconciliationFrame:SetResizable(false)
    local maxlevel = ZL.AuctionFrame2:GetFrameLevel()
    ReconciliationFrame:SetFrameLevel(maxlevel + 10)
    ReconciliationFrame.resizeButton:Hide()
    ReconciliationFrame:SetSize(GetScreenWidth()-65, GetScreenHeight()-50)
    ReconciliationFrame:SetScript("OnShow", function (self)
        ZL.ReconciliationFrame:Update()
    end)
    ReconciliationFrame:Hide()
    -- 注册 ESC 键关闭窗口
    tinsert(UISpecialFrames, ReconciliationFrame:GetName())
    local UIScale = UIParent:GetEffectiveScale()
    local height = 20
    local gapwidth = 20
    local gapheight = 0

    local itemNamewidth = 150/UIScale
    local myamountwidth = 100/UIScale
    local otheramountwidth = 100/UIScale
    local sortwidth = (GetScreenWidth()-40-23-2*gapwidth-3*(itemNamewidth+myamountwidth+otheramountwidth-3))/3
    sortwidth = sortwidth > 170 and 170 or sortwidth
    local board_r, board_g, board_b, board_a = 0.8, 0.8, 0.8, 0.3

    ReconciliationFrame.sortHeaderBtn1 = CreateHeaderButton(ReconciliationFrame, sortwidth, 21)
    local sortHeaderBtn1 = ReconciliationFrame.sortHeaderBtn1
    sortHeaderBtn1:SetPoint("TOPLEFT", ReconciliationFrame.TitleBottom, "BOTTOMLEFT", 3, -5)
    sortHeaderBtn1:SetText("|cff00d100种类|r")

    ReconciliationFrame.itemNameHeaderBtn1 = CreateHeaderButton(ReconciliationFrame, itemNamewidth, 21)
    local itemNameHeaderBtn1 = ReconciliationFrame.itemNameHeaderBtn1
    itemNameHeaderBtn1:SetPoint("TOPLEFT", sortHeaderBtn1, "TOPRIGHT", -1, 0)
    itemNameHeaderBtn1:SetText("|cff00d100物品×总数量|r")

    ReconciliationFrame.myAmountHeaderBtn1 = CreateHeaderButton(ReconciliationFrame, myamountwidth, 21)
    local myAmountHeaderBtn1 = ReconciliationFrame.myAmountHeaderBtn1
    myAmountHeaderBtn1:SetPoint("TOPLEFT", itemNameHeaderBtn1, "TOPRIGHT", -1, 0)
    myAmountHeaderBtn1:SetText("|cff00d100我的|r")

    ReconciliationFrame.otherAmountHeaderBtn1 = CreateHeaderButton(ReconciliationFrame, otheramountwidth, 21)
    local otherAmountHeaderBtn1 = ReconciliationFrame.otherAmountHeaderBtn1
    otherAmountHeaderBtn1:SetPoint("TOPLEFT", myAmountHeaderBtn1, "TOPRIGHT", -1, 0)
    otherAmountHeaderBtn1:SetText("|cffFEB6FB对方|r")
    do
        local texture = otherAmountHeaderBtn1:CreateTexture(nil, "BACKGROUND")
        texture:SetColorTexture(1,1,1,0.5)
        texture:SetPoint("TOPLEFT", sortHeaderBtn1, "BOTTOMLEFT", 0, 0)
        texture:SetPoint("BOTTOMRIGHT", otherAmountHeaderBtn1, "BOTTOMRIGHT", 0, -1)
    end

    ReconciliationFrame.sortHeaderBtn2 = CreateHeaderButton(ReconciliationFrame, sortwidth, 21)
    local sortHeaderBtn2 = ReconciliationFrame.sortHeaderBtn2
    sortHeaderBtn2:SetPoint("TOPLEFT", otherAmountHeaderBtn1, "TOPRIGHT", gapwidth, 0)
    sortHeaderBtn2:SetText("|cff00d100种类|r")

    ReconciliationFrame.itemNameHeaderBtn2 = CreateHeaderButton(ReconciliationFrame, itemNamewidth, 21)
    local itemNameHeaderBtn2 = ReconciliationFrame.itemNameHeaderBtn2
    itemNameHeaderBtn2:SetPoint("TOPLEFT", sortHeaderBtn2, "TOPRIGHT", -1, 0)
    itemNameHeaderBtn2:SetText("|cff00d100物品×总数量|r")

    ReconciliationFrame.myAmountHeaderBtn2 = CreateHeaderButton(ReconciliationFrame, myamountwidth, 21)
    local myAmountHeaderBtn2 = ReconciliationFrame.myAmountHeaderBtn2
    myAmountHeaderBtn2:SetPoint("TOPLEFT", itemNameHeaderBtn2, "TOPRIGHT", -1, 0)
    myAmountHeaderBtn2:SetText("|cff00d100我的|r")

    ReconciliationFrame.otherAmountHeaderBtn2 = CreateHeaderButton(ReconciliationFrame, otheramountwidth, 21)
    local otherAmountHeaderBtn2 = ReconciliationFrame.otherAmountHeaderBtn2
    otherAmountHeaderBtn2:SetPoint("TOPLEFT", myAmountHeaderBtn2, "TOPRIGHT", -1, 0)
    otherAmountHeaderBtn2:SetText("|cffFEB6FB对方|r")
    do
        local texture = otherAmountHeaderBtn2:CreateTexture(nil, "BACKGROUND")
        texture:SetColorTexture(1,1,1,0.5)
        texture:SetPoint("TOPLEFT", sortHeaderBtn2, "BOTTOMLEFT", 0, 0)
        texture:SetPoint("BOTTOMRIGHT", otherAmountHeaderBtn2, "BOTTOMRIGHT", 0, -1)
    end

    ReconciliationFrame.sortHeaderBtn3 = CreateHeaderButton(ReconciliationFrame, sortwidth, 21)
    local sortHeaderBtn3 = ReconciliationFrame.sortHeaderBtn3
    sortHeaderBtn3:SetPoint("TOPLEFT", otherAmountHeaderBtn2, "TOPRIGHT", gapwidth, 0)
    sortHeaderBtn3:SetText("|cff00d100种类|r")

    ReconciliationFrame.itemNameHeaderBtn3 = CreateHeaderButton(ReconciliationFrame, itemNamewidth, 21)
    local itemNameHeaderBtn3 = ReconciliationFrame.itemNameHeaderBtn3
    itemNameHeaderBtn3:SetPoint("TOPLEFT", sortHeaderBtn3, "TOPRIGHT", -1, 0)
    itemNameHeaderBtn3:SetText("|cff00d100物品×总数量|r")

    ReconciliationFrame.myAmountHeaderBtn3 = CreateHeaderButton(ReconciliationFrame, myamountwidth, 21)
    local myAmountHeaderBtn3 = ReconciliationFrame.myAmountHeaderBtn3
    myAmountHeaderBtn3:SetPoint("TOPLEFT", itemNameHeaderBtn3, "TOPRIGHT", -1, 0)
    myAmountHeaderBtn3:SetText("|cff00d100我的|r")

    ReconciliationFrame.otherAmountHeaderBtn3 = CreateHeaderButton(ReconciliationFrame, otheramountwidth, 21)
    local otherAmountHeaderBtn3 = ReconciliationFrame.otherAmountHeaderBtn3
    otherAmountHeaderBtn3:SetPoint("TOPLEFT", myAmountHeaderBtn3, "TOPRIGHT", -1, 0)
    otherAmountHeaderBtn3:SetText("|cffFEB6FB对方|r")
    do
        local texture = otherAmountHeaderBtn3:CreateTexture(nil, "BACKGROUND")
        texture:SetColorTexture(1,1,1,0.5)
        texture:SetPoint("TOPLEFT", sortHeaderBtn3, "BOTTOMLEFT", 0, 0)
        texture:SetPoint("BOTTOMRIGHT", otherAmountHeaderBtn3, "BOTTOMRIGHT", 0, -1)
    end

    --创建滚动条
    ReconciliationFrame.scrollFrame = CreateFrame("ScrollFrame", nil, ReconciliationFrame, "UIPanelScrollFrameTemplate")
    ReconciliationFrame.scrollFrame:SetFrameLevel(ReconciliationFrame:GetFrameLevel() + 1) -- 设置为比父框架高1级
    ReconciliationFrame.scrollFrame:SetPoint("TOPLEFT", sortHeaderBtn1, "BOTTOMLEFT", 0, 0)
    ReconciliationFrame.scrollFrame:SetPoint("BOTTOMRIGHT", ReconciliationFrame, "BOTTOMRIGHT", 0, 3)
    ReconciliationFrame.scrollFrame:SetClipsChildren(true) --必须设置为true，scrollFrame所有的子元素才会被剪切
    ReconciliationFrame.scrollFrame:SetScript("OnMouseWheel", ZL.ScrollFrame_OnMouseWheel) --重设滑动条鼠标滚轮事件
    --重设ScrollBar位置，放置到scrollFrame内，防止被剪切掉
    ReconciliationFrame.scrollFrame.ScrollBar:ClearAllPoints()
    ReconciliationFrame.scrollFrame.ScrollBar:SetPoint("TOPLEFT", ReconciliationFrame.scrollFrame, "TOPRIGHT", -20, -16)
    ReconciliationFrame.scrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", ReconciliationFrame.scrollFrame, "BOTTOMRIGHT", 0, 16)
    ReconciliationFrame.scrollFrame.content = CreateFrame("Frame", "ZLCONTENT", ReconciliationFrame.scrollFrame)
    local content = ReconciliationFrame.scrollFrame.content
    content:SetPoint("TOPRIGHT", ReconciliationFrame.scrollFrame, "TOPRIGHT", 0, 0)
    content:SetSize(ReconciliationFrame.scrollFrame:GetWidth()-20, 100)
    ReconciliationFrame.scrollFrame:SetScrollChild(content)
    local function OnEnter(self)
        self:GetParent():SetNormalTexture("Interface\\FriendsFrame\\UI-FriendsFrame-HighlightBar")
    end
    local function OnLeave(self)
        self:GetParent():ClearNormalTexture()
    end

    ReconciliationFrame.entries = setmetatable({}, {
        __index = function (t, v)
            local f = CreateFrame("Button", nil, content)
            f:SetSize((sortwidth+myamountwidth+otheramountwidth+itemNamewidth-3), height)

            f.Background = f:CreateTexture(nil,"BACKGROUND")
            f.Background:SetPoint("TOPLEFT")
            f.Background:SetPoint("BOTTOMRIGHT")
            f.Background:SetColorTexture(0.15, 0.15, 0.15, 1)
            f:SetScript("OnEnter", function (self)
                self:SetNormalTexture("Interface\\FriendsFrame\\UI-FriendsFrame-HighlightBar")
            end)
            f:SetScript("OnLeave", function (self)
                self:ClearNormalTexture()
            end)

            f.bossfontstring = f:CreateFontString(nil, "OVERLAY")
            f.bossfontstring:SetPoint("TOPLEFT", 3, -1)
            -- f.bossfontstring:SetPoint("TOPRIGHT", f, "TOPLEFT", -3, -1)
            f.bossfontstring:SetWidth(sortwidth-6)
            f.bossfontstring:SetFont(GameFontNormal:GetFont(), 15, "THICKOUTLINE")
            f.bossfontstring:SetWordWrap(true)
            f.bossfontstring:SetJustifyH("CENTER")
            f.bossfontstring:SetJustifyV("MIDDLE")
            f.bossfontstring:SetTextColor(0.95, 0.95, 0.95, 1)

            f.itemicon = CreateButton(nil, f, "", "TOPLEFT", f.bossfontstring, "TOPRIGHT", 3, 1, 20, 20)
            f.itemicon:SetScript("OnEnter", function(self)
                OnEnter(self)
            end)
            f.itemicon:SetScript("OnLeave", function(self)
                OnLeave(self)
            end)
            f.itemicon:SetScript("OnDoubleClick", function (self, button)
                local itemlink = f.itemLink
                if (not itemlink) or itemlink == "" then return end
                local itemId = tonumber(itemlink:match("|Hitem:(%d+):"))
                if itemId then
                    local selectCharKey = ZL.ConcernFrame.profileDropdown:GetText()
                    selectCharKey = selectCharKey:match("^|T%d+:0:0:2:0|t (.+)")
                    if (not ZL.opt.concernList) or (not ZL.opt.concernList[selectCharKey]) or
                    (not ZL.opt.concernList[selectCharKey]["itemList"][itemId]) then
                        ZL.addItem(itemId)
                    else
                        ZL.deleteItem(itemId)
                    end
                    ZL.AuctionFrame2:Update()
                end
            end)
            -- f.itemicon.itemNumbersIcon = f.itemicon:CreateTexture(nil, "OVERLAY")
            -- local itemNumbersIcon = f.itemicon.itemNumbersIcon
            -- itemNumbersIcon:SetPoint("TOP", f.itemicon, "TOP")
            -- itemNumbersIcon:SetSize(15,15)
            f.level = f.itemicon:CreateFontString(nil, "OVERLAY")
            f.level:SetPoint("BOTTOMLEFT", 1, 0)
            f.level:SetPoint("BOTTOMRIGHT", -1, 0)
            f.level:SetFont(GameFontNormal:GetFont(), 8, "OUTLINE")
            f.level:SetWordWrap(true)
            f.level:SetJustifyH("LEFT")
            f.level:SetTextColor(0.95, 0.95, 0.95, 1)
            f.itemNamebox = CreateEditBox(f, itemNamewidth-f.itemicon:GetWidth()-1, 20, 12, "RIGHT", nil, nil, board_r, board_g, board_b, board_a)
            f.itemNamebox:SetPoint("TOPLEFT", f.itemicon, "TOPRIGHT", 0, 0)
            f.itemNamebox.Background:SetColorTexture(0,0,0,0)
            f.itemNamebox.BorderRight:Hide()
            -- f.itemNamebox.BorderTop:Hide()
            f.itemNamebox:Disable()
            f.itemNamebox:SetScript("OnEnter", function (self)
                OnEnter(self)
                local itemlink = f.itemLink
                if (not itemlink) or itemlink == "" then return end
                local itemId = tonumber(itemlink:match("|Hitem:(%d+):"))
                if itemId then
                    local uiScale, x, y = UIParent:GetEffectiveScale(), GetCursorPosition()
                    x, y = x/uiScale, y/uiScale
                    local screenWidth = GetScreenWidth() / 2
                    if x < screenWidth then
                        GameTooltipShow(self, "TOPLEFT", "TOPRIGHT")
                    else
                        GameTooltipShow(self, "TOPRIGHT", "TOPLEFT")
                    end
                    GameTooltip:ClearLines()
                    GameTooltip:SetHyperlink(itemlink)
                    GameTooltip:Show()
                    if _G["ShoppingTooltip1"] and _G["ShoppingTooltip1"]:IsShown() then
                        _G["ShoppingTooltip1"]:ClearAllPoints()
                        if x < screenWidth then
                            _G["ShoppingTooltip1"]:SetPoint("TOPLEFT", GameTooltip, "TOPRIGHT", 0, 0)
                        else
                            _G["ShoppingTooltip1"]:SetPoint("TOPRIGHT", GameTooltip, "TOPLEFT", 0, 0)
                        end
                    end
                    if _G["ShoppingTooltip2"] and _G["ShoppingTooltip2"]:IsShown() then
                        _G["ShoppingTooltip2"]:ClearAllPoints()
                        if x < screenWidth then
                            _G["ShoppingTooltip2"]:SetPoint("TOPLEFT", _G["ShoppingTooltip1"], "TOPRIGHT", 0, 0)
                        else
                            _G["ShoppingTooltip2"]:SetPoint("TOPRIGHT", _G["ShoppingTooltip1"], "TOPLEFT", 0, 0)
                        end
                    end
                end
            end)
            f.itemNamebox:SetScript("OnLeave", function (self)
                OnLeave(self)
                GameTooltip:Hide()
            end)
            f.itemNamebox.fontstring = f.itemNamebox:CreateFontString(nil, "OVERLAY")
            f.itemNamebox.fontstring:SetPoint("TOPLEFT", 3, -1)
            f.itemNamebox.fontstring:SetPoint("BOTTOMRIGHT", -3, 1)
            f.itemNamebox.fontstring:SetFont(GameFontNormal:GetFont(), 13, "OUTLINE")
            f.itemNamebox.fontstring:SetWordWrap(true)
            f.itemNamebox.fontstring:SetJustifyH("LEFT")
            f.itemNamebox.fontstring:SetJustifyV("MIDDLE")
            f.itemNamebox.fontstring:SetTextColor(0.95, 0.95, 0.95, 1)

            f.myAmountbox = CreateEditBox(f, myamountwidth-1, 20, 12, "LEFT", nil, nil, board_r, board_g, board_b, board_a)
            f.myAmountbox:SetPoint("TOPLEFT", f.itemNamebox, "TOPRIGHT", 0, 0)
            f.myAmountbox.Background:SetColorTexture(0,0,0,0)
            f.myAmountbox.BorderRight:Hide()
            -- f.myAmountbox.BorderTop:Hide()
            f.myAmountbox:Disable()
            f.myAmountbox:SetScript("OnEnter", function (self)
                OnEnter(self)
            end)
            f.myAmountbox:SetScript("OnLeave", function (self)
                OnLeave(self)
                GameTooltip:Hide()
            end)
            f.myAmountbox.fontstring = f.myAmountbox:CreateFontString(nil, "OVERLAY")
            f.myAmountbox.fontstring:SetPoint("TOPLEFT", 3, -1)
            f.myAmountbox.fontstring:SetPoint("BOTTOMRIGHT", -3, 1)
            f.myAmountbox.fontstring:SetFont(GameFontNormal:GetFont(), 13, "OUTLINE")
            f.myAmountbox.fontstring:SetWordWrap(true)
            f.myAmountbox.fontstring:SetJustifyH("RIGHT")
            f.myAmountbox.fontstring:SetJustifyV("MIDDLE")
            f.myAmountbox.fontstring:SetTextColor(0.95, 0.95, 0.95, 1)


            f.otherAmountbox = CreateEditBox(f, myamountwidth-1, 20, 12, "LEFT", nil, nil, board_r, board_g, board_b, board_a)
            f.otherAmountbox:SetPoint("TOPLEFT", f.myAmountbox, "TOPRIGHT", 0, 0)
            f.otherAmountbox.Background:SetColorTexture(0,0,0,0)
            -- f.otherAmountbox.BorderRight:Hide()
            -- f.otherAmountbox.BorderTop:Hide()
            f.otherAmountbox:Disable()
            f.otherAmountbox:SetScript("OnEnter", function (self)
                OnEnter(self)
            end)
            f.otherAmountbox:SetScript("OnLeave", function (self)
                OnLeave(self)
                GameTooltip:Hide()
            end)
            f.otherAmountbox.fontstring = f.otherAmountbox:CreateFontString(nil, "OVERLAY")
            f.otherAmountbox.fontstring:SetPoint("TOPLEFT", 3, -1)
            f.otherAmountbox.fontstring:SetPoint("BOTTOMRIGHT", -3, 1)
            f.otherAmountbox.fontstring:SetFont(GameFontNormal:GetFont(), 13, "OUTLINE")
            f.otherAmountbox.fontstring:SetWordWrap(true)
            f.otherAmountbox.fontstring:SetJustifyH("RIGHT")
            f.otherAmountbox.fontstring:SetJustifyV("MIDDLE")
            f.otherAmountbox.fontstring:SetTextColor(0.95, 0.95, 0.95, 1)

            f.checkIcon = f:CreateTexture(nil, "OVERLAY")
            f.checkIcon:SetSize(18, 18)
            f.checkIcon:SetPoint("LEFT", f.otherAmountbox, "RIGHT")
            -- f.checkIcon:SetTexture([[interface/raidframe/readycheck-ready]])
            t[v] = f
            return f
        end
    })

    function ReconciliationFrame:Update()
        for _, f in ipairs(ReconciliationFrame.entries) do f:Hide() end
        local finalZhangdan = ZL.Reconciliation()
        if finalZhangdan and next(finalZhangdan) then
            local bossNameList = {"|cffff0000对方未记账|r", "|cffff6600记账不符|r", "|cffff8899我未记账|r", "|cffff0000我漏记的装备|r", "|cff00ff00记账相符|r", "|cffffff00都未记账|r", "|cff00ff00总结|r"}
            local maxrows = math.floor(ReconciliationFrame.scrollFrame:GetHeight() / (height+gapheight)) --全屏支持的最大行数
            local bossMaxRows = 0
            -- local bossNameRowsList = {}
            for bossName, value1 in pairs(finalZhangdan) do
                local rows = 0
                for _ in pairs(value1) do
                    rows = rows + 1
                    bossMaxRows = bossMaxRows + 1
                end
                -- bossNameRowsList[bossName] = rows
                bossMaxRows = bossMaxRows + 1 --每个分类后面空一行
            end
            bossMaxRows = bossMaxRows - 1 --剔除掉最后一个分类增加的空行
            local aveMaxRows = math.floor(bossMaxRows/3) --3列平均每列多少行
            if bossMaxRows < maxrows then
                maxrows = bossMaxRows
            else
                maxrows = aveMaxRows > maxrows and aveMaxRows or maxrows
            end
            
            local index = 1  --总行数，用来计算ReconciliationFrame.entries[index]
            local bili = 1
            local bossindex = 0 -- 分类数
            local oldbossindex = 1
            local f, f2
            local column = 0 --列
            local row = 1 --每一列中的行数
            local rowMax = 1 --取每列中最大的行
            for _, bossName in ipairs(bossNameList) do
                local value1 = finalZhangdan[bossName]
                if value1 and type(value1) == "table" then
                    bossindex = bossindex + 1
                    local index2 = 1 --每一次分类中的行数
                    oldbossindex = index --重新初始化oldbossindex，用于设置f.bossfontstring的高度
                    if next(value1) then
                        local itemList = {}
                        if bossName == "|cff00ff00总结|r" then
                            itemList = {"|cff00ff00拍卖收入|r", "|cff00ff00差额|r(我的-对方)", "人均预估"}
                        else
                            for itemlink in pairs(value1) do
                                tinsert(itemList, itemlink)
                            end
                            table.sort(itemList)
                        end
                        for _, itemLink in ipairs(itemList) do
                            local value2 = value1[itemLink]
                            f = ReconciliationFrame.entries[index]
                            f:ClearAllPoints()
                            if index == 1 or row == maxrows + 1 then
                                --刚开始第一行或者每列最大行的时候增加一列
                                f:SetPoint("TOPLEFT", content, "TOPLEFT", (sortwidth+itemNamewidth+myamountwidth+otheramountwidth+gapwidth-3)*column, 0)
                                -- f:SetPoint("TOPLEFT", content, "TOPLEFT", (sortwidth+itemNamewidth+myamountwidth+otheramountwidth+gapwidth-3)*column + sortwidth, 0)
                                column = column + 1
                                if row > rowMax then
                                    rowMax = row
                                end
                                row = 1
                                index2 = 1
                                oldbossindex = index
                            else
                                f:SetPoint("TOPLEFT", ReconciliationFrame.entries[index-1], "BOTTOMLEFT", 0, -gapheight)
                                if row > rowMax then
                                    rowMax = row
                                end
                            end
                            if index2 == 1 then
                                f.bossfontstring:SetText(bossName)
                                f.bossfontstring:Show()
                                f.itemNamebox.BorderTop:Show()
                                f.myAmountbox.BorderTop:Show()
                                f.otherAmountbox.BorderTop:Show()
                            else
                                f.bossfontstring:SetText("")
                                f.bossfontstring:Hide()
                                f.itemNamebox.BorderTop:Hide()
                                f.myAmountbox.BorderTop:Hide()
                                f.otherAmountbox.BorderTop:Hide()
                            end
                            if row == maxrows then
                                f2 = ReconciliationFrame.entries[oldbossindex]
                                f2.bossfontstring:SetHeight(index2*height + (index2-1)*gapheight)
                            end
                            f.myAmountbox.fontstring:Show()
                            f.otherAmountbox.fontstring:Show()
                            f:Show()
                            local itemId, _, _, _, icon = GetItemInfoInstant(itemLink)
                            local itemLevel = select(4, cGetItemInfo(itemLink))
                            f.level:SetTextScale(1)
                            f.itemNamebox.fontstring:SetTextScale(1)
                            f.myAmountbox.fontstring:SetTextScale(1)
                            f.otherAmountbox.fontstring:SetTextScale(1)
                            if icon then
                                f.itemLink = itemLink
                                f.itemicon:SetNormalTexture(icon)
                                f.level:SetText(itemLevel)
                            else
                                f.itemLink = nil
                                f.itemicon:ClearNormalTexture()
                                f.level:SetText("")
                            end
                            if value2["mytotalCounts"] == 1 then
                                f.itemNamebox.fontstring:SetText(itemLink)
                            else
                                f.itemNamebox.fontstring:SetText(itemLink .. "|cff00ff00×" .. value2["mytotalCounts"] .. "|r")
                            end
                            if value2["mycounts"] >= 1 and value2["mytotalAmount"] then
                                if itemLink == "|cff00ff00差额|r(我的-对方)" then
                                    if value2["mytotalAmount"] > 0 then
                                        f.myAmountbox.fontstring:SetText("|cffff0000" .. format("%.0f", value2["mytotalAmount"]) .. "|r")
                                    elseif value2["mytotalAmount"] == 0 then
                                        f.myAmountbox.fontstring:SetText("|cff00ff00" .. format("%.0f", value2["mytotalAmount"]) .. "|r")
                                    else
                                        f.myAmountbox.fontstring:SetText("|cff0000ff" .. format("%.0f", value2["mytotalAmount"]) .. "|r")
                                    end
                                elseif itemLink == "|cff00ff00拍卖收入|r" then
                                    f.myAmountbox.fontstring:SetText("|cff00d100" .. format("%.0f", value2["mytotalAmount"]) .. "|r")
                                elseif itemLink == "人均预估" then
                                    f.myAmountbox.fontstring:SetText("|cff00d100" .. value2["mytotalAmount"] .. "|r")
                                else
                                    f.myAmountbox.fontstring:SetText("|cff00d100" .. format("%.0f", value2["mytotalAmount"]) .. "|r(" .. value2["mycounts"] .. ")")
                                end
                            else
                                f.myAmountbox.fontstring:SetText("")
                            end
                            if value2["othercounts"] and value2["othercounts"] >= 1 and value2["othertotalAmount"] and value2["othertotalAmount"] ~= 0 then
                                if itemLink == "|cff00ff00拍卖收入|r" then
                                    f.otherAmountbox.fontstring:SetText("|cffFEB6FB" .. value2["othertotalAmount"] .. "|r")
                                else
                                    f.otherAmountbox.fontstring:SetText("|cffFEB6FB" .. value2["othertotalAmount"] .. "|r(" .. value2["othercounts"] .. ")")
                                end
                            else
                                if itemLink == "|cff00ff00差额|r(我的-对方)" then
                                    if value2["mytotalAmount"] > 0 then
                                        f.otherAmountbox.fontstring:SetText("|cffff0000对方少记|r")
                                    elseif value2["mytotalAmount"] == 0 then
                                        f.otherAmountbox.fontstring:SetText("")
                                    else
                                        f.otherAmountbox.fontstring:SetText("|cff0000ff我少记|r")
                                    end
                                else
                                    f.otherAmountbox.fontstring:SetText("")
                                end
                            end
                            if value2["texture"] == nil then
                                f.checkIcon:SetTexture()
                            elseif value2["texture"] == false then
                                f.checkIcon:SetTexture([[interface/raidframe/ReadyCheck-NotReady]])
                            elseif value2["texture"] == true then
                                f.checkIcon:SetTexture([[interface/raidframe/readycheck-ready]])
                            elseif value2["texture"] == "?" then
                                f.checkIcon:SetTexture([[interface/raidframe/ReadyCheck-Waiting]])
                            end
                            bili = f.level:GetStringWidth()>0 and (f.level:GetWidth())/(f.level:GetUnboundedStringWidth()+5) or 1
                            if bili < 1 then
                                f.level:SetTextScale(bili)
                            else
                                f.level:SetTextScale(1)
                            end
                            bili = f.itemNamebox.fontstring:GetStringWidth()>0 and f.itemNamebox.fontstring:GetWidth()/(f.itemNamebox.fontstring:GetUnboundedStringWidth()+5) or 1
                            if bili < 1 then
                                f.itemNamebox.fontstring:SetTextScale(bili)
                            else
                                f.itemNamebox.fontstring:SetTextScale(1)
                            end
                            bili = f.myAmountbox.fontstring:GetStringWidth()>0 and f.myAmountbox.fontstring:GetWidth()/(f.myAmountbox.fontstring:GetUnboundedStringWidth()+5) or 1
                            if bili < 1 then
                                f.myAmountbox.fontstring:SetTextScale(bili)
                            else
                                f.myAmountbox.fontstring:SetTextScale(1)
                            end
                            bili = f.otherAmountbox.fontstring:GetStringWidth()>0 and f.otherAmountbox.fontstring:GetWidth()/(f.otherAmountbox.fontstring:GetUnboundedStringWidth()+5) or 1
                            if bili < 1 then
                                f.otherAmountbox.fontstring:SetTextScale(bili)
                            else
                                f.otherAmountbox.fontstring:SetTextScale(1)
                            end
                            index2 = index2 + 1
                            index = index + 1
                            row = row + 1
                        end
                        itemList = nil
                    end
                    f= ReconciliationFrame.entries[oldbossindex]
                    f.bossfontstring:SetHeight((index2-1)*height + (index2-2)*gapheight)
                    if row <= maxrows then
                        f = ReconciliationFrame.entries[index]
                        f:SetPoint("TOPLEFT", ReconciliationFrame.entries[index-1], "BOTTOMLEFT", 0, -gapheight)
                        f:Hide()
                        index = index + 1
                        row = row + 1
                        if row > rowMax then
                            rowMax = row
                        end
                    end
                end
            end
            rowMax = rowMax - 1
            rowMax = rowMax > 20 and rowMax or 20 --最低显示20行高度
            local contetHeight = (height+gapheight)*maxrows-gapheight
            content:SetHeight(contetHeight)
            if column == 1 then
                sortHeaderBtn2:Hide()
                itemNameHeaderBtn2:Hide()
                myAmountHeaderBtn2:Hide()
                otherAmountHeaderBtn2:Hide()

                sortHeaderBtn3:Hide()
                itemNameHeaderBtn3:Hide()
                myAmountHeaderBtn3:Hide()
                otherAmountHeaderBtn3:Hide()
            elseif column == 2 then
                sortHeaderBtn2:Show()
                itemNameHeaderBtn2:Show()
                myAmountHeaderBtn2:Show()
                otherAmountHeaderBtn2:Show()

                sortHeaderBtn3:Hide()
                itemNameHeaderBtn3:Hide()
                myAmountHeaderBtn3:Hide()
                otherAmountHeaderBtn3:Hide()
            elseif column == 3 then
                sortHeaderBtn2:Show()
                itemNameHeaderBtn2:Show()
                myAmountHeaderBtn2:Show()
                otherAmountHeaderBtn2:Show()

                sortHeaderBtn3:Show()
                itemNameHeaderBtn3:Show()
                myAmountHeaderBtn3:Show()
                otherAmountHeaderBtn3:Show()
            end
            local ReconciliationFrameHeight = contetHeight + sortHeaderBtn1:GetHeight() + 42
            ReconciliationFrameHeight = ReconciliationFrameHeight > (GetScreenHeight()-50) and (GetScreenHeight()-50) or ReconciliationFrameHeight
            ReconciliationFrame:SetSize(23+column*gapwidth+column*(sortwidth+itemNamewidth+myamountwidth+otheramountwidth-3), ReconciliationFrameHeight)
            ReconciliationFrame.Title:SetText(format("对账单：|cffffffff%s|r—%s—|cffffffff%s|r", ZL_PLAYERLIST.zhangDan["type"], ZL_PLAYERLIST.zhangDan["player"], ZL_PLAYERLIST.zhangDan["time"]))
        else
            sortHeaderBtn2:Hide()
            itemNameHeaderBtn2:Hide()
            myAmountHeaderBtn2:Hide()
            otherAmountHeaderBtn2:Hide()

            sortHeaderBtn3:Hide()
            itemNameHeaderBtn3:Hide()
            myAmountHeaderBtn3:Hide()
            otherAmountHeaderBtn3:Hide()
            local ReconciliationFrameHeight = (height+gapheight)*20 + itemNameHeaderBtn1:GetHeight() + 42
            ReconciliationFrame:SetSize(23+(sortwidth+itemNamewidth+myamountwidth+otheramountwidth-3), ReconciliationFrameHeight)
            content:SetHeight(20 * height)
            ReconciliationFrame.Title:SetText("对账单")
        end
        finalZhangdan = nil
        ReconciliationFrame:ClearAllPoints()
        ReconciliationFrame:SetPoint("CENTER")
    end

    ReconciliationFrame:RegisterEvent("CHAT_MSG_RAID_WARNING")
    ReconciliationFrame:RegisterEvent("CHAT_MSG_RAID_LEADER")
    ReconciliationFrame:RegisterEvent("CHAT_MSG_RAID")
    ReconciliationFrame:SetScript("OnEvent", function (self, _, msg, sender, ...)
        sender = ZL.GetClassColorName(ZL.GetFullName(sender))
        if msg:find("—通报账单—") then
            zhangDan = defaultZhangDan(sender, "BiaoGe")
            return
        elseif msg:find("RaidLedger:.... 收入 ....") then
            zhangDan = defaultZhangDan(sender, "RaidLedger")
            return
        end

        if not zhangDan then return end

        if sender == zhangDan.player then
            if zhangDan.type == "BiaoGe" then
                local itemLink, counts, _, amount = msg:match("^(" .. itemStr .. ")[H]?[x]?(%d*) (.+) (.+)$")
                if itemLink then
                    counts = counts or 1
                    counts = tonumber(counts) or 1
                    amount = amount or 0
                    amount = tonumber(amount) or 0
                    if not zhangDan.zhangDan[itemLink] then zhangDan.zhangDan[itemLink] = {counts=0, totalAmount=0} end
                    zhangDan.zhangDan[itemLink].counts = zhangDan.zhangDan[itemLink].counts + counts
                    zhangDan.zhangDan[itemLink].totalAmount = zhangDan.zhangDan[itemLink].totalAmount + amount
                    return
                end
            elseif zhangDan.type == "RaidLedger" then
                local itemLink, counts, amount = msg:match("^%.%.%. %d+%. .+ (" .. itemStr .. ") %((%d+)%) (%d+)[铜金]")
                if itemLink then
                    counts = counts or 1
                    counts = tonumber(counts) or 1
                    amount = amount or 0
                    amount = tonumber(amount) or 0
                    if not zhangDan.zhangDan[itemLink] then zhangDan.zhangDan[itemLink] = {counts=0, totalAmount=0} end
                    zhangDan.zhangDan[itemLink].counts = zhangDan.zhangDan[itemLink].counts + counts
                    zhangDan.zhangDan[itemLink].totalAmount = zhangDan.zhangDan[itemLink].totalAmount + amount
                    return
                end
            elseif zhangDan.type == "BigFoot" then

            end
            if msg:find("(—感谢使用BiaoGe插件—)") or msg:find("(RaidLedger: ...平均每人收入:)") then
                local total = 0
                for _, data in pairs(zhangDan.zhangDan) do
                    total = total + data.totalAmount
                end
                zhangDan.totalAmount = total
                ZL_PLAYERLIST.zhangDan = zhangDan
                zhangDan = nil
            end
        end
    end)
end

function ZL:CreateQuickRecordFrame()
    ZL.QuickRecordFrame = ZLCreateFrame("ZLQuickRecordFrame", ZL, nil, nil, "DIALOG", true, true, "快速记账")
    local QuickRecordFrame = ZL.QuickRecordFrame

    -- QuickRecordFrame.Background:SetColorTexture(0, 0, 0, 0.95)
    QuickRecordFrame:SetPoint("CENTER")
    QuickRecordFrame:SetResizable(false)
    QuickRecordFrame.resizeButton:Hide()
    QuickRecordFrame:Hide()

    -- 注册 ESC 键关闭窗口
    tinsert(UISpecialFrames, QuickRecordFrame:GetName())

    local itemFontString = QuickRecordFrame:CreateFontString(nil, "OVERLAY")
    itemFontString:SetPoint("TOPLEFT", QuickRecordFrame.TitleBottom, "BOTTOMLEFT", 10, -10)
    itemFontString:SetPoint("TOPRIGHT", QuickRecordFrame.TitleBottom, "BOTTOMRIGHT", -10, -10)
    itemFontString:SetFont(GameFontNormal:GetFont(), 17, "OUTLINE")
    itemFontString:SetWordWrap(false)
    itemFontString:SetHeight(20)
    itemFontString:SetJustifyH("CENTER")
    itemFontString:SetTextColor(1, 1, 1, 1)

    local countFontString = QuickRecordFrame:CreateFontString(nil, "OVERLAY")
    countFontString:SetPoint("TOPLEFT", itemFontString, "BOTTOMLEFT", 0, -10)
    countFontString:SetFont(GameFontNormal:GetFont(), 14, "OUTLINE")
    countFontString:SetWordWrap(false)
    countFontString:SetHeight(20)
    countFontString:SetJustifyH("LEFT")
    countFontString:SetTextColor(1, 0.82, 0, 1)
    countFontString:SetText("数量：")

    QuickRecordFrame.countBox = CreateEditBox(QuickRecordFrame, 140, 20, 14, "RIGHT", nil, nil, nil, nil, nil, nil, "ZLFASTCOUNTBOX")
    local countBox = QuickRecordFrame.countBox
    countBox:SetPoint("TOPLEFT", countFontString, "TOPRIGHT", 10, 0)
    countBox:SetText(1)
    countBox:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "LEFT", "RIGHT")
        GameTooltip:AddLine("提示")
        GameTooltip:AddLine("激活输入框，输入或鼠标滚轮设置", 1, 1, 1)
        GameTooltip:Show()
    end)
    countBox:SetScript("OnLeave", function (self)
        GameTooltip:Hide()
    end)
    countBox:SetScript("OnEditFocusGained", function (self)
        self:HighlightText()
    end)
    countBox:SetScript("OnEditFocusLost", function (self)
        self:HighlightText(0, 0)
    end)
    countBox:SetScript("OnMouseWheel", function (self, delta)
        if self:HasFocus() then
            local number = self:GetNumber() or 1
            if delta > 0 then
                number = number + 1
            else
                number = number - 1
            end
            if number < 1 then number = 1 end
            self:SetNumber(number)
        end
    end)
    countBox:SetScript("OnTextChanged", function (self)
        local number = self:GetNumber() or 1
        if number < 1 then number = 1 end
        self:SetNumber(number)
    end)


    local buyerFontString = QuickRecordFrame:CreateFontString(nil, "OVERLAY")
    buyerFontString:SetPoint("TOPLEFT", countFontString, "BOTTOMLEFT", 0, -10)
    buyerFontString:SetFont(GameFontNormal:GetFont(), 14, "OUTLINE")
    buyerFontString:SetWordWrap(false)
    buyerFontString:SetHeight(20)
    buyerFontString:SetJustifyH("LEFT")
    buyerFontString:SetTextColor(1, 0.82, 0, 1)
    buyerFontString:SetText("买家：")

    local function GroupMembersActivityMenu(self)
        local menuList = {}
        if IsInRaid(1) then
            local WagesGroupMembers = ZL.GetCurrentRoster()
            if WagesGroupMembers then
                for i, data in ipairs(WagesGroupMembers) do
                    if #data > 0 then
                        local memberList = {}
                        for _, playerName in ipairs(data) do
                            tinsert(memberList, {
                                text=playerName,
                                func = function ()
                                    self:SetText(playerName)
                                    self:ClearFocus()
                                end,
                            })
                        end
                        tinsert(menuList, {
                            text = i .. "队",
                            func = function ()
                                self:ClearFocus()
                            end,
                            hasArrow = true,
                            menuTable = memberList,
                        })
                    end
                end
                tinsert(menuList, {isSeparator = true})
                tinsert(menuList, {text = CANCEL, func = function() self:ClearFocus() end})
            end
        end
        return menuList
    end

    QuickRecordFrame.buyerBox = CreateEditBox(QuickRecordFrame, 140, 20, 14, "RIGHT", nil, nil, nil, nil, nil, nil, "ZLFASTBUYERBOX")
    local buyerBox = QuickRecordFrame.buyerBox
    buyerBox:SetPoint("TOPLEFT", buyerFontString, "TOPRIGHT", 10, 0)
    buyerBox.fontString = buyerBox:CreateFontString(nil, "OVERLAY")
    buyerBox.fontString:SetPoint("LEFT", buyerBox, "LEFT", 5, 0)
    buyerBox.fontString:SetFont(GameFontNormal:GetFont(), 13, "OUTLINE")
    buyerBox.fontString:SetWordWrap(false)
    buyerBox.fontString:SetHeight(20)
    buyerBox.fontString:SetJustifyH("LEFT")
    buyerBox.fontString:SetText("|cff595959可选|r")
    buyerBox:SetScript("OnTextChanged", function (self)
        if #(self:GetText()) > 0 then
            buyerBox.fontString:Hide()
        else
            buyerBox.fontString:Show()
        end
    end)
    buyerBox:SetScript("OnEditFocusGained", function (self)
        self:HighlightText()
        local groupMembers = GroupMembersActivityMenu(self)
        if #groupMembers > 0 then
            ZL.GUI:ToggleMenu(self, GroupMembersActivityMenu(self), 'cursor')
        end
    end)
    buyerBox:SetScript("OnEditFocusLost", function (self)
        ZL.GUI:CloseMenu()
        self:HighlightText(0, 0)
    end)

    local amountFontString = QuickRecordFrame:CreateFontString(nil, "OVERLAY")
    amountFontString:SetPoint("TOPLEFT", buyerFontString, "BOTTOMLEFT", 0, -10)
    amountFontString:SetFont(GameFontNormal:GetFont(), 14, "OUTLINE")
    amountFontString:SetWordWrap(false)
    amountFontString:SetHeight(20)
    amountFontString:SetJustifyH("LEFT")
    amountFontString:SetTextColor(1, 0.82, 0, 1)
    amountFontString:SetText("金额：")

    QuickRecordFrame.amountBox = CreateEditBox(QuickRecordFrame, 140, 20, 14, "RIGHT", nil, nil, nil, nil, nil, nil, "ZLFASTAMOUNTBOX")
    local amountBox = QuickRecordFrame.amountBox
    amountBox:SetPoint("TOPLEFT", amountFontString, "TOPRIGHT", 10, 0)
    amountBox:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "LEFT", "RIGHT")
        GameTooltip:AddLine("提示")
        GameTooltip:AddLine("激活输入框，输入或鼠标滚轮设置", 1, 1, 1)
        GameTooltip:Show()
    end)
    amountBox:SetScript("OnLeave", function (self)
        GameTooltip:Hide()
    end)
    amountBox:SetScript("OnEditFocusGained", function (self)
        self:HighlightText()
    end)
    amountBox:SetScript("OnEditFocusLost", function (self)
        self:HighlightText(0, 0)
    end)
    amountBox:SetScript("OnMouseWheel", function (self, delta)
        if self:HasFocus() then
            local number = self:GetNumber() or 0
            if delta > 0 then
                number = number + 100
            else
                number = number - 100
            end
            if number < 0 then number = 0 end
            self:SetNumber(number)
        end
    end)
    amountBox:SetScript("OnTextChanged", function (self)
        local number = self:GetNumber() or 0
        if number < 0 then number = 0 end
        self:SetNumber(number)
    end)

    function ZL.saveQuickRecordData(itemLink, buyerName, amount, counts )
        counts = tonumber(counts) or (countBox:GetNumber() or 1)
        amount = tonumber(amount) or (amountBox:GetNumber() or 0)
        buyerName = buyerName or (buyerBox:GetText() or "")
        itemLink = itemLink or itemFontString:GetText()
        local bossName = QuickRecordFrame.bossName
        local isOk
        local msgStr
        if amount <= 0 then
            msgStr = "|cffff0000请输入金额大于0的数！|r"
            ZL.Print(msgStr)
            amountBox:SetFocus()
            return
        end
        if counts > 1 then
            local totalCounts = 0
            for _, bossNameData in pairs(ZL_PLAYERLIST.lootList) do
                if type(bossNameData) == "table" then
                    for _itemLink, itemLinkData in pairs(bossNameData) do
                        if _itemLink == itemLink then
                            totalCounts = totalCounts + itemLinkData["拾取1"]["counts"]
                        end
                    end
                end
            end
            if totalCounts < counts then
                msgStr = "|cffff0000拾取中可记录物品数量小于输入数量，请核实输入数量是否正确或到拾取窗口记录！|r"
                ZL.Print(msgStr, true)
                countBox:SetFocus()
                return
            end
        end
        if ZL_PLAYERLIST.lootList[bossName] and ZL_PLAYERLIST.lootList[bossName][itemLink]
        and ZL_PLAYERLIST.lootList[bossName][itemLink]["拾取1"]["counts"] >= counts then
            isOk = true
        end
        if isOk then
            local itemLinkData = ZL_PLAYERLIST.lootList[bossName][itemLink]
            itemLinkData["拾取1"]["counts"] = itemLinkData["拾取1"]["counts"] - counts
            if buyerName == "" then
                local i = 1
                while true do
                    if not itemLinkData["未知" .. i] then
                        itemLinkData["未知" .. i] = {
                            counts = counts,
                            totalAmount = amount,
                        }
                        break
                    end
                    i = i + 1
                end
            else
                if not itemLinkData[buyerName] then itemLinkData[buyerName] = {counts=0, totalAmount=0} end
                itemLinkData[buyerName]["counts"] = itemLinkData[buyerName]["counts"] + counts
                itemLinkData[buyerName]["totalAmount"] = itemLinkData[buyerName]["totalAmount"] + amount
            end
        else
            local remainCounts = counts
            for _, bossNameData in pairs(ZL_PLAYERLIST.lootList) do
                if type(bossNameData) == "table" then
                    for _itemLink, itemLinkData in pairs(bossNameData) do
                        if _itemLink == itemLink and itemLinkData["拾取1"]["counts"] > 0 then
                            if itemLinkData["拾取1"]["counts"] >= remainCounts then
                                if buyerName == "" then
                                    local i = 1
                                    while true do
                                        if not itemLinkData["未知" .. i] then
                                            itemLinkData["未知" .. i] = {
                                                counts = remainCounts,
                                                totalAmount = amount,
                                            }
                                            break
                                        end
                                        i = i + 1
                                    end
                                else
                                    if not itemLinkData[buyerName] then itemLinkData[buyerName] = {counts=0, totalAmount=0} end
                                    itemLinkData[buyerName]["counts"] = itemLinkData[buyerName]["counts"] + remainCounts
                                    itemLinkData[buyerName]["totalAmount"] = itemLinkData[buyerName]["totalAmount"] + amount
                                end
                                itemLinkData["拾取1"]["counts"] = itemLinkData["拾取1"]["counts"] - remainCounts
                                remainCounts = 0
                            else
                                if buyerName == "" then
                                    local i = 1
                                    while true do
                                        if not itemLinkData["未知" .. i] then
                                            itemLinkData["未知" .. i] = {
                                                counts = itemLinkData["拾取1"]["counts"],
                                                totalAmount = amount*itemLinkData["拾取1"]["counts"]/counts,
                                            }
                                            break
                                        end
                                        i = i + 1
                                    end
                                else
                                    if not itemLinkData[buyerName] then itemLinkData[buyerName] = {counts=0, totalAmount=0} end
                                    itemLinkData[buyerName]["counts"] = itemLinkData[buyerName]["counts"] + itemLinkData["拾取1"]["counts"]
                                    itemLinkData[buyerName]["totalAmount"] = itemLinkData[buyerName]["totalAmount"] + amount*itemLinkData["拾取1"]["counts"]/counts
                                end
                                itemLinkData["拾取1"]["counts"] = 0
                                remainCounts = remainCounts - itemLinkData["拾取1"]["counts"]
                            end
                            if remainCounts <= 0 then break end
                        end
                    end
                    if remainCounts <= 0 then break end
                end
            end
        end
        msgStr = itemLink .. " 数量：|cffffffff" .. counts .. "|r 金额：|cffffffff" .. amount .. "|r"
        if buyerName == "" then
            msgStr = msgStr .. "\n记录保存成功！"
        else
            msgStr = msgStr .. "\n买家：" .. buyerName .. "\n记录保存成功！"
        end
        ZL.Print(msgStr, true)
        if ZL.OverviewFrame:IsShown() then
            ZL.OverviewFrame:Update(ZL_PLAYERLIST.lootList)
        end
        if ZL.QuickRecordFrame then
            QuickRecordFrame:Hide()
        end
    end
    local saveQuickRecordData = ZL.saveQuickRecordData
    amountBox:SetScript("OnEnterPressed", function (self)
        saveQuickRecordData()
    end)
    amountBox:SetScript("OnMouseDown", function (self, button)
        if button == "RightButton" and self:HasFocus() then
            C_Timer.After(0.5, function ()
                saveQuickRecordData()
            end)
        end
    end)

    local editboxList = {"ZLFASTCOUNTBOX", "ZLFASTBUYERBOX", "ZLFASTAMOUNTBOX"}
    local function OnTabPressed(self)
        EditBox_HandleTabbing(self, editboxList)
    end
    countBox:SetScript("OnTabPressed", OnTabPressed)
    buyerBox:SetScript("OnTabPressed", OnTabPressed)
    amountBox:SetScript("OnTabPressed", OnTabPressed)

    QuickRecordFrame.cancelBtn = CreateButton(nil, QuickRecordFrame, "取消", "TOPRIGHT", amountBox, "BOTTOMRIGHT", 0, -10, 50, 20)
    local cancelBtn = QuickRecordFrame.cancelBtn
    cancelBtn:SetScript("OnClick", function ()
        QuickRecordFrame:Hide()
    end)

    QuickRecordFrame.yesBtn = CreateButton(nil, QuickRecordFrame, "确定", "RIGHT", cancelBtn, "LEFT", -10, 0, 50, 20)
    local yesBtn = QuickRecordFrame.yesBtn
    yesBtn:SetScript("OnClick", function ()
        saveQuickRecordData()
    end)

    local width = 30 + countFontString:GetWidth() + 10 + countBox:GetWidth()
    QuickRecordFrame:SetSize(width, 190)
    QuickRecordFrame:SetHyperlinksEnabled(true)
    QuickRecordFrame:SetScript("OnHyperlinkEnter", function (self, link)
        local leftPos = self:GetLeft()
        local rightPos = GetScreenWidth() - self:GetRight()
        if rightPos > leftPos then
            GameTooltipShow(self, "TOPLEFT", "TOPRIGHT")
        else
            GameTooltipShow(self, "TOPRIGHT", "TOPLEFT")
        end
        GameTooltip:SetHyperlink(link)
        GameTooltip:Show()
        if _G["ShoppingTooltip1"] and _G["ShoppingTooltip1"]:IsShown() then
            _G["ShoppingTooltip1"]:ClearAllPoints()
            if rightPos > leftPos then
                _G["ShoppingTooltip1"]:SetPoint("TOPLEFT", GameTooltip, "TOPRIGHT", 0, 0)
            else
                _G["ShoppingTooltip1"]:SetPoint("TOPRIGHT", GameTooltip, "TOPLEFT", 0, 0)
            end
        end
        if _G["ShoppingTooltip2"] and _G["ShoppingTooltip2"]:IsShown() then
            _G["ShoppingTooltip2"]:ClearAllPoints()
            if rightPos > leftPos then
                _G["ShoppingTooltip2"]:SetPoint("TOPLEFT", _G["ShoppingTooltip1"], "TOPRIGHT", 0, 0)
            else
                _G["ShoppingTooltip2"]:SetPoint("TOPRIGHT", _G["ShoppingTooltip1"], "TOPLEFT", 0, 0)
            end
        end
    end)
    QuickRecordFrame:SetScript("OnHyperlinkLeave", GameTooltip_Hide)
    QuickRecordFrame:SetScript("OnHide", function (self)
        QuickRecordFrame.bossName = nil
    end)

    function QuickRecordFrame:Update(itemLink)
        itemFontString:SetText(itemLink)
        countBox:SetText(1)
        buyerBox:SetText("")
        amountBox:SetNumber(0)
    end

end
