<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Transaction;
use Illuminate\Support\Facades\Validator;
use Auth;

class IncomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('emi-income');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    
    public function manage_addition_income()
    {
        return view('addition_income');
    }
    
    function fetchaddition_income(Request $request){
        
        $result = array('data' => array());
        $report=Transaction::select('transaction.*')
                    
                            ->where('transaction.type','=','unauthorized-income')
                            ->orderBy('transaction.id', 'asc');


        if(!empty($request->datefrom)){
         $datefrom=date('Y-m-d',strtotime($request->datefrom));

         $report->whereDate('transaction.transaction_date', '>=', $datefrom);

        }
        if (!empty($request->dateto)) {
            $dateto=date('Y-m-d',strtotime($request->dateto));
            $report->whereDate('transaction.transaction_date', '<=', $dateto);
        }
        
        

        $data=$report->get();

         $i=1;
         foreach ($data as $key => $value) {
            
            $button='';
        
                if (auth()->user()->role == 'admin' ) {
                $button.='<td> <a type="button" class="btn bg-gradient-danger btn-sm" data-id="'.$value['id'].'" id="delete_addition_income">Delete</a></td>
                <td> <a href="'.url('editIncome/'.$value['id']).'" class="btn bg-gradient-primary btn-sm">Edit</a></td>';
            }
            


                $result['data'][$key] = array(
                $i,
                $value['transaction_date'],
                $value['description'],
                $value['amount'],
                $value['payment_mode'],
                $button,
            );
       $i++;  }
     echo json_encode($result);
    }


    /**
     * 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(), [
        'date' => 'required',
 
        'payment_mode' => 'required',
        'description' => 'required',
        'amount' => 'required',
        
        ]);

        if ($validator->fails()) {
        return redirect('income')
                 ->withErrors($validator)
                 ->withInput();
        }else{
            try {
                $data = request()->all();
                $data['created_by']=Auth::user()->id;
                $data['transaction_date']=$request->date;
                $data['payment_mode']=$request->payment_mode;
                $data['description']=$request->description;
                $data['amount']=$request->amount;
                $data['type']='unauthorized-income';
             
                $insert=Transaction::create($data);
                if($insert){
                    return redirect('income')->with('success','Successfully Submitted');
                }
            } catch (\Throwable $th) {
                return $th->getMessage();
            }

        }
    }

    /**
     * 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)
    {
         $data=Transaction::where('transaction.type','=','unauthorized-income')->where('id',$id)->first();
                           
        return view('editIncome',compact('data'));
    }

    /**
     * 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(), [
        'date' => 'required',
 
        'payment_mode' => 'required',
        'description' => 'required',
        'amount' => 'required',
        
        ]);

        if ($validator->fails()) {
        return redirect('income')
                 ->withErrors($validator)
                 ->withInput();
        }else{
            try {
                
                $update=Transaction::find($id);
                $update->created_by=Auth::user()->id;
                $update->transaction_date=$request->date;
                $update->payment_mode=$request->payment_mode;
                $update->description=$request->description;
                $update->amount=$request->amount;
               $insert= $update->save();
             
                if($insert){
                    return redirect('manage_addition_income')->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)
    {
        $delete=Transaction::find($id);
        $delete->delete();
    }
}
