<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Instruksi;

class BarcodeController extends Controller
{
    public function index()
    {
        $barcodes = Instruksi::all();
        return view("kelolabarcode", [
            'barcodes'    => $barcodes
        ]);
    }
    
    public function tambah(Request $request)
    {
        $this->validate($request, [
            'isiBarcode' => 'required',
            'poin' => 'required',
        ]);
        
        $barcode = new Instruksi();
        $barcode->poin = $request->poin;
        $barcode->instruksi = $request->isiBarcode;
        $barcode->save();
        return redirect('kelola/barcode');
    }
    
    public function delete($id)
    {
        Instruksi::findOrFail($id)->delete();
        return redirect('kelola/barcode');
    }
    
    public function edit($id)
    {
        return view('editbarcode', [
            'barcode'   => Instruksi::find($id),
        ]);
    }
    
    public function update($id,Request $request)
    {
        $barcode = Instruksi::find($id);
        $barcode->poin = $request->poin;
        $barcode->instruksi = $request->isiBarcode;
        $barcode->update();
        return redirect('kelola/barcode');
    }
    
    public function aktifkan($id)
    {
        $barcodes = Instruksi::all();
        foreach($barcodes as $bar)
        {
            $bar->aktif = 0;
            $bar->update();
        }
        
        $barcode = Instruksi::find($id);
        $barcode->aktif = 1;
        $barcode->update();
        return redirect('kelola/barcode');
    }
}
