<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\BankAgencyModel;
use DataTables;
use Gate;
class BankAgencyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct(){
        // $this->middleware("permission:Bank-List",['only'=>['index']]);
        // $this->middleware("permission:Bank-Create",['only'=>['store']]);
        // $this->middleware("permission:Bank-Edit",['only'=>['update']]);
        // $this->middleware("permission:Bank-Delete",['only'=>['destroy']]);
    }

    public function index(Request $request)
    {
        if($request->ajax()){
            $bank = BankAgencyModel::all();
            return DataTables::of($bank)->addIndexColumn()->editColumn('date',function($bank){
                return date('d-m-Y',strtotime($bank->created_at));
            })
            ->addColumn('action',function($bank){
                $btn_delete='';
                $btn_edit='';
                if(empty($bank->status)){
                    // if(Gate::check('Bank-Edit'))
                     $btn_edit = '<span class="btn btn-info btn-sm btn-edit fa fa-edit" data-id="'.$bank->id.'" data-name="'.$bank->name.'" data-des="'.$bank->des.'"></span>';
                    // if(Gate::check('Bank-Delete'))
                        $btn_delete = '<span class="btn btn-danger btn-sm btn-delete fa fa-trash" data-id="'.$bank->id.'"></span>';
                }

                return $btn_edit.$btn_delete;
            })->make(true);
        }
        return view('administrator.bank-agency.index');
    }

    /**
     * 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'
        ]);
        BankAgencyModel::create($request->all());
        return redirect()->back()->with('success','Bank 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)
    {
        //
    }

    /**
     * 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'
        ]);
        BankAgencyModel::where('id',$id)->update([
            'name'  => $request->name,
            'des'   => $request->des
        ]);
        return redirect()->back()->with('success','Bank Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        BankAgencyModel::destroy($id);
        return response()->json(['status'=>200,'success'=>true,'data'=>BankAgencyModel::all()]);
    }
}
