<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use App\Models\Employee;
use App\Models\EmployeeAccount;
use App\Models\EmployeeRecord;
use App\Models\Sale;
use App\Models\SaleDetail;
use DB;

class EmployeeController extends Controller
{    
     public function index(Request $request){
        //$this->checkpermission('employee-list');
        $show = $request->show;
        if($show != ''){
         $employees = Employee::latest()->paginate($show);
        }else{
            $employees = Employee::latest()->paginate(setting()->ddspinp);
        }
        
        return view('backend.employee.index',compact('employees'));
    }
    
    /*public function employees(Request $request){
        // সার্চ ভ্যালু নেয়া
        $searchValue = $request->input('search');
    
        // সার্চ ভ্যালু একটি সংখ্যা কিনা তা যাচাই
        if (is_numeric($searchValue)) {
            // এটি একটি সংখ্যা, তাই আমরা ফোন কলামে অনুসন্ধান করব
            $employees = Employee::where('phone', $searchValue)->get();
        } else {
            // এটি একটি স্ট্রিং, তাই আমরা নাম কলামে অনুসন্ধান করব
            $employees = Employee::where('name', 'LIKE', "%{$searchValue}%")->get();
        }
        if($employees){
            return view('backend.employee.list', compact('employees'));
        }else{
            return redirect()->route('employee.index')->with('error','Woops!! no result found');
        }
    }*/
    public function create(){
        return view('backend.employee.create');
    }
    /*-----employee list for ledger section-----*/
    public function employeeList2(){
        $employee = Employee::orderBy('employee_name','ASC')->get();
        return response()->json($employee);
    }
    public function store(Request $request){
        /*request()->validate([
            'name'=>'required',
            'phone'=>'required',
		//	'address'=>'required',
            'image'=>'mimes:jpeg,jpg,png,bmp',
        ]);*/
        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required',
            'address'=>'required',
            'salary'=>'required',
            'image'=>'mimes:jpeg,jpg,png,bmp',
        ]);

        // get form image
        $image = $request->file('image');
        $slug = str_slug($request->name);
        // image section
        if (isset($image)){
            $currentDate = Carbon::now()->toDateString();
            $imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
            if (!Storage::disk('public')->exists('employee')){
                Storage::disk('public')->makeDirectory('employee');
            }
            $doctor = Image::make($image)->resize(220,220)->stream();
            Storage::disk('public')->put('employee/'.$imagename,$doctor);
        } else {
            $imagename = "default.png";
        }
        
        $employee = new Employee();
		$employee->name = $request->name;
		$employee->phone = $request->phone;
        $dt = Carbon::parse($request->join_date);
        $employee->join_date = $dt->toDateTimeString();
        $employee->salary = $request->salary;
        $employee->address = $request->address;
        $employee->image = $imagename;
        $employee->created_by = Auth::id();
        $employee->save();
        
        return redirect()->route('employee.list')->with('success','Employee Information Save Successfull');
    }
    public function status($id){
        $employee = Employee::find($id);
        if($employee->status != 'Inactive'){
            $employee->status = "Inactive";
        }else{
            $employee->status = "Active";
        }
        $employee->updated_by = Auth::id();
        $employee->save();
        return back()->with('success','Employee Status Change succesfull');
    }
    public function edit($id){
        $employee = Employee::find($id);
        return view('backend.employee.edit',compact('employee'));
    }
    public function update(Request $request, $id){  
        $this->validate($request,[
            'name'=>'required',
            'phone'=>'required',
            'address'=>'required',
            'salary'=>'required',
            'image'=>'mimes:jpeg,jpg,png,bmp',
        ]);
      
        $image = $request->file('image');
        $slug = str_slug($request->name);
        $employee = Employee::find($id);
        // image section
        if (isset($image)){
            $currentDate = Carbon::now()->toDateString();
            $imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
            if (!Storage::disk('public')->exists('employee')){
                Storage::disk('public')->makeDirectory('employee');
            }
            //delete old image
            if (Storage::disk('public')->exists('employee/'.$employee->image)){
                Storage::disk('public')->delete('employee/'.$employee->image);
            }
            $doctor = Image::make($image)->resize(220,220)->stream();
            Storage::disk('public')->put('employee/'.$imagename,$doctor);
        } else {
            $imagename = $employee->image;
        }
        
		$employee->name = $request->name;
		$employee->phone = $request->phone;
        $dt = Carbon::parse($request->join_date);
        $employee->join_date = $dt->toDateTimeString();
        $employee->salary = $request->salary;
        $employee->address = $request->address;
        $employee->image = $imagename;
        $employee->updated_by = Auth::id();
        $employee->save();

        return redirect()->route('employee.list')->with('success','Employee Information Update Successfull');
    }
    public function destroy($id){
		$employee = Employee::find($id);
        if (Storage::disk('public')->exists('employee/'.$employee->image))
        {
            Storage::disk('public')->delete('employee/'.$employee->image);
        }
        $employee->delete();
        return redirect()->back()->with('success','Employee Information Successfully Deleted ');
    }
}
