<?php
namespace App\Http\Controllers\Web;

use App\Services\Common\CategoryService;
use App\Services\Common\HotWordService;
use Illuminate\Http\Request;

class CommonController extends WebBaseController
{
    protected $categoryService;
    protected $hotWordService;

    /**
     * CommonController constructor.
     * @param $categoryService
     * @param $hotWordService
     */
    public function __construct(CategoryService $categoryService, HotWordService $hotWordService)
    {
        $this->categoryService = $categoryService;
        $this->hotWordService = $hotWordService;
    }


    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('app.index');
    }

    /**
     * 职位分类对应描述及关联分类ajax返回
     */
    public function ajaxGetCategoryContent(Request $request)
    {
        $id = $request->id;
        !$id && response()->json(['status'=>0, 'msg'=>'请选择分类!']);
        $current_category = $this->categoryService->getCategoryJobs($id);
        $data = array();
        $data['show_relation'] = 0;
        $data['relation_data'][] = array('id'=>$id,'name'=>$current_category['name'],'desc'=>$current_category['content'],'current'=>1);
        if ($current_category['relation1']) {
            $idarr = explode(".", $current_category['relation1']);
            $relation_id = $idarr[2]==0?$idarr[1]:$idarr[2];
            $relation1 = $this->categoryService->getCategoryJobs($relation_id);
            if ($relation1['content']) {
                $data['show_relation'] = 1;
                $data['relation_data'][] = array('id'=>$relation1['id'],'name'=>$relation1['name'],'desc'=>$relation1['content'],'current'=>0);
            }
        }
        if ($current_category['relation2']) {
            $idarr = explode(".", $current_category['relation2']);
            $relation_id = $idarr[2]==0?$idarr[1]:$idarr[2];
            $relation2 = $this->categoryService->getCategoryJobs($relation_id);
            if ($relation2['content']) {
                $data['show_relation'] = 1;
                $data['relation_data'][] = array('id'=>$relation2['id'],'name'=>$relation2['name'],'desc'=>$relation2['content'],'current'=>0);
            }
        }
        return  response()->json(['status'=>0, 'msg'=>'请选择分类!', 'data'=>$data]);
    }

    //关键字联想
    public function hotWord(Request $request)
    {
        $key = $request->input('query', '');
        $key = trim($key);
        $hotword_type = session('hotword_show_type', 1);
        if (!$key) {
            return  response()->json(['status'=>0, 'msg'=>'请输入关健字!']);
        }
        //查询关键字
        $reg = $this->hotWordService->getHotWord($key, $hotword_type);
        $this->hotWordService->setMemberHotword($key, $hotword_type);       //会员关键字记录表
        if ($reg) {
            return  response()->json(['status'=>1, 'msg'=>'联想词获取成功!','data'=>array('query'=>$key,'suggestions'=>$reg)]);
            //$this->ajaxReturn(1,'联想词获取成功!',array('query'=>$key,'suggestions'=>$reg));
        } else {
            return  response()->json(['status'=>0, 'msg'=>'']);
        }
    }
    //联系方式图形化
    public function getFontImg(Request $request)
    {
        $text = $request->input('text', '');
        $type = $request->input('type', 'phone');
        $pwdhash = config('aix.system.site_other.site_other.pwb_hash');
        $text = decrypt($text, $pwdhash);
        //$img=\Intervention\Image\Facades\Image::canvas(200, 30, '#FFFFFF');
        $img=\Intervention\Image\Facades\Image::canvas(200, 30);
        $img->text($text, 0, 20, function ($font) {
            //$font->file(public_path('themes/default/assets/app/fonts/Source-Sans-Pro-Semibold.ttf'));     //引入字体
            $font->file(5);
            $font->size(14);
            $font->color('#643200');
            $font->align('left');
            //$font->valign('top');
        });
        $img->brightness(1);
        return $img->response('png');
    }
    public function listShowType(Request $request)
    {
        $action = $request->input('action');
        $type   = $request->input('type', '');
        session([$action.'_show_type' => $type]);
    }

}