trangshopeeaffiliate/app/Http/Controllers/Web/ZaloChannelController.php
2026-07-11 22:56:22 +07:00

94 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\ZaloChannel;
use Illuminate\Http\Request;
class ZaloChannelController extends Controller
{
public function index()
{
$channels = ZaloChannel::orderByDesc('is_allowed')->orderBy('name')->paginate(15, ['*'], 'channels_page');
$whitelistedIds = ZaloChannel::pluck('thread_id')->toArray();
$discoveredGroups = \App\Models\ZaloDiscoveredGroup::whereNotIn('group_id', $whitelistedIds)
->orderBy('name')
->paginate(15, ['*'], 'groups_page');
return view('zalo_channels.index', compact('channels', 'discoveredGroups'));
}
public function store(Request $request)
{
$request->validate([
'thread_id' => 'required|string|unique:zalo_channels,thread_id',
'name' => 'nullable|string',
]);
ZaloChannel::create([
'thread_id' => trim($request->input('thread_id')),
'name' => $request->input('name') ?: 'Kênh chưa đặt tên',
'is_allowed' => true,
]);
return back()->with('success', 'Đã thêm kênh Zalo vào danh sách cho phép.');
}
public function toggle($id)
{
$channel = ZaloChannel::findOrFail($id);
$channel->is_allowed = !$channel->is_allowed;
$channel->save();
$statusText = $channel->is_allowed ? 'cho phép' : 'chặn';
return back()->with('success', "Đã thay đổi trạng thái kênh sang {$statusText}.");
}
public function quickToggle(Request $request)
{
$request->validate([
'thread_id' => 'required|string',
'name' => 'nullable|string',
]);
$threadId = $request->input('thread_id');
$name = $request->input('name');
$channel = ZaloChannel::where('thread_id', $threadId)->first();
if ($channel) {
$channel->delete();
return back()->with('success', 'Đã xóa kênh khỏi danh sách cho phép (Bot sẽ KHÔNG phản hồi nhóm này nữa).');
} else {
ZaloChannel::create([
'thread_id' => $threadId,
'name' => $name ?: 'Kênh Zalo',
'is_allowed' => true,
]);
return back()->with('success', 'Đã thêm kênh vào danh sách cho phép (Bot SẼ phản hồi nhóm này).');
}
}
public function destroy($id)
{
$channel = ZaloChannel::findOrFail($id);
$channel->delete();
return back()->with('success', 'Đã xóa kênh Zalo khỏi danh sách.');
}
public function bulkDestroy(Request $request)
{
$request->validate([
'ids' => 'required|array',
'ids.*' => 'exists:zalo_channels,id'
]);
ZaloChannel::whereIn('id', $request->input('ids'))->delete();
return back()->with('success', 'Đã xóa các kênh Zalo được chọn thành công.');
}
}