<?php

namespace App\Http\Controllers\Inventory;

use App\Group;
use App\Product;
use App\Department;
use App\ProductUnit;
use App\Brand;

use App\GoodsIssueProduct;
use App\GoodsinProduct;
use App\RequisitionProduct;
use App\NewRequisitionProduct;
use App\PurchaseProduct;
use App\QuotationProduct;
use App\TempData;

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

class ProductController extends Controller
{
    
    public function boom(){
      GoodsinProduct::whereNotNull('deleted_at')->where('unit_id',13)->delete();
      dd('deleted');
    }
    
    
    public function index()
    {
        $items = Product::all();
        $departments = Department::all()->pluck('name','id');
        $groups = Group::all()->pluck('name','id');
        $units = ProductUnit::all()->pluck('name','id');
        $brands = Brand::all()->pluck('name','id');
        return view('inventory.item.add-item',compact('items','departments','groups','units','brands'));
    }

    public function store(Request $request){
        $this->validate($request,[
            'name'=>'required|unique:products',
            //'item_code'=>'required|unique:products',
            'product_unit_id'=>'required',
            // 'department_id'=>'required',
            'group_id'=>'required',
            'brand_id'=>'required',
            //'price'=>'required',
        ]);

        //dd($request->all());
        $item =  Product::query()->orderBy('created_at','DESC')->first();

        if($item !=null){
         $item_code = intval(explode("-",$item->item_code)[1])+1;
        }else{
          $item_code = 1001;
        }
        $request['item_code']=$request->group_id."-".$item_code;
        $request['name']=strtoupper($request['name']);
        Product::query()->create($request->all());
        session()->flash('success','Item information successfully stored');
        return redirect()->back()->with('success','Product Added Successfully');
    }

    public function convertItem(){
        $items = Product::get(['item_code','name','id']);
        return view('inventory.item.convert-item',compact('items'));
    }

    public function convertItemStore(Request $request){
        // return $request;
        foreach ($request->from_item_id as $key => $value) {
            if($value != $request->to_item_id){
                GoodsIssueProduct::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);
                GoodsinProduct::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);
                RequisitionProduct::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);
                NewRequisitionProduct::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);
                PurchaseProduct::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);
                QuotationProduct::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);
                TempData::where('product_id', $value)->update(['product_id'=>$request->to_item_id]);

                Product::find($value)->delete();
            }
            
        }
        return back()->with('success','Successfully converted !');
    }

    public function convertCategory(){
        $categories = Group::pluck('name','id');
        return view('inventory.item.convert-category',compact('categories'));
    }

    public function convertCategoryStore(Request $request){
        foreach ($request->from_category_id as $key => $value) {
            if($value != $request->to_category_id){
                Product::where('group_id', $value)->update(['group_id'=>$request->to_category_id]);
                Group::find($value)->delete();
            }
        }
        return back()->with('success','Successfully converted !');
    }

    public function edit($id){
        $items = Product::all();

        $departments = Department::all()->pluck('name','id');
        $groups = Group::all()->pluck('name','id');
        $units = ProductUnit::all()->pluck('name','id');
        $brands = Brand::all()->pluck('name','id');
        $item = Product::query()->findOrFail($id);

     //   dd($item->unit);

        return view('inventory.item.edit-item-inventory',compact('items','departments','groups','units','item','brands'));

    }

    public function update(Request $request,$id){
        Product::query()->findOrFail($id)->update($request->all());
        session()->flash('success','Item information successfully updated');
        return redirect()->route('inventory.item.add');
    }

    public function destroy(Request $request){
        Product::query()->findOrFail($request->id)->delete();
    }

    /* department wise Product List start */

    public function groupWiseProducts(Request $request){
        $items = ($request->group_id !=null ? Product::where('group_id',$request->group_id)->get() : Product::all() );
        return view('inventory.item.items-inventory', compact('items'));
        // $data = [];
        // $sl=0;
        //     foreach ($products as $key=>$product){
        //         $data['sl'][$key] = ++$sl;
        //         $data['name'][$key] = $product->name;
        //         $data['code'][$key] = $product->item_code;
        //         $data['group'][$key] = $product->group_id !=null ? $product->group->name : "N/A";
        //         $data['unit'][$key] = $product->product_unit_id !=null ? $product->unit->name : "N/A";
        //         $data['department'][$key] = $product->department_id !=null ? $product->department->name : "N/A";
        //         $data['price'][$key] = $product->price;
        //         $data['description'][$key] = $product->description;

        //         $data["edit"][$key] =  '<a href="'.route('inventory.item.edit',$product->id).'" class="btn btn-edit btn-success far fa-edit"></a>  <button type="button" data-id="'.$product->id.'" data-url="'.route('inventory.item.destroy').'" class="btn btn-danger fas fa-trash-alt erase"></button>';
        //     }
        //     return $data;

    }

    /* department wise Product List End */

    public function search_product_item(Request $request)
    {
        $item = request('searchTerm');
        $items =  Product::query()
            ->where('name','like',"%".$item."%")
            ->orWhere('item_code','like',"%".$item."%")
            ->take(100)
            ->get();
           return  $items;
    }
}
