<?php


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DataTables;
use DB;
use App\AgencyTypeModel;
class AgencyTypeController extends Controller
{
  
   function __construct()
   {
       $this->middleware('permission:Agency-Type-List');
//        $this->middleware('permission:Agency-Level-Edit', ['only' => ['edit','update']]);
   }
    public function index(){
        $data = AgencyTypeModel::all();
        $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-type.index',compact('data','condition','commission'));
    }
    public function create()
    {
        return view('administrator.agency-type.create',compact('condition','commission'));
    }
    public function store(Request $request)
    {
        $this->validate($request,[
            'agency_type_conid' => 'required',
            'agency_type_name' => 'required',
            'agency_type_condition' => 'required',
            'agency_type_percentage' => 'required'
        ]);

        DB::table('nso007_agency_type')->insert([
            'agency_type_conid' => $request->agency_type_conid,
            'agency_type_amount_get' => $request->agency_type_amount_get,
            'agency_type_name' => $request->agency_type_name,
            'agency_type_condition' => $request->agency_type_condition,
            'agency_type_percentage' => $request->agency_type_percentage
        ]);

        \LogActivity::addToLog('Create Agency Type successfully !');
        return redirect()->route('agency-type.index')
            ->with('success','Create Agency Type successfully !');
    }
    public function edit($id){
        $agency_group = AgencyTypeModel::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-type.edit',compact('agency_group','condition','commission'));
    }
    public function update(Request $request, $id)
    {
        $agency_type = AgencyTypeModel::find($id);

        $this->validate($request,[
            'agency_type_conid' => 'required',
            'agency_type_name' => 'required',
            'agency_type_condition' => 'required',
            'agency_type_percentage' => 'required'
        ]);

        $agency_type->agency_type_conid     = $request->agency_type_conid;
        $agency_type->agency_type_amount_get     = $request->agency_type_amount_get;
        $agency_type->agency_type_name     = $request->agency_type_name;
        $agency_type->agency_type_condition    = $request->agency_type_condition;
        $agency_type->agency_type_percentage    = $request->agency_type_percentage;

        $agency_type->update();


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