<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Book;

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

    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $books = Book::orderBy('id', 'DESC')->paginate(25);
        return view('admin.book.index')->with(compact('books'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate(request(), [
            'title' => 'required',
            'slug' => 'required',
            'category_id' => 'required',
            'author_id' => 'required',
            'availability' => 'required',
            'price' => 'required',
            'description' => 'required'
        ]);

        Book::create([
            'title' => request()->get('title'),
            'slug' => request()->get('slug'),
            'category_id' => request()->get('category_id'),
            'author_id' => request()->get('author_id'),
            'availability' => request()->get('availability'),
            'price' => request()->get('price'),
            'publisher' => request()->get('publisher'),
            'country_of_publisher' => request()->get('country'),
            'rating' => request()->get('rating'),
            'isbn-10' => request()->get('isbn10'),
            'audience' => request()->get('aud'),
            'format' => request()->get('format'),
            'language' => request()->get('lang'),
            'description' => request()->get('description'),
            'book_upload' => request()->get('book_up'),
            'book_img' => request()->get('book_img')
        ]);

        return redirect()->to('/admin/book');
    }

    /**
     * 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)
    {
        $book = Book::find($id);
        return view('admin.book.edit')->with(compact('book'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $book = Book::find($id);

        $book->update([

            'title' => request()->get('title'),
            'slug' => request()->get('slug'),
            'category_id' => request()->get('category'),
            'author_id' => request()->get('author_id'),
            'availability' => request()->get('avail'),
            'price' => request()->get('price'),
            'publisher' => request()->get('publisher'),
            'country_of_publisher' => request()->get('cop'),
            'rating' => request()->get('rating'),
            'isbn-10' => request()->get('isbn10'),
            'audience' => request()->get('audience'),
            'format' => request()->get('format'),
            'language' => request()->get('lang'),
            'description' => request()->get('descp'),
            'book_upload' => request()->get('book_up'),
            'book_img' => request()->get('book_img')

        ]);

        return redirect()->to('admin/book');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $book = Book::find($id);
        $book->delete();

        return redirect()->to('admin/book');
    }
    public function status(Request $request, $id)
    {
        if ($request->ajax()) {
            $book = Book::find($id);
            $newStatus = ($book->status == 'DEACTIVE') ? 'ACTIVE' : 'DEACTIVE';
            $book->update([
                'status' => $newStatus
            ]);
            return $newStatus;
        }
    }

    public function statusActive(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Book::where('id', $value)->update(['status' => 'ACTIVE']);
            }
            $record = Book::find($request->statusAll);
            return $record;
        }
        return false;
    }

    public function statusDeactive(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Book::where('id', $value)->update(['status' => 'DEACTIVE']);
            }
            $record = Book::find($request->statusAll);
            return $record;
        }
        return false;
    }

    public function deleteAll(Request $request)
    {
        if ($request->ajax()) {
            foreach ($request->statusAll as $key => $value) {
                Book::where('id', $value)->delete();
            }
            $record = Book::find($request->statusAll);
            return $record;
        }
        return false;
    }
}
