<?php

namespace App\Http\Controllers\Admin;

use App\Config;
use App\Metatag;
use App\TrackingCode;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\CategoryGroup  $categorygroup
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {
        $config = Config::where('id', '=' , 1)->first();
        //print_r($config); exit;

        $this->authorize('update', $config);

        $imetatags = Metatag::where('mt_page', '=', 'i')->selectRaw('`mt_title` as imt_title, `mt_desc` as imt_desc, `mt_keyw` as imt_keyw, `mt_h1` as imt_h1')->first();
        $cmetatags = Metatag::where('mt_page', '=', 'c')->selectRaw('`mt_title` as cmt_title, `mt_desc` as cmt_desc, `mt_keyw` as cmt_keyw, `mt_h1` as cmt_h1')->first();
        $codes     = TrackingCode::find(1)->first();


        $data = [];
        $data['config']    = $config;
        $data['imetatags'] = $imetatags;
        $data['cmetatags'] = $cmetatags;
        $data['codes']     = $codes;

        //print_r($data['cmetatags']); exit;


        return view('admin.config.form', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\CategoryGroup  $categorygroup
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Config $config)
    {

        $this->authorize('update', $config);

        $this->validate(
            $request,
            [
                'company_name'       => 'required|string|min:3',
                'address'            => 'required|string|min:15',
                'vat_number'         => 'nullable|string|min:6',
                'reg_number'         => 'nullable|string|min:6',

                'bank_account'       => 'nullable|string|min:6',
                'logo'               => 'required|string|min:6',

                'tel1'               => 'required|string|min:6',
                'tel2'               => 'nullable|string|min:6',
                'tel3'               => 'nullable|string|min:6',
                'fax'                => 'nullable|string|min:6',
                'cel_number'         => 'nullable|string|min:6',

                'web'                => 'required|string|min:10',
                'email'              => 'required|string|min:8',
                'wtime'              => 'nullable|string|min:6',
                'gmap'               => 'nullable|string|min:6',
                //'exrate'             => 'required|integer|min:1',

                'sale_status'        => 'required|integer|in:1,0',
                'sale_title'         => 'nullable|string|min:3',
                'sale_discount'      => 'nullable|integer|min:1',
                'sale_pricelim'      => 'nullable|integer|min:0',
                'sale_sticker'       => 'nullable|string|min:4',

                'sale_sticker_shown' => 'required|integer|in:1,0',
                'def_desc'           => 'required|string|min:15',
                'title_sufix'        => 'required|string|min:6',
                'ga'                 => 'nullable|string|min:120',
                'invheader'          => 'nullable|string|min:10',


                'imt_title'          => 'required|string|min:6',
                'imt_desc'           => 'required|string|min:15',
                'imt_keyw'           => 'required|string|min:6',
                'imt_h1'             => 'required|string|min:6',

                'cmt_title'          => 'required|string|min:6',
                'cmt_desc'           => 'required|string|min:15',
                'cmt_keyw'           => 'required|string|min:6',
                'cmt_h1'             => 'required|string|min:6',


                'adw_rm'             => 'nullable|string|min:120',
                'adw_con'            => 'nullable|string|min:120',
                'fb_rm'              => 'nullable|string|min:120',
                'fb_con'             => 'nullable|string|min:120',

            ]
        );

        $config->company_name       = $request->company_name;
        $config->address            = $request->address;
        $config->vat_number         = $request->vat_number;
        $config->reg_number         = $request->reg_number;
        $config->bank_account       = $request->bank_account;
        $config->logo               = $request->logo;
        $config->tel1               = $request->tel1;
        $config->tel2               = $request->tel2;
        $config->tel3               = $request->tel3;
        $config->fax                = $request->fax;
        $config->cel_number         = $request->cel_number;
        $config->web                = $request->web;
        $config->email              = $request->email;
        $config->wtime              = $request->wtime;
        $config->gmap               = $request->gmap;
        //$config->exrate             = $request->exrate;
        $config->sale_status        = $request->sale_status;
        $config->sale_title         = $request->sale_title;
        $config->sale_discount      = $request->sale_discount;
        $config->sale_pricelim      = $request->sale_pricelim;
        $config->sale_sticker       = $request->sale_sticker;
        $config->sale_sticker_shown = $request->sale_sticker_shown;
        $config->def_desc           = $request->def_desc;
        $config->title_sufix        = $request->title_sufix;
        $config->ga                 = $request->ga;
        $config->invheader          = $request->invheader;

        $config->save();

        $imt = Metatag::find(1);
        $imt->mt_title = $request->imt_title;
        $imt->mt_desc  = $request->imt_desc;
        $imt->mt_keyw  = $request->imt_keyw;
        $imt->mt_h1    = $request->imt_h1;
        
        $imt->save();


        $cmt = Metatag::find(2);
        $cmt->mt_title = $request->cmt_title;
        $cmt->mt_desc  = $request->cmt_desc;
        $cmt->mt_keyw  = $request->cmt_keyw;
        $cmt->mt_h1    = $request->cmt_h1;

        $cmt->save();

        $tc = TrackingCode::find(1);

        $tc->adw_rm  = $request->adw_rm;
        $tc->adw_con = $request->adw_con;
        $tc->fb_rm   = $request->fb_rm;
        $tc->fb_con  = $request->fb_con;

        $tc->save();


        return redirect()->route('admin.config.edit')->with('result', __('adminforms.config.update_controller_msg'));
    }
}
