<?php
namespace App\Http\Controllers\Mobile\Active;

use App\Http\Controllers\Mobile\MobileBaseController;
use App\Services\Common\CategoryService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class RecruitController extends MobileBaseController
{
    protected $categoryService;
    public function __construct(CategoryService $categoryService)
    {
        $this->categoryService = $categoryService;
    }

    public function index(Request $request)
    {
        $offset = isset($request->page) ? $request->page : 0;
        $limit  = 8;

        $citycategory = 'jjkfq';
        $trade        = $request->input('trade', '');
        $param_array  = ['citycategory', 'trade', 'nature'];

        $params = [];
        if ($request->all()) {
            foreach ($request->all() as $k => $v) {
                if (in_array($k, $param_array) && $v) {
                    $params[$k] = $v;
                }
            }
        }

        $filter_where = [
            'AIX_trade'        => 100,
            'AIX_company_type' => 100,
        ];
        $categories   = $this->categoryService->getCategories($filter_where);

        $subsites = Cache::get('subsites_list');
        if ($subsites) {
            if (!array_has($params, 'citycategory')) {
                if (get_subsite_id() > 0) {
                    $citycategory = $subsites[get_subsite_id()]['district'];
                }
            }
        }

        $title = '晋江市新春招聘会';
        $citys = $this->categoryService->getCitys($citycategory);

        if (empty($citycategory)) {
            $district_str = "(district like ? or district like ?)";
            $district_arr = ["%.623%", "%623.%"];
        } else {
            $select_id    = $citys['select']['id'];
            $district_str = "(district like ? or district like ?)";
            $district_arr = ["%.{$select_id}%", "%{$select_id}.%"];
            $title        = $citys['select']['name'] . '专区';
        }
        $whereRaw = "jobs.updated_at > '2022-06-01 00:00:00' and jobs.deleted_at is null and jobs.valid = 1 and jobs.audit = 1 and jobs.display = 1 and education > 68";
        $companys = DB::table('jobs')->join('companys', 'jobs.company_id', '=', 'companys.id')->whereRaw($whereRaw)->groupBy('jobs.company_id')->select('jobs.company_id')->pluck('company_id')->toArray();

        $where   = [];
        $where[] = ['user_status', '=', 1];
        $where[] = ['audit', '=', 1];
        $where[] = ['deleted_at', '=', null];
        if (!empty($trade)) {
            $where[] = ['trade', '=', $trade];
        }

        $list = DB::table('companys')->where($where)->whereRaw($district_str, $district_arr)->whereIn('id', $companys)->orderBy('sort_index', 'desc')->orderBy('id', 'desc')->offset($limit * $offset)->limit($limit)->get();
        $more = count($list) >= $limit ? true : false;

        $res = [];
        foreach ($list as $val) {

            $item = [
                'id'          => $val->id,
                'companyname' => $val->companyname,
                'jobs'        => [],
                'tag_arr'     => [],
            ];

            //在招职位
            $jobs_where = [
                ['company_id', '=', $val->id],
                ['valid', '=', 1],
                ['display', '=', 1],
                ['audit', '=', 1],
                ['deleted_at', '=', null],
                ['education', '>', 68],
            ];

            $jobs = DB::table('jobs')->where($jobs_where)->get();
            if (!$jobs->isEmpty()) {
                foreach ($jobs as $value) {
                    $job = [
                        'id'        => $value->id,
                        'jobs_name' => $value->jobs_name,
                        'amount'    => $value->amount,
                        'wage'      => $value->wage,
                        'wage_min'  => $value->wage_min,
                        'wage_max'  => $value->wage_max,
                    ];
                    array_push($item['jobs'], $job);
                }
            }

            //企业福利
            if (!empty($val->tag)) {
                $tags            = explode(',', $val->tag);
                $tag_arr         = DB::table('categorys')->whereIn('id', $tags)->limit(3)->get(['demand'])->toArray();
                $item['tag_arr'] = $tag_arr;
            }

            array_push($res, $item);
        }

        if ($request->ajax()) {
            if (count($res)) {
                return response()->json(['status' => 1, 'data' => view('mobile.app.active.ajax.ajax_recruit', [
                    'res'        => $res,
                    'city'       => $citys,
                    'params'     => $params,
                    'categories' => $categories,
                    'more'       => $more,
                ])->render()]);
            }
            return response()->json(['status' => 0]);
        }

        return view('mobile.app.active.recruit', [
            'title'           => $title,
            'res'             => $res,
            'city'            => $citys,
            'params'          => $params,
            'categories'      => $categories,
            'more'            => $more,
            'share_title'     => "【{$title}】免费找工作",
            'share_desc'      => '晋江好福利!免费找工作、找人才,还能抽奖,100%中奖,万份好礼免费拿!',
            'share_image_url' => theme_asset('mobile/images/online2021/share_logo.jpg'),
            'share_link'      => route('mobile.active.spring_special') . '?citycategory=' . $citycategory,
        ]);
    }
}