<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;
use DataTables;
use DB;
use App\AgencyGroupModel;
class AgencyGroupController extends Controller

{
  
    function __construct()
    {
        $this->middleware('permission:Agency-GroupType');
        $this->middleware('permission:Agency-GroupType-Create', ['only' => ['create','store']]);
        $this->middleware('permission:Agency-GroupType-Edit', ['only' => ['edit','update']]);
    }
    public function index(){
        $data = AgencyGroupModel::all();
        return view('administrator.agency-group.index',compact('data'));
    }
    public function create()
    {
        $condition = DB::table('nso007_commission_condition')->select('condition', 'id')->pluck('condition','id')->prepend('Please Select', '');
        $commission = DB::table('nso007_commission_condition')->select('commission', 'commission')->pluck('commission','commission')->prepend('Please Select', '');
        return view('administrator.agency-group.create',compact('condition','commission'));
    }
    public function store(Request $request)
    {
        $this->validate($request,[
            'agency_group_conid' => 'required',
            'agency_group_amount_get' => 'required',
            'agency_group_name' => 'required',
            'agency_group_condition' => 'required',
            'agency_group_percentage' => 'required'
        ]);
        DB::table('nso007_agency_group')->insert([
            'agency_group_conid' => $request->agency_group_conid,
            'agency_group_amount_get' => $request->agency_group_amount_get,
            'agency_group_name' => $request->agency_group_name,
            'agency_group_condition' => $request->agency_group_condition,
            'agency_group_percentage' => $request->agency_group_percentage
        ]);
        \LogActivity::addToLog('Create Agency Group successfully !');
        return redirect()->route('agency-group.index')
            ->with('success','Create Agency Group successfully !');
    }
    public function edit($id){
        $agency_group = AgencyGroupModel::find($id);
        $condition = DB::table('nso007_commission_condition')->select('condition', 'id')->pluck('condition','id');
        $commission = DB::table('nso007_commission_condition')->select('commission', 'commission')->pluck('commission','commission');
        return view('administrator.agency-group.edit',compact('agency_group','condition','commission'));
    }
    public function update(Request $request, $id)
    {
        $agency_group = AgencyGroupModel::find($id);

        $this->validate($request, [
            'agency_group_conid' => 'required',
            'agency_group_amount_get' => 'required',
            'agency_group_name' => 'required',
            'agency_group_condition' => 'required',
            'agency_group_percentage' => 'required'

        ]);

        $agency_group->agency_group_conid     = $request->agency_group_conid;
        $agency_group->agency_group_amount_get     = $request->agency_group_amount_get;
        $agency_group->agency_group_name     = $request->agency_group_name;
        $agency_group->agency_group_condition    = $request->agency_group_condition;
        $agency_group->agency_group_percentage    = $request->agency_group_percentage;

        $agency_group->update();


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