<?php

namespace App\Http\Controllers\Administrator;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\SelectOptionModel;
use App\CommissionRateModel;
use DB;
use Auth;
use DataTables;
use Gate;

class CommissionRateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    function __construct()
    {
        $this->middleware('permission:Agency-Create-Commission-Rate', ['only' => ['create','store']]);
        $this->middleware('permission:Agency-Commission-Rate-Edit', ['only' => ['update']]);
    }
    public function index()
    {
        return view('administrator.agency.commission.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $select_option = SelectOptionModel::select(
                        DB::raw('CONCAT(id," ",title) AS option_mix'), 'id')
                        ->where('parent', 1)
                        ->where('status', 1)
                        ->pluck('option_mix', 'id')
                        ->prepend('Please Select', '');
        return view('administrator.agency.commission.create', compact('select_option'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'select_option'       => 'required',
            'rate'             => 'required|numeric',
        ]);

        $user_by = Auth::user()->id;

        $rate = new CommissionRateModel;

        $rate->agency_type        = $request->select_option;
        $rate->commission_rate    = $request->rate;
        $rate->description        = $request->description;
        $rate->commission_condition   = 35;

        $rate->save();

        \LogActivity::addToLog('Commission rate created successfully!');
        return redirect()->route('agency-commission-rate.index')
        ->with('success','Commission rate 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)
    {
        $commission_rate = CommissionRateModel::find($id);
        $select_option = SelectOptionModel::select(
                        DB::raw('CONCAT(id," ",title) AS option_mix'), 'id')
                        ->where('parent', 1)
                        ->where('status', 1)
                        ->pluck('option_mix', 'id')
                        ->prepend('Please Select', '');
        return view('administrator.agency.commission.edit', compact('select_option', 'commission_rate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $commission_rate = CommissionRateModel::find($id);
       
        $this->validate($request, [
            'agency_type'      => 'required',
            'commission_rate'      => 'required|numeric',
        ]);

        $commission_rate->agency_type          = $request->agency_type;
        $commission_rate->commission_rate      = $request->commission_rate;
        $commission_rate->description          = $request->description;
        $commission_rate->commission_condition        = 35;
        $commission_rate->update();
        
        \LogActivity::addToLog('Commission updated successfully!');
        return redirect()->route('agency-commission-rate.index')
        ->with('success','Commission rate updated successfully !');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function get_commission_list()
    {
        $rate = CommissionRateModel::leftjoin('nso007_select_option as option', 'nso007_agency_commission_rate.agency_type', '=', 'option.id')
        ->select(

            'nso007_agency_commission_rate.id',
            'nso007_agency_commission_rate.commission_rate',
            'nso007_agency_commission_rate.commission_condition',
            'nso007_agency_commission_rate.description',
            'nso007_agency_commission_rate.updated_at',
            'option.title',
            'option.parent'
        )->get();
        return  Datatables::of($rate)
        ->addIndexColumn()
        ->editColumn('commission_rate', function($rate) {
            return number_format($rate->commission_rate, 2).' %';
        })
         ->editColumn('commission_condition', function($rate) {
            return number_format($rate->commission_condition, 2).' %';
        })
        ->addColumn('action', function($rate) {
            if(Gate::check('Agency-Commission-Rate-Edit')) {
                if ($rate->parent != 1) {
                    $action = '<a href="#" title="Edit"><span class="button_primary btn-xs fa fa-edit"></span></a>';
                } else {
                    $action = '<a href="' . route("agency-commission-rate.edit", $rate->id) . '" title="Edit"><span class="button_info btn-xs fa fa-edit"></span></a>';
                }
                return $action;
            }
        })
        ->make(true);
    }
}
