<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\Models\Product;
use App\Models\Productcategory;
use App\Models\Unit;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->checkpermission('product-list');

        $show = $request->show;
        if($show != ''){
            $products = Product::latest()->paginate($show);
        }else{
            $products = Product::latest()->paginate(setting()->ddspinp);
        }
        //return $products;
        return view('backend.product.list', compact('products'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $this->checkpermission('product-create');
       $categories = Productcategory::orderBy('name','ASC')->where('status',1)->get();
        $units = Unit::all();
        return view('backend.product.create', compact('categories','units'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'category_id' => 'required',
            'unit_id' => 'required',
            'product_name' => 'required',
            'code' => 'required|unique:products',
            'square_foot' => 'required',
            'supplier_price' => 'required',
            'sale_price' => 'required',
            're_order_label' => 'required',
            'worker_wage' => 'required',
        ]);

        $product = new Product();
        $product->productcategory_id = $request->category_id;
        $product->unit_id = $request->unit_id;
        $product->product_name = $request->product_name;
        $product->code = str_slug($request->code);
        $product->square_foot = $request->square_foot;
        $product->supplier_price = $request->supplier_price;
        $product->sale_price = $request->sale_price;
        $product->re_order_label = $request->re_order_label;
        $product->worker_wage = $request->worker_wage;
        $product->status = $request->status;
        $product->created_by = Auth::user()->username;
        $product->created_at = date('Y-m-d H:i:s');
        $product->save();
        
        
        return redirect()->route('product.create')->with('success', 'successfully created ');
        
    }

    /**
     * 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)
    {
        $this->checkpermission('product-edit');
        $product = Product::find($id);
        $categories = Productcategory::all();
        $units = Unit::all();
        return view('backend.product.edit', compact('product', 'categories','units'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'category_id' => 'required',
            'unit_id' => 'required',
            'product_name' => 'required',
            'code' => 'required|unique:products,code,'.$id,
            'square_foot' => 'required',
            'supplier_price' => 'required',
            'sale_price' => 'required',
            're_order_label' => 'required',
            'worker_wage' => 'required',
        ]);
        $pc = Product::find($id);
        $pc->productcategory_id = $request->category_id;
        $pc->unit_id = $request->unit_id;
        $pc->product_name = $request->product_name;
        $pc->code = str_slug($request->code);
        $pc->square_foot = $request->square_foot;
        $pc->supplier_price = $request->supplier_price;
        $pc->sale_price = $request->sale_price;
        $pc->re_order_label = $request->re_order_label;
        $pc->worker_wage = $request->worker_wage;
        $pc->status = $request->status;
        $pc->modified_by = Auth::user()->username;
        $pc->updated_at = date('Y-m-d H:i:s');
        $pc->update();

        
        return redirect()->route('product.list')->with('success', 'successfully updated');
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $check = $this->checkpermission('product-delete');
        if ($check) {
            $this->checkpermission('product-delete');
        } else {
            $product = Product::find($id);
            $message = $product->delete();
            if ($message) {
                return redirect()->route('product.list')->with('success_message', 'successfully Deleted');
            } else {
                return redirect()->route('product.update')->with('error_message', 'failed to  Delete');
            }
        }
    }

   
}
