<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ExtandWarranty;
use App\Models\Carmodel;
use App\Models\Fuletype;
use App\Exports\ExportExtandWarranty;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Maatwebsite\Excel\Facades\Excel;
use Auth, DB, Mail, Validator, File, DataTables, Exception;
use App\Imports\ImportExtandWarranty;
class ExtandWarrantyController extends Controller{
    /** construct */
        public function __construct(){
            $this->middleware('permission:extand_warranties-create', ['only' => ['create']]);
            $this->middleware('permission:extand_warranties-edit', ['only' => ['edit']]);
            $this->middleware('permission:extand_warranties-view', ['only' => ['view']]);
            $this->middleware('permission:extand_warranties-delete', ['only' => ['delete']]);
        }
    /** construct */

    /** index */ 
        public function index(Request $request){
            if($request->ajax()){
                
                
                $data = ExtandWarranty::select('extand_warranties.years as plan_name','extand_warranties.amount','extand_warranties.profit','extand_warranties.gst_percentage','extand_warranties.gst','extand_warranties.total_profit','extand_warranties.status','car_model.name as car_name','fule_type.title as fuel_type','extand_warranties.id')
                    ->leftJoin('car_model','car_model.id','=','extand_warranties.car_model')
                    ->leftjoin('fule_type','fule_type.id','=','extand_warranties.fule_type')
                    //->where('extand_warranties.status', 'active')
                    //->orWhereNull('extand_warranties.fule_type')
                    //->orWhereNull('extand_warranties.status')
                    ->orderBy('extand_warranties.id' , 'desc');
                   //dd($data); 
                return Datatables::of($data)
                        ->addIndexColumn()
                        ->addColumn('action', function($data){
                            $return = '<div class="btn-group">';

                            if(auth()->user()->can('extand_warranties-view')){
                                $return .= '<a href="'.route('extand_warranties.view', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                            }   
                            
                            if(auth()->user()->can('extand_warranties-edit')){
                                $return .= '<a href="'.route('extand_warranties.edit', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-pencil"></i>
                                            </a> &nbsp;';
                            }   

                            if (auth()->user()->can('extand_warranties-delete')) {
                                $return .= '<a href="javascript:;" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                                    <i class="fa fa-bars"></i>
                                                </a> &nbsp;
                                                <ul class="dropdown-menu">
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="active" data-id="' . base64_encode($data->id) . '">Active</a></li>
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="inactive" data-id="' . base64_encode($data->id) . '">Inactive</a></li>
                                                    <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="deleted" data-id="' . base64_encode($data->id) . '">Delete</a></li>
                                                </ul>';
                            }

                            $return .= '</div>';

                            return $return;
                        })

                     
                        ->editColumn('years', function ($data) {
                            return $data->years.' Years';
                        })
                     
                        ->editColumn('status', function ($data) {
                            if ($data->status == 'active') {
                                return '<span class="badge badge-pill badge-success">Active</span>';
                            } else if ($data->status == 'inactive') {
                                return '<span class="badge badge-pill badge-warning">Inactive</span>';
                            } else if ($data->status == 'deleted') {
                                return '<span class="badge badge-pill badge-danger">Deleted</span>';
                            }else if ($data->status == 'pdi_hold') {
                                return '<span class="badge badge-pill badge-warning">Hold By PDI</span>';
                            }
                        })

                        ->rawColumns(['years', 'action' ,'status'])
                        ->filter(function ($query) {
                                if (request()->get('car_model', false)) {
                                    $query->where('car_model.name', 'like', "%" . request()->get("car_model") . "%");
                                }
                                if (request()->get('fuel_type', false)) {
                                    $query->where('fule_type.title', 'like', "%" . request()->get("fuel_type") . "%");
                                }
                                if (request()->get('plan_name', false)) {
                                    $query->where('extand_warranties.years', 'like', "%" . request()->get("plan_name") . "%");
                                }
                                if (request()->get('amount', false)) {
                                    $query->where('extand_warranties.amount', 'like', "%" . request()->get("amount") . "%");
                                }
                                if (request()->get('profit', false)) {
                                    $query->where('extand_warranties.profit', 'like', "%" . request()->get("profit") . "%");
                                }
                                if (request()->get('gst_percentage', false)) {
                                    $query->where('extand_warranties.gst_percentage', 'like', "%" . request()->get("gst_percentage") . "%");
                                }
                                if (request()->get('gst', false)) {
                                    $query->where('extand_warranties.gst', 'like', "%" . request()->get("gst") . "%");
                                }
                                if (request()->get('total_profit', false)) {
                                    $query->where('extand_warranties.total_profit', 'like', "%" . request()->get("total_profit") . "%");
                                }
                            })
                        ->make(true);
            }
            return view('extand_warranties.index');
        }
    /** index */

    /** create */
        public function create(Request $request){
            $Carmodel = Carmodel::where(['status' => 'active'])->get();
            $Fuletype = Fuletype::where(['status' => 'active'])->get();
            return view('extand_warranties.create')->with(['Carmodel'=>$Carmodel,'Fuletype'=>$Fuletype]);; 
        }
    /** create */

    /** insert */
        public function insert(Request $request){
            if($request->ajax()){
                return true;
            }
            $crud = [
                'years' => $request->years,
                'amount' => $request->amount,
                'profit' => $request->profit,
                'gst_percentage' => $request->gst_percentage,
                'gst' => $request->gst,
                'total_profit' => $request->total_profit,
                'car_model' => $request->car_model,
                'fule_type' => $request->fule_type,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
                'created_by' => auth()->user()->id,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth()->user()->id
            ];
            DB::beginTransaction();
            try {
                DB::enableQueryLog();
                $last_id = ExtandWarranty::insertGetId($crud);
                if ($last_id) {
                    
                    DB::commit();
                    return redirect()->route('extand_warranties')->with('success', 'Record inserted successfully');
                } else {
                    DB::rollback();
                    return redirect()->back()->with('error', 'Failed to insert record')->withInput();
                }
            } catch (\Throwable $th) {
                DB::rollback();
                return redirect()->back()->with('error', 'Something went wrong, please try again later')->withInput();
            }
        }
    /** insert */

    /** view */
        public function view(Request $request){
            $id = base64_decode($request->id);
            $Carmodel = Carmodel::where(['status' => 'active'])->get();
            $data = ExtandWarranty::where(['id' => $id])->first();
            $Fuletype = Fuletype::where(['status' => 'active'])->get();
            return view('extand_warranties.view')->with(['data' => $data,'Carmodel'=>$Carmodel,'Fuletype'=>$Fuletype]);
        }
    /** view */
        
    /** edit */
        public function edit(Request $request){
            $id = base64_decode($request->id);
            $Carmodel = Carmodel::where(['status' => 'active'])->get();
            $data = ExtandWarranty::where(['id' => $id])->first();
            $Fuletype = Fuletype::where(['status' => 'active'])->get();
            return view('extand_warranties.edit')->with(['data' => $data,'Carmodel'=>$Carmodel,'Fuletype'=>$Fuletype]);
        }
    /** edit */

    /** update */
        public function update(Request $request){
            if($request->ajax()){
                return true;
            }
   
            $crud = [
                'years' => $request->years,
                'fule_type' => $request->fule_type,
                'car_model' => $request->car_model,
                'amount' => $request->amount,
                'profit' => $request->profit,
                'gst_percentage' => $request->gst_percentage,
                'gst' => $request->gst,
                'total_profit' => $request->total_profit,
                'updated_at' => date('Y-m-d H:i:s'),
                'updated_by' => auth('sanctum')->user()->id
            ];

            DB::beginTransaction();
            try {
                DB::enableQueryLog();
                $update = ExtandWarranty::where(['id' => $request->id])->update($crud);
                if ($update) {
                    
                    DB::commit();
                    return redirect()->route('extand_warranties')->with('success', 'Record updated successfully');
                } else {
                    DB::rollback();
                    return redirect()->back()->with('error', 'Failed to update record')->withInput();
                }
            } catch (\Throwable $th) {
                DB::rollback();
                return redirect()->back()->with('error', 'Something went wrong, please try again later')->withInput();
            } 
        }
    /** update */

    /** change-status */
        public function change_status(Request $request){
            if (!$request->ajax()) { exit('No direct script access allowed'); }  
            $id = base64_decode($request->id);
            $data = ExtandWarranty::where(['id' => $id])->first();

            if (!empty($data)) {
                $update = ExtandWarranty::where(['id' => $id])->update(['status' => $request->status, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth()->user()->id]);
                if ($update) {
                    return response()->json(['code' => 200]);
                } else {
                    return response()->json(['code' => 201]);
                }
            } else {
                return response()->json(['code' => 201]);
            }
        }
    /** change-status */
    
    /** Import */
        public function import(Request $request){
           
            if(Excel::import(new ImportExtandWarranty, $request->file('file')->store('file'))){
                return redirect()->route('extand_warranties')->with('sucess' ,'File Imported Sucessfully');
            }else{
                return redirect()->route('extand_warranties')->with('error' ,'Faild To Import File!');
            }
        }
    /** Import */

    /** Export */
        public function export(Request $request){
            $slug = $request->slug;
            $name = 'Extand_warranty_'.Date('YmdHis').'.xlsx';

            try {
                return Excel::download(new ExportExtandWarranty($slug), $name);
            }catch(\Exception $e){
                return redirect()->back()->with('error', $e->getMessage());
            }
        }
    /** Export */
}
