<?php

namespace App\Http\Controllers\Administrator\Construction;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DataTables;
use DB;
use Auth;
use Illuminate\Support\Facades\Crypt;
use App\ProjectModel;
use Gate;

class WareHouseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct(){
        $this->middleware('permission:Construction-Warehouse-List',['only'=>['index','get_data_index']]);
        $this->middleware('permission:Construction-Warehouse-Create',['only'=>['store']]);
        $this->middleware('permission:Construction-Warehouse-Edit',['only'=>['warehouse_update']]);
    }
    public function index()
    {
        $projects = ProjectModel::when(empty(\Branch::branch_key()), function ($query) {
            $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
        })
        ->pluck("project_name","id")
        ->prepend("Please Select","");
        return view("administrator.construction.warehouse.index",compact('projects'));
    }
    public function get_data_index(Request $request)
    {
        $warehouse_name = $request->warehouse_name;
        $project = $request->project;
        $data = DB::table('nso007_warehouse_construction')->join("nso007_project_stock","nso007_project_stock.id","nso007_warehouse_construction.project_id")
        ->select("nso007_warehouse_construction.*","nso007_project_stock.project_name")
        ->when(empty(\Branch::branch_key()), function ($query) {
            $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
        })
        ->when(request('warehouse_name'),function($query) use ($warehouse_name){
            $query->where("name","LIKE","%".$warehouse_name."%");
        })
        ->when(request('project'),function($query) use ($project){
            $query->where("nso007_warehouse_construction.project_id",$project);
        })
        ->get();
        return  Datatables::of($data)
            ->setRowId('id')
            ->addIndexColumn()
            ->addColumn('action', function($data) {
                $button ="";
                if(Gate::check('Construction-Warehouse-Edit'))
                    $button .= " <a title='Edit' onclick='edit($data->id)' class='btn btn-sm btn-primary' ><i class='fa fa-edit'></i></a> ";
                return $button;
            })      
            ->make(true);
    }
    public function get_for_edit($id)
    {
        $data = array(
            "projects"=>ProjectModel::when(empty(\Branch::branch_key()), function ($query) {
                $query->where('nso007_project_stock.branch_id', \Branch::branch_id());
            })->get(),
            "warehouse"=>DB::table('nso007_warehouse_construction')->where("id",$id)->first()
        );
        return json_encode($data);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function 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,[
            "name"=>'required',
            "project_id"=>'required'
        ]);
        DB::beginTransaction();
        try {
            DB::table('nso007_warehouse_construction')->insert(['name'=>$request->name,'project_id'=>$request->project_id,'description'=>$request->description]);
            DB::commit();
            \LogActivity::addToLog('Warehouse Created successfully!');
            return redirect()->route('warehouse-construction.index')->with("success","Create Successfully!");
        } catch (Exception $e) {
             DB::rollback();
             return \back()->with('warning',$e);
        }
    }
    public function warehouse_update(Request $request)
    {
        $this->validate($request,[
            "name"=>'required',
            "project_id"=>'required'
        ]);
        DB::beginTransaction();
        try {
            DB::table('nso007_warehouse_construction')->where('id',$request->id)->update(['name'=>$request->name,'project_id'=>$request->project_id,'description'=>$request->description]);
            DB::commit();
            \LogActivity::addToLog('Warehouse Created successfully!');
            return redirect()->route('warehouse-construction.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)
    {
        //
    }

    /**
     * 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)
    {
        //
    }
}
