<?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\Type;
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 view('backend.product.list', compact('products'));
    }
    public function create(){
        $this->checkpermission('product-create');
        $types = Type::all();
        $units = Unit::all();
        return view('backend.product.create', compact('types','units'));
    }
    public function store(Request $request){
        $this->validate($request, [
            'type_id' => 'required',
            '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',
        ]);
        $product = new Product();
        $product->type_id = $request->type_id;
        $product->productcategory_id = $request->category_id;
        $product->unit_id = $request->unit_id;
        $product->product_name = $request->product_name;
        $product->code = $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->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.list')->with('success', 'successfully created ');
        
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
        $this->checkpermission('product-edit');
        $product = Product::find($id);
        $types = Type::all();
        $units = Unit::all();
        return view('backend.product.edit', compact('product', 'types','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, [
            'type_id' => 'required',
            'category_id' => 'required',
            'unit_id' => 'required',
            'product_name' => 'required',
            'code' => 'required',
            'square_foot' => 'required',
            'supplier_price' => 'required',
            'sale_price' => 'required',
            're_order_label' => 'required',
        ]);
        $pc = Product::find($id);
        $pc->type_id = $request->type_id;
        $pc->productcategory_id = $request->category_id;
        $pc->unit_id = $request->unit_id;
        $pc->product_name = $request->product_name;
        $pc->code = $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->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');
            }
        }
    }

   
}
