<?php
#app/Http/Controller/ContentFront.php
namespace App\Http\Controllers;

use App\Models\ShopEmailTemplate;
use App\Models\ShopNews;
use App\Models\ShopPage;
use App\Models\ShopSubscribe;
use App\Modules\Cms\Models\CmsCategory;
use Illuminate\Http\Request;

class ContentFront extends GeneralController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [getContact description]
     * @return [type] [description]
     */
    public function getContact()
    {
        $page = $this->getPage('contact');
        return view(
            'templates.' . sc_store('template') . '.shop_contact',
            array(
                'title' => trans('front.contact'),
                'description' => '',
                'page' => $page,
                'keyword' => '',
                'og_image' => '',
            )
        );
    }

    /**
     * [postContact description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function postContact(Request $request)
    {
        $validator = $request->validate([
            'name' => 'required',
            'title' => 'required',
            'content' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
        ], [
            'name.required' => trans('validation.required'),
            'content.required' => trans('validation.required'),
            'title.required' => trans('validation.required'),
            'email.required' => trans('validation.required'),
            'email.email' => trans('validation.email'),
            'phone.required' => trans('validation.required'),
            'phone.regex' => trans('validation.phone'),
        ]);
        //Send email
        $data = $request->all();
        $data['content'] = str_replace("\n", "<br>", $data['content']);

        if (sc_config('contact_to_admin')) {
            $checkContent = (new ShopEmailTemplate)
                ->where('group', 'contact_to_admin')
                ->where('status', 1)
                ->first();
            if ($checkContent) {
                $content = $checkContent->text;
                $dataFind = [
                    '/\{\{\$title\}\}/',
                    '/\{\{\$name\}\}/',
                    '/\{\{\$email\}\}/',
                    '/\{\{\$phone\}\}/',
                    '/\{\{\$content\}\}/',
                ];
                $dataReplace = [
                    $data['title'],
                    $data['name'],
                    $data['email'],
                    $data['phone'],
                    $data['content'],
                ];
                $content = preg_replace($dataFind, $dataReplace, $content);
                $data_email = [
                    'content' => $content,
                ];

                $config = [
                    'from' =>  $data['email'],
                    'to' => sc_store('email'),
                    'replyTo' => $data['email'],
                    'subject' => $data['title'],
                ];
                sc_send_mail('mail.contact_to_admin', $data_email, $config, []);
            }
        }

        return redirect()
            ->route('contact')
            ->with('success', trans('front.thank_contact'));
    }

    /**
     * Render page
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public function pages($key = null)
    {
        $page = $this->getPage($key);
        if ($page) {
            return view(
                'templates.' . sc_store('template') . '.shop_page',
                array(
                    'title' => $page->title,
                    'description' => '',
                    'keyword' => '',
                    'page' => $page,
                )
            );
        } else {
            return $this->pageNotFound();
        }
    }

    /**
     * Get page info
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public function getPage($key = null)
    {
        return ShopPage::where('key', $key)
            ->where('status', 1)
            ->first();
    }

    /**
     * Render news
     * @return [type] [description]
     */
    public function news()
    {
        $news = (new ShopNews)
            ->getItemsNews($limit = sc_config('product_new'), $opt = 'paginate');
        return view(
            'templates.' . sc_store('template') . '.shop_news',
            array(
                'title' => trans('front.blog'),
                'description' => sc_store('description'),
                'keyword' => sc_store('keyword'),
                'news' => $news,
            )
        );
    }

    /**
     * News detail
     *
     * @param   [type]  $name 
     * @param   [type]  $id
     *
     * @return  view
     */
    public function newsDetail($name, $id)
    {
        $news_currently = ShopNews::find($id);
        if ($news_currently) {
            $title = ($news_currently) ? $news_currently->title : trans('front.not_found');
            return view(
                'templates.' . sc_store('template') . '.shop_news_detail',
                array(
                    'title' => $title,
                    'news_currently' => $news_currently,
                    'description' => sc_store('description'),
                    'keyword' => sc_store('keyword'),
                    'blogs' => (new ShopNews)->getItemsNews($limit = 4),
                    'og_image' => $news_currently->getImage(),
                )
            );
        } else {
            return view(
                'templates.' . sc_store('template') . '.notfound',
                array(
                    'title' => trans('front.not_found'),
                    'description' => '',
                    'keyword' => sc_store('keyword'),
                )
            );
        }
    }

    /**
     * email subscribe
     * @param  Request $request
     * @return json
     */
    public function emailSubscribe(Request $request)
    {
        $validator = $request->validate([
            'subscribe_email' => 'required|email',
            ], [
            'email.required' => trans('validation.required'),
            'email.email'    => trans('validation.email'),
        ]);
        $data       = $request->all();
        $checkEmail = ShopSubscribe::where('email', $data['subscribe_email'])
            ->first();
        if (!$checkEmail) {
            ShopSubscribe::insert(['email' => $data['subscribe_email']]);
        }
        return redirect()->back()
            ->with(['success' => trans('subscribe.subscribe_success')]);
    }

    /**
     * postSchedule
     * @param  Request $request
     * @return json
     */
    public function postSchedule(Request $request) {
        $validator = $request->validate([
            'full_name' => 'required',
            'content' => 'required',
            'phone' => 'required',
            'time_schedule' => 'required'
        ], [
            'full_name.required' => trans('validation.required'),
            'content.required' => trans('validation.required'),
            'phone.required' => trans('validation.required'),
            'phone.regex' => trans('validation.phone'),
            'time_schedule.required' => 'Vui lòng nhập lịch hẹn'
        ]);
        $data       = $request->all();
        if (sc_config('contact_to_admin')) {
            $checkContent = (new ShopEmailTemplate)
                ->where('group', 'other')
                ->where('status', 1)
                ->first();
            if ($checkContent) {
                $content = $checkContent->text;
                $dataFind = [
                    '/\{\{\$full_name\}\}/',
                    '/\{\{\$phone\}\}/',
                    '/\{\{\$content\}\}/',
                    '/\{\{\$time_schedule\}\}/'
                ];
                $dataReplace = [
                    $data['full_name'],
                    $data['phone'],
                    $data['content'],
                    $data['time_schedule'],
                ];
                $content = preg_replace($dataFind, $dataReplace, $content);
                $data_email = [
                    'content' => $content,
                ];

                $config = [
                    'to' => sc_store('email'),
                    'replyTo' => $data['email'],
                    'subject' => 'Lịch hẹn',
                ];
                sc_send_mail('mail.contact_to_admin', $data_email, $config, []);
            }
        }
        return redirect()->back()
            ->with(['success' => trans('subscribe.subscribe_success')]);
    }

    public function faq()
    {
        $category_currently = CmsCategory::find(6);
        $entries = (new CmsCategory)->getContentsToCategory(6, $limit = sc_config('product_new'), $opt = 'paginate');
        return view(
            'templates.' . sc_store('template') . '.faq',
            array(
                'title' => $category_currently['title'],
                'description' => $category_currently['description'],
                'keyword' => $category_currently['keyword'],
                'entries' => $entries,
            )
        );
    }

    /**
     * postSchedule
     * @param  Request $request
     * @return json
     */
    public function postFaq(Request $request) {
        $validator = $request->validate([
            'full_name' => 'required',
            'content' => 'required',
            'phone' => 'required',
        ], [
            'full_name.required' => trans('validation.required'),
            'content.required' => trans('validation.required'),
            'phone.required' => trans('validation.required'),
            'phone.regex' => trans('validation.phone'),
        ]);
        $data       = $request->all();
        if (sc_config('contact_to_admin')) {
            $checkContent = (new ShopEmailTemplate)
                ->where('group', 'faq')
                ->where('status', 1)
                ->first();
            if ($checkContent) {
                $content = $checkContent->text;
                $dataFind = [
                    '/\{\{\$full_name\}\}/',
                    '/\{\{\$phone\}\}/',
                    '/\{\{\$content\}\}/',
                ];
                $dataReplace = [
                    $data['full_name'],
                    $data['phone'],
                    $data['content'],
                ];
                $content = preg_replace($dataFind, $dataReplace, $content);
                $data_email = [
                    'content' => $content,
                ];

                $config = [
                    'to' => sc_store('email'),
                    'subject' => 'Faq',
                ];
                sc_send_mail('mail.contact_to_admin', $data_email, $config, []);
            }
        }
        return redirect()->back()
            ->with(['success' => trans('subscribe.subscribe_success')]);
    }
}
