<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\AdminDetail;
use App\Models\WebsiteSetting;
use App\Models\accounting\{
    AccountBank,
    AccountTaxSlab
};

class WebsiteSettingController extends Controller
{
    private $admin_id, $res, $allData;
    public function __construct()
	{	
		$this->middleware(function ($request, $next) 
        {
			// fetch session and use it in entire class with constructor
            if(session()->get('loggedIn')['user_type'] == 'admin')
			{
				$this->admin_id = session()->get('loggedIn')['id'];
			}
            else
            {
				$this->admin_id = session()->get('loggedIn')['admin_id'];
			}				
			    return $next($request);
		});

	}

    

    public function index()
    {            
        $this->data["mainSuperAdminMenu"] = "website-settings";
        $this->data["superadminSubMenu"] = "website-settings";

        $this->data["admin_detail"] = AdminDetail::where("admin_id",$this->admin_id)->first();
        $this->data["banks"] = AccountBank::where("admin_id",$this->admin_id)->get();
        $this->data["country"] = DB::table("country_codes")->get();
        $this->data["taxes"] = AccountTaxSlab::where("admin_id",$this->admin_id)->paginate(10);

        return view('website_settings.list',$this->data);               
    }

    public function addAdminDetail(Request $request)
    {                    
        $this->data["mainSuperAdminMenu"] = "website-settings";
        $this->data["superadminSubMenu"] = "website-settings";

        $this->allData = $request->all();
        
        //return $request->all();

        $validator = Validator::make($request->all(), [
            'company_name' => "required|string|max:255",
            'contact_name' => "required|string|max:255",
            //'company_logo' => 'nullable|file|mimetypes:image/*|max:1024',
            //'favicon' => 'nullable|file|mimetypes:image/*|max:100'
        ]);
        
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }

        // Handle logo upload
        if ($request->hasFile('company_logo')) {
            $imageName = time() . '.' . $request->file('company_logo')->getClientOriginalExtension();
            $request->file('company_logo')->move(public_path('admin/images/website'), $imageName);
            $url = asset('public/admin/images/website') . '/' . $imageName;

            $this->allData["company_logo"] = $url;
            //return $this->allData["company_logo"];
        }
        
        // Handle favicon upload
    
        if ($request->hasFile('favicon')) 
        {
            $imageName = time() . '.' . $request->file('favicon')->getClientOriginalExtension();
            $request->file('favicon')->move(public_path('admin/images/website'), $imageName);
            $url = asset('public/admin/images/website') . '/' . $imageName;
            $this->allData["favicon"] = $url;
        }

        if(isset($request->admin_detail_id) && !empty($request->admin_detail_id))
        {
            $res = AdminDetail::updateOrCreate(["id"=>$request->admin_detail_id],$this->allData);
            if($res)
            {
                return back()->with('success', 'Website settings updated successfully');
            }
            return back()->with('error', 'Failed !');
        }

        $this->allData["admin_id"] = $this->admin_id;
        $res = AdminDetail::updateOrCreate(["id"=>$request->admin_detail_id],$this->allData);

        if($res)
        {
            return back()->with('success', 'Website settings create successfully');
        }
        return back()->with('error', 'Failed !');
        
        
    }

    public function addTaxSlab(Request $request)
    {
        $this->data["mainSuperAdminMenu"] = "website-settings";
        $this->data["superadminSubMenu"] = "website-settings";

        $this->allData = $request->all();
        
        //return $request->all();

        $validator = Validator::make($request->all(), [
            'tax' => "required",
            'igst' => "required"
        ]);
        
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }


        if(isset($request->tax_id) && !empty($request->tax_id))
        {
            $res = AccountTaxSlab::updateOrCreate(["id"=>$request->tax_id],$this->allData);
            if($res)
            {
                return back()->with('success', 'Tax updated successfully');
            }
            return back()->with('error', 'Failed !');
        }

        $this->allData["admin_id"] = $this->admin_id;
        $res = AccountTaxSlab::updateOrCreate(["id"=>$request->tax_id],$this->allData);

        if($res)
        {
            return back()->with('success', 'Tax create successfully');
        }
        return back()->with('error', 'Failed !');
    }

    public function deleteTax(Request $request)
    {
        
        $res = AccountTaxSlab::where("id",$request->id)->delete();
        if($res)
        {
            return response()->json(["message"=>"success"],200);
        }
        return response()->json(["message"=>"failed"],424);
    }

    

}
