<?php

namespace App\Http\Controllers\Web\Hardware\Aio;

use App\Http\Controllers\Web\WebBaseController;
use App\Services\Company\JobsService;
use Illuminate\Http\Request;
use App\Services\Common\CategoryService;
use App\Services\Common\SearchService;
use App\Services\Company\PersonalJobsApplyService;
use App\Services\Person\ResumeService;

class JobController extends WebBaseController
{
    protected $categoryService;
    protected $jobsService;
    protected $searchService;
    protected $personalJobsApplyService;
    protected $resumeService;

    /**
     * JobController constructor.
     * @param $categoryService
     * @param $jobsService
     * @param $searchService
     * @param $personalJobsApplyService
     * @param $resumeService
     */
    public function __construct(CategoryService $categoryService, JobsService $jobsService, SearchService $searchService, PersonalJobsApplyService $personalJobsApplyService,ResumeService $resumeService)
    {
        $this->categoryService = $categoryService;
        $this->jobsService = $jobsService;
        $this->searchService = $searchService;
        $this->personalJobsApplyService = $personalJobsApplyService;
        $this->resumeService = $resumeService;
    }

    public function index(Request $request)
    {
        $params = array();
        $param_array = array('citycategory','wage','trade','education','key');
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array) && $v) {
                    $params[$k] = $v;
                }
            }
        }

        //职位类别信息
        $filter_where = array(
            'AIX_wage'         => 100,      //薪资
            'AIX_trade'        => 100,      //职位类别信息
            'AIX_education'    => 100       //学历要求
        );
        $categories = $this->categoryService->getCategories($filter_where);     //过滤条件信息
        //所在地区(默认地区)
        $citycategory = $request->input('citycategory', '');
        $citys = $this->categoryService->getCitys($citycategory);

        //查询数据
        $where = $this->setWhere($params, $citys);
        $order_by = array('stime'=>'desc', 'refresh_time'=>'desc');
        $search_key = '';
        if ($params) {
            $search_key = array_has($params, 'key')?$params['key']:'';
        }

        $list = $this->searchService->search('Job', $where, $order_by, $search_key, 6);
        if ($list->total()>0) {
            $list_items = $this->jobsService->dealjobFilelds($list->items());
        } else {
            $list_items = array();
        }
        $return_data = array(
            'categories' => $categories,
            'params'     => $params,
            'arealist'   => $citys,
            'list'       => $list,
            'list_items' => $list_items,
            'user'       => $this->getUser()
        );

        return view('app.hardware.aio.jobs.index', $return_data);
    }
    public function jobDetail($id)
    {
        //获取职位详细信息
        $job_rst = $this->jobsService->getJobInfo(array('id'=>$id), $this->getUser());
        if ($job_rst['status'] == 0) {
            $back_url = \Illuminate\Support\Facades\URL::previous();
            return $this->showMessage($job_rst['error'], $back_url, true, '上一页', '3');
        }
        $job_info = $job_rst['job'];
        $return_data = array(
            'info' => $job_info,
            'user' => $this->getUser()
        );
        return view('app.hardware.aio.jobs.detail', $return_data);
    }
    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 jobApply(Request $request)
    {
        //判断个人资料是否完善
        $jobs_id = $request->input('jobs_id');
        $resume_id = $request->input('resume_id');
        $user = $this->getUser();
        if (!$jobs_id) {
            $return_data = array(
                'status' => 0,
                'html'   => '请选择要投递的职位!'
            );
            return response()->json($return_data);
        }
        if (!is_array($jobs_id)) {
            $jobs_id = explode(',', $jobs_id);
        }
        if ($resume_id) {
            $this->resumeService->isOwn($resume_id,$user);
            $apply_rst = $this->personalJobsApplyService->applyJobs($jobs_id, $resume_id, $this->getUser());
            if (array_get($apply_rst, 'status') == 0) {
                $return_data = array(
                    'status'    => array_get($apply_rst, 'status'),
                    'html'      => array_get($apply_rst, 'error')
                );
                return response()->json($return_data);
            } else {
                $html = '投递成功!';
                if (array_get($apply_rst, 'status') == 1 && count($jobs_id) ==1) {
                    $html = $apply_rst['data']['list'][$jobs_id[0]]['tip'];
                }
                $return_data = array(
                    'status'    => array_get($apply_rst, 'status'),
                    'html'      => $html
                );
                return response()->json($return_data);
            }
        } else {
            //未选择投递简历前
            $rst = $this->personalJobsApplyService->ifJobApply($jobs_id, $this->getUser());
            //判断是否需要选择简历
            if (array_get($rst, 'status') == 2) {
                $page_data = array('status'=>array_get($rst, 'status'),'resumes'=>array_get($rst, 'resumes'),'def_resume'=>array_get($rst, 'def_resume'),'jobs_id'=>implode(',', $jobs_id));
                $html = view('app.hardware.aio.jobs.ajax_apply', $page_data)->render();
                $return_data = array( 'status'=>2, 'html' => $html);
                return response()->json($return_data);
            } else {
                $return_data = array();
                if (array_get($rst, 'status') == 0) {
                    $return_data = array(
                        'status'    => array_get($rst, 'status'),
                        'html'      => array_get($rst, 'error')
                    );
                    return response()->json($return_data);
                } else {
                    $html = '投递成功!';
                    if (array_get($rst, 'status') == 1 && count($jobs_id) ==1) {
                        $html = $rst['data']['list'][$jobs_id[0]]['tip'];
                    }
                    $return_data = array(
                        'status'    => array_get($rst, 'status'),
                        'html'      => $html
                    );
                    return response()->json($return_data);
                }
            }
        }
    }

    public function setWhere($params, $citys)
    {
        $where = array(
            array('valid','=',1),
            array('display','=',1)
        );
        if (config('aix.companyset.comset.show_set.jobs_display')==1) {
            $where[] = array('audit','=',1);
        } else {
            $where[] = array('audit','<>',3);
        }
        if ($params) {
            foreach ($params as $k => $v) {
                if ($k == 'citycategory') {
                    //地标地段
                    $where[] = array('district','=',$citys['select']['id']);
                } elseif ($k =='wage') {
                    //获取薪资最大值和最小值
                    $filter_where = array( 'AIX_wage'=> 100);
                    $categories = $this->categoryService->getCategories($filter_where);
                    if ($categories) {
                        $wage = $categories['AIX_wage'][$params['wage']]['origin_demand'];
                        if ($wage) {
                            $wage = format_wage($wage);
                        }
                        $wage_arr = explode_wage($wage);
                        $where[] = $wage_arr[0];
                        $where[] = $wage_arr[1];
                    }
                } elseif ($k !='sort' && $k !='key' && $k !='search_type' && $k !='m_zoom') {
                    $where[] = array($k, '=', $v);
                }
            }
        }
        $where[] = ['subsite_ids','=', get_subsite_id()];
        return $where;
    }
}