<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\{ProductCategory};
use Session;
use Illuminate\Support\Facades\File;

class ProductCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $q_name = $request->q_name;
        $product_category = ProductCategory::orderBy('id', 'asc');
        
        if (!empty($q_name)) {
            $product_category->where('name', 'like', '%'.$q_name.'%');
        }

        $product_category = $product_category->simplePaginate(15);

        return view('apps.admin.product_category.index')->with('product_category', $product_category)
                                                        ->with('q_name', $q_name);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function insert(Request $request)
    {
        $data = $request->all();

        $image = $request->file('image');

        $file_name = $image->getClientOriginalName();
        $file_size = $image->getSize();
        $file_type = $image->getClientOriginalExtension();

        // simpan gambar
        $destinationPath = 'img_assets/category_product';
        $image->move($destinationPath, $file_name);
        
        $data['image'] = $file_name;
        $data['is_discount'] = empty($data['is_discount']) ? 0 : 1;

        
        $product_category = ProductCategory::create($data);

        Session::flash('flash_message', 'Data telah ditambah');
        return redirect()->route('admin.product_category');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(ProductCategory $product_category)
    {   
        return view('apps.admin.product_category.edit')->with('product_category', $product_category);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $product_category = ProductCategory::findOrFail($request->id); 
        $data = $request->all();

         if ($request->hasFile('image')) { // apakah user mau edit gambarnya???
            // iya nih aku mau ganti gambar
            // hapus file
            File::delete('img_assets/product/'.$product_category->image);

            // buat file baru
            $image = $request->file('image');
            $file_name = $image->getClientOriginalName();
            $file_size = $image->getSize();
            $file_type = $image->getClientOriginalExtension();
    
            $destinationPath = 'img_assets/category_product';
            $image->move($destinationPath, $file_name);

            $data['image'] = $file_name;
        }

        $product_category->update($data);

        Session::flash('flash_message', 'Data telah diubah');
        return redirect()->route('admin.product_category');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request)
    {
        $product_category = ProductCategory::findOrFail($request->id);
        $product_category->delete();
        
        return redirect()->back();
    }
}
