<?php

namespace App\Http\Controllers;

use App\DataTables\CouponsDataTable;
use App\Http\Requests\CreateCouponsRequest;
use App\Http\Requests\UpdateCouponsRequest;
use App\Http\Controllers\AppBaseController;
use App\Repositories\CouponsRepository;
use Illuminate\Http\Request;
use Flash;

class CouponsController extends AppBaseController
{
    /** @var CouponsRepository $couponsRepository*/
    private $couponsRepository;

    public function __construct(CouponsRepository $couponsRepo)
    {
        $this->couponsRepository = $couponsRepo;
    }

    /**
     * Display a listing of the Coupons.
     */
    public function index(CouponsDataTable $couponsDataTable)
    {
    return $couponsDataTable->render('coupons.index');
    }


    /**
     * Show the form for creating a new Coupons.
     */
    public function create()
    {
        return view('coupons.create');
    }

    /**
     * Store a newly created Coupons in storage.
     */
    public function store(CreateCouponsRequest $request)
    {
        $input = $request->all();
        if(!isset($input['status'])){
          $input['status']=null;
        }
        $input['balance'] = $input['discount_value'];
        $coupons = $this->couponsRepository->create($input);

        Flash::success('Voucher saved successfully.');

        return redirect(route('coupons.index'));
    }

    /**
     * Display the specified Coupons.
     */
    public function show($id)
    {
        $coupons = $this->couponsRepository->find($id);

        if (empty($coupons)) {
            Flash::error('Voucher not found');

            return redirect(route('coupons.index'));
        }

        return view('coupons.show')->with('coupons', $coupons);
    }

    /**
     * Show the form for editing the specified Coupons.
     */
    public function edit($id)
    {
        $coupons = $this->couponsRepository->find($id);

        if (empty($coupons)) {
            Flash::error('Voucher not found');

            return redirect(route('coupons.index'));
        }

        return view('coupons.edit')->with('coupons', $coupons);
    }

    /**
     * Update the specified Coupons in storage.
     */
    public function update($id, UpdateCouponsRequest $request)
    {
        $coupons = $this->couponsRepository->find($id);

        if (empty($coupons)) {
            Flash::error('Voucher not found');

            return redirect(route('coupons.index'));
        }
        $input = $request->all();
        if(!isset($input['status'])){
          $input['status']=null;
        }
        $input['balance'] = $input['discount_value'];
        $coupons = $this->couponsRepository->update($input, $id);

        Flash::success('Voucher updated successfully.');

        return redirect(route('coupons.index'));
    }

    /**
     * Remove the specified Coupons from storage.
     *
     * @throws \Exception
     */
    public function destroy($id)
    {
        $coupons = $this->couponsRepository->find($id);

        if (empty($coupons)) {
            Flash::error('Voucher not found');

            return redirect(route('coupons.index'));
        }

        $this->couponsRepository->delete($id);

        Flash::success('Voucher deleted successfully.');

        return redirect(route('coupons.index'));
    }
}
