<?php

namespace App\Http\Controllers;

use App\Models\FormData;
use Illuminate\Http\Request;
use App\Exports\FormDataExport;
use Maatwebsite\Excel\Facades\Excel;
use DataTables;


class DataController extends Controller
{
    #Index
    public function index(Request $request)
    {
        return view('formdata');
    }

    #Datatable
    public function formdata(Request $request)
    {
        if ($request->ajax()) {

            $query = FormData::query();
            if ($request->from_date != '' && $request->to_date != '') {
                
                $query=$query->whereDate('created_at', '>=', $request->from_date)
                ->whereDate('created_at', '<=', $request->to_date);
                // $query=$query->whereBetween("created_at",array($request->from_date, $request->to_date));
            }

            $data=$query->where('employee_status', 0)->orderBy('created_at','DESC')->get();

            return Datatables::of($data)->addIndexColumn()
                ->addColumn('created_date', function($row){
                    $created_date=date("d-M-Y",strtotime($row->created_at));
                    return $created_date;
                })
                ->rawColumns(['created_date'])
                // ->addColumn('action', function($row){
                //     $btn = '<a href="javascript:void(0)" class="btn btn-primary btn-sm">View</a>';
                //     return $btn;
                // })
                // ->rawColumns(['action'])
                ->make(true);
        }
        return view('formdata');

    }

    #Filter data
    public function searchFormdata(Request $request)
    {
        return view('formdata')->with(["from_date"=>$request->from_date ,"to_date"=>$request->to_date]);

    }


    /**
     * Export
    * @return \Illuminate\Support\Collection
    */
    public function export(Request $request) 
    {
        $from_date=$request->from_date;
        $to_date = $request->to_date;
        return Excel::download(new FormDataExport($from_date,$to_date), 'livedata'.now().'.xlsx');
    }
    

}
