<?php

namespace App\Http\Controllers\Administrator\Construction;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\ConstructionModel\CategoriesModel;
use Auth;
use DataTables;
class CategoriesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("administrator.construction.categories.index");
    }
    public function get_data_categories(Request $request)
    {
        $categories = CategoriesModel::get();
        return  Datatables::of($categories)
            ->addIndexColumn()
            ->addColumn('action', function($categories) {
                return "
                    <a title='Edit' href='#' data-id='".$categories->id."' class='btn btn-sm btn-primary' onclick='edit(this)'><i class='fa fa-edit'></i></a>
                ";
            })
            ->rawColumns(['action' => 'action'])      
            ->make(true);
    }

    public function create()
    {
        return view("administrator.construction.brand.create");
    }

    static public function Categories_Model_Save($objCategories,$input)
    {
        $objCategories->name = $input->name;
        $objCategories->user_id = Auth::user()->id;
        $objCategories->description = $input->description;
        $objCategories->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|unique:nso007_product_categories,name'
        ]);
        DB::beginTransaction();
        try {
            $categories = new CategoriesModel;
            $this::Categories_Model_Save($categories,$request);
            DB::commit();
            \LogActivity::addToLog('Categories Created successfully!');
            return redirect()->route('construction-categories.index')->with("success","Create Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return \back()->with('warning',$e);
        }
    }
    public function edit_data_categories($id)
    {
        return json_encode(CategoriesModel::find($id));
    }
    public function update_data_categories(Request $request)
    {
        DB::beginTransaction();
        try {
            $this->validate($request,[
                'name'=>'required|unique:nso007_product_categories,name,'.$request->id
            ]);
            $categories = CategoriesModel::find($request->id);
            $this::Categories_Model_Save($categories,$request);
            DB::commit();
            \LogActivity::addToLog('Categories update successfully!');
            return redirect()->route('construction-categories.index')->with("success","Update Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return redirect()->route('construction-categories.index')->with('warning',$e);
        }
    }
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

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