<?php

namespace App\Http\Controllers\Administrator\Construction;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ProvinceModel;
use DB;
use App\BranchModel;
use App\ConstuctionModel\SuppliersModel;
use Auth;
use DataTables;
use Gate;
class SupplierController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct(){
        $this->middleware('permission:Construction-Supplier-List',['only'=>['index','get_data_supplier']]);
        $this->middleware('permission:Construction-Supplier-Create',['only'=>['create','store']]);
        $this->middleware('permission:Construction-Supplier-Edit',['only'=>['edit','update']]);
    }
    public function index()
    {
        // $supplier = DB::table("nso007_construction_suppliers")->select("name")->get();
        // $user = DB::table('users')->select('user_firstname')->get();
        // dd($supplier->union($user));
        $provinces = ProvinceModel::select(
            DB::raw('CONCAT(province_id," ",province_en," ",province_km) AS province_mixed'),'province_id')
            ->pluck('province_mixed','province_id')
            ->prepend('Please Select', '');
        return view("administrator.construction.supplier.index",compact('provinces'));
    }

    public function get_data_supplier(Request $request)
    {
        $name = $request->name;
        $mail = $request->mail;
        $phone = $request->phone;
        $address = $request->address;
        $supplier = SuppliersModel::when(request('name'),function($query) use($name){
             $query->where("name","LIKE","%".$name."%");
        })
        ->when(request('phone'),function($query) use ($phone){
             $query->where("phone",$phone);
        })
        ->when(request('mail'),function($query) use ($mail){
             $query->where("email","LIKE","%".$mail."%");
        })
        ->when(request('address'),function($query) use ($address){
             $query->where("address","LIKE","%".$address."%");
        })
        ->get();
        return  Datatables::of($supplier)
            ->addIndexColumn()
            ->addColumn('action', function($supplier) {
                $button = '';
                if(Gate::check('Construction-Supplier-Edit'))
                    $button = "<a title='Edit' href='".route('construction-suppliers.edit',encrypt($supplier->id))."' class='btn btn-sm btn-primary' ><i class='fa fa-edit'></i></a>";
                return $button;
            })      
            ->make(true);
    }

    static public function Supplier_Model_Save($objSupplier,$input)
    {
        $objSupplier->name = $input->name;
        $objSupplier->phone = $input->phone;
        $objSupplier->email = $input->email;
        $objSupplier->address = $input->address;
        $objSupplier->description = $input->description;
        $objSupplier->user_id = Auth::user()->id;
        $objSupplier->save();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $provinces = ProvinceModel::select(
            DB::raw('CONCAT(province_id," ",province_en," ",province_km) AS province_mixed'),'province_id')
            ->pluck('province_mixed','province_id')
            ->prepend('Please Select', '');
        return view("administrator.construction.supplier.create",compact('provinces'));
    }

    /**
     * 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',
            'phone'=>'required|unique:nso007_construction_suppliers,phone',
        ]);
        DB::beginTransaction();
        try {
            $SuppliersModel = new SuppliersModel;
            $this::Supplier_Model_Save($SuppliersModel,$request);
            DB::commit();
            \LogActivity::addToLog('Supplier Created successfully!');
            return redirect()->route('construction-suppliers.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)
    {
        $id = decrypt($id);
        $supplier = SuppliersModel::find($id);
        return view('administrator.construction.supplier.edit',compact('supplier'));
    }

    /**
     * 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',
            'phone'=>'required|unique:nso007_construction_suppliers,phone,'.$id,
        ]);
        DB::beginTransaction();
        try {
            $SuppliersModel = SuppliersModel::find($id);
            $this::Supplier_Model_Save($SuppliersModel,$request);
            DB::commit();
            \LogActivity::addToLog('Supplier Update successfully!');
            return redirect()->route('construction-suppliers.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)
    {
        //
    }
}
