<?php

namespace App\Controllers\Frontend\User;

use App\Controllers\FrontendBaseController;

class Discounts extends FrontendBaseController
{
    public $title = 'Discounts';
    public $menu = 'user';
    public $submenu = 'discounts';

    public function index()
    {
        if (is_role('Admin') || is_role('Manager') || is_role('Shopkeeper')) {
            return redirect()->to(landing_url());
        }
        $this->permissionCheck('user_discounts_view');

        $now = date('Y-m-d H:i:s');
        $discounts = db_connect()->table('rewarder_discounts d')
            ->select('d.*, sh.name as shop_name')
            ->join('rewarder_shops sh', 'sh.id = d.shop_id', 'left')
            ->where('d.status', 1)
            ->groupStart()
                ->where('d.start_at', null)->orWhere('d.start_at <=', $now)
            ->groupEnd()
            ->groupStart()
                ->where('d.end_at', null)->orWhere('d.end_at >=', $now)
            ->groupEnd()
            ->orderBy('d.id', 'desc')
            ->get()
            ->getResult();

        return view('frontend/user/discounts/list', compact('discounts'));
    }
}

