<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ExpenseCategory;
use Illuminate\Support\Facades\Validator;
use Auth;
class ExpenseCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('expense-category');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
        'name' => 'required',
        
        ]);

        if ($validator->fails()) {
        return redirect('expense_category')
                 ->withErrors($validator)
                 ->withInput();
        }else{
            try {
                $data = request()->all();
                $data['created_by'] = Auth::user()->id;
                $insert=ExpenseCategory::create($data);
                if($insert){
                    return redirect('expense_category')->with('success','Successfully Submitted');
                }
            } catch (\Throwable $th) {
                return $th->getMessage();
            }

        }
    }

    function get_expense_category(){
    $result = array('data' => array());
    $data=ExpenseCategory::select('expense_category.*','users.name as user_name')
                            ->leftjoin('users','users.id','=','expense_category.created_by')->get();
                            
    $i=1;
    foreach ($data as $key => $value) {
    
 
    $button='';
    $button='<td>
    <a  class="btn bg-gradient-info btn-sm"   href="'.url('edit-expense-category/'.$value['id']).'">Edit</a></td>';
    if (auth()->user()->role == 'admin' ) {
    $button.=' <a type="button" class="btn bg-gradient-danger btn-sm" data-id="'.$value['id'].'" id="delete">Delete</a>
    </td>';
    }
    $result['data'][$key] = array(
    $i,
    $value['name'],
    $value['user_name'],
    $button,
    );
    $i++;  }
    echo json_encode($result);
    }
    function delete_expense_category($id){
         $delete = ExpenseCategory::find($id);
        $delete->delete();
    }
    /**
     * 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)
    {
        $category=ExpenseCategory::findOrfail($id);
        return view('edit-expense-category',compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
        'name' => 'required',
        
        ]);

        if ($validator->fails()) {
        return redirect('edit-expense-category/'.$id)
                 ->withErrors($validator)
                 ->withInput();
        }else{
            try {
                $update=ExpenseCategory::findOrFail($id);
                $update->name=$request->name;
                $update->created_by = Auth::user()->id;
                $update->save();
                if($update){
                    return redirect('expense_category')->with('success','Successfully Submitted');
                }
            } catch (\Throwable $th) {
                return $th->getMessage();
            }

        }
    }

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