<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\Models\Expensetype;
use App\Models\Expense;
use PDF;

class ExpensesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->checkpermission('expenses-list');
        $expenses = Expense::orderBy('created_at', 'DEC')->paginate(10);
        return view('backend.expenses.list', compact('expenses'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $this->checkpermission('expenses-create');
        $types = Expensetype::latest()->get();
        return view('backend.expenses.create', compact('types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'type_name'=>'required',
            'expense_amount'=>'required',
            ]);
            
        $ex = new Expense();
        $ex->expensetype_id = $request->type_name;
        $ex->user_id = Auth::id();
        $dt = Carbon::parse($request->expense_date);
        $ex->expense_date = $dt->toDateTimeString();
        $ex->expense_amount = $request->expense_amount;
        $ex->description = $request->description;
        $ex->save();

        return redirect()->route('expenses.list')->with('success','Expense Amount Save Successfull');
    }

    /**
     * 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)
    {
        $this->checkpermission('expenses-edit');
        $expenses = Expense::find($id);
        return view('backend.expenses.edit', compact('expenses'));
    }

    /**
     * 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, [
            'expenses_name' => 'required',
            'party_name' => 'required',
            'totalamount' => 'required',
            'paidamount' => 'required',
            'product_name' => 'required',
        ]);

        $expenses = Expense::find($id);
        $expenses->expenses_name = $request->expenses_name;
        $expenses->party_name = $request->party_name;
        $expenses->totalamount = $request->totalamount;
        $expenses->paidamount = $request->paidamount;
        $expenses->dueamount = $request->totalamount - $request->paidamount;
        $expenses->product_name = $request->product_name;
        $expenses->modified_by = Auth::user()->username;
        $expenses->updated_at = date('Y-m-d H:i:s');
        if ($expenses->update()) {
            return redirect()->route('expenses.list')->with('success', 'successfully updated');
        } else {
            return redirect()->route('expenses.update')->with('error', 'failed to  update');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $check = $this->checkpermission('expenses-delete');
        if ($check) {
            $this->checkpermission('expenses-delete');
        } else {
            $staff = Expense::find($id);
            $message = $staff->delete();
            if ($message) {
                return redirect()->route('expenses.list')->with('success_message', 'successfully Deleted');
            } else {
                return redirect()->route('expenses.update')->with('error_message', 'failed to  Delete');
            }
        }
    }

    public function expensesheadinglist(){
        $this->checkpermission('expenses-type-list');
        $expenses = Expensetype::latest()->get();
        return view('backend.expense_heading.list',compact('expenses'));
    }
    public function expensesheadingcreate(){
        $this->checkpermission('expenses-type-entry');
        return view('backend.expense_heading.create');
    }
    public function expensesheadingstore(Request $request){
        $this->validate($request, [
            'name' => 'required',
        ]);
        $message = new Expensetype();
        $message->type_name = $request->name;
        $message->save();
        
        if ($message) {
            return back()->with('success_message', 'Success Fully created');
        } else {
            return back()->with('error_message', 'Failed To create');
        }
    }
    public function expensesheadingedit($id){
        $this->checkpermission('expenses-type-edit');
        $title = Expensetype::find($id);
        return view('backend.expense_heading.edit', compact('title'));
    }
    public function expensesheadingupdate(Request $request, $id){
        $expenses = Expensetype::find($id);
        $expenses->type_name = $request->name;
        $expenses->save();

        return redirect()->route('expensesheading.list')->with('success_message', 'successfully updated');
    }

    /*-----------------------*/
    public function export()
    {
        $allexpenses = Expense::orderBy('created_at','DEC')->get();
        $pdf = PDF::loadview('backend.pdfbill.expenses', compact('allexpenses'));
        return $pdf->download('expenses.pdf');
    }
}
