<?php

namespace App\Http\Controllers\Administrator\Construction;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\ConstructionModel\SubcontractModel;
use Auth;
use DataTables;
use App\ConstructionModel\CategoriesModel;
use App\ConstructionModel\BrandModel;
use App\ConstructionModel\ProductModel;
class ProductStockController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("administrator.construction.product.index");
    }

    public function get_data_product(Request $request)
    {
        $products = ProductModel::get();
        return  Datatables::of($products)
            ->addIndexColumn()
            ->editColumn("category",function($products)
            {
                return $products->category->name;
            })
            ->editColumn("brand",function($products)
            {
                return $products->brand->name;
            })
            ->editColumn("unit_price",function($products)
            {
                return "$".$products->current_price;
            })
            ->editColumn("qty",function($products)
            {
                return "0";
            })
            ->addColumn('action', function($products) {
                return "
                    <a title='Edit' href='".route('construction-product.edit',encrypt($products->id))."' class='btn btn-sm btn-primary' ><i class='fa fa-edit'></i></a>
                    <a title='view' href='".route('construction-product.show',encrypt($products->id))."' class='btn btn-sm btn-success' ><i class='fa fa-eye'></i></a>
                    ";
            })      
            ->make(true);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = CategoriesModel::pluck("name","id")->prepend("please select","");
        $brands = BrandModel::pluck("name","id")->prepend("please select","");
         return view("administrator.construction.product.create",compact('categories','brands'));
    }
    static public function save_product_model($objProduct,$input)
    {
        $objProduct->name = $input->name;
        $objProduct->category_id = $input->category_id;
        $objProduct->brand_id = $input->brand_id;
        $objProduct->current_price = $input->current_price;
        $objProduct->measurement = $input->measurement;
        $objProduct->user_id = Auth::user()->id;
        $objProduct->description = $input->description;
        $objProduct->save();
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'name'=>'required',
            'category_id'=>'required',
            'brand_id'=>'required',
            'measurement'=>'required'
        ]);
        DB::beginTransaction();
        try {
            $Product = new ProductModel;
            $this::save_product_model($Product,$request);
            DB::commit();
            \LogActivity::addToLog('Product Created successfully!');
            return redirect()->route('construction-product.index')->with("success","Create Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return \back()->with('warning',$e);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        dd("Underconstruct");
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $id = decrypt($id);
        $categories = CategoriesModel::pluck("name","id")->prepend("please select","");
        $brands = BrandModel::pluck("name","id")->prepend("please select","");
        $product = ProductModel::where("id",$id)->first();
        return view("administrator.construction.product.edit",compact('categories','brands','product'));
    }

    /**
     * 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,[
            'name'=>'required',
            'category_id'=>'required',
            'brand_id'=>'required',
            'measurement'=>'required'
        ]);
        DB::beginTransaction();
        try {
            $Product = ProductModel::find($id);
            $this::save_product_model($Product,$request);
            DB::commit();
            \LogActivity::addToLog('Product Update successfully!');
            return redirect()->route('construction-product.index')->with("success","Update Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return \back()->with('warning',$e);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
