<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Alert;
use App\PrivacyPolicy;

class PrivacyPolicyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,PrivacyPolicy $model)
    {
        //
        $policies = $model::latest()->limit(1)->get();
        return view('backend.privacy_policy',compact(['policies']));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, PrivacyPolicy $model)
    {
        //getting values
        $data_policy = [
            'title' => $request->title,
            'description' => $request->description,
            'published_date' => date('Y-m-d'),
        ]; 

        $data = $model::create($data_policy);

        if($data){
            return response()->json(["success"=>"Privacy Policy Created Successfully."]);
        }
        else{
            return response()->json(["errors"=>"Unable To Create Privacy Policy."]);
        }
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, PrivacyPolicy $model)
    {
        //getting the id first
        $id = $request->policy;

        $data_policy = [
            'title' => $request->title,
            'description' => $request->description,
            'published_date' => date('Y-m-d'),
        ]; 

        $data = $model::whereId($id)->update($data_policy);

        if($data){
            return response()->json(["success"=>"Privacy Policy updated Successfully."]);
        }
        else{
            return response()->json(["errors"=>"Unable to update Privacy Policy."]);
        }   
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
