<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Auth;
use App\Settings;
use App\Http\Controllers\AdminController;
use Config;
class SettingsController extends AdminController {

    public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['url'] = "settings";
        $this->breadcrumbs[1]['name'] = "Settings";
    }

    public function index() {

        $setting_record = Settings::get();
        
        $settings=array();
        $categories = Config::get("settings.categories");
        foreach($setting_record as $record){
            
            $settings[$record->category]["title"]=$categories[$record->category];
            $settings[$record->category]["data"][]=$record;
        }
        
        $data["pageHeader"] = false;
        $data["model"] = $setting_record;
        $data["title"] = "Settings";
        $data["SubTitle"] = "settings";
        $data['breadcrumbs'] = $this->breadcrumbs;
        $data["categories"] = $categories;
        $data["settings"] = $settings;
        return view('admin/settings/index', $data);
    }

    public function update(Request $postData) {

        $active_tab = $postData->active_tab;

        $setting_record = Settings::where('title', 'App Settings Version')->first();

        $input = $postData->all();
        foreach ($input['IDS'] as $key => $ids) {
            $data['content'] = $input['Content'][$key];
            $data["updated_at"] = date("Y-m-d H:i:s");
//            $data["UpdatedBy"] = Auth::user()->PKUserID;
            $data["id"] = $ids;

            Settings::where('id', '=', $ids)->update($data);
        }
        \Session::flash('success', 'Settings Updated successfully!');
        return redirect('admin/settings');
       // d($input);
    }

}
