<?php

namespace App\Http\Controllers\Admin\Core\Mail;

use App\Datatables\Admin\Core\Mail\MailTemplatesDataTable;
use App\Repositories\Core\Mail\MailTemplates;
use Illuminate\Http\Request;

class MailTemplateController extends Controller
{
    public function __construct()
    {
        // $this->middleware('ability:admin');
    }

    public function index(MailTemplatesDataTable $dataTable)
    {
        return $dataTable->render('Admin.Core.Mail.MailTemplate.index', $data ?? []);
    }

    public function modalCreate()
    {
        $data = MailTemplates::init();

        return view('Admin.Core.Mail.MailTemplate.modal', $data ?? []);
    }

    public function modalEdit($id = false)
    {
        $data = MailTemplates::init();
        $data['mail_template'] = MailTemplates::edit($id);

        return view('Admin.Core.Mail.MailTemplate.modal', $data);
    }

    public function storeAjax(Request $request)
    {
        $data = $request->all();
        unset($data['proengsoft_jsvalidation']);
        $ret = MailTemplates::store($data);

        return response()->json(['error' => 0]);
    }

    public function destroy(Request $request, $id = false)
    {
        $id = $id ?? $request->input('id');
        MailTemplates::destroy($id);

        return response()->json(['error' => 0]);
    }

    public function getVarsByMailable($mailable)
    {
        $data['vars'] = MailTemplates::getVarsByMailable($mailable);

        return view('Admin.Core.Mail.MailTemplate.partials.vars', $data);
    }

    public function preview($template_id, $model_id)
    {
        return MailTemplates::preview($template_id, $model_id);
    }

    public function modalPreview($template_id)
    {
        $data = MailTemplates::getDataFormodalPreview($template_id);

        return view('Admin.Core.Mail.MailTemplate.partials.modalPreview', $data);
    }
}
