<?php

namespace App\Http\Controllers;

use App\PromotionPackage;
use Illuminate\Http\Request;

class PromotionPackageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function __construct()
    {
        $this->middleware('auth'); //Ordinady
        $this->middleware('role:developer');
    }

    public function index()
    {
        $PromotionPackages=PromotionPackage::all();
        return view('PromotionPackage.index',compact('PromotionPackages'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('PromotionPackage.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request-> validate([
            'product_id'=>'required',
            'serviceType_id'=>'required',
            'qty'=>'numeric',
            'discount'=>'numeric',
            'narration'=>'max:255', 
        ]);

        PromotionPackage::create($request->all());
        return redirect(route('PromotionPackage.index'))->with('Message','Saved Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\PromotionPackage  $promotionPackage
     * @return \Illuminate\Http\Response
     */
    public function show(PromotionPackage $PromotionPackage)
    {
        // 
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\PromotionPackage  $promotionPackage
     * @return \Illuminate\Http\Response
     */
    public function edit(PromotionPackage $PromotionPackage)
    {
        
        return view('Promotionpackage.edit',compact('PromotionPackage')); 
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\PromotionPackage  $promotionPackage
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, PromotionPackage $PromotionPackage)
    {
        $request-> validate([
            'product_id'=>'required',
            'serviceType_id'=>'required',
            'qty'=>'numeric',
            'discount'=>'numeric',
            'narration'=>'max:255',        
            
        ]);

        $PromotionPackage->update([
            'product_id'=>$request->product_id,
            'serviceType_id'=>$request->serviceType_id,
            'qty'=>$request->qty,
            'discount'=>$request->discount,
            'narration'=>$request->narration,
        ]);
        return redirect(route('PromotionPackage.index'))->with('Message','Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\PromotionPackage  $promotionPackage
     * @return \Illuminate\Http\Response
     */
    public function destroy(PromotionPackage $PromotionPackage)
    {
        $PromotionPackage->delete();
        return redirect(route('PromotionPackage.index'))->with('Message','Deleted Successfully');
    }
}
