<?php

namespace App\Http\Controllers\Jkq\Person;

use App\Http\Controllers\Jkq\JkqBaseController;
use App\Models\PersonJobsSubscribe;
use App\Services\Common\CategoryService;
use App\Services\Person\PersonFavoriteService;
use App\Services\Person\ResumeService;
use App\Validators\PersonValidatorRequest;

class PersonFavoriteController extends JkqBaseController
{
    /**
     * @var PersonFavoriteService
     */
    protected $PersonFavoriteService;
    protected $resumeService;
    protected $categoryService;

    /**
     * PersonFavoriteController constructor.
     * @param PersonFavoriteService $PersonFavoriteService
     * @param ResumeService $resumeService
     * @param CategoryService $categoryService
     */
    public function __construct(PersonFavoriteService $PersonFavoriteService, ResumeService $resumeService, CategoryService $categoryService)
    {
        $this->PersonFavoriteService = $PersonFavoriteService;
        $this->resumeService = $resumeService;
        $this->categoryService = $categoryService;
    }


    public function jobsF()
    {
        $res = $this->PersonFavoriteService->jobsF(auth('web-member')->user());

        return view('jkq.person.jobs_favorite', ['content'=>$res]);
    }

    public function delPersonFavorite()
    {
        if (request()->method()=='POST') {
            $res = $this->PersonFavoriteService->delPersonFavorite(request()->id,auth('web-member')->user());
            if ($res) {
                return $this->sendSuccessResponse('删除成功');
            } else {
                return $this->sendErrorResponse('删除失败');
            }
        } else {
            return view('jkq.person.ajax.resume_delete', ['tpis'=>'删除后将无法恢复,您确定要删除选中的收藏职位吗?']);
        }
    }
    public function resumeApply()
    {
        if (request()->method()=='POST') {
            $res = $this->PersonFavoriteService->resumeApply(request()->all(), auth('web-member')->user());
            if ($res['code']==1) {
                return $this->sendSuccessResponse($res['info']);
            } else {
                return $this->sendErrorResponse($res['info']);
            }
        } else {
            $timeG = config('aix.personal_set.per_set.per_set.apply_job_space');
            if ($timeG>0) {
                $res = $this->resumeService->getSuccessResumeCount(auth('web-member')->user());
                if ($res['code']==0) {
                    return json_encode(['code'=>0,'info'=>$res['info']]);
                } elseif ($res['code']==1) {
                    $html = view('jkq.person.ajax.apply_resume', ['resume'=>$res['info'],'timeG'=>$timeG])->render();
                    return json_encode(['code'=>1,'info'=>$html]);
                } else {
                    $html = view('jkq.person.ajax.resume_tip', ['resume'=>$res['info'],'tpis'=>$timeG.'天内已投递的职位不再投递!'])->render();
                    return json_encode(['code'=>1,'info'=>$html]);
                }
            } else {
                return json_encode(['code'=>0,'info'=>'投递功能已关闭!']);
            }
        }
    }

    public function attentionCom()
    {
        $res = $this->PersonFavoriteService->attentionCom(auth('web-member')->user());
        return view('jkq.person.attention_com', ['content'=>$res]);
    }

    public function deleteCompany()
    {
        $res = $this->PersonFavoriteService->deleteCompany(request()->id,auth('web-member')->user());
        if ($res) {
            return $this->sendSuccessResponse('删除成功');
        } else {
            return $this->sendErrorResponse('删除失败');
        }
    }

    public function getJobsSub()
    {
        $res = $this->PersonFavoriteService->getJobsSub();
        $count = $this->PersonFavoriteService->getCount();
        return view('jkq.person.attention_sub', ['content'=>$res,'count'=>$count]);
    }

    public function getJobsSubEdit()
    {
        $back_url = \Illuminate\Support\Facades\URL::previous();
        if(request()->id){
            $sub_info = PersonJobsSubscribe::find(request()->id);
            if(!$sub_info){
                return $this->showMessage('对不起,您只能查看自己的职位订阅器!', $back_url, true, '上一页', '3');
            }

            if($sub_info->uid!=auth('web-member')->user()->id){
                return $this->showMessage('对不起,您只能查看自己的职位订阅器!', $back_url, true, '上一页', '3');
            }
        }
        $res = $this->PersonFavoriteService->getJobsSubEdit(request()->id);
        $district = $this->categoryService->getDefaultDistrict();
        return view('jkq.person.attention_sub_edit', ['content'=>$res['sub'],'defaultCity'=>$district->defaultCity]);
    }

    public function getJobsSubAdd(PersonValidatorRequest $request)
    {
        if($request->id) {
            $id = $request->id;
            $user = auth('web-member')->user();
            $this->PersonFavoriteService->isOwn($id,$user);
        }
        $res= $this->PersonFavoriteService->getJobsSubAdd($request->all(), auth('web-member')->user());
        if ($res) {
            return $this->sendSuccessResponse('更新成功');
        } else {
            return $this->sendErrorResponse('更新失败');
        }
    }

    public function getJobsSubout()
    {
        $id = request()->get('id');
        $user = auth('web-member')->user();
        $this->PersonFavoriteService->isOwn($id,$user);
        $res= $this->PersonFavoriteService->getJobsSubout(request()->all());
        if ($res['code']==1) {
            return $this->sendSuccessResponse($res['data']);
        } else {
            return $this->sendErrorResponse('退订失败');
        }
    }

    public function getJobsAddVar()
    {
        $res= $this->PersonFavoriteService->getJobsAddVar();
        if ($res) {
            return $this->sendSuccessResponse('ok');
        } else {
            return $this->sendErrorResponse('您最多可以添加 5 个职位订阅器');
        }
    }

    public function getJobsSubDel()
    {
        if (request()->method()=='POST') {
            $id = request()->get('id');
            $user = auth('web-member')->user();
            $this->PersonFavoriteService->isOwn($id,$user);
            $res= $this->PersonFavoriteService->getJobsSubDel(request()->id);
            if ($res) {
                return $this->sendSuccessResponse('删除成功');
            } else {
                return $this->sendErrorResponse('删除失败');
            }
        } else {
            return view('jkq.person.ajax.resume_delete', ['tpis'=>'删除后将无法恢复,您确定要删除该订阅器吗?']);
        }
    }

}