ESX = nil

Citizen.CreateThread(function()
	while ESX == nil do
		TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
		Citizen.Wait(0)
	end
end)

AddEventHandler('esx:playerLoaded', function (source)
	TriggerEvent("playerSpawned")
end)

TriggerEvent('es:addGroupCommand', 'clearinventory', 'superadmin', function(source, args, user)
    local source = source
    local xPlayer = ESX.GetPlayerFromId(args[1])
    if xPlayer ~= nil then
        exports.ghmattimysql:execute('DELETE FROM user_inventory2 WHERE name = @name AND item_id != @itemid AND item_id != @itemid2 AND item_id != @itemid3', {
            ['@name'] = xPlayer.identifier,
            ['@itemid'] = "motelkeys",
            ['@itemid2'] = "idcard",
            ['@itemid3'] = "driverlicense"
        })
    else
        TriggerClientEvent('mythic_notify:client:SendAlert', source, {type = 'error', text = 'Bu id oyunda yok'})
    end
end)

RegisterServerEvent('inventory:clearinventory')
AddEventHandler('inventory:clearinventory', function()
    local source = source
    local xPlayer = ESX.GetPlayerFromId(source)
    if xPlayer ~= nil then
        exports.ghmattimysql:execute('DELETE FROM user_inventory2 WHERE name = @name AND item_id != @itemid AND item_id != @itemid2 AND item_id != @itemid3', {
            ['@name'] = xPlayer.identifier,
            ['@itemid'] = "motelkeys",
            ['@itemid2'] = "idcard",
            ['@itemid3'] = "driverlicense"
        })
    end
end)

RegisterServerEvent('cash:remove')
AddEventHandler('cash:remove', function(source, cash)
    local src = source
	local xPlayer = ESX.GetPlayerFromId(src)
	if xPlayer.getMoney() >= cash then
	xPlayer.removeMoney(cash)
	TriggerClientEvent("banking:removeBalance", source, cash)
	end
end)



RegisterServerEvent('people-search')
AddEventHandler('people-search', function(target)
    local source = source
    local xPlayer = ESX.GetPlayerFromId(target)
    local identifier = xPlayer.identifier
	TriggerClientEvent("server-inventory-open", source, "1", identifier)

end)

RegisterServerEvent("server-item-quality-update")
AddEventHandler("server-item-quality-update", function(player, data)
	local quality = data.quality
	local slot = data.slot
	local itemid = data.item_id

    exports.ghmattimysql:execute("UPDATE user_inventory2 SET `quality` = @quality WHERE name = @name AND slot = @slot AND item_id = @item_id", {['quality'] = quality, ['name'] = player, ['slot'] = slot, ['item_id'] = itemid})
  
end)

RegisterServerEvent('police:SeizeCash')
AddEventHandler('police:SeizeCash', function(target)
    local src = source
    local xPlayer = ESX.GetPlayerFromId(src)
    local identifier = xPlayer.identifier
    local zPlayer = ESX.GetPlayerFromId(target)

    if not xPlayer.job.name == 'police' then
        print('steam:'..identifier..' User attempted sieze cash')
        return
    end
    TriggerClientEvent("banking:addBalance", src, zPlayer.getMoney())
    TriggerClientEvent("banking:removeBalance", target, zPlayer.getMoney())
    zPlayer.setMoney(0)
    TriggerClientEvent('notification', target, 'Your cash was seized',1)
    TriggerClientEvent('notification', src, 'Seized persons cash', 1)
 

end)



RegisterServerEvent('Stealtheybread')
AddEventHandler('Stealtheybread', function(target, amount)
    local src = source
    local xPlayer = ESX.GetPlayerFromId(src)
    local identifier = xPlayer.identifier
    local zPlayer = ESX.GetPlayerFromId(target)
    local zPlayerMoney = zPlayer.getMoney()

    if not xPlayer.job.name == 'police' then
        print('steam:'..identifier..' User attempted sieze cash')
        return
    end
    if zPlayerMoney > amount then        
        xPlayer.addMoney(amount)
        zPlayer.removeMoney(amount)
        TriggerClientEvent("banking:updateCash", src, xPlayer.getMoney(), true)
        TriggerClientEvent('notification', target, 'Your cash was robbed off you.', 1)
    else
        xPlayer.addMoney(zPlayerMoney)
        zPlayer.removeMoney(zPlayerMoney)
        TriggerClientEvent("banking:updateCash", src, xPlayer.getMoney(), true)
        TriggerClientEvent('notification', target, 'Your cash was robbed off you.', 1)
    end
end)