local _, ZLAUTO = ...

local ZL = ZLAUTO.mainFrame
local Prefix = ZL.Prefix
local tableSort = ZL.tableSort
local autoBid = ZL.autoBid
local GameTooltipShow = ZL.GameTooltipShow
local GetStartAndIncrementBid = ZL.GetStartAndIncrementBid
local GetCurrentRoster = ZL.GetCurrentRoster
local LoadItemDataByID = ZL.LoadItemDataByID

local cGetItemInfo, cGetItemIcon
if WOW_PROJECT_ID == 1 then
    cGetItemInfo = C_Item.GetItemInfo
    cGetItemIcon = C_Item.GetItemIconByID
else --WOW_PROJECT_ID == 11 then
    cGetItemInfo = GetItemInfo
    cGetItemIcon = GetItemIcon
end


--定义静态弹出对话框
--重置当前拍卖记录
StaticPopupDialogs["ZL_RESETCURRENT"] = {
	text = "%s",
	button1 = "重置",
	button2 = "取消",
    hasEditBox = true,
    EditBoxOnEnterPressed = function(self) self:GetParent().button1:Click() end,
    -- EditBoxOnTextChanged = function(self)
	-- 	if strlen(self:GetText()) > 0 then
	-- 		self:GetParent().button1:Enable()
	-- 	else
	-- 		self:GetParent().button1:Disable()
	-- 	end
	-- end,
    OnShow = function(self)
		-- self.button1:Disable()
        self.editBox:SetNumeric(false)
        self.editBox:SetAutoFocus(false)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self)
        if ZL_CURRENT["auctionlist"] and (#ZL_CURRENT["auctionlist"] > 0) then
            for num, data in ipairs(ZL_CURRENT["auctionlist"]) do
                local f = ZL.AuctionFrame2.entries[num]
                if data.state == "auctioning" then
                    ZL_CURRENT.auctionlist[num].state = "waiting"
                    ZL_CURRENT.auctionlist[num].countdownremaining = 0
                    ZL_CURRENT.auctionlist[num].isPaused = false
                    ZL_CURRENT.auctionlist[num].bidderlist = {}
                    f:SetValues(num)
                end
                f:Hide()
            end
        end
        local totalincome = 0
        ZL_CURRENT.butie = {}
        local _, RLBuTie = ZL.GetAveWages()
        if RLBuTie and RLBuTie > 0 then
            totalincome = totalincome - RLBuTie
            table.insert(ZL_CURRENT.butie, {player="", notes="RL补贴", amount=-RLBuTie, isTraded=true})
        end
        if ZL.opt.Tbutie and ZL.opt.Tbutie > 0 then
            totalincome = totalincome - ZL.opt.Tbutie
            table.insert(ZL_CURRENT.butie, {player="", notes="T补贴", amount=-ZL.opt.Tbutie, isTraded=true})
        end
        if ZL.opt.Nbutie and ZL.opt.Nbutie > 0 then
            totalincome = totalincome - ZL.opt.Nbutie
            table.insert(ZL_CURRENT.butie, {player="", notes="奶补贴", amount=-ZL.opt.Nbutie, isTraded=true})
        end
        if ZL.opt.DPSbutie and ZL.opt.DPSbutie > 0 then
            totalincome = totalincome - ZL.opt.DPSbutie
            table.insert(ZL_CURRENT.butie, {player="", notes="DPS补贴", amount=-ZL.opt.DPSbutie, isTraded=true})
        end
        if ZL.opt.FOODbutie and ZL.opt.FOODbutie > 0 then
            totalincome = totalincome - ZL.opt.FOODbutie
            table.insert(ZL_CURRENT.butie, {player="", notes="食物补贴", amount=-ZL.opt.FOODbutie, isTraded=true})
        end
        if ZL.opt.xiuXingbutie and ZL.opt.xiuXingbutie > 0 then
            totalincome = totalincome - ZL.opt.xiuXingbutie
            table.insert(ZL_CURRENT.butie, {player="", notes="修星补贴", amount=-ZL.opt.xiuXingbutie, isTraded=true})
        end
        if ZL.opt.ANMUbutie and ZL.opt.ANMUbutie > 0 then
            totalincome = totalincome - ZL.opt.ANMUbutie
            table.insert(ZL_CURRENT.butie, {player="", notes="工具人补贴", amount=-ZL.opt.ANMUbutie, isTraded=true})
        end
        if ZL_CURRENT.IncomeList and #ZL_CURRENT.IncomeList > 0 then
            for _, fdata in ipairs(ZL_CURRENT.IncomeList) do
                totalincome = totalincome + fdata.amount
            end
        end
        ZL_CURRENT.totalincome = totalincome
        if (not ZL_CURRENT.name) and (strlen(self.editBox:GetText()) == 0) then
            ZL_CURRENT.name = GetInstanceInfo()
        elseif strlen(self.editBox:GetText()) > 0 then
            ZL_CURRENT.name = self.editBox:GetText()
        end
        if not ZL_CURRENT.auctiondate then ZL_CURRENT.auctiondate = date("%Y-%m-%d %H:%M:%S", time()) end
        table.insert(ZL_HISTORY, ZL_CURRENT)
        ZL_CURRENT = {}
        ZL.AuctionFrame2:Update()
        ZL.AuctionRecordFrame.historyFrame:Update()
        ZL.AuctionRecordFrame.currentIncomeFrame:Update()
        ZL.AuctionRecordFrame.playerBalanceFrame:Update()
        ZL.TransmitAddonInfo_Pro()
	end,
	OnCancel = function(self)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}

--流拍物品申请重拍确认
StaticPopupDialogs["ZL_RETAKE"] = {
	text = "%s",
	button1 = "重拍",
	button2 = "取消",
    hasEditBox = false,
    OnShow = function(self)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
		if ZL.retake and #ZL.retake > 0 then
            local retake = ZL.retake
            local function linshi()
                if #retake > 0 then
                    local fdata = retake[1]
                    local AuctioningCount = ZL.GetAuctioningCount()
                    if (not ZL_CURRENT.globalConfig.allowMultipleAuctions) and AuctioningCount > 0 then
                        C_Timer.After(2, linshi)
                    else
                        local _, _index = ZL.FetchDataFromItemId(fdata.itemlink, fdata.itemindex)
                        if _index then
                            local f = ZL.AuctionFrame2.entries[_index]
                            f.MLframe.functionFrame.CancelBtn:Click()
                            tremove(ZL.retake, 1)
                            C_Timer.After(2, linshi)
                        end
                    end
                end
            end
            linshi()
        end
	end,
	OnCancel = function(self, data)
        SendChatMessage("因暂时不方便拍卖，请稍后重新提交重拍申请", "RAID")
        ZL.retake = {}
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    -- preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}

--拾取绑定物品加入到打包拍卖确认提醒
StaticPopupDialogs["ZL_ISPACKAGE"] = {
	text = "%s",
	button1 = "打包拍卖",
	button2 = "取消",
    hasEditBox = false,
    OnShow = function(self)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
		ZL.AddToPackage(data)
	end,
	OnCancel = function(self)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--保存当前团员名单信息警告按钮----已弃用
StaticPopupDialogs["ZL_SAVEGROUPMEMBERS"] = {
	text = "警告！警告！确定覆盖之前的团队名单信息么？",
	button1 = "确定",
    button2 = "取消",
    OnShow = function(self)
		if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
        ZL_CURRENT.GroupMembersList = ZL.GetCurrentRoster()
        ZL.AuctionRecordFrame.playerBalanceFrame:Update()
        print(Prefix("团员名单保存成功！"))
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--进入全新副本时提醒是否重置拍卖记录
StaticPopupDialogs["ZL_RESETCURRENTINFO"] = {
	text = "%s",
	button1 = "确定",
    button2 = "取消",
    OnShow = function(self)
		if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
        ZL.AuctionFrame2.MLFrame.reset:Click()
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--手动添加收支记录
StaticPopupDialogs["ZL_ADDINCOME"] = {
    text = "【|cffffd100%s|r】请输入必填项目",
    button1 = "确定",
    button2 = "取消",
    -- hasEditBox2 = true, -- 玩家名字
	-- hasEditBox3 = true, -- 收支金额
    -- hasEditBox4 = true, -- 名称
    OnShow = function(self, data)
		self.button1:Disable()
        -- self.text:SetJustifyH("LEFT")
        local xSize = 130 --self.editBox:GetWidth()
        local ySize = 30 --self.editBox:GetHeight()
        if not self.editBox2 then
			self.editBox2 = CreateFrame("EditBox", "StaticPopupDialogsEditBox2", self, "InputBoxTemplate")
			self.editBox2:SetSize(xSize, ySize)
			self.editBox2:SetPoint("TOP", self.text, "BOTTOM", 10, -10)
			self.editBox2:SetAutoFocus(false)
			self.editBox2:SetMaxLetters(150)
			self.editBox2.text = self.editBox2:CreateFontString(nil, "OVERLAY")
			self.editBox2.text:SetFontObject(GameFontWhite)
			self.editBox2.text:SetPoint("RIGHT", self.editBox2, "LEFT", -15, 0)
			self.editBox2.text:SetText("玩家名：")
            self.editBox2.text2 = self.editBox2:CreateFontString(nil, "OVERLAY")
			self.editBox2.text2:SetFontObject(GameFontWhite)
			self.editBox2.text2:SetPoint("LEFT", self.editBox2, "RIGHT", 5, 0)
		end
        if not self.editBox3 then
			self.editBox3 = CreateFrame("EditBox", "StaticPopupDialogsEditBox3", self, "InputBoxTemplate")
			self.editBox3:SetSize(xSize, ySize)
			self.editBox3:SetPoint("TOP", self.editBox2, "BOTTOM", 0, 8)
			self.editBox3:SetAutoFocus(false)
			self.editBox3:SetMaxLetters(9)
            self.editBox3:SetNumeric(true)
            self.editBox3:SetScript("OnEnter", function (self)
                GameTooltip:ClearAllPoints()
                GameTooltip:ClearLines()
                GameTooltip:SetOwner(self, "ANCHOR_NONE")
                GameTooltip:AddLine("输入大于0的数，最大9个字符！")
                GameTooltip:SetPoint("LEFT", self, "RIGHT")
                GameTooltip:Show()
            end)
            self.editBox3:SetScript("OnLeave", function (self) GameTooltip:Hide() end)
			self.editBox3.text = self.editBox3:CreateFontString(nil, "OVERLAY")
			self.editBox3.text:SetFontObject(GameFontWhite)
			self.editBox3.text:SetPoint("RIGHT", self.editBox3, "LEFT", -15, 0)
			self.editBox3.text:SetText("单　价：")
            self.editBox3.text2 = self.editBox3:CreateFontString(nil, "OVERLAY")
			self.editBox3.text2:SetFontObject(GameFontWhite)
			self.editBox3.text2:SetPoint("LEFT", self.editBox3, "RIGHT", 5, 0)
			self.editBox3.text2:SetText("(必填)")
		end
        if not self.editBox4 then
			self.editBox4 = CreateFrame("EditBox", "StaticPopupDialogsEditBox4", self, "InputBoxTemplate")
			self.editBox4:SetSize(xSize, ySize)
			self.editBox4:SetPoint("TOP", self.editBox3, "BOTTOM", 0, 8)
			self.editBox4:SetAutoFocus(false)
			self.editBox4:SetMaxLetters(150)
            self.editBox4:SetScript("OnLeave", function (self) GameTooltip:Hide() end)
            self.editBox4:SetScript("OnMouseUp", function (self)
                if (select(1, GetCursorInfo())) == "item" then
                    self:SetText(select(3, GetCursorInfo()))
                    ClearCursor()
                end
            end)
            self.editBox4:SetScript("OnEditFocusGained", function ()
                ZL.focusEditBox = self.editBox4
            end)
            self.editBox4:SetScript("OnEditFocusLost", function ()
                if ZL.focusEditBox == self.editBox4 then
                    ZL.focusEditBox = nil
                end
            end)
			self.editBox4.text = self.editBox4:CreateFontString(nil, "OVERLAY")
			self.editBox4.text:SetFontObject(GameFontWhite)
			self.editBox4.text:SetPoint("RIGHT", self.editBox4, "LEFT", -15, 0)
			self.editBox4.text:SetText("名　称：")
            self.editBox4.text2 = self.editBox4:CreateFontString(nil, "OVERLAY")
			self.editBox4.text2:SetFontObject(GameFontWhite)
			self.editBox4.text2:SetPoint("LEFT", self.editBox4, "RIGHT", 5, 0)
			self.editBox4.text2:SetText("(必填)")
		end
        if not self.editBox7 then
			self.editBox7 = CreateFrame("EditBox", "StaticPopupDialogseditBox7", self, "InputBoxTemplate")
			self.editBox7:SetSize(xSize, ySize)
			self.editBox7:SetPoint("TOP", self.editBox4, "BOTTOM", 0, 8)
			self.editBox7:SetAutoFocus(false)
            self.editBox7:SetNumeric(true)
            self.editBox7:SetNumber(1)
            self.editBox7:SetScript("OnEnter", function (self)
                GameTooltip:ClearAllPoints()
                GameTooltip:ClearLines()
                GameTooltip:SetOwner(self, "ANCHOR_NONE")
                GameTooltip:AddLine("输入大于0的数，默认为1")
                GameTooltip:SetPoint("LEFT", self, "RIGHT")
                GameTooltip:Show()
            end)
            self.editBox7:SetScript("OnLeave", function (self) GameTooltip:Hide() end)
			self.editBox7.text = self.editBox7:CreateFontString(nil, "OVERLAY")
			self.editBox7.text:SetFontObject(GameFontWhite)
			self.editBox7.text:SetPoint("RIGHT", self.editBox7, "LEFT", -15, 0)
			self.editBox7.text:SetText("数　量：")
            self.editBox7.text2 = self.editBox7:CreateFontString(nil, "OVERLAY")
			self.editBox7.text2:SetFontObject(GameFontWhite)
			self.editBox7.text2:SetPoint("LEFT", self.editBox7, "RIGHT", 5, 0)
			self.editBox7.text2:SetText("(必填)")
		end
        local function CreateMenu(self, level, menuList)
            local currentRoster = ZL.GetWagesGroupMembers()
            if not currentRoster then return end
            local info = UIDropDownMenu_CreateInfo()
            if (level or 1) == 1 then
                for i = 1, #currentRoster do
                    if #currentRoster[i] > 0 then
                        info.notCheckable = 1
                        info.text = "小队 " .. i
                        info.hasArrow = true
                        info.menuList = currentRoster[i]
                        UIDropDownMenu_AddButton(info, level)
                    end
                end
            else
                for _, name in ipairs(menuList) do
                    info.text = name
                    info.hasArrow = false
                    info.func = function()
                        self:GetParent().editBox2:SetText(name)
                        info.checked = true
                        CloseDropDownMenus()
                    end
                    UIDropDownMenu_AddButton(info, level)
                end
            end
        end
        if not self.dropDown then
            self.dropDown = CreateFrame("FRAME", "WPDemoDropDown", self, "UIDropDownMenuTemplate")
            self.dropDown:SetPoint("RIGHT", self.editBox2, "RIGHT", 17, -1)
            self.dropDown:SetScale(0.8)
            UIDropDownMenu_SetWidth(self.dropDown, 8)
            UIDropDownMenu_Initialize(self.dropDown, CreateMenu)
        end
        self.dropDown:Show()
        self.editBox2:SetScript("OnTextChanged", function (self)
            if data == "收入" or type(data) == "table" then
                if strlen(self:GetParent().editBox2:GetText()) > 0 and self:GetParent().editBox3:GetNumber() > 0 and
                    strlen(self:GetParent().editBox4:GetText()) > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            elseif data == "支出" then
                if self:GetParent().editBox3:GetNumber() > 0 and strlen(self:GetParent().editBox4:GetText()) > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            end
        end)
        self.editBox3:SetScript("OnTextChanged", function (self)
            if data == "收入" or type(data) == "table" then
                if strlen(self:GetParent().editBox2:GetText()) > 0 and self:GetParent().editBox3:GetNumber() > 0 and
                    strlen(self:GetParent().editBox4:GetText()) > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            elseif data == "支出" then
                if self:GetParent().editBox3:GetNumber() > 0 and strlen(self:GetParent().editBox4:GetText()) > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            end
        end)
        self.editBox4:SetScript("OnTextChanged", function (self)
            if data == "收入" or type(data) == "table" then
                if strlen(self:GetParent().editBox2:GetText()) > 0 and self:GetParent().editBox3:GetNumber() > 0 and
                    strlen(self:GetParent().editBox4:GetText()) > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            elseif data == "支出" then
                if self:GetParent().editBox3:GetNumber() > 0 and strlen(self:GetParent().editBox4:GetText()) > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            end
        end)
        self.editBox4:SetScript("OnEnter", function (self)
            GameTooltip:ClearAllPoints()
            GameTooltip:ClearLines()
            GameTooltip:SetOwner(self, "ANCHOR_NONE")
            if data == "收入" or type(data) == "table" then
                GameTooltip:AddLine("如：罚款，垃圾或其他收入")
            elseif data == "支出" then
                GameTooltip:AddLine("如：补贴，物资消耗支出等")
            end
            GameTooltip:AddLine("如需输入物品名，|cffffd100Shift+左键|r点击\n背包物品或拍卖图标", 1, 1, 1)
            GameTooltip:SetPoint("LEFT", self, "RIGHT")
            GameTooltip:Show()
        end)
        self.editBox2:SetText("")
		self.editBox3:SetText("")
        self.editBox4:SetText("")
        self.editBox2:Show()
        self.editBox3:Show()
        self.editBox4:Show()
        self.editBox7:Show()
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
		self:SetHeight(90 + self.editBox2:GetHeight() + self.editBox3:GetHeight() + self.editBox4:GetHeight() + self.editBox7:GetHeight())
        do
            local editboxList = {"StaticPopupDialogsEditBox2", "StaticPopupDialogsEditBox3", "StaticPopupDialogsEditBox4", "StaticPopupDialogseditBox7"}
            local function OnTabPressed(self)
                EditBox_HandleTabbing(self, editboxList)
            end
            self.editBox2:SetScript("OnTabPressed", OnTabPressed)
            self.editBox3:SetScript("OnTabPressed", OnTabPressed)
            self.editBox4:SetScript("OnTabPressed", OnTabPressed)
            self.editBox7:SetScript("OnTabPressed", OnTabPressed)
        end
        if data == "收入" then
            self.editBox2.text2:SetText("(必填)")
            self.editBox7.text2:SetText("(必填)")
        elseif data == "支出" then
            self.editBox2.text2:SetText("(可选)")
            self.editBox7.text2:SetText("(可选)")
        elseif type(data) == "table" then
            self.editBox2.text2:SetText("(必填)")
            self.editBox7.text2:SetText("(必填)")
            self.editBox3:SetNumber(data.startbidamount)
            self.editBox4:SetText(data.itemlink)
            self.editBox7:SetNumber(data.itemCount)
        end
    end,
    OnUpdate = function(self)
		-- 确保高度保持不变
		self:SetHeight(85 + self.editBox2:GetHeight() + self.editBox3:GetHeight() + self.editBox4:GetHeight())
	end,
    OnAccept = function (self, data)
        local itemList
        local tradeId
        local player = ZL.GetClassColorName(self.editBox2:GetText())
        local notes = self.editBox4:GetText()
        local itemId = tonumber(notes:match("|Hitem:(%d+):"))
        if itemId then
            tradeId = tostring(itemId) .. tostring(time())
        end
        local amount = 0
        local itemCount = self.editBox7:GetNumber()
        if ((not itemCount) or itemCount <= 0) then
            itemCount = 1
        end
        if not ZL_CURRENT.IncomeList then ZL_CURRENT.IncomeList = {} end
        if data == "收入" or data == "支出" then
            if data == "收入" then
                amount = self.editBox3:GetNumber() * itemCount
            elseif data == "支出" then
                amount = -self.editBox3:GetNumber() * itemCount
            end
            table.insert(ZL_CURRENT.IncomeList, {
                player = player,
                amount = amount,
                notes = notes,
                itemCount = itemCount,
                itemList = itemList,
                tradeId = tradeId,
            })
            if ZL.PlayerIsML(player) and amount > 0 then
                --ML自己拍到的物品直接记录到已支付
                if not ZL_CURRENT.playerPayToML then ZL_CURRENT.playerPayToML = {} end
                local oldPayToML = ZL_CURRENT.playerPayToML[player] and ZL_CURRENT.playerPayToML[player] or 0
                ZL_CURRENT.playerPayToML[player] = oldPayToML + amount
                ZL.AuctionRecordFrame.playerBalanceFrame:Update()
            end
            ZL.AuctionRecordFrame.currentIncomeFrame:Update()
            ZL.AuctionRecordFrame.playerBalanceFrame:Update()
            local fdata = {
                type = "添加收支",
                data = ZL_CURRENT.IncomeList,
            }
            MyAddon:TransmitAddonInfo("AUTOAUC SYNC11", fdata)
        elseif type(data) == "table" then
            data.bidderlist = {}
            table.insert(data.bidderlist, {
                bidder = player,
                bidamount = self.editBox3:GetNumber(),
                bidCount = itemCount,
                time = GetTime()*1000,
            })
            ZL.AuctionFrame2:CountDown(data.index)
        end


    end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--清除所有历史记录警告
StaticPopupDialogs["ZL_CLEARHISTORYWARNING"] = {
	text = "警告！警告！将清空所有历史拍卖记录，并且无法恢复！是否确定清空？",
	button1 = "确定",
    button2 = "取消",
    OnShow = function(self)
		if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
        for _, f in ipairs(ZL.AuctionRecordFrame.historyFrame.entries) do
            f:Hide()
        end
        ZL_HISTORY = {}
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--删除拍卖警告
StaticPopupDialogs["ZL_DELETEAUCTIONWARNING"] = {
	text = "%s",
	button1 = "确定",
    button2 = "取消",
    OnShow = function(self)
		if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
        local curAuctionList = ZL_CURRENT.auctionlist[data]
        local itemlink = curAuctionList.itemlink
        if curAuctionList.itemGUID and next(curAuctionList.itemGUID, nil) then
            for itemGUID, _ in pairs(curAuctionList.itemGUID) do
                if ZL_CURRENT.itemGUIDUsed then
                    ZL_CURRENT.itemGUIDUsed[itemGUID] = nil
                end
            end
        end
        if itemlink == "[打包拍卖]" then
            local itemList = curAuctionList["itemList"]
            if itemList and next(itemList, nil) then
                for _itemlink, _data in pairs(itemList) do
                    for _, __itemindex in ipairs (_data.itemindex) do
                        local _, __index = ZL.FetchDataFromItemId(_itemlink, __itemindex)
                        if __index then ZL_CURRENT.auctionlist[__index].isPackage = nil end
                    end
                end
            end
        end
        --将需要删除的拍卖物品发送给其他成员
        local fdata = {
            type = "删除拍卖物品",
            itemindex = curAuctionList.itemindex,
            itemlink = itemlink,
        }
        MyAddon:TransmitAddonInfo("AUTOAUC SYNC10", fdata)
        --ML开始删除自己的拍卖物品
        table.remove(ZL_CURRENT.auctionlist, data)
        ZL.AuctionFrame2:Update()
        print(Prefix("注意：已删除拍卖物品" .. itemlink))
        ZL.ScrollingMessageFrame:AddMessage("注意：已删除拍卖物品" .. itemlink)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--自动出价
StaticPopupDialogs["ZL_AUTOBID"] = {
    text = "输入%s的最大心里价位(g):",
	button1 = "开始",
	button2 = "取消",
    hasEditBox = true,
    EditBoxOnEnterPressed = function(self) self:GetParent().button1:Click() end,
    EditBoxOnTextChanged = function(self)
		if self:GetNumber() > 0 then
			self:GetParent().button1:Enable()
		else
			self:GetParent().button1:Disable()
		end
	end,
    OnShow = function(self)
		self.button1:Disable()
        self.editBox:SetNumeric(false)
        self.editBox:SetAutoFocus(false)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
		data.f.maxAutoBid = self.editBox:GetNumber() or 0
        if data.f.maxAutoBid > 0 then
            data.f.bidFrame.bidEditBox:Disable()
            data.f.bidFrame.bidBtn:SetText("停止")
            data.f.bidFrame.lgnoreBtn:Disable()
            local itemCount = ZL_CURRENT.auctionlist[data.index].itemCount
            local bidCount = data.f.bidFrame.bidCounts:GetNumber()
            bidCount = bidCount > 0 and bidCount or 1
            bidCount = bidCount > itemCount and itemCount or bidCount
            autoBid(data.index, data.f.maxAutoBid, bidCount)
            if data.f.autoBid then
                if (not data.f.autoBid:IsCancelled()) then
                    data.f.autoBid:Cancel()
                end
                data.f.autoBid = nil
            end
            data.f.autoBid = C_Timer.NewTicker(2, function (self)
                autoBid(data.index, data.f.maxAutoBid, bidCount)
            end)
        end
        self.editBox:ClearFocus()
	end,
	OnCancel = function(self, data)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--撤销指定玩家的出价
StaticPopupDialogs["ZL_REVOKEBID"] = {
    text = "输入你想撤销出价的玩家名字:",
	button1 = "确定",
	button2 = "取消",
    hasEditBox = true,
    EditBoxOnEnterPressed = function(self) self:ClearFocus();self:GetParent().button1:Click() end,
    EditBoxOnTextChanged = function(self)
		if strlen(self:GetText()) > 0 then
			self:GetParent().button1:Enable()
		else
			self:GetParent().button1:Disable()
		end
	end,
    OnShow = function(self)
		self.button1:Disable()
        self.editBox:SetNumeric(false)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
		local playerName = self.editBox:GetText()
        MyAddon:TransmitAddonInfo("AUTOAUC REVOKE", {itemId=data.itemId, itemindex=data.itemindex, playerName=playerName})
	end,
	OnCancel = function(self, data)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--增加玩家收入金额
StaticPopupDialogs["ZL_ADDTOPLAYER"] = {
    text = "输入你想|cffffd100增加|r[|cffffd100%s|r]的收入金额：",
	button1 = "确定",
	button2 = "取消",
    hasEditBox = true,
    EditBoxOnEnterPressed = function(self) self:GetParent().button1:Click() end,
    EditBoxOnTextChanged = function(self)
		if self:GetNumber() > 0 then
			self:GetParent().button1:Enable()
		else
			self:GetParent().button1:Disable()
		end
	end,
    OnShow = function(self)
		self.button1:Disable()
        self.editBox:SetNumeric(false)
        self.editBox:SetAutoFocus(false)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
		local addamount = self.editBox:GetNumber() or 0
        if (not ZL_CURRENT.playerPayToML) then ZL_CURRENT.playerPayToML = {} end
        ZL_CURRENT.playerPayToML[data]= (ZL_CURRENT.playerPayToML[data] or 0 )+ addamount
        ZL.AuctionRecordFrame.playerBalanceFrame:Update()
	end,
	OnCancel = function(self, data)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--扣除玩家收入金额
StaticPopupDialogs["ZL_REMFROMPLAYER"] = {
    text = "输入你想|cffffd100扣除|r[|cffffd100%s|r]的收入金额：",
	button1 = "确定",
	button2 = "取消",
    hasEditBox = true,
    EditBoxOnEnterPressed = function(self) self:GetParent().button1:Click() end,
    EditBoxOnTextChanged = function(self)
		if self:GetNumber() > 0 then
			self:GetParent().button1:Enable()
		else
			self:GetParent().button1:Disable()
		end
	end,
    OnShow = function(self)
		self.button1:Disable()
        self.editBox:SetNumeric(false)
        self.editBox:SetAutoFocus(false)
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.editBox5 then self.editBox5:Hide() end
        if self.editBox6 then self.editBox6:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        if self.dropDown then self.dropDown:Hide() end
    end,
	OnAccept = function(self, data)
		local addamount = self.editBox:GetNumber() or 0
        if (not ZL_CURRENT.playerPayToML) then ZL_CURRENT.playerPayToML = {} end
        ZL_CURRENT.playerPayToML[data] = (ZL_CURRENT.playerPayToML[data] or 0) - addamount
        ZL.AuctionRecordFrame.playerBalanceFrame:Update()
	end,
	OnCancel = function(self, data)
	end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}
--手动创建拍卖物品
StaticPopupDialogs["ZL_ADDAUCTIONITEM"] = {
    text = "请填写需要拍卖的物品ID和数量：",
    button1 = "确定",
    button2 = "取消",
    OnShow = function(self)
		self.button1:Disable()
        local xSize = 130 --self.editBox:GetWidth()
        local ySize = 30 --self.editBox:GetHeight()
         if not self.editBox5 then
			self.editBox5 = CreateFrame("EditBox", "StaticPopupDialogseditBox5", self, "InputBoxTemplate")
			self.editBox5:SetSize(xSize, ySize)
			self.editBox5:SetPoint("TOP", self.text, "BOTTOM", 10, -10)
			self.editBox5:SetAutoFocus(false)
            self.editBox5:SetNumeric(true)
            self.editBox5:SetScript("OnTextChanged", function (self)
                if self:GetParent().editBox5:GetNumber() > 0 and self:GetParent().editBox6:GetNumber() > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            end)
            self.editBox5:SetScript("OnMouseUp", function (self)
                if (select(1, GetCursorInfo())) == "item" then
                    self:SetText(select(2, GetCursorInfo()))
                    ClearCursor()
                end
            end)
            self.editBox5:SetScript("OnEnter", function (self)
                GameTooltipShow(self, "LEFT", "RIGHT")
                GameTooltip:AddLine("提示")
                GameTooltip:AddLine("鼠标点击物品，移动到这里，\n释放鼠标后可以自动获取到物品ID", 1, 1, 1)
                GameTooltip:Show()
            end)
            self.editBox5:SetScript("OnLeave", function ()
                GameTooltip:Hide()
            end)
			self.editBox5.text = self.editBox5:CreateFontString(nil, "OVERLAY")
			self.editBox5.text:SetFontObject(GameFontWhite)
			self.editBox5.text:SetPoint("RIGHT", self.editBox5, "LEFT", -15, 0)
			self.editBox5.text:SetText("物品ID：")
            self.editBox5.text2 = self.editBox5:CreateFontString(nil, "OVERLAY")
			self.editBox5.text2:SetFontObject(GameFontWhite)
			self.editBox5.text2:SetPoint("LEFT", self.editBox5, "RIGHT", 5, 0)
            self.editBox5.text2:SetText("(必填)")
		end
        if not self.editBox6 then
			self.editBox6 = CreateFrame("EditBox", "StaticPopupDialogseditBox6", self, "InputBoxTemplate")
			self.editBox6:SetSize(xSize, ySize)
			self.editBox6:SetPoint("TOP", self.editBox5, "BOTTOM", 0, 5)
			self.editBox6:SetAutoFocus(false)
            self.editBox6:SetNumeric(true)
            self.editBox6:SetScript("OnTextChanged", function (self)
                if self:GetParent().editBox5:GetNumber() > 0 and self:GetParent().editBox6:GetNumber() > 0 then
                    self:GetParent().button1:Enable()
                else
                    self:GetParent().button1:Disable()
                end
            end)
            -- self.editBox6:SetScript("OnEnter", function (self)
            --     GameTooltipShow(self, "LEFT", "RIGHT")
            --     GameTooltip:AddLine("注意")
            --     GameTooltip:AddLine("如是拍卖打包的物品，数量填|cffffd1001|r", 1, 1, 1)
            --     GameTooltip:Show()
            -- end)
            -- self.editBox6:SetScript("OnLeave", function (self) GameTooltip:Hide() end)
			self.editBox6.text = self.editBox6:CreateFontString(nil, "OVERLAY")
			self.editBox6.text:SetFontObject(GameFontWhite)
			self.editBox6.text:SetPoint("RIGHT", self.editBox6, "LEFT", -15, 0)
			self.editBox6.text:SetText("数　量：")
            self.editBox6.text2 = self.editBox6:CreateFontString(nil, "OVERLAY")
			self.editBox6.text2:SetFontObject(GameFontWhite)
			self.editBox6.text2:SetPoint("LEFT", self.editBox6, "RIGHT", 5, 0)
			self.editBox6.text2:SetText("(必填)")
		end
        self.editBox5:SetText("")
		self.editBox6:SetText("")
        if self.editBox2 then self.editBox2:Hide() end
        if self.editBox3 then self.editBox3:Hide() end
        if self.editBox4 then self.editBox4:Hide() end
        if self.dropDown then self.dropDown:Hide() end
        if self.editBox7 then self.editBox7:Hide() end
        self.editBox5:Show()
        self.editBox6:Show()
        if self.data then
            self.editBox5:SetNumber(self.data.itemId)
        end
		self:SetHeight(85 + self.editBox5:GetHeight() + self.editBox6:GetHeight() + self.editBox5:GetHeight())
        do
            local editboxList = {"StaticPopupDialogseditBox5", "StaticPopupDialogseditBox6"}
            local function OnTabPressed(self)
                EditBox_HandleTabbing(self, editboxList)
            end
            self.editBox5:SetScript("OnTabPressed", OnTabPressed)
            self.editBox6:SetScript("OnTabPressed", OnTabPressed)
        end
    end,
    OnUpdate = function(self)
		-- 确保高度保持不变
		self:SetHeight(85 + self.editBox5:GetHeight() + self.editBox6:GetHeight() + self.editBox5:GetHeight())
	end,
    OnAccept = function (self, data)
        local itemId = self.editBox5:GetNumber()
        local itemCount = self.editBox6:GetNumber()
        if data and data.itemGuid then
            ZL_CURRENT["itemGUIDUsed"][data.itemGuid] = true
        end
        local function func(itemlink)
            ZL.ManualCreateAuctionItem(itemlink, itemCount or 1, data and data.itemGuid)
            print(Prefix("已创建拍卖物品：" .. itemlink))
        end
        LoadItemDataByID(itemId, func, self.editBox5)
     end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = false,
    preferredIndex = 3,  -- 避免与其他弹出窗口冲突
}