<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use App\Models\InvoiceTerm;

class InvoiceTermsController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:invoice-terms-list|invoice-terms-create|invoice-terms-edit|invoice-terms-delete', ['only' => ['index','show']]);
        $this->middleware('permission:invoice-terms-create', ['only' => ['create','store']]);
        $this->middleware('permission:invoice-terms-edit', ['only' => ['edit','update']]);
        $this->middleware('permission:invoice-terms-delete', ['only' => ['destroy']]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $invoiceTermData = InvoiceTerm::orderBy('id', 'desc')->paginate(10);

        $allInvoiceTermCount = InvoiceTerm::orderBy('id', 'desc')->count();
        $activeInvoiceTermCount = InvoiceTerm::orderBy('id','desc')->where('status',true)->count();
        $inactiveInvoiceTermCount = InvoiceTerm::orderBy('id','desc')->where('status',false)->count();

        return view('admin.invoiceTerms.index',compact('invoiceTermData','allInvoiceTermCount','activeInvoiceTermCount','inactiveInvoiceTermCount'));
    }

    //To get active data...
    public function activeList()
    {
        $invoiceTermData = InvoiceTerm::orderBy('id','desc')->where('status',true)->paginate(10);

        $allInvoiceTermCount = InvoiceTerm::orderBy('id', 'desc')->count();
        $activeInvoiceTermCount = InvoiceTerm::orderBy('id','desc')->where('status',true)->count();
        $inactiveInvoiceTermCount = InvoiceTerm::orderBy('id','desc')->where('status',false)->count();

        return view('admin.invoiceTerms.activeList',compact('invoiceTermData','allInvoiceTermCount','activeInvoiceTermCount','inactiveInvoiceTermCount'));
    }

    //To get inactive data...
    public function inActiveList()
    {
        $invoiceTermData = InvoiceTerm::orderBy('id','desc')->where('status',false)->paginate(10);

        $allInvoiceTermCount = InvoiceTerm::orderBy('id', 'desc')->count();
        $activeInvoiceTermCount = InvoiceTerm::orderBy('id','desc')->where('status',true)->count();
        $inactiveInvoiceTermCount = InvoiceTerm::orderBy('id','desc')->where('status',false)->count();
        
        return view('admin.invoiceTerms.inActiveList',compact('invoiceTermData','allInvoiceTermCount','activeInvoiceTermCount','inactiveInvoiceTermCount'));
    }

    /**
     * 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)
    {
        $request->validate([
            'invoice_title'=> 'required',
            'invoice_terms'=> 'required',
        ]);

        $data = $request->all();

        if(InvoiceTerm::create($data)){
            Toastr::success('InvoiceTerm created successfully.', 'Success', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Sorry, Something is wrong.!', 'Error', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    /**
     * 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)
    {
        //
    }

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

        $data = $request->all();
        $singleInvoiceTermData = InvoiceTerm::where('id', $id)->first();

        if($singleInvoiceTermData->update($data)){
            Toastr::success('InvoiceTerm updated successfully.', 'Success', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Sorry, Something is wrong.!', 'Error', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $singleInvoiceTermData = InvoiceTerm::where('id', $id)->first();
        if($singleInvoiceTermData->delete()){
            Toastr::success('InvoiceTerm deleted successfully.', 'Success', ["progressbar" => true]);
            return redirect()->back();
        }else{
            Toastr::error('Soething is wrong!.', 'Error', ["progressbar" => true]);
            return redirect()->back();
        }
    }

    //To active status...
    public function invoiceTermActive($id)
    {
        InvoiceTerm::where('id', $id)->update(['status' => true]);
        Toastr::success('InvoiceTerm activated successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }
    
    //To inactive status...
    public function invoiceTermInactive($id)
    {
        InvoiceTerm::where('id', $id)->update(['status' => false]);
        Toastr::success('InvoiceTerm in-activated successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }

    //To active default invoice terms...
    public function defaultInvoiceTerm($id)
    {
        InvoiceTerm::where('id', $id)->update(['is_default' => true]);
        InvoiceTerm::whereNotIn('id', [$id])->update(['is_default' => false]);
        Toastr::success('InvoiceTerm added in default successfull.', 'Success', ["progressbar" => true]);
        return redirect()->back();
    }
}
