<?php

namespace App\Http\Controllers;

use App\Models\ThemeColor;
use Illuminate\Support\Facades\File;
use App\Models\CompanyInfo;
use App\Models\Contacts;
use Illuminate\Http\Request;

use League\Csv\Reader;
use Spatie\Permission\Models\Role;
use App\Helpers\DBFunctions;
use App\User;

use DB;
use Auth;
use JavaScript;

class SettingsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function view() {

        $com    = CompanyInfo::all();
        $themes = ThemeColor::all(); //where('id','>', 0)->get();

        return view('admin.settings.index', compact('com', 'themes'));
    }

    public function apply_theme(Request $request) {
        //dd($request->all());
        $tid = $request->tid;

        ThemeColor::where('id', '>', 0)->update(['active' => 0]);
        $rdata = ThemeColor::where('id', $tid)->update(['active' => 1]);

        $data = ['success' => true,
            'data' => $rdata,
            'message' => 'Theme updated',];

        return response()->json($data);
    }

    public function custom_theme(Request $request) {
        //dd($request->all());

        $thems = ThemeColor::all();

        return view('admin.settings.themes', compact('thems'));

    }

    public function update(Request $request) {

        //dd($request->all());
        try {
            DB::beginTransaction();

            /*$this->validate($request, [
                'firstname' => 'required',
                'lastname'  => 'required'
                //'email'=>'required|email|unique:users',
            ]);*/

            $lid = $request['_id'];

            //dd($stid);
            if($request->hasFile('photo')){
                foreach ($request->files as $files) {
                    //$photo = $lid."_".$files->getClientOriginalName();
                    $photo = $lid."_".$files->getClientOriginalName();
                    $p = "images/logo/";
                    $path = base_path("public/".$p);
                    //dd($photo);

                    $pa = "images/logo/".$request->reservephoto;
                    // dd($pa);
                    $fpath = base_path("public/".$pa);
                    // unlink($fpath);

                    $files->move($path,$photo);
                    File::makeDirectory($path, 0777, true, true);
                }
            }
            else{
                $photo = $request->reservephoto;
            }

            $data = [
                'company_name'  => $request['company_name'],
                'address'       => $request['address'],
                'postcode'      => $request['postcode'],
                'contact_no'    => $request['contact_no'],
                'email'         => $request['email'],
                'website'       => $request['website'],
                'logo'          => $photo
            ];

            //$sub = new CompanyInfo();
            $sub = CompanyInfo::where('id', $lid)->first();
            $sub->fill($data);
            $sub->update();

            DB::commit();

            return redirect()->route('settings.view')->with('info', 'Settings was updated.');

        } catch (\Exception $e) {

            DB::rollBack();
            return redirect()->route('settings.view')->with('info', $e->getMessage());
            //"{{ route('teams.edit',[$id]) }}"
        }

    }



}