<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use DataTables;
use App\AgencyLevelModel;
use DB;
use App\CommissionConditionModel;
class AgencyLevelController extends Controller

{
  
    function __construct()
    {
        $this->middleware('permission:Agency-Level-List');
        $this->middleware('permission:Agency-Level-Edit', ['only' => ['update_agency_level']]);
    }
    public function index(){
        $data = AgencyLevelModel::all();
        $condition = CommissionConditionModel::select('condition','id')->from('nso007_commission_condition')
            ->pluck('condition', 'id')
            ->prepend('Please Select', '');
        $commission = CommissionConditionModel::select('commission')->from('nso007_commission_condition')
            ->pluck('commission', 'commission')
            ->prepend('Please Select', '');
        return view('administrator.agency-level.index',compact('data', 'condition', 'commission'));
    }
    public function store(Request $request)
    {
        $this->validate($request,[
            'agency_level' => 'required',
            'commission_level' => 'required'
        ]);
        foreach ($request->agency_level as $key => $value) {
            DB::table('nso007_agency_level_commission')->insert([
                'agency_level' => $request->agency_level[$key],
                'commission_level' => $request->commission_level[$key]
            ]);
        }

        \LogActivity::addToLog('Create Agency Level Successfully !');
        return back()->with('success','Create Agency Level Successfully');
    }
    public function edit($id){
        $bank = AgencyLevelModel::find($id);
        return view('administrator.agency-level.edit',compact('bank'));
    }
    public function update(Request $request, $id)
    {
        $agency_level = AgencyLevelModel::find($id);

        $this->validate($request, [
            'agency_level'      => 'required',
            'commission_level'      => 'required',

        ]);

        $agency_level->agency_level     = $request->agency_level;
        $agency_level->commission_level    = $request->commission_level;

        $agency_level->update();


        \LogActivity::addToLog('Bank updated successfully !');
        return redirect()->route('agency-level.index')
            ->with('success','bank updated successfully !');
    }
    public function update_agency_level(Request $request){
        $this->validate($request,[
            'agency_level'=>'required',
            'commission_level'=>'required'
        ]);
         DB::table("nso007_agency_level_commission")->delete();
        for($i=0;$i<count($request['agency_level']);$i++){
            DB::table("nso007_agency_level_commission")->insert(['agency_level'=>$request->agency_level[$i],'commission_level'=>$request->commission_level[$i]]);
        }

        \LogActivity::addToLog('Agency level updated successfully !');
        return redirect()->route('agency-level.index')
            ->with('success','bank updated successfully !');
    }
}