<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\General_Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Exception;

class PanelSettingController extends Controller
{
    private $folder = "setting";
    public $common;
    public function __construct()
    {
        $this->common = new Common;
    }

    public function index()
    {
        try {

            $params['result'] = Setting_Data();
            if ($params['result']) {

                $params['result']['panel_login_page_image'] = $this->common->getImage($this->folder, $params['result']['panel_login_page_image']);
                $params['result']['panel_login_page_bg_image'] = $this->common->getImage($this->folder, $params['result']['panel_login_page_bg_image']);
                $params['result']['company_logo'] = $this->common->getImage($this->folder, $params['result']['company_logo']);

                return view('admin.panel_setting.index', $params);
            } else {
                return view('errors.404');
            }
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
    public function save(Request $request)
    {
        try {

            $rules = [
                'panel_login_page_view' => 'required',
            ];
            if ($request['panel_login_page_view'] == 1) {
                $rules['panel_login_page_image'] = 'image|mimes:jpeg,png,jpg';
            } else {
                $rules['panel_login_page_bg_color'] = 'required';
                $rules['panel_login_page_bg_image'] = 'image|mimes:jpeg,png,jpg';
            }
            $validator = Validator::make($request->all(), $rules);
            if ($validator->fails()) {
                $errs = $validator->errors()->all();
                return response()->json(['status' => 400, 'errors' => $errs]);
            }

            $data = $request->all();

            $data['panel_login_page_view'] = $data['panel_login_page_view'] ?? 0;
            if ($data['panel_login_page_view'] == 1) {

                if (isset($data['panel_login_page_bg_image'])) {

                    $files = $data['panel_login_page_bg_image'];
                    $data['panel_login_page_bg_image'] = $this->common->saveImage($files, $this->folder, 'panel_');
                    $this->common->deleteImageToFolder($this->folder, basename($data['old_panel_login_page_bg_image']));
                }

                $data['panel_login_page_bg_color'] = "#000000";
                $data['panel_login_page_image'] = "";
                $this->common->deleteImageToFolder($this->folder, basename($data['old_panel_login_page_image']));
            } else if ($data['panel_login_page_view'] == 2) {

                $data['panel_login_page_bg_image'] = "";
                $this->common->deleteImageToFolder($this->folder, basename($data['old_panel_login_page_bg_image']));

                $data['panel_login_page_bg_color'] = $data['panel_login_page_bg_color'] ?? "#00000";
                if (isset($data['panel_login_page_image'])) {

                    $files = $data['panel_login_page_image'];
                    $data['panel_login_page_image'] = $this->common->saveImage($files, $this->folder, 'panel_');
                    $this->common->deleteImageToFolder($this->folder, basename($data['old_panel_login_page_image']));
                }
            }

            foreach ($data as $key => $value) {
                $setting = General_Setting::where('key', $key)->first();
                if (isset($setting['id'])) {
                    $setting['value'] = $value;
                    $setting->save();
                }
            }

            return response()->json(['status' => 200, 'success' => __('label.setting_save_successfully')]);
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
    public function company_info(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'company_name' => 'required',
                'company_logo' => 'image|mimes:jpeg,png,jpg,webp',
            ]);
            if ($validator->fails()) {
                $errs = $validator->errors()->all();
                return response()->json(['status' => 400, 'errors' => $errs]);
            }

            $data = $request->all();
            if (isset($data['company_logo'])) {

                $file = $data['company_logo'];
                $data['company_logo'] = $this->common->saveImage($file, $this->folder, 'cmp_');
                $this->common->deleteImageToFolder($this->folder, basename($data['old_company_logo']));
            }

            foreach ($data as $key => $value) {
                $setting = General_Setting::where('key', $key)->first();
                if (isset($setting['id'])) {
                    $setting['value'] = $value;
                    $setting->save();
                }
            }
            return response()->json(['status' => 200, 'success' => __('label.setting_save_successfully')]);
        } catch (Exception $e) {
            return response()->json(['status' => 400, 'errors' => $e->getMessage()]);
        }
    }
}
