<?php
namespace App\Http\Controllers\Jkq\Content;

use App\Http\Controllers\Jkq\JkqBaseController;
use App\Services\Company\JobsService;
use Illuminate\Http\Request;
use App\Exceptions\ResponseException;
use App\Services\Company\CompanyService;
use Illuminate\Support\Facades\View;

class CompanyController extends JkqBaseController
{
    protected $jobsService;
    protected $companyService;

    /**
     * JobsController constructor.
     * @param $jobsService
     * @param $companyService
     */
    public function __construct(JobsService $jobsService, CompanyService $companyService)
    {
        $this->jobsService = $jobsService;
        $this->companyService = $companyService;
    }
    public function companyShow(Request $request)
    {
        //获取企业信息
        $preview = $request->input('preview', 0);
        $company_id = $request->input('id');
        $tpl = $request->input('tpl');
        if ($preview && !$tpl) {
            $back_url = \Illuminate\Support\Facades\URL::previous();
            return $this->showMessage('模板不存在,请联系管理员!', $back_url, true, '上一页', '3');
        }
        $company_info = $this->companyService->getCompanyInfo(array(array('id','=',$company_id)));
        $this->putSeoData('company', $company_info);

        //在招职位
        $jobs_where = array(
            array('company_id','=',$company_info->id),
            array('valid','=',1),
            array('display','=',1)
        );
        $jobs_display = config('aix.companyset.comset.show_set.jobs_display');
        if ($jobs_display == 1) {
            $jobs_where[] = array('audit','=','1');
        } else {
            $jobs_where[] = array('audit','<>','3');
        }
        $jobs = $this->jobsService->getOtherJobs($jobs_where, 4);
        $all_jobs = $this->jobsService->getOtherJobs($jobs_where);
        if ($jobs->total() == 0) {
            $jobs = null;
            $all_jobs = null;
        }
        //相似职位信息。
        $category_id = [];
        $likeJobs = collect([]);
        if(!empty($all_jobs)){
            foreach ($all_jobs as $key=>$value){
                if($value->category){
                    $category_id[$key] = $value->category;
                }
            }
            $category_id = array_unique($category_id);
        }

        if($category_id){
            $new_jobs = array_diff_key($jobs_where,[0]);
            $likeJobs = $this->jobsService->getOtherJobs($new_jobs, 6,1,$category_id);
        }
        //看过该公司的人还看过(获取相同行业的企业)
        $company_where = array(
            'trade' => $company_info->trade,
            'except_id' =>$company_info->id
        );
        $other_companys = $this->companyService->getOtherCompanies($company_where, 5);

        $user = $this->getUser();
        if ($user && $user->utype==2) {
            //获取当前时间段内申请过的职位
            $space_time = (integer)config('aix.personal_set.per_set.per_set.apply_job_space');
            if ($space_time>0) {
                $stime = date('Y-m-d H:i:s', strtotime(date("Y-m-d", strtotime("-".$space_time." day"))));
                $apply_where = array(array('personal_uid','=',auth('web-member')->user()->id),array('created_at','>=',$stime));
            } else {
                $apply_where = array(array('personal_uid','=',auth('web-member')->user()->id));
            }
            $applys = $this->jobsService->getApplyJobs($apply_where);
        } else {
            $applys = array();
        }

        $return_data = array(
            'info'=>$company_info,
            'jobs'=>$jobs,
            'applys' => $applys,
            'all_jobs'=>$all_jobs,
            'other_companys'=>$other_companys,
            'company_img' => $company_info->img,
            'tpl'         => $tpl,
            'likeJobs'=>$likeJobs,
            'category_id'=>implode(',',$category_id)
        );
        if (!$tpl) {
            $tpl = $company_info->tpl;
            $is_scan = false;
        } else {
            $is_scan = true;
        }
        if ($tpl == 'default') {
            return view('jkq.common.tpl_company.default.company_show', $return_data);
        } else {
            if (View::exists('company.'.$tpl.'.index')) {
                $return_data['tpl'] = $tpl;
                return view('company.'.$tpl.'.index', $return_data);
            } else {
                if ($is_scan) {
                    $back_url = \Illuminate\Support\Facades\URL::previous();
                    return $this->showMessage('模板不存在,请联系管理员!', $back_url, true, '上一页', '3');
                } else {
                    return view('jkq.common.tpl_company.default.company_show', $return_data);
                }
            }
        }
    }
    public function moreCompany(Request $request)
    {
        $company_id = $request->input('id', 0);
        if (!$company_id) {
            return response()->json(['status'=>0, 'error'=>'参数错误!']);
        }
        $jobs_where = array(
            array('company_id','=',$company_id),
            array('valid','=',1),
            array('display','=',1)
        );
        $jobs_display = config('aix.companyset.comset.show_set.jobs_display');
        if ($jobs_display == 1) {
            $jobs_where[] = array('audit','=','1');
        } else {
            $jobs_where[] = array('audit','<>','3');
        }
        $limit = $request->input('limit', 10);
        $jobs = $this->jobsService->getOtherJobs($jobs_where, $limit);

        if ($jobs->isEmpty()) {
            return response()->json(['status'=>0, 'error'=>'没有更多职位了!']);
        }
        return response()->json(['status'=>1, 'jobs'=>json_encode($jobs->items()), 'jobs_num'=>count($jobs->items())]);
    }
    //关注企业
    public function companyFocus(Request $request)
    {
        $company_id = $request->input('company_id');
        if (!$company_id) {
            throw new ResponseException('请选择要关注的企业!');
        }

        $rst = $this->companyService->focusCompany($company_id, auth('web-member')->user()->id);
        return response()->json($rst);
    }
    //企业访客统计
    public function addStatistics(Request $request)
    {
        $company_id = $request->input('company_id');
        $this->companyService->addStatistics($company_id, 0, $this->getUser(), 0, 1);
        return response()->json(['status'=>1]);
    }
    public function getUser()
    {
        $user = array();
        if (auth('web-member')->check()) {
            $user = auth('web-member')->user();
        } elseif (auth('web-company')->check()) {
            $user = auth('web-company')->user();
        }
        return $user;
    }
    //在招职位页面
    public function jobs(Request $request)
    {
        //获取企业信息
        $company_id = $request->input('id');
        $company_info = $this->companyService->getCompanyInfo(array(array('id','=',$company_id)));
        $this->putSeoData('company', $company_info);

        //在招职位
        $jobs_where = array(
            array('company_id','=',$company_info->id),
            array('valid','=',1),
            array('display','=',1)
        );
        $jobs_display = config('aix.companyset.comset.show_set.jobs_display');
        if ($jobs_display == 1) {
            $jobs_where[] = array('audit','=','1');
        } else {
            $jobs_where[] = array('audit','<>','3');
        }
        $jobs = $this->jobsService->getOtherJobs($jobs_where);
        $user = $this->getUser();
        if ($user && $user->utype==2) {
            //获取当前时间段内申请过的职位
            $space_time = (integer)config('aix.personal_set.per_set.per_set.apply_job_space');
            if ($space_time>0) {
                $stime = date('Y-m-d H:i:s', strtotime(date("Y-m-d", strtotime("-".$space_time." day"))));
                $apply_where = array(array('personal_uid','=',auth('web-member')->user()->id),array('created_at','>=',$stime));
            } else {
                $apply_where = array(array('personal_uid','=',auth('web-member')->user()->id));
            }
            $applys = $this->jobsService->getApplyJobs($apply_where);
        } else {
            $applys = array();
        }

        //看过该公司的人还看过(获取相同行业的企业)
        $company_where = array(
            'trade' => $company_info->trade,
            'except_id' =>$company_info->id
        );
        $other_companys = $this->companyService->getOtherCompanies($company_where, 5);

        if ($request->input('tpl', '')) {
            $tpl = $request->input('tpl', '');
        } else {
            $tpl = $company_info->tpl;
        }

        //相似职位信息。
        $category_id = [];
        $likeJobs = collect([]);

        //在招职位bug,临时修改
        if( !empty($jobs)){
            foreach ($jobs as $key=>$value){
                if($value->category){
                    $category_id[$key] = $value->category;
                }
            }
            $category_id = array_unique($category_id);
        }
        if($category_id){
            $likeJobs = $this->jobsService->getOtherJobs($jobs_where, 6,1,$category_id);
        }
        if ($jobs->isEmpty()) {
            $jobs = null;
        }
        $return_data = array(
            'info'=>$company_info,
            'jobs'=>$jobs,
            'other_companys'=>$other_companys,
            'applys' => $applys,
            'tpl'    => $tpl,
            'likeJobs'=>$likeJobs,
            'category_id'=>implode(',',$category_id)
        );

        if ($tpl == 'default') {
            return view('jkq.common.tpl_company.default.company_jobs', $return_data);
        } else {
            if (View::exists('company.'.$tpl.'.company_jobs')) {
                $return_data['tpl'] = $tpl;
                return view('company.'.$tpl.'.company_jobs', $return_data);
            } else {
                return view('jkq.common.tpl_company.default.company_jobs', $return_data);
            }
        }
    }
}