<?php

namespace App\Http\Controllers\HR;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Hr\SalaryTaxModel;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Gate;

class SalaryTaxController extends Controller
{
   

    function __construct()
    {
      $this->middleware('permission:Salary tax List', ['only' => ['index']]);
      $this->middleware('permission:Salary tax Create', ['only' => ['create','store']]);
      $this->middleware('permission:Salary tax Edit', ['only' => ['edit','update']]);
    }
    public function index(Request $request)
    {
        if($request->ajax())
        {
            $data = SalaryTaxModel::take(20);
            return DataTables::of($data)
                        ->addIndexColumn()
                        ->addColumn('action', function($data){
                            $btn_edit="";
                            $btn_delete="";
                            if(Gate::check('Salary tax Edit')){
                            $btn_edit .= '<a href="'.route('salary-tax.edit', encrypt($data->id)).'" class="button_warning"><span class="fa fa-pencil-square-o"></span></a>';
                            }
                            if(Gate::check('Salary tax Delete')) {
                                $btn_delete .= '<a class="button_danger btn_delete" data-id="' . $data->id . '"><span class="fa fa-trash-o"></span></a>';
                            }
                            return $btn_edit.$btn_delete;
                        })
                        ->rawColumns(['action', 'action'])
                        ->make(true);
        }
        return view('hr.salary-tax.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('hr.salary-tax.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',
            'rate'          => 'required',
            'from_salary'   => 'required',
            'to_salary'     => 'required',
            'add'           => 'required',
            'minus'         => 'required',
        ]);

        $name           = $request->name;
        $rate           = $request->rate;
        $from_salary    = $request->from_salary;
        $to_salary      = $request->to_salary;
        $add            = $request->add;
        $minus          = $request->minus;

        $new_rate           = implode('/', $rate);
        $new_from_salary    = implode('/', $from_salary);
        $new_to_salary      = implode('/', $to_salary);
        $new_add            = implode('/', $add);
        $new_minus          = implode('/', $minus);

        $salary_tax                 = new SalaryTaxModel();
        $salary_tax->name           = $name;
        $salary_tax->rate           = $new_rate;
        $salary_tax->from_salary    = $new_from_salary;
        $salary_tax->to_salary      = $new_to_salary;
        $salary_tax->add            = $new_add;
        $salary_tax->minus          = $new_minus;
        $salary_tax->save();


        return redirect()->route('salary-tax.index')->with('succes', 'Data was added successful!');

    }

    /**
     * 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)
    {
        $id = decrypt($id);
        $salary_tax = SalaryTaxModel::findOrFail($id);
        return view('hr.salary-tax.edit', compact('salary_tax'));
    }

    /**
     * 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',
            'rate'          => 'required',
            'from_salary'   => 'required',
            'to_salary'     => 'required',
            'add'           => 'required',
            'minus'         => 'required',
        ]);

        $name           = $request->name;
        $rate           = $request->rate;
        $from_salary    = $request->from_salary;
        $to_salary      = $request->to_salary;
        $add            = $request->add;
        $minus          = $request->minus;

        $new_rate           = implode('/', $rate);
        $new_from_salary    = implode('/', $from_salary);
        $new_to_salary      = implode('/', $to_salary);
        $new_add            = implode('/', $add);
        $new_minus          = implode('/', $minus);

        $salary_tax                 = SalaryTaxModel::findOrFail($id);
        $salary_tax->name           = $name;
        $salary_tax->rate           = $new_rate;
        $salary_tax->from_salary    = $new_from_salary;
        $salary_tax->to_salary      = $new_to_salary;
        $salary_tax->add            = $new_add;
        $salary_tax->minus          = $new_minus;
        $salary_tax->update();


        return redirect()->route('salary-tax.index')->with('succes', 'Data was updated successful!');
    }

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

    //delete salary tax
    public function delete_salary_tax(Request $request)
    {
    
        $id = $request->delete_id;
        SalaryTaxModel::where('id', $id)->delete();
        return redirect()->route('salary-tax.index')->with('succes', 'Data was deleted successful!');

    }
}
