<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Common;
use Auth;

class ManageConfigController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    
  public function index()
    {
    	$heads = Common::where('parent_id','=',0)->where('type','=',0)->orderBy('name')->get();
      $masters = Common::where('parent_id','!=',0)->where('type','=',0)->orderBy('parent_id')->get();
    
    	return view('configuration/index',compact('heads','masters'));
    }

     public function edit(Request $request)
     {
        $master = Common::where('id','=',$request->get('option'))->get();

        return $master;
     }

    public function store(Request $request)
    {
    	if($request->get('master_id') == NULL)
       {
          $master = Common::where('parent_id','=',$request->get('head'))
                          ->where('name','=',strtoupper($request->get('attribute')))->count();

          if($master > 0)
          {
          	return redirect('manage/config/index')->with('exist','true'); 
          }
          
          $data = Common::find($request->get('head'))->name;
       	  Common::create(array( 'name'          => strtoupper($request->get('attribute')),
       	  	                    'masterkeyword' => $data,
       	  	                    'parent_id'     => $request->get('head') ));

       	  return redirect('manage/config/index')->with('save','true');
       }
       else
       {
          $data = Common::find($request->get('head'))->name;
       	  Common::where('id','=',$request->get('master_id'))
       	        ->update(array( 'name'          => strtoupper($request->get('attribute')),
       	  	                    'masterkeyword' => $data,
       	  	                    'parent_id'     => $request->get('head') ));

       	  return redirect('manage/config/index')->with('Update','true');              
           
       }
    }

   
}
