<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\DonerAndPartner;
use Auth;

class DonerPartnerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = DonerAndPartner::orderBy('id','desc')->paginate(20);
        return view('back-end.dp.index', compact('data'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            "name" => "required|max:255",
            "email" => "required",
            "address" => "required",
        ]);
        $data = new DonerAndPartner();
        $data->user_id = Auth::user()->id;
        $data->type_id = $request->type_id;
        $data->name = $request->name;
        $data->email = $request->email;
        $data->amount = $request->amount;
        $data->web_url = $request->web_url;
        $data->instegram_url = $request->instegram_url;
        $data->facebook_url = $request->facebook_url;
        $data->x_url = $request->x_url;
        $data->address = $request->address;
        $data->phone = $request->phone;

        $data->details = $request->bio;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'dp_images-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('dp_images/' . $date, $filename, 'public');
        } else {
            $path = "image/noimage.jpg";
        }
        $data->image = $path;
        $is_saved = $data->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('dp.index');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
         $data=DonerAndPartner::find($id);
        return view('back-end.dp.show',compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = DonerAndPartner::find($id);
        return view('back-end.dp.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            "name" => "required|max:255",
            "email" => "required",
            "address" => "required",
        ]);
        $data = DonerAndPartner::find($id);
        $data->user_id = Auth::user()->id;
        $data->type_id = $request->type_id;
        $data->name = $request->name;
        $data->email = $request->email;
        $data->web_url = $request->web_url;
        $data->instegram_url = $request->instegram_url;
        $data->facebook_url = $request->facebook_url;
        $data->x_url = $request->x_url;
        $data->amount = $request->amount;
        $data->address = $request->address;
        $data->phone = $request->phone;
        $data->details = $request->bio;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'dp_images-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('dp_images/' . $date, $filename, 'public');
        } else {
            $path = $data->image;
        }
        $data->image = $path;
        $is_saved = $data->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('dp.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $data = DonerAndPartner::find($id)->delete();
        return redirect()->route('dp.index');
    }

    public function status($id)
    {
        $data = DonerAndPartner::find($id);
        $message = "";
        if ($data->status == 1) {
            DonerAndPartner::where('id', $id)->update([
                'status' => 0,
            ]);
            $message = "Status was Un-Published";
        }
        if ($data->status == 0) {
            DonerAndPartner::where('id', $id)->update([
                'status' => 1,
            ]);
            $message = "Status was Published";
        }
        session()->flash("msg", $message);
        return back();
    }
}
