local addonName, ZLAUTO = ...

local ZL = ZLAUTO.mainFrame
if ZL.BUILD_DATA >= 4 then return end

local ScrollFrame_OnMouseWheel = ZL.ScrollFrame_OnMouseWheel
local ZLCreateFrame = ZL.ZLCreateFrame
local CreateButton = ZL.CreateButton
local CreateEditBox = ZL.CreateEditBox
local GameTooltipShow = ZL.GameTooltipShow
local emptytable = ZL.emptytable
local PickupContainerItem = C_Container and C_Container.PickupContainerItem or PickupContainerItem

local auctionBuyItemInfo = {} --用来缓存购买物品的信息itemId, count, buyoutPrice
local auctionFailed = {
    ERR_AUCTION_ALREADY_BID, --"你已经对该物品竞价了。"
    ERR_AUCTION_BID_INCREMENT, --"你的提价幅度太小了。"
    ERR_AUCTION_BID_OWN, --"你不能对自己拍卖的物品竞标"
    ERR_AUCTION_DATABASE_ERROR, --"内部拍卖错误"
    ERR_AUCTION_DOUBLE_BID, --"您已经是最高出价者"
    ERR_AUCTION_HIGHER_BID, --"已经有更高的出价了。"
    ERR_AUCTION_HOUSE_BUSY, --"拍卖行当前用户过多。"
    ERR_AUCTION_HOUSE_DISABLED, --"拍卖行目前暂时关闭。|n请稍后再试。"
    ERR_AUCTION_HOUSE_UNAVAILABLE, --"拍卖行当前不可用。"
    ERR_AUCTION_MIN_BID, --"你的出价必须不低于最低竞标价格。"
    ERR_ITEM_NOT_FOUND, --"未找到指定物品"
}
local function isAuctionBidFailed(msg)
    for _, v in ipairs(auctionFailed) do
        if msg == v then
            return true
        end
    end
    return false
end

local function SetCopper(money, frameType)
    if not money or type(money) ~= "number" then return end
    frameType = frameType or "UnitPrice"  --StackPrice
    local gold = floor(money / (COPPER_PER_GOLD))
	local silver = floor((money - (gold * COPPER_PER_GOLD)) / COPPER_PER_SILVER)
	local copper = mod(money, COPPER_PER_SILVER)
    HookAuctionFrame = ZL.HookAuctionFrame
    if frameType == "UnitPrice" then
        HookAuctionFrame.UnitGoldPrice:SetNumber(gold)
        HookAuctionFrame.UnitSilverPrice:SetNumber(silver)
        HookAuctionFrame.UnitCopperPrice:SetNumber(copper)
    elseif frameType == "StackPrice" then
        HookAuctionFrame.StackGoldPrice:SetNumber(gold)
        HookAuctionFrame.StackSilverPrice:SetNumber(silver)
        HookAuctionFrame.StackCopperPrice:SetNumber(copper)
    else
        return
    end
end
local function SetAuctionPriceDownCpper(money)
    if not money or type(money) ~= "number" then return end
    local gold = floor(money / (COPPER_PER_GOLD))
	local silver = floor((money - (gold * COPPER_PER_GOLD)) / COPPER_PER_SILVER)
	local copper = mod(money, COPPER_PER_SILVER)
    HookAuctionFrame = ZL.HookAuctionFrame
    HookAuctionFrame.glodPriceDown:SetNumber(gold)
    HookAuctionFrame.silverPriceDown:SetNumber(silver)
    HookAuctionFrame.copperPriceDown:SetNumber(copper)
end
local function GetAuctionSortColumn(sortTable)
	local existingSortColumn, existingSortReverse = GetAuctionSort(sortTable, 1);
    if (existingSortColumn == "totalbuyout" or existingSortColumn == "unitbid" or existingSortColumn == "unitprice") then
		existingSortColumn = "bid";
	end
	return existingSortColumn, existingSortReverse
end

local function Set_AuctionSort_Column(sortTable)
    SetCVar("auctionSortByBuyoutPrice", 1)
    SetCVar("auctionSortByUnitPrice", 1)
    AuctionFrame_SetSort(sortTable or "list", "bid", false)
end
local function IsCanDo()
    local existingSortColumn, existingSortReverse = GetAuctionSortColumn("list")
    if existingSortColumn ~= "bid" or existingSortReverse ~= false or
    GetCVar("auctionSortByBuyoutPrice") ~= "1" or GetCVar("auctionSortByUnitPrice") ~= "1" then
        return
    else
        return true
    end
end
local function MoneyInputEidtBox(parent, maxLetters, width, atlasName)
    -- local editBox = CreateFrame("EditBox", nil, parent or ZL.HookAuctionFrame, "InputBoxTemplate")
    local editBox = CreateEditBox(parent or ZL.HookAuctionFrame, width or 40, 20, 14, "RIGHT", true, nil, nil, nil, nil, nil)
    if maxLetters then
        editBox:SetMaxLetters(maxLetters)
    end
    if atlasName then
        editBox.Icon = editBox:CreateTexture(nil, "ARTWORK")
        editBox.Icon:SetSize(12, 14)
        editBox.Icon:SetPoint("RIGHT", 13, -2)
        editBox.Icon:SetAtlas(atlasName)
    end
    return editBox
end

local function UpdateSortAnim(isPlay)
    local sortBtn  = ZL.HookAuctionFrame.sortBtn
    if isPlay then
        sortBtn.SpellHighlightAnim:Play()
        sortBtn.SpellHighlightTexture:Show()
    else
        sortBtn.SpellHighlightAnim:Stop()
        sortBtn.SpellHighlightTexture:Hide()
    end
end
local function UpdateUnitPrice()
    local HookAuctionFrame = ZL.HookAuctionFrame
    local gold = HookAuctionFrame.StackGoldPrice:GetNumber()
    local silver = HookAuctionFrame.StackSilverPrice:GetNumber()
    local copper = HookAuctionFrame.StackCopperPrice:GetNumber()
    local count = HookAuctionFrame.StackCount:GetNumber()
    local stackMoney = gold*10000 + silver*100 + copper
    local unitMoney = floor(stackMoney/count)
    SetCopper(unitMoney)
end
local function UpdateStackPrice()
    local HookAuctionFrame = ZL.HookAuctionFrame
    local gold = HookAuctionFrame.UnitGoldPrice:GetNumber()
    local silver = HookAuctionFrame.UnitSilverPrice:GetNumber()
    local copper = HookAuctionFrame.UnitCopperPrice:GetNumber()
    local count = HookAuctionFrame.StackCount:GetNumber()
    local unitMoney = gold*10000 + silver*100 + copper
    local stackMoney = unitMoney*count
    SetCopper(stackMoney, "StackPrice")
end
local function UpdateAuctionPriceDown()
    local HookAuctionFrame = ZL.HookAuctionFrame
    local gold = HookAuctionFrame.glodPriceDown:GetNumber()
    local silver = HookAuctionFrame.silverPriceDown:GetNumber()
    local copper = HookAuctionFrame.copperPriceDown:GetNumber()
    ZL.opt.AucitonPriceDown = gold*10000 + silver*100 + copper
end
local function UpdateDepositMoney()
    local HookAuctionFrame = ZL.HookAuctionFrame
    local StackSize = HookAuctionFrame.StackCount:GetNumber()
    local NumStacks = HookAuctionFrame.NumStacks:GetNumber()
    local perDepositMoney = GetAuctionDeposit(1, 1, 1, StackSize, 1)
    HookAuctionFrame.depositMoneyLabel:SetText(format("单组保管费：|cffffffff%s|r", GetMoneyString(perDepositMoney)))
    HookAuctionFrame.totalDepositMoneyLabel:SetText(format("所有保管费：|cffffffff%s|r", GetMoneyString(perDepositMoney*NumStacks)))
end

local function UpdateStartAuctionCopperInfo(UnitPrice)
    local HookAuctionFrame = ZL.HookAuctionFrame
    SetCopper(UnitPrice)
    SetCopper(UnitPrice*HookAuctionFrame.StackCount:GetNumber(), "StackPrice")
end
local function UpdateStartAuctionStackInfo()
    local stackCount, numStack
    local HookAuctionFrame = ZL.HookAuctionFrame
    local _, _, _, _, _, _, _, _stackCount, _totalCount, itemID = GetAuctionSellItemInfo()
    if not itemID then
        stackCount = 1
        numStack = 1
    else
        if ZL.opt.defaultStackCountMax then
            stackCount = min(_stackCount, _totalCount)
        else
            stackCount = ZL.opt.DefaultStackCount
        end

        if ZL.opt.defaultNumStacksMax then
            numStack = _totalCount
        else
            numStack = ZL.opt.DefaultNumStacks
        end

        if stackCount * numStack > _totalCount then
            if ZL.opt.defaultStackCountOrdefaultNumStacksFirst == 1 then
                numStack = floor(_totalCount/stackCount)
            else
                stackCount = floor(_totalCount/numStack)
            end
        end
    end
    HookAuctionFrame.StackCount:SetNumber(stackCount)
    HookAuctionFrame.NumStacks:SetNumber(numStack)
end

local function BrowseButtonRightClickMenu()
    local menuList = {}
    local i = GetSelectedAuctionItem("list")
    local _, _, selectcount, _, _, _, _, _, _, selectbuyoutPrice, selectbidAmount, _, _, owner = GetAuctionItemInfo("list", i)
    tinsert(menuList, {
        text = "取消拍卖",
        disabled = owner ~= UnitName("player"),
        func = function ()
            local selectItemLink = GetAuctionItemLink("list", i)
            local isExist, count, buyoutPrice, bidAmount, saleStatus, ownerItemLink, _
            for index = 1, GetNumAuctionItems("owner") do
                ownerItemLink = GetAuctionItemLink("owner", index)
                if ownerItemLink == selectItemLink then
                    _, _, count, _, _, _, _, _, _, buyoutPrice, bidAmount, _, _, _, _, saleStatus = GetAuctionItemInfo("owner", index)
                    if saleStatus ~= 1 and selectcount == count and selectbuyoutPrice == buyoutPrice and selectbidAmount == bidAmount then
                        isExist = true
                        CancelAuction(index)
                        break
                    end
                end
            end
            if not isExist then
                ZL.Print("没有符合要求的拍卖物品，请点搜索按钮刷新")
            end
        end
    })
    return menuList
end

local function BrowseNameOnMouseUpMenu()
    local menuList = {}
    local ownerList = {}
    local name, saleStatus, _
    for index = 1, GetNumAuctionItems("owner") do
        name, _, _, _, _, _, _, _, _, _, _, _, _, _, _, saleStatus = GetAuctionItemInfo("owner", index)
        if saleStatus ~= 1 and not tIndexOf(ownerList, name) then
            tinsert(ownerList, name)
        end
    end
    if #ownerList > 0 then
        table.sort(ownerList)
        tinsert(menuList, {text="拍卖中", isTitle=1})
        for _, key in ipairs(ownerList) do
            tinsert(menuList, {
                text = key,
                func = function ()
                    ChatEdit_ActivateChat(ChatEdit_ChooseBoxForSend())
                    ChatEdit_InsertLink(format('"%s"', key))
                    ChatEdit_ChooseBoxForSend():HighlightText()
                end
            })
        end
    end
    local AuctionSortKeyList = ZL.opt.AuctionSortKeyList
    if AuctionSortKeyList and #AuctionSortKeyList > 0 then
        tinsert(menuList, {text="历史搜索词", isTitle=1})
        for _, key in ipairs(AuctionSortKeyList) do
            if not tIndexOf(ownerList, key) then
                tinsert(menuList, {
                    text = key,
                    func = function ()
                        ChatEdit_ActivateChat(ChatEdit_ChooseBoxForSend())
                        ChatEdit_InsertLink(format('"%s"', key))
                        ChatEdit_ChooseBoxForSend():HighlightText()
                    end
                })
            end
        end
    end
    return menuList
end

local function BuyItemsInfoFrame_Update(self)
    local batchAuctions = GetNumAuctionItems("list")
    if batchAuctions > 0 and self.index <= batchAuctions then
        local name, texture, count, _, _, _, _, _, _, buyoutPrice, _, _, _, _, _, _, itemId = GetAuctionItemInfo("list", self.index)
        self.itemId = itemId
        self.itemName = name
        self.currentBuyoutPrice = buyoutPrice
        self.currentCount = count
        self.currentItemName:SetText("|T" .. texture .. ":18:18:-2:0|t" .. self.itemLink)
        local unitPrice = floor(buyoutPrice/count)
        unitPrice = unitPrice == 0 and 1 or unitPrice
        self.currentBuyInfo:SetTextScale(1)
        self.totalBuyInfo:SetTextScale(1)
        self.currentBuyInfo:SetText(format("购买：%d |CFFFFDE00×|r %s |CFFFFDE00=|r %s", count, GetMoneyString(unitPrice), GetMoneyString(buyoutPrice)))
        local bili = self.currentBuyInfo:GetStringWidth()>0 and self.currentBuyInfo:GetWidth()/(self.currentBuyInfo:GetUnboundedStringWidth()+5) or 1
        if bili < 1 then
            self.currentBuyInfo:SetTextScale(bili)
        else
            self.currentBuyInfo:SetTextScale(1)
        end
        if self.totalMoney then
            self.totalBuyInfo:SetText(format("总计：%d |CFFFFDE00×|r %s |CFFFFDE00=|r %s", self.totalCount, GetMoneyString(floor(self.totalMoney/self.totalCount)), GetMoneyString(self.totalMoney)))
            bili = self.totalBuyInfo:GetStringWidth()>0 and self.totalBuyInfo:GetWidth()/(self.totalBuyInfo:GetUnboundedStringWidth()+5) or 1
            if bili < 1 then
                self.currentBuyInfo:SetTextScale(bili)
            else
                self.currentBuyInfo:SetTextScale(1)
            end
        else
            self.totalBuyInfo:SetText(" ")
        end

    else
        self:Hide()
    end
end

local function AuctionBuyItemsHistory(itemId, count, price)
    ZL.opt.BuyItemsHistoryList = ZL.opt.BuyItemsHistoryList or {}
    local list = ZL.opt.BuyItemsHistoryList
    list[itemId] = list[itemId] or {}
    local buyDate = date("%Y-%m-%d", time())
    list[itemId][buyDate] = list[itemId][buyDate] or {0, 0}
    list[itemId][buyDate][1] = list[itemId][buyDate][1] + count
    list[itemId][buyDate][2] = list[itemId][buyDate][2] + price
end
local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW")
f:SetScript("OnEvent", function (slef, event, ...)
    if event == "AUCTION_HOUSE_SHOW" then
        if not ZL.HookAuctionFrame then
            if AuctionFrame and _G["BrowseButton1"] then
                ZL:CreateHookAuctionFrame()
                local id = 1
                local btn = _G["BrowseButton" .. id]
                local ownerTextur
                while btn do
                    ownerTextur = btn:CreateTexture(nil, "BACKGROUND")
                    ownerTextur:SetAllPoints()
                    ownerTextur:SetTexture([[Interface\QuestFrame\UI-QuestLogTitleHighlight]])
                    ownerTextur:Hide()
                    btn.ownerTextur = ownerTextur
                    btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                    btn:HookScript("OnClick", function (self, button)
                        local index = mod(self.itemIndex,50) == 0 and 50 or mod(self.itemIndex,50)
                        local name, _, count, _, _, _, _, _, _, buyoutPrice, _, _, _, owner, _, saleStatus, _itemID = GetAuctionItemInfo("list", index)
                        auctionBuyItemInfo = {
                            itemId = _itemID,
                            count = count,
                            buyoutPrice = buyoutPrice,
                            itemName = name,
                        }
                        if button == "LeftButton" then
                            local itemID = select(10, GetAuctionSellItemInfo())
                            if itemID then
                                -- local _, _, count, _, _, _, _, _, _, buyoutPrice, _, _, _, owner, _, _, _itemID = GetAuctionItemInfo("list", index)
                                if itemID == _itemID and buyoutPrice > 0 then
                                    local UnitPrice = owner == UnitName("player") and floor(buyoutPrice/count) or (floor(buyoutPrice/count) - ZL.opt.AucitonPriceDown)
                                    UpdateStartAuctionCopperInfo(UnitPrice)
                                    UpdateStartAuctionStackInfo()
                                end
                            end
                        else
                            if IsModifierKeyDown() then
                                if IsShiftKeyDown() then
                                    if name and saleStatus ~=1 and buyoutPrice > 0 and owner ~= UnitName("player") then
                                        HookAuctionFrame.BuyItemsInfoFrame:Hide()
                                        PlaceAuctionBid("list", index, buyoutPrice)
                                    end
                                end
                            else
                                ZL.GUI:ToggleMenu(self, BrowseButtonRightClickMenu(), "cursor")
                            end
                        end
                    end)
                    id = id + 1
                    btn = _G["BrowseButton" .. id]
                end
                id = 1
                btn = _G["AuctionsButton" .. id]
                while btn do
                    btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                    btn:HookScript("OnClick", function (self, button)
                        if button == "RightButton" then
                            local index = GetSelectedAuctionItem("owner")
                            local saleStatus = select(16, GetAuctionItemInfo("owner", index))
                            if saleStatus ~= 1 then
                                CancelAuction(index)
                            end
                        end
                    end)
                    id = id + 1
                    btn = _G["AuctionsButton" .. id]
                end
                _G["BrowseBuyoutButton"]:HookScript("OnEnable", function (self)
                    if not HookAuctionFrame.BuyItemsInfoFrame:IsShown() then
                        HookAuctionFrame.BuyAuctionBtn:Enable()
                    end
                end)
                _G["BrowseBuyoutButton"]:HookScript("OnDisable", function (self)
                    if not HookAuctionFrame.BuyItemsInfoFrame:IsShown() then
                        HookAuctionFrame.BuyAuctionBtn:Disable()
                    end
                end)
                _G["BrowseBuyoutButton"]:HookScript("OnClick", function ()
                    HookAuctionFrame.BuyItemsInfoFrame:Hide()
                end)
                _G["BrowseBidButton"]:HookScript("OnClick", function ()
                    HookAuctionFrame.BuyItemsInfoFrame:Hide()
                end)
                hooksecurefunc("AuctionFrameBrowse_Search", function ()
                    HookAuctionFrame.BuyItemsInfoFrame:Hide()
                end)
                hooksecurefunc("FauxScrollFrame_Update", function (frame)
                    if frame == _G["BrowseScrollFrame"] then
                        for i = 1, NUM_BROWSE_TO_DISPLAY do
                            btn = _G["BrowseButton" .. i]
                            if btn.itemIndex then
                                local index = mod(btn.itemIndex,50) == 0 and 50 or mod(btn.itemIndex,50)
                                local owner = select(14, GetAuctionItemInfo("list", index))
                                if owner == UnitName("player") then
                                    btn.ownerTextur:Show()
                                else
                                    btn.ownerTextur:Hide()
                                end
                            end
                        end
                    end
                end)
                hooksecurefunc("AuctionFrameBrowse_Search", function ()
                    -- _G["BrowseName"]:ClearFocus()
                    local txt = _G["BrowseName"]:GetText() or ""
                    txt = txt:gsub('"', "")
                    if #txt > 0 then
                        ZL.opt.AuctionSortKeyList = ZL.opt.AuctionSortKeyList or {}
                        local index = tIndexOf(ZL.opt.AuctionSortKeyList, txt)
                        if index then
                            tremove(ZL.opt.AuctionSortKeyList, index)
                        end
                        tinsert(ZL.opt.AuctionSortKeyList, 1, txt)
                        if #ZL.opt.AuctionSortKeyList > 20 then
                            for i = #ZL.opt.AuctionSortKeyList, 21, -1 do
                                tremove(ZL.opt.AuctionSortKeyList, i)
                            end
                        end
                    end
                end)
                btn = nil
                ownerTextur = nil
            end
        end
        Set_AuctionSort_Column()
    end
end)

function ZL:CreateHookAuctionFrame()
    ZL.HookAuctionFrame = ZLCreateFrame(nil, AuctionFrame, 300, AuctionFrame:GetHeight()-27, nil, nil, nil, "AutoAuction(|cffffffff拍卖行|r)")
    local HookAuctionFrame = ZL.HookAuctionFrame
    HookAuctionFrame.Background:SetColorTexture(0.05, 0.05, 0.05, 1)
    HookAuctionFrame:SetPoint("TOPLEFT", AuctionFrame, "TOPRIGHT", 0, -15)
    HookAuctionFrame.resizeButton:Hide()
    HookAuctionFrame:SetScript("OnShow", function ()
        HookAuctionFrame.New_Auction_Update()
        UpdateDepositMoney()
        UpdateStartAuctionStackInfo()
    end)
    HookAuctionFrame:SetScript("OnMouseUp", function (self, button)
        if button == "LeftButton" then
            local infoType = GetCursorInfo()
            if infoType and infoType == "item" then
                ClickAuctionSellItemButton()
                ClearCursor()
            end
            local edit = GetCurrentKeyBoardFocus()
            if edit then
                edit:ClearFocus()
            end
        end
    end)
    local showBtn = CreateButton(nil, _G["AuctionFrameBrowse"], ">>", "LEFT", BrowseSearchButton, "RIGHT", 13, 3, 30, BrowseSearchButton:GetHeight()-4, 14, true, 0)
    HookAuctionFrame.showBtn = showBtn
    showBtn:SetScript("OnClick", function ()
        if HookAuctionFrame:IsShown() then
            HookAuctionFrame:Hide()
        else
            HookAuctionFrame:Show()
        end
    end)
    local CancelAuctionBtn = CreateButton(nil, AuctionFrameBrowse, "取消有更低价的", "BOTTOMRIGHT", AuctionFrameCloseButton, "TOPRIGHT", 0, 0, 120, AuctionFrameCloseButton:GetHeight()-6, 13, true, 0)
    HookAuctionFrame.CancelAuctionBtn = CancelAuctionBtn
    CancelAuctionBtn:Disable()
    CancelAuctionBtn:SetScript("OnClick", function (self)
        if not IsCanDo() then
            ZL.Print("未按指定排序，无法操作！")
            return
        end
        local _, _, count, _, _, _, _, _, _, buyoutPrice, _, _, _, _, _, _, itemId = GetAuctionItemInfo("list", 1)
        if itemId and buyoutPrice > 0 then
            local itemLink = GetAuctionItemLink("list", 1)
            local ownerItemLink, ownercount, ownerbuyoutPrice, ownersaleStatus
            for index = 1, GetNumAuctionItems("owner") do
                ownerItemLink = GetAuctionItemLink("owner", index)
                if ownerItemLink == itemLink then
                    _, _, ownercount, _, _, _, _, _, _, ownerbuyoutPrice, _, _, _, _, _, ownersaleStatus = GetAuctionItemInfo("owner", index)
                    if ownersaleStatus ~= 1 and ownerbuyoutPrice > 0 and ownerbuyoutPrice/ownercount > buyoutPrice/count then
                        CancelAuction(index)
                        break
                    end
                end
            end
            local isExist
            for index = 1, GetNumAuctionItems("owner") do
                ownerItemLink = GetAuctionItemLink("owner", index)
                if ownerItemLink == itemLink then
                    _, _, ownercount, _, _, _, _, _, _, ownerbuyoutPrice, _, _, _, _, _, ownersaleStatus = GetAuctionItemInfo("owner", index)
                    if ownersaleStatus ~= 1 and ownerbuyoutPrice/ownercount > buyoutPrice/count then
                        isExist = true
                        break
                    end
                end
            end
            if not isExist then
                self:Disable()
                ZL.Print(itemLink .. "已全部取消有更低价的拍卖。")
            end
        end
    end)
    local BuyAuctionBtn = CreateButton(nil, AuctionFrameBrowse, "连续购买", "RIGHT", CancelAuctionBtn, "LEFT", -3, 0, 80, AuctionFrameCloseButton:GetHeight()-6, 13, true, 0)
    HookAuctionFrame.BuyAuctionBtn = BuyAuctionBtn
    BuyAuctionBtn:Disable()
    BuyAuctionBtn:SetScript("OnClick", function ()
        HookAuctionFrame.BuyItemsInfoFrame:Show()
    end)
    local BuyItemsInfoFrame = CreateFrame("Frame", nil, AuctionFrameBrowse, "BackdropTemplate")
    HookAuctionFrame.BuyItemsInfoFrame = BuyItemsInfoFrame
    BuyItemsInfoFrame:Hide()
    BuyItemsInfoFrame:SetSize(450, 200)
    BuyItemsInfoFrame:SetPoint("CENTER", AuctionFrameBrowse, "CENTER", 120, 0)
    BuyItemsInfoFrame:SetFrameStrata("DIALOG")
    BuyItemsInfoFrame:SetToplevel(true)
    BuyItemsInfoFrame:SetMovable(true)
    BuyItemsInfoFrame:SetHyperlinksEnabled(true)
    BuyItemsInfoFrame:SetScript("OnMouseUp", function (self)
        self:StopMovingOrSizing()
    end)
    BuyItemsInfoFrame:SetScript("OnMouseDown", function (self)
        self:StartMoving()
    end)
    BuyItemsInfoFrame:SetScript("OnHyperlinkEnter", function(self, link)
        local itemID = GetItemInfoInstant(link)
        if itemID then
            GameTooltip:SetOwner(self, "ANCHOR_CURSOR", 0, 0)
            GameTooltip:ClearLines()
            GameTooltip:SetHyperlink(link)
            GameTooltip:Show()
        end
    end)
    BuyItemsInfoFrame:SetScript("OnHyperlinkLeave", function(self)
        GameTooltip:Hide()
    end)
    BuyItemsInfoFrame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-Dark",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        tile = true,
        tileSize = 32,
        edgeSize = 32,
        insets = { left = 11, right = 12, top = 12, bottom = 11 }
    })
    BuyItemsInfoFrame:SetScript("OnShow", function (self)
        BuyAuctionBtn:Disable()
        self.BuyItemsInfoFrameBuyBtn:Enable()
        self.totalNeedBuyCount:SetText("")
        local index = GetSelectedAuctionItem("list")
        self.itemLink = GetAuctionItemLink("list", index)
        self.index = index
        self.itemId = nil
        self.totalMoney = nil
        self.totalCount = nil
        self.currentBuyoutPrice = nil
        self.currentCount = nil
        BuyItemsInfoFrame_Update(self)
    end)
    BuyItemsInfoFrame:SetScript("OnHide", function (self)
        if _G["BrowseBuyoutButton"]:IsEnabled() then
            BuyAuctionBtn:Enable()
        else
            BuyAuctionBtn:Disable()
        end
    end)
    local currentItemName = BuyItemsInfoFrame:CreateFontString(nil, "ARTWORK")
    currentItemName:SetSize(BuyItemsInfoFrame:GetWidth()-40, 24)
    currentItemName:SetFont(GameFontNormal:GetFont(), 20)
    currentItemName:SetPoint("TOP", 0, -20)
    BuyItemsInfoFrame.currentItemName = currentItemName

    local currentBuyInfo = BuyItemsInfoFrame:CreateFontString(nil, "ARTWORK")
    currentBuyInfo:SetFont(GameFontNormal:GetFont(), 16)
    currentBuyInfo:SetSize(BuyItemsInfoFrame:GetWidth()-40, 20)
    currentBuyInfo:SetJustifyH("LEFT")
    currentBuyInfo:SetPoint("TOPLEFT", BuyItemsInfoFrame, "TOPLEFT", 20, -(30+currentItemName:GetHeight()))
    BuyItemsInfoFrame.currentBuyInfo = currentBuyInfo
    local totalBuyInfo = BuyItemsInfoFrame:CreateFontString(nil, "ARTWORK")
    totalBuyInfo:SetFont(GameFontNormal:GetFont(), 16)
    totalBuyInfo:SetSize(BuyItemsInfoFrame:GetWidth()-40, 20)
    totalBuyInfo:SetJustifyH("LEFT")
    totalBuyInfo:SetPoint("TOPLEFT", currentBuyInfo, "BOTTOMLEFT", 0, -5)
    BuyItemsInfoFrame.totalBuyInfo = totalBuyInfo
    local totalNeedBuyCountLabel = BuyItemsInfoFrame:CreateFontString(nil, "ARTWORK")
    totalNeedBuyCountLabel:SetFont(GameFontNormal:GetFont(), 14)
    totalNeedBuyCountLabel:SetHeight(20)
    totalNeedBuyCountLabel:SetJustifyH("LEFT")
    totalNeedBuyCountLabel:SetPoint("TOPLEFT", totalBuyInfo, "BOTTOMLEFT", 0, -15)
    totalNeedBuyCountLabel:SetText("数量")
    local totalNeedBuyCount = MoneyInputEidtBox(BuyItemsInfoFrame, nil, 60)
    BuyItemsInfoFrame.totalNeedBuyCount = totalNeedBuyCount
    totalNeedBuyCount:SetPoint("LEFT", totalNeedBuyCountLabel, "RIGHT", 5, 0)

    -- local perBuyMoneyLabel = BuyItemsInfoFrame:CreateFontString(nil, "ARTWORK")
    -- perBuyMoneyLabel:SetFont(GameFontNormal:GetFont(), 14)
    -- perBuyMoneyLabel:SetHeight(20)
    -- perBuyMoneyLabel:SetJustifyH("LEFT")
    -- perBuyMoneyLabel:SetPoint("LEFT", totalNeedBuyCount, "RIGHT", 10, 0)
    -- perBuyMoneyLabel:SetText("均价")
    -- local perBuyMoney = MoneyInputEidtBox(BuyItemsInfoFrame, nil, 60)
    -- BuyItemsInfoFrame.perBuyMoney = perBuyMoney
    -- perBuyMoney:SetPoint("LEFT", perBuyMoneyLabel, "RIGHT", 5, 0)

    local BuyItemsInfoFrameCloseBtn = CreateFrame("Button", nil, BuyItemsInfoFrame, "UIPanelButtonTemplate")
    BuyItemsInfoFrame.BuyItemsInfoFrameCloseBtn = BuyItemsInfoFrameCloseBtn
    BuyItemsInfoFrameCloseBtn:SetSize(60, 25)
    BuyItemsInfoFrameCloseBtn:SetPoint("BOTTOMRIGHT", BuyItemsInfoFrame, "BOTTOMRIGHT", -20, 20)
    BuyItemsInfoFrameCloseBtn:SetText("关闭")
    BuyItemsInfoFrameCloseBtn:SetScript("OnClick", function ()
        BuyItemsInfoFrame:Hide()
    end)

    local BuyItemsInfoFrameBuyBtn = CreateFrame("Button", nil, BuyItemsInfoFrame, "UIPanelButtonTemplate")
    BuyItemsInfoFrame.BuyItemsInfoFrameBuyBtn = BuyItemsInfoFrameBuyBtn
    BuyItemsInfoFrameBuyBtn:SetSize(60, 25)
    BuyItemsInfoFrameBuyBtn:SetPoint("RIGHT", BuyItemsInfoFrameCloseBtn, "LEFT", -5, 0)
    BuyItemsInfoFrameBuyBtn:SetText("购买")
    BuyItemsInfoFrameBuyBtn:SetScript("OnClick", function (self)
        local needBuyCount = BuyItemsInfoFrame.totalNeedBuyCount:GetNumber()
        if needBuyCount > 0 and (BuyItemsInfoFrame.totalCount or 0 )>= needBuyCount then
            ZL.Print("已购买足够数量！")
            return
        end
        local batchAuctions = GetNumAuctionItems("list")
        if batchAuctions == 0 then
            ZL.Print("当前页没有拍卖物品！", true)
            return
        end
        local index = BuyItemsInfoFrame.index
        if index > batchAuctions then
            ZL.Print("已到末尾，如需购买，请关掉窗口后重新选择！", true)
            return
        end
        local isExsit = true
        local name, _, count, _, _, _, _, _, _, buyoutPrice, _, _, _, owner = GetAuctionItemInfo("list", index)
        if not name or buyoutPrice == 0 or owner == UnitName("player") or GetAuctionItemLink("list", index) ~= BuyItemsInfoFrame.itemLink then
            isExsit = false
            for i = index+1, batchAuctions do
                name, _, _, _, _, _, _, _, _, buyoutPrice, _, _, _, owner = GetAuctionItemInfo("list", i)
                if name and buyoutPrice > 0 and owner ~= UnitName("player") and GetAuctionItemLink("list", i) == BuyItemsInfoFrame.itemLink then
                    isExsit = true
                    BuyItemsInfoFrame.index = i
                    break
                end
            end
        end
        if not isExsit then
            ZL.Print("当前页未找到符合要求的拍卖物品，请关闭窗口后重新搜索！", true)
            return
        end
        index = BuyItemsInfoFrame.index
        buyoutPrice = select(10, GetAuctionItemInfo("list", index))
        if buyoutPrice > GetMoney() then
            ZL.Print("金币不足！", true)
        end
        PlaceAuctionBid("list", index, buyoutPrice)
        self:Disable()
    end)

    local lockBtnFrame = CreateFrame("Button", nil, _G["BrowsePriceOptionsButtonFrame"].Button)
    BuyItemsInfoFrame.lockBtnFrame = lockBtnFrame
    lockBtnFrame:SetPoint("TOPLEFT", _G["BrowseQualitySort"], "TOPLEFT")
    lockBtnFrame:SetPoint("BOTTOMRIGHT", _G["BrowsePriceOptionsButtonFrame"].Button, "BOTTOMRIGHT")
    lockBtnFrame:SetScript("OnClick", function ()
        if ZL.opt.AuctionSortLock then
            local link = "手动排序功能已锁定，点击" .. format("|cff87CEEB|Hgarrmission:%s:%s|h[%s]|h|r", "AutoAuction", "unAuctionSortLock", "解锁")
            ZL.Print(link)
        end
    end)
    local quicklyCancelAuctionLabel = _G["AuctionFrameAuctions"]:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    quicklyCancelAuctionLabel:SetJustifyH("LEFT")
    quicklyCancelAuctionLabel:SetPoint("RIGHT", _G["AuctionsCancelAuctionButton"], "LEFT", -5, 0)
    quicklyCancelAuctionLabel:SetText("右键可快速取消拍卖")
    quicklyCancelAuctionLabel:SetTextColor(1, 0.82, 0)
    local soldMoneyAuctionLabel = _G["AuctionFrameAuctions"]:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    soldMoneyAuctionLabel:SetJustifyH("LEFT")
    soldMoneyAuctionLabel:SetPoint("BOTTOMLEFT", _G["AuctionsTabText"], "TOPLEFT", 0, 4)
    soldMoneyAuctionLabel:SetPoint("BOTTOMRIGHT", _G["AuctionsBidSort"], "TOPRIGHT", 0, 4)
    soldMoneyAuctionLabel:SetTextColor(1, 0.82, 0)
    soldMoneyAuctionLabel:SetJustifyH("CENTER")

    local historySearchKeyBtn = CreateFrame("Frame", nil, _G["BrowseName"])
    historySearchKeyBtn:SetSize(25, 25)
    historySearchKeyBtn:SetPoint("RIGHT", _G["BrowseName"], "RIGHT", 3, 0)
    local historySearchKeyTexture = historySearchKeyBtn:CreateTexture(nil, "ARTWORK")
    historySearchKeyTexture:SetAllPoints()
    historySearchKeyTexture:SetTexture([[Interface\ChatFrame\UI-ChatIcon-ScrollDown-Up]])
    historySearchKeyBtn:SetScript("OnMouseDown", function (self, button)
        if button == "LeftButton" then
            local menuList = BrowseNameOnMouseUpMenu()
            if #menuList > 0 then
                ZL.GUI:ToggleMenu(self, BrowseNameOnMouseUpMenu(), "cursor")
            end
            menuList = nil
        end
    end)
    local quickBuyItemsRightBtn = CreateFrame("CheckButton", nil, _G["IsUsableCheckButton"], "UICheckButtonTemplate")
    quickBuyItemsRightBtn:SetPoint("LEFT", _G["IsUsableCheckButton"], "RIGHT", _G["IsUsableCheckButton"].Text:GetWidth()+5, 0)
    quickBuyItemsRightBtn:SetSize(24, 24)
    quickBuyItemsRightBtn:SetChecked(ZL.opt.QuickBuyItems)
    quickBuyItemsRightBtn.Text:SetText("快速购买")
    quickBuyItemsRightBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "TOPLEFT", "BOTTOMRIGHT")
        GameTooltip:AddLine("Shift+右键：快速购买单组物品")
        GameTooltip:Show()
    end)
    quickBuyItemsRightBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    quickBuyItemsRightBtn:SetScript("OnClick", function (self)
        ZL.opt.QuickBuyItems = self:GetChecked()
        if ZL.opt.QuickBuyItems then
            ZL.Print("已开启|cff4080FFShift+右键|r：快速购买单组物品")
        end
    end)

    local ItemName = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    ItemName:SetSize(HookAuctionFrame:GetWidth() - 114, 20)
    ItemName:SetJustifyH("LEFT")
    ItemName:SetPoint("TOPLEFT", 5, -30)
    HookAuctionFrame.ItemName = ItemName

    local infoBtn = CreateFrame("Button", nil, HookAuctionFrame)
    HookAuctionFrame.infoBtn = infoBtn
    infoBtn:SetSize(30, 30)
    infoBtn:SetPoint("LEFT", ItemName, "RIGHT", 0, 0)
    infoBtn:SetNormalTexture(616343)
    infoBtn:SetHighlightTexture(616343)
    infoBtn:SetScript("OnClick", function ()
        ZL.Print("按Ctrl+V复制观看")
        ChatEdit_ActivateChat(ChatEdit_ChooseBoxForSend())
        ChatEdit_InsertLink("https://space.bilibili.com/476889394/lists")
        ChatEdit_ChooseBoxForSend():HighlightText()
    end)
    infoBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(HookAuctionFrame, "TOPLEFT", "TOPRIGHT")
        GameTooltip:AddLine("使用方法")
        GameTooltip:AddLine("1、禁用其他带有拍卖功能的插件，避免卡顿；", 1, 1, 1)
        GameTooltip:AddLine("2、拍卖行选择搜索界面；", 1, 1, 1)
        GameTooltip:AddLine("3、背包|CFFFFDE00Shift+左键|r选择需要拍卖的物品；", 1, 1, 1)
        GameTooltip:AddLine("4、点击搜索", 1, 1, 1)
        GameTooltip:AddLine("5、确认单价、堆叠数和组数无误", 1, 1, 1)
        GameTooltip:AddLine("6、点击|CFFFFDE00开始拍卖|r", 1, 1, 1)
        GameTooltip:AddLine(" ")
        GameTooltip:AddLine("注意事项")
        GameTooltip:AddLine("1、搜索文本只支持原生方法：", 1, 1, 1)
        GameTooltip:AddLine("如:手动输入、复制粘贴、Shift+左键点击物品\n或链接", 1, 1, 1)
        GameTooltip:AddLine("2、有些插件会污染原生Shift+左键点击功能，\n造成搜索卡顿", 1, 1, 1)
        GameTooltip:AddLine("3、只能手动点击搜索按钮，其他一切方法都\n会造成搜索卡顿", 1, 1, 1)
        GameTooltip:AddLine(" ")
        GameTooltip:AddLine("点击观看视频")
        GameTooltip:Show()
    end)
    infoBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    local lockBtn = CreateFrame("Button", nil, HookAuctionFrame)
    HookAuctionFrame.lockBtn = lockBtn
    lockBtn:SetSize(30, 30)
    lockBtn:SetPoint("LEFT", infoBtn, "RIGHT", -4, 0)
    local lockTexture = [[Interface\BUTTONS\LockButton-Locked-Up.blp]]
    local unlockTexture = [[Interface\BUTTONS\LockButton-Unlocked-Up.blp]]
    if ZL.opt.AuctionSortLock then
        lockBtn:SetNormalTexture(lockTexture)
        lockBtn:SetHighlightTexture(lockTexture)
        lockBtnFrame:Show()
    else
        lockBtn:SetNormalTexture(unlockTexture)
        lockBtn:SetHighlightTexture(unlockTexture)
        lockBtnFrame:Hide()
    end
    lockBtn:SetScript("OnClick", function ()
        if ZL.opt.AuctionSortLock then
            ZL.opt.AuctionSortLock = false
        else
            ZL.opt.AuctionSortLock = true
        end
        if ZL.opt.AuctionSortLock then
            Set_AuctionSort_Column()
            HookAuctionFrame.sortBtn.SpellHighlightAnim:Stop()
            HookAuctionFrame.sortBtn.SpellHighlightTexture:Hide()
            lockBtn:SetNormalTexture(lockTexture)
            lockBtn:SetHighlightTexture(lockTexture)
            lockBtnFrame:Show()
        else
            lockBtn:SetNormalTexture(unlockTexture)
            lockBtn:SetHighlightTexture(unlockTexture)
            lockBtnFrame:Hide()
        end
    end)
    lockBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "TOPLEFT", "BOTTOMRIGHT")
        if ZL.opt.AuctionSortLock then
            GameTooltip:AddLine("提示")
            GameTooltip:AddLine("左侧排序按钮已锁定，点击解锁", 1, 1, 1)
        else
            GameTooltip:AddLine("注意")
            GameTooltip:AddLine("排序未锁定，单价可能无法自动\n获取到最低价，点击锁定", 1, 0, 0)
        end
        GameTooltip:Show()
    end)
    lockBtn:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    local sortBtn = CreateButton(nil, HookAuctionFrame, "排序", "LEFT", lockBtn, "RIGHT", 2, 0, 45, 20, 14, true, 0)
    sortBtn:SetScript("OnClick", function ()
        Set_AuctionSort_Column()
        UpdateSortAnim()
    end)
    sortBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "TOPLEFT", "BOTTOMRIGHT")
        GameTooltip:AddLine("提示")
        GameTooltip:AddLine("按钮如发光，表示未按指定要求排序;", 1, 1, 1)
        GameTooltip:AddLine("单价可能无法自动获取到最低一口价;", 1, 1, 1)
        GameTooltip:AddLine("点击左侧按钮可锁定排序", 1, 1, 1)
        GameTooltip:Show()
    end)
    sortBtn.SpellHighlightTexture = sortBtn:CreateTexture("SpellHighlightTexture1", "OVERLAY", nil, 1)
    local SpellHighlightTexture = sortBtn.SpellHighlightTexture
    SpellHighlightTexture:SetAtlas("bags-newitem")
    SpellHighlightTexture:SetSize(60, 26)
    SpellHighlightTexture:SetPoint("CENTER")
    SpellHighlightTexture:SetBlendMode("ADD")
    SpellHighlightTexture:Hide()
    -- 创建 SpellHighlightAnim 动画组（循环播放）
    sortBtn.SpellHighlightAnim = SpellHighlightTexture:CreateAnimationGroup() --"SpellHighlightAnim"
    local SpellHighlightAnim = sortBtn.SpellHighlightAnim
    SpellHighlightAnim:SetLooping("REPEAT")
    -- 第一个 Alpha 动画（淡入）
    local alpha1 = SpellHighlightAnim:CreateAnimation("Alpha")
    alpha1:SetChildKey("SpellHighlightTexture")
    alpha1:SetSmoothing("OUT")
    alpha1:SetDuration(0.75)
    alpha1:SetOrder(1)
    alpha1:SetFromAlpha(0)
    alpha1:SetToAlpha(1)
    -- 第二个 Alpha 动画（淡出）
    local alpha2 = SpellHighlightAnim:CreateAnimation("Alpha")
    alpha2:SetChildKey("SpellHighlightTexture")
    alpha2:SetSmoothing("IN")
    alpha2:SetDuration(0.75)
    alpha2:SetOrder(2)
    alpha2:SetFromAlpha(1)
    alpha2:SetToAlpha(0)
    HookAuctionFrame.sortBtn = sortBtn
    local ItemButton = CreateFrame("Button", nil, HookAuctionFrame)
    ItemButton:SetSize(40, 40)
    ItemButton:SetPoint("TOPLEFT", ItemName, "BOTTOMLEFT", 0, -5)
    ItemButton:SetHighlightTexture([[Interface\Buttons\ButtonHilight-Square]])
    ItemButton:SetPushedTexture([[Interface\Buttons\UI-Quickslot-Depress]])
    ItemButton:SetScript("OnEnter", function (self)
        if self.itemID then
            GameTooltipShow(self, "TOPLEFT", "BOTTOMRIGHT")
            GameTooltip:SetItemByID(self.itemID)
            GameTooltip:Show()
        end
    end)
    ItemButton:SetScript("OnLeave", function ()
        GameTooltip:Hide()
    end)
    ItemButton:SetScript("OnClick", function (self)
        -- local infoType, itemId, itmeLink = GetCursorInfo()
        -- if infoType and infoType == "item" then
        --     print(itmeLink)
        -- end
        -- if self.itemID then
        --     _G["BrowseName"]:SetText(ItemName:GetText())
        -- end
    end)
    -- ItemButton:RegisterForDrag("LeftButton")
    -- ItemButton:EnableMouse(true)
    -- ItemButton:SetScript("OnReceiveDrag", function (self)
    --     print(11111)
    -- end)
    ItemButton.EmptySlot = ItemButton:CreateTexture(nil, "BACKGROUND")
    ItemButton.EmptySlot:SetTexture("Interface\\BUTTONS\\UI-Slot-Background")
    ItemButton.EmptySlot:SetDrawLayer("BACKGROUND", 1) -- 设置textureSubLevel
    ItemButton.EmptySlot:SetAllPoints()
    ItemButton.EmptySlot:SetTexCoord(0, 0.640625, 0, 0.640625)

    ItemButton.icon = ItemButton:CreateTexture(nil, "BACKGROUND")
    -- ItemButton.icon:SetTexture(132881)
    ItemButton.icon:SetDrawLayer("BACKGROUND", 2)
    ItemButton.icon:SetAllPoints()

    ItemButton.IconBorder = ItemButton:CreateTexture(nil, "BACKGROUND")
    ItemButton.IconBorder:SetTexture("Interface\\Common\\WhiteIconFrame")
    ItemButton.IconBorder:SetDrawLayer("BACKGROUND", 3)
    ItemButton.IconBorder:SetAllPoints()
    HookAuctionFrame.ItemButton = ItemButton

    local UnitPriceLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    UnitPriceLabel:SetText("单价")
    UnitPriceLabel:SetHeight(20)
    UnitPriceLabel:SetPoint("TOPLEFT", ItemButton, "TOPRIGHT", 5, 0)
    local UnitGoldPrice = MoneyInputEidtBox(HookAuctionFrame, 8, 90, "auctionhouse-icon-coin-gold")
    UnitGoldPrice:SetPoint("LEFT", UnitPriceLabel, "RIGHT", 8, 0)
    UnitGoldPrice:SetScript("OnTextChanged", function ()
        UpdateStackPrice()
    end)
    HookAuctionFrame.UnitGoldPrice = UnitGoldPrice
    local UnitSilverPrice = MoneyInputEidtBox(HookAuctionFrame, 2, 30, "auctionhouse-icon-coin-silver")
    UnitSilverPrice:SetPoint("LEFT", UnitGoldPrice, "RIGHT", 20, 0)
    UnitSilverPrice:SetScript("OnTextChanged", function ()
        UpdateStackPrice()
    end)
    HookAuctionFrame.UnitSilverPrice = UnitSilverPrice
    local UnitCopperPrice = MoneyInputEidtBox(HookAuctionFrame, 2, 30, "auctionhouse-icon-coin-copper")
    UnitCopperPrice:SetPoint("LEFT", UnitSilverPrice, "RIGHT", 20, 0)
    UnitCopperPrice:SetScript("OnTextChanged", function ()
        UpdateStackPrice()
    end)
    HookAuctionFrame.UnitCopperPrice = UnitCopperPrice

    local StackPriceLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    StackPriceLabel:SetText("堆叠")
    StackPriceLabel:SetHeight(20)
    StackPriceLabel:SetPoint("TOPLEFT", UnitPriceLabel, "BOTTOMLEFT", 0, -3)
    local StackGoldPrice = MoneyInputEidtBox(HookAuctionFrame, 8, 90, "auctionhouse-icon-coin-gold")
    StackGoldPrice:SetPoint("LEFT", StackPriceLabel, "RIGHT", 8, 0)
    StackGoldPrice:SetScript("OnTextChanged", function ()
        UpdateUnitPrice()
    end)
    HookAuctionFrame.StackGoldPrice = StackGoldPrice
    local StackSilverPrice = MoneyInputEidtBox(HookAuctionFrame, 2, 30, "auctionhouse-icon-coin-silver")
    StackSilverPrice:SetPoint("LEFT", StackGoldPrice, "RIGHT", 20, 0)
    StackSilverPrice:SetScript("OnTextChanged", function ()
        UpdateUnitPrice()
    end)
    HookAuctionFrame.StackSilverPrice = StackSilverPrice
    local StackCopperPrice = MoneyInputEidtBox(HookAuctionFrame, 2, 30, "auctionhouse-icon-coin-copper")
    StackCopperPrice:SetPoint("LEFT", StackSilverPrice, "RIGHT", 20, 0)
    StackCopperPrice:SetScript("OnTextChanged", function ()
        UpdateUnitPrice()
    end)
    HookAuctionFrame.StackCopperPrice = StackCopperPrice

    local StackCountLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    StackCountLabel:SetText("堆叠数")
    StackCountLabel:SetHeight(20)
    StackCountLabel:SetPoint("TOPLEFT", ItemButton, "BOTTOMLEFT", 0, -10)
    local StackCount = MoneyInputEidtBox(nil, nil, 42)
    StackCount:SetPoint("LEFT", StackCountLabel, "RIGHT", 8, 0)
    StackCount:SetNumber(1)
    StackCount:SetScript("OnTextChanged", function (self)
        local number = self:GetNumber()
        local name, _, _, _, _, _, _, stackCount, totalCount = GetAuctionSellItemInfo()
        if name then
            local maxStackCount = min(stackCount, totalCount)
            if number > maxStackCount then
                self:SetNumber(maxStackCount)
            elseif self:GetNumber() == 0 then
                self:SetNumber(1)
            end
            local oldNumStacks = HookAuctionFrame.NumStacks:GetNumber()
            local maxNumStacks = floor(totalCount/self:GetNumber())
            if oldNumStacks > maxNumStacks then
                HookAuctionFrame.NumStacks:SetNumber(maxNumStacks)
            end
            HookAuctionFrame.MaxNumStacksBtn:SetText("最大值:" .. totalCount)
        elseif self:GetNumber() == 0 then
            self:SetNumber(1)
        end
        UpdateStackPrice()
        UpdateDepositMoney()
    end)
    HookAuctionFrame.StackCount = StackCount
    local MaxStackCountBtn = CreateFrame("Button", nil, HookAuctionFrame)
    MaxStackCountBtn:SetSize(StackCountLabel:GetWidth() + 8 + StackCount:GetWidth(), 20)
    MaxStackCountBtn:SetNormalFontObject("GameFontNormalSmall")
    MaxStackCountBtn:SetHighlightFontObject("GameFontHighlightSmall")
    MaxStackCountBtn:SetHighlightTexture("UIPanelButtonHighlightTexture")
    MaxStackCountBtn:SetText("最大值:")
    do
        local fontString = MaxStackCountBtn:GetFontString()
        fontString:SetFont(GameFontNormal:GetFont(), 13)
        fontString:ClearAllPoints()
        fontString:SetPoint("LEFT")
        fontString:SetJustifyH("LEFT")
    end
    MaxStackCountBtn:SetPoint("TOPLEFT", StackCountLabel, "BOTTOMLEFT", 0, -3)
    MaxStackCountBtn:SetScript("OnClick", function ()
        local name, _, _, _, _, _, _, stackCount, totalCount = GetAuctionSellItemInfo()
        if name then
            StackCount:SetNumber(min(stackCount, totalCount))
        end
    end)
    HookAuctionFrame.MaxStackCountBtn = MaxStackCountBtn

    local NumStacksLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    NumStacksLabel:SetText("组数")
    NumStacksLabel:SetHeight(20)
    NumStacksLabel:SetPoint("LEFT", StackCount, "RIGHT", 10, 0)
    local NumStacks = MoneyInputEidtBox(nil, 3, 43)
    NumStacks:SetPoint("LEFT", NumStacksLabel, "RIGHT", 8, 0)
    NumStacks:SetNumber(1)
    NumStacks:SetScript("OnTextChanged", function (self)
        local number = self:GetNumber()
        local _stackCount = StackCount:GetNumber()
        local name, _, _, _, _, _, _, stackCount, totalCount = GetAuctionSellItemInfo()
        if name then
            if number > totalCount then
                self:SetNumber(totalCount)
            elseif number == 0 then
                self:SetNumber(1)
            end
            local maxStackCount = min(stackCount, totalCount, floor(totalCount/self:GetNumber()))
            if _stackCount > maxStackCount then
                StackCount:SetNumber(maxStackCount)
            end
            MaxStackCountBtn:SetText("最大值:" .. min(stackCount, totalCount))
        elseif self:GetNumber() == 0 then
            self:SetNumber(1)
        end
        UpdateDepositMoney()
    end)
    HookAuctionFrame.NumStacks = NumStacks
    local MaxNumStacksBtn = CreateFrame("Button", nil, HookAuctionFrame)
    MaxNumStacksBtn:SetSize(NumStacksLabel:GetWidth() + 8 + NumStacks:GetWidth(), 20)
    MaxNumStacksBtn:SetNormalFontObject("GameFontNormalSmall")
    MaxNumStacksBtn:SetHighlightFontObject("GameFontHighlightSmall")
    MaxNumStacksBtn:SetHighlightTexture("UIPanelButtonHighlightTexture")
    MaxNumStacksBtn:SetText("最大值:")
    do
        local fontString = MaxNumStacksBtn:GetFontString()
        fontString:SetFont(GameFontNormal:GetFont(), 13)
        fontString:ClearAllPoints()
        fontString:SetPoint("LEFT")
        fontString:SetJustifyH("LEFT")
    end
    MaxNumStacksBtn:SetPoint("TOPLEFT", NumStacksLabel, "BOTTOMLEFT", 0, -3)
    MaxNumStacksBtn:SetScript("OnClick", function ()
        local name, _, _, _, _, _, _, _, totalCount = GetAuctionSellItemInfo()
        if name then
            NumStacks:SetNumber(totalCount)
        end
    end)
    HookAuctionFrame.MaxNumStacksBtn = MaxNumStacksBtn
    HookAuctionFrame.StartAuctionBtn = CreateButton(nil, HookAuctionFrame, "开始拍卖", "TOPLEFT", NumStacks, "TOPRIGHT", 10, 0, 80, 43, 14, true, 0)
    local StartAuctionBtn = HookAuctionFrame.StartAuctionBtn
    StartAuctionBtn:SetScript("OnClick", function (self)
        local name, texture, _, quality, _, _, _, _, _, itemID = GetAuctionSellItemInfo()
        if texture then
            local stackSize = StackCount:GetNumber()
            local numStacks = NumStacks:GetNumber()
            local buyoutPrice = (UnitGoldPrice:GetNumber()*10000 + UnitSilverPrice:GetNumber()*100 + UnitCopperPrice:GetNumber()) * stackSize
            local minBid = buyoutPrice * ZL.opt.AuctionStartPrice/100
            local runTime = 1
            if stackSize > 0 and numStacks > 0 and buyoutPrice > 0 then
                if PostAuction(minBid, buyoutPrice, runTime, stackSize, numStacks, false) then
                    AuctionsCreateAuctionButton:ClearPost();
                else
                    AuctionsCreateAuctionButton:CachePendingPost(minBid, buyoutPrice, runTime, stackSize, numStacks, false);
                end
            end
        end
    end)

    local depositMoneyLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    depositMoneyLabel:SetSize(HookAuctionFrame:GetWidth() - 10, 18)
    depositMoneyLabel:SetFont(GameFontNormal:GetFont(), 13)
    depositMoneyLabel:SetJustifyH("LEFT")
    depositMoneyLabel:SetTextColor(1, 0.82, 0)
    depositMoneyLabel:SetPoint("TOPLEFT", MaxStackCountBtn, "BOTTOMLEFT", 0, -5)
    HookAuctionFrame.depositMoneyLabel = depositMoneyLabel

    local totalDepositMoneyLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK")
    totalDepositMoneyLabel:SetSize(HookAuctionFrame:GetWidth() - 10, 18)
    totalDepositMoneyLabel:SetFont(GameFontNormal:GetFont(), 13)
    totalDepositMoneyLabel:SetJustifyH("LEFT")
    totalDepositMoneyLabel:SetTextColor(1, 0.82, 0)
    totalDepositMoneyLabel:SetPoint("TOPLEFT", depositMoneyLabel, "BOTTOMLEFT", 0, -3)
    HookAuctionFrame.totalDepositMoneyLabel = totalDepositMoneyLabel

    do
        local line = HookAuctionFrame:CreateTexture(nil, "OVERLAY")
        line:SetColorTexture(1, 1, 1, 0.5)
        line:SetPoint("TOPLEFT", totalDepositMoneyLabel, "BOTTOMLEFT", -5, -5)
        line:SetPoint("TOPRIGHT", totalDepositMoneyLabel ,"BOTTOMRIGHT", 5, -7)
    end
    local startPriceLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    startPriceLabel:SetText("起始价格")
    startPriceLabel:SetHeight(20)
    startPriceLabel:SetPoint("TOPLEFT", totalDepositMoneyLabel, "BOTTOMLEFT", 0, -11)
    local startPrice = MoneyInputEidtBox(HookAuctionFrame, 3, 60)
    startPrice:SetPoint("LEFT", startPriceLabel, "RIGHT", 8, 0)
    startPrice:SetNumber(ZL.opt.AuctionStartPrice)
    startPrice:SetScript("OnTextChanged", function (self)
        if self:GetNumber() == 0 then self:SetNumber(1) end
        ZL.opt.AuctionStartPrice = self:GetNumber()
    end)
    local startPriceLabel2 = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    startPriceLabel2:SetText("%单价(一口价)")
    startPriceLabel2:SetHeight(20)
    startPriceLabel2:SetPoint("LEFT", startPrice, "RIGHT", 5, 0)

    local priceDownLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    priceDownLabel:SetText("压价金额")
    priceDownLabel:SetHeight(20)
    priceDownLabel:SetPoint("TOPLEFT", startPriceLabel, "BOTTOMLEFT", 0, -5)
    local glodPriceDown = MoneyInputEidtBox(HookAuctionFrame, 8, 90, "auctionhouse-icon-coin-gold")
    glodPriceDown:SetPoint("LEFT", priceDownLabel, "RIGHT", 8, 0)
    glodPriceDown:SetScript("OnTextChanged", function ()
        UpdateAuctionPriceDown()
    end)
    HookAuctionFrame.glodPriceDown = glodPriceDown
    local silverPriceDown = MoneyInputEidtBox(HookAuctionFrame, 2, 30, "auctionhouse-icon-coin-silver")
    silverPriceDown:SetPoint("LEFT", glodPriceDown, "RIGHT", 20, 0)
    silverPriceDown:SetScript("OnTextChanged", function ()
        UpdateAuctionPriceDown()
    end)
    HookAuctionFrame.silverPriceDown = silverPriceDown
    local copperPriceDown = MoneyInputEidtBox(HookAuctionFrame, 2, 30, "auctionhouse-icon-coin-copper")
    copperPriceDown:SetPoint("LEFT", silverPriceDown, "RIGHT", 20, 0)
    copperPriceDown:SetScript("OnTextChanged", function ()
        UpdateAuctionPriceDown()
    end)
    HookAuctionFrame.copperPriceDown = copperPriceDown
    SetAuctionPriceDownCpper(ZL.opt.AucitonPriceDown)

    local defaultStackCountLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    defaultStackCountLabel:SetText("默认堆叠")
    defaultStackCountLabel:SetHeight(20)
    defaultStackCountLabel:SetPoint("TOPLEFT", priceDownLabel, "BOTTOMLEFT", 0, -5)
    local defaultStackCount = MoneyInputEidtBox(nil, nil, 42)
    defaultStackCount:SetPoint("LEFT", defaultStackCountLabel, "RIGHT", 8, 0)
    defaultStackCount:SetNumber(ZL.opt.DefaultStackCount)
    defaultStackCount:SetScript("OnTextChanged", function (self)
        if self:GetNumber() == 0 then self:SetNumber(1) end
        ZL.opt.DefaultStackCount = self:GetNumber()
        UpdateStartAuctionStackInfo()
    end)
    if ZL.opt.defaultStackCountMax then
        defaultStackCount:Disable()
    else
        defaultStackCount:Enable()
    end
    local defaultStackCountMaxBtn = CreateFrame("CheckButton", nil, HookAuctionFrame, "UICheckButtonTemplate")
    defaultStackCountMaxBtn:SetPoint("LEFT", defaultStackCount, "RIGHT", 5, 0)
    defaultStackCountMaxBtn:SetSize(24, 24)
    defaultStackCountMaxBtn:SetChecked(ZL.opt.defaultStackCountMax)
    defaultStackCountMaxBtn.Text:SetText("最大值")
    defaultStackCountMaxBtn.Text:SetFontObject(GameFontHighlightSmall)
    defaultStackCountMaxBtn:SetScript("OnClick", function (self)
        ZL.opt.defaultStackCountMax = self:GetChecked()
        if ZL.opt.defaultStackCountMax then
            defaultStackCount:Disable()
        else
            defaultStackCount:Enable()
        end
        UpdateStartAuctionStackInfo()
    end)
    local defaultStackCountMaxFisrtBtn = CreateFrame("CheckButton", nil, HookAuctionFrame, "UICheckButtonTemplate")
    defaultStackCountMaxFisrtBtn:SetPoint("LEFT", defaultStackCountMaxBtn, "RIGHT", defaultStackCountMaxBtn.Text:GetWidth() + 5, 0)
    defaultStackCountMaxFisrtBtn:SetSize(24, 24)
    defaultStackCountMaxFisrtBtn:SetChecked(ZL.opt.defaultStackCountOrdefaultNumStacksFirst == 1 and true or false)
    defaultStackCountMaxFisrtBtn.Text:SetText("优先")
    defaultStackCountMaxFisrtBtn.Text:SetFontObject(GameFontHighlightSmall)
    defaultStackCountMaxFisrtBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "TOPLEFT", "BOTTOMRIGHT")
        GameTooltip:AddLine("堆叠数和组数冲突时：\n|cffffffff堆叠数|r优先")
        GameTooltip:Show()
    end)
    defaultStackCountMaxFisrtBtn:SetScript("OnLeave", function (self)
        GameTooltip:Hide()
    end)

    local defaultNumStacksLabel = HookAuctionFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    defaultNumStacksLabel:SetText("默认组数")
    defaultNumStacksLabel:SetHeight(20)
    defaultNumStacksLabel:SetPoint("TOPLEFT", defaultStackCountLabel, "BOTTOMLEFT", 0, -5)
    local defaultNumStacks = MoneyInputEidtBox(nil, nil, 42)
    defaultNumStacks:SetPoint("LEFT", defaultNumStacksLabel, "RIGHT", 8, 0)
    defaultNumStacks:SetNumber(ZL.opt.DefaultNumStacks)
    defaultNumStacks:SetScript("OnTextChanged", function (self)
        if self:GetNumber() == 0 then self:SetNumber(1) end
        ZL.opt.DefaultNumStacks = self:GetNumber()
        UpdateStartAuctionStackInfo()
    end)
    if ZL.opt.defaultNumStacksMax then
        defaultNumStacks:Disable()
    else
        defaultNumStacks:Enable()
    end
    local defaultNumStacksMaxBtn = CreateFrame("CheckButton", nil, HookAuctionFrame, "UICheckButtonTemplate")
    defaultNumStacksMaxBtn:SetPoint("LEFT", defaultNumStacks, "RIGHT", 5, 0)
    defaultNumStacksMaxBtn:SetSize(24, 24)
    defaultNumStacksMaxBtn:SetChecked(ZL.opt.defaultNumStacksMax)
    defaultNumStacksMaxBtn.Text:SetText("最大值")
    defaultNumStacksMaxBtn.Text:SetFontObject(GameFontHighlightSmall)
    defaultNumStacksMaxBtn:SetScript("OnClick", function (self)
        ZL.opt.defaultNumStacksMax= self:GetChecked()
        if ZL.opt.defaultNumStacksMax then
            defaultNumStacks:Disable()
        else
            defaultNumStacks:Enable()
        end
        UpdateStartAuctionStackInfo()
    end)

    local defaultNumStacksMaxFirstBtn = CreateFrame("CheckButton", nil, HookAuctionFrame, "UICheckButtonTemplate")
    defaultNumStacksMaxFirstBtn:SetPoint("LEFT", defaultNumStacksMaxBtn, "RIGHT", defaultNumStacksMaxBtn.Text:GetWidth() + 5, 0)
    defaultNumStacksMaxFirstBtn:SetSize(24, 24)
    defaultNumStacksMaxFirstBtn:SetChecked(ZL.opt.defaultStackCountOrdefaultNumStacksFirst == 2 and true or false)
    defaultNumStacksMaxFirstBtn.Text:SetText("优先")
    defaultNumStacksMaxFirstBtn.Text:SetFontObject(GameFontHighlightSmall)
    defaultNumStacksMaxFirstBtn:SetScript("OnClick", function (self)
        ZL.opt.defaultStackCountOrdefaultNumStacksFirst = 2
        self:SetChecked(true)
        defaultStackCountMaxFisrtBtn:SetChecked(false)
        UpdateStartAuctionStackInfo()
    end)
    defaultNumStacksMaxFirstBtn:SetScript("OnEnter", function (self)
        GameTooltipShow(self, "TOPLEFT", "BOTTOMRIGHT")
        GameTooltip:AddLine("堆叠数和组数冲突时：\n|cffffffff组数|r优先")
        GameTooltip:Show()
    end)
    defaultNumStacksMaxFirstBtn:SetScript("OnLeave", function (self)
        GameTooltip:Hide()
    end)
    defaultStackCountMaxFisrtBtn:SetScript("OnClick", function (self)
        ZL.opt.defaultStackCountOrdefaultNumStacksFirst = 1
        self:SetChecked(true)
        defaultNumStacksMaxFirstBtn:SetChecked(false)
        UpdateStartAuctionStackInfo()
    end)

    local itemSoldSoundBtn = CreateFrame("CheckButton", nil, HookAuctionFrame, "UICheckButtonTemplate")
    itemSoldSoundBtn:SetPoint("BOTTOMLEFT", 5, 5)
    itemSoldSoundBtn:SetSize(24, 24)
    itemSoldSoundBtn:SetChecked(ZL.opt.AuctionItemSold)
    itemSoldSoundBtn.Text:SetText("售出语音")
    itemSoldSoundBtn.Text:SetFontObject(GameFontHighlightSmall)
    itemSoldSoundBtn:SetScript("OnClick", function (self)
        ZL.opt.AuctionItemSold = self:GetChecked()
    end)

    function HookAuctionFrame.New_Auction_Update()
        local name, texture, _, quality, _, _, _, stackCount, totalCount, itemID = GetAuctionSellItemInfo()
        if name then
            ItemButton.icon:SetTexture(texture)
            ItemButton.itemID = itemID
            ItemName:SetText(name)
            local color = ITEM_QUALITY_COLORS[quality]
            ItemName:SetVertexColor(color.r, color.g, color.b)
            local maxStackCount = min(stackCount, totalCount)
            if StackCount:GetNumber() > maxStackCount then
                StackCount:SetNumber(maxStackCount)
            end
            if NumStacks:GetNumber() > totalCount then
                NumStacks:SetNumber(totalCount)
            end
            MaxStackCountBtn:SetText("最大值:" .. min(stackCount, totalCount))
            MaxNumStacksBtn:SetText("最大值:" .. totalCount)
        else
            ItemButton.icon:SetTexture()
            ItemButton.itemID = nil
            ItemName:SetText("")
            UnitGoldPrice:SetNumber(0)
            UnitSilverPrice:SetNumber(0)
            UnitCopperPrice:SetNumber(0)
            StackGoldPrice:SetNumber(0)
            StackSilverPrice:SetNumber(0)
            StackCopperPrice:SetNumber(0)
            -- StackCount:SetNumber(1)
            -- NumStacks:SetNumber(1)
            MaxStackCountBtn:SetText("最大值:")
            MaxNumStacksBtn:SetText("最大值:")
        end
    end
    local New_Auction_Update = HookAuctionFrame.New_Auction_Update
    HookAuctionFrame:RegisterEvent("NEW_AUCTION_UPDATE")
    HookAuctionFrame:RegisterEvent("AUCTION_ITEM_LIST_UPDATE")
    HookAuctionFrame:RegisterEvent("AUCTION_OWNED_LIST_UPDATE")
    HookAuctionFrame:RegisterEvent("CHAT_MSG_SYSTEM")
    HookAuctionFrame:RegisterEvent("UI_ERROR_MESSAGE")
    HookAuctionFrame:SetScript("OnEvent", function (self, event, ...)
        local arg = emptytable(...)
        if event == "NEW_AUCTION_UPDATE" then
            New_Auction_Update()
            UpdateDepositMoney()
            UpdateStartAuctionStackInfo()
        elseif event == "AUCTION_ITEM_LIST_UPDATE" then
            CancelAuctionBtn:Disable()
            if IsCanDo() then
                UpdateSortAnim()
            else
                UpdateSortAnim(true)
                return
            end
            local currentAuctions, totalAuctions = GetNumAuctionItems("list")
            if totalAuctions == 0 then
                return
            elseif totalAuctions > 50 then
                if AuctionFrameBrowse.page and AuctionFrameBrowse.page > 0 then
                    return
                end
            end
            local itemID = select(10, GetAuctionSellItemInfo())
            local isExist, count, buyoutPrice, owner, itemId, UnitPrice, itemLink, _
            local updateTime = time()
            self.historyPrice = self.historyPrice or {}
            for i = 1, currentAuctions do
                _, _, count, _, _, _, _, _, _, buyoutPrice, _, _, _, owner, _, _, itemId = GetAuctionItemInfo("list", 1)
                if itemId and buyoutPrice > 0 then
                    itemLink = GetAuctionItemLink("list", i)
                    UnitPrice = owner == UnitName("player") and floor(buyoutPrice/count) or (floor(buyoutPrice/count) - ZL.opt.AucitonPriceDown)
                    if itemID and itemId == itemID and not isExist then
                        isExist = true
                        UpdateStartAuctionCopperInfo(UnitPrice)
                        UpdateStartAuctionStackInfo()
                    end
                    if not self.historyPrice[itemLink] or self.historyPrice[itemLink][2] < updateTime then
                        self.historyPrice[itemLink] = {UnitPrice, updateTime}
                    end
                    if i == 1 then
                        --判断是否有更低价的拍卖物品
                        local ownerItemLink, ownercount, ownerbuyoutPrice, ownersaleStatus
                        for index = 1, GetNumAuctionItems("owner") do
                            ownerItemLink = GetAuctionItemLink("owner", index)
                            if ownerItemLink == itemLink then
                                _, _, ownercount, _, _, _, _, _, _, ownerbuyoutPrice, _, _, _, _, _, ownersaleStatus = GetAuctionItemInfo("owner", index)
                                if ownersaleStatus ~= 1 and ownerbuyoutPrice > 0 and ownerbuyoutPrice/ownercount > buyoutPrice/count then
                                    CancelAuctionBtn:Enable()
                                    break
                                end
                            end
                        end
                    end
                end
            end
        elseif event == "AUCTION_OWNED_LIST_UPDATE" then
            do
                local ownerNum = GetNumAuctionItems("owner")
                if ownerNum > 0 then
                    local minBid, bidAmount, _
                    local totalMoney, soldMoney = 0, 0
                    for index = 1, GetNumAuctionItems("owner") do
                        _, _, _, _, _, _, _, minBid, _, _, bidAmount = GetAuctionItemInfo("owner", index)
                        if bidAmount > 0 then
                            totalMoney = totalMoney + bidAmount
                            soldMoney = soldMoney + bidAmount
                        else
                            totalMoney = totalMoney + minBid
                        end
                    end
                    soldMoneyAuctionLabel:SetText(format("税后总金额：|cffffffff%s|r  税后收入：|cffffffff%s|r", GetMoneyString(totalMoney*0.95), GetMoneyString(soldMoney*0.95)))
                else
                    soldMoneyAuctionLabel:SetText("")
                end
            end
            if not IsCanDo() then return end
            CancelAuctionBtn:Disable()
            if AuctionFrameBrowse.page and AuctionFrameBrowse.page > 0 then
                return
            end
            local isExist
            local _, _, count, _, _, _, _, _, _, buyoutPrice, _, _, _, _, _, _, itemId = GetAuctionItemInfo("list", 1)
            if itemId and buyoutPrice > 0 then
                local itemLink = GetAuctionItemLink("list", 1)
                local ownerItemLink, ownercount, ownerbuyoutPrice, ownersaleStatus, _
                for index = 1, GetNumAuctionItems("owner") do
                    ownerItemLink = GetAuctionItemLink("owner", index)
                    if ownerItemLink == itemLink then
                        _, _, ownercount, _, _, _, _, _, _, ownerbuyoutPrice, _, _, _, _, _, ownersaleStatus = GetAuctionItemInfo("owner", index)
                        if ownersaleStatus ~= 1 and ownerbuyoutPrice > 0 and ownerbuyoutPrice/ownercount > buyoutPrice/count then
                            isExist = true
                            break
                        end
                    end
                end
            end
            if isExist then
                CancelAuctionBtn:Enable()
            end
        elseif event == "CHAT_MSG_SYSTEM" then
            if strmatch(arg[1], ERR_AUCTION_WON_S:gsub("%%s", "(.+)")) then
                local name = strmatch(arg[1], ERR_AUCTION_WON_S:gsub("%%s", "(.+)"))
                if BuyItemsInfoFrame:IsShown() and name == BuyItemsInfoFrame.itemName then
                    print(name, BuyItemsInfoFrame.itemName, BuyItemsInfoFrame.itemId, BuyItemsInfoFrame.currentCount, BuyItemsInfoFrame.currentBuyoutPrice)
                    AuctionBuyItemsHistory(BuyItemsInfoFrame.itemId, BuyItemsInfoFrame.currentCount, BuyItemsInfoFrame.currentBuyoutPrice)
                    BuyItemsInfoFrame.totalMoney = (BuyItemsInfoFrame.totalMoney or 0) + BuyItemsInfoFrame.currentBuyoutPrice
                    BuyItemsInfoFrame.totalCount = (BuyItemsInfoFrame.totalCount or 0) + BuyItemsInfoFrame.currentCount
                    BuyItemsInfoFrameBuyBtn:Enable()
                    BuyItemsInfoFrame_Update(BuyItemsInfoFrame)
                elseif name == auctionBuyItemInfo.itemName then
                    AuctionBuyItemsHistory(auctionBuyItemInfo.itemId, auctionBuyItemInfo.count, auctionBuyItemInfo.buyoutPrice)
                    print(name, auctionBuyItemInfo.itemName, auctionBuyItemInfo.itemId, auctionBuyItemInfo.count, auctionBuyItemInfo.buyoutPrice)
                end
            elseif ZL.opt.AuctionItemSold and strmatch(arg[1], ERR_AUCTION_SOLD_S:gsub("%%s", "(.+)")) then
                ZL.SpeakText(arg[1])
            end
        elseif event == "UI_ERROR_MESSAGE" then
            if BuyItemsInfoFrame:IsShown() and isAuctionBidFailed(arg[2]) then
                BuyItemsInfoFrameBuyBtn:Enable()
                BuyItemsInfoFrame.index = BuyItemsInfoFrame.index + 1
                BuyItemsInfoFrame_Update(BuyItemsInfoFrame)
            end
        end
        arg:Release()
    end)
end