<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\CategoryModel;
use App\ProjectModel;
use App\ItemGroupModel;
use DataTables;
use Gate;
use DB;

class ItemGroupController extends Controller
{
    function __construct()
    {
         $this->middleware('permission:Item-Group-List');
         $this->middleware('permission:Item-Group-Create', ['only' => ['create','store']]);
         $this->middleware('permission:Item-Group-Edit', ['only' => ['edit','update']]);
         $this->middleware('permission:Item-Group-Delete', ['only' => ['destroy']]);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('administrator.item-group.index');
    }

    public function datatable_get_item_group()
    {  

        $itemgroup = ItemGroupModel::select(
                'nso007_item_group.item_group_code',
                'nso007_item_group.item_group_status',
                'nso007_item_group.id',
                'nso007_item_group.item_group_des',
                'nso007_item_group.item_group_name'
            )
            ->get();
        return  Datatables::of($itemgroup)
           ->addIndexColumn()
           ->editColumn('item_group_des', function ($itemgroup)
           {
               return substr($itemgroup->item_group_des, 0, 150)." ...";
           })
           ->addColumn('action',function($itemgroup){
               $delete='';
               $edits='';
               if(Gate::check('Item-Group-Edit')) {
                   $edits = '<a href="' . route('item-group.edit', $itemgroup->id) . '" class="button_success fa fa-edit" data-toggle="tooltip" data-placement="top" title="Edit"></a>';
               }
               if ($itemgroup->item_group_status == 0)
               if(Gate::check('Item-Group-Delete')) {
                   $delete = ' <a href="#" data-id = "' . $itemgroup->id . '" data-toggle="modal" data-target="#form-delete" class="button_danger fa fa-trash confirm_delete" data-toggle="tooltip" data-placement="top" title="Delete"></a>';
               }
                   return $edits.$delete;

           })
           ->make(true);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
//        $categories = CategoryModel::select(
//            DB::raw('CONCAT(category_code," | ",category_name) AS category_mixed'),'id')
//            ->pluck('category_mixed','id')
//            ->prepend('Please Select', '');
//
//        $projects = ProjectModel::when(empty(\Branch::branch_key()), function ($query) {
//            $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
//            })->select(
//            DB::raw('CONCAT(project_code," | ",project_name) AS project_mixed'),'id')
//            ->pluck('project_mixed','id')
//            ->prepend('Please Select', '');
        return view('administrator.item-group.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
//            'item_group_pro_id'      => 'required',
//            'item_group_cat_id'      => 'required',
            'item_group_code'      => 'required|unique:nso007_item_group,item_group_code',
            'item_group_name'      => 'required',

        ]);
        $item_group = new ItemGroupModel;
//        $item_group->item_group_pro_id = $request->item_group_pro_id;
//        $item_group->item_group_cat_id = $request->item_group_cat_id;
        $item_group->item_group_code = $request->item_group_code;
        $item_group->item_group_name = $request->item_group_name;
        $item_group->item_group_des = $request->item_group_des;
        $item_group->save();
        \LogActivity::addToLog('Item Group created successfully');
        return redirect()->route('item-group.index')
        ->with('success','Item Group created successfully');
    }

    /**
     * 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)
    {
        $item_group = ItemGroupModel::find($id);

//        $categories = CategoryModel::select(
//            DB::raw('CONCAT(category_code," | ",category_name) AS category_mixed'),'id')
//            ->pluck('category_mixed','id');
//
//        $projects = ProjectModel::select(
//            DB::raw('CONCAT(project_code," | ",project_name) AS project_mixed'),'id')
//            ->pluck('project_mixed','id');
//        $cat = CategoryModel::where('id',$item_group['item_group_cat_id'])->first();
//        $pro = ProjectModel::where('id',$item_group['item_group_pro_id'])->first();

        return view('administrator.item-group.edit',compact('item_group'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $item_group = ItemGroupModel::find($id);
        $this->validate($request, [
//            'item_group_pro_id'      => 'required',
//            'item_group_cat_id'      => 'required',
            'item_group_code'      => 'required',
            'item_group_name'      => 'required',
        
        ]);
//        $item_group->item_group_pro_id = $request->item_group_pro_id;
//        $item_group->item_group_cat_id = $request->item_group_cat_id;
        $item_group->item_group_code = $request->item_group_code;
        $item_group->item_group_name = $request->item_group_name;
        $item_group->item_group_des = $request->item_group_des;
        $item_group->update();
        \LogActivity::addToLog('Item Group updated successfully');
        return redirect()->route('item-group.index')
        ->with('success','Item Group updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $delete_item_group = ItemGroupModel::find($request->delete_id);
        $delete_item_group->delete();
        \LogActivity::addToLog('Delete Item Group successfully');
        return response()->json(['success' => true, 'message' => 'Delete Item Group successfully']);
    }
}
