<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\odd_job\JobModel;
use app\common\model\odd_job\UserModel;
use app\common\model\odd_job\WorkerModel;
use app\common\model\SettingModel;

class OddJob extends AdminBaseController
{

    /**
     * 公司
     */
    public function company()
    {
        return view();
    }

    public function listCompany()
    {
        $map = $this->dealEqualInput(['status','wtype'],$this->dealLikeInput(['title']));

        $list  = WorkerModel::where($map)
            ->order(['priority'=>'desc','id' => 'desc'])
            ->append(['wtype_text', 'status_text'])
            ->limit(input('limit'))
            ->page(input('page'))
            ->select();
        $count = WorkerModel::where($map)->count();

        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 岗位
     */
    public function job()
    {
        return view();
    }

    public function listJob()
    {
        $map = $this->dealEqualInput(['status','wtype'],$this->dealLikeInput(['title']));

        $list  = JobModel::with(['worker'])
            ->where($map)
            ->order(['updatetime'=>'desc'])
            ->append(['wtype_text', 'status_text'])
            ->limit(input('limit'))
            ->page(input('page'))
            ->select();
        $count = JobModel::where($map)->count();

        $list = $list->toArray();
        foreach ($list as $k => $v) {
            if (!empty($v['company_name'])) {
                $list[$k]['worker']['title'] = $v['company_name'];
            }
        }

        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 求职者
     */
    public function user()
    {
        return view();
    }

    public function listUser()
    {
        $map = $this->dealEqualInput(['status','authstatus','followstatus'],$this->dealLikeInput(['keywords'=>'nickname|realname|mobile']));

        $list  = UserModel::where($map)
            ->order(['id'=>'desc'])
            ->append(['status_text', 'authstatus_text','followstatus_text','education_text','worker_text'])
            ->limit(input('limit'))
            ->page(input('page'))
            ->select();
        $count = UserModel::where($map)->count();

        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 列表
     */
    public function setting()
    {
        $system = SettingModel::getConfigValue(SettingModel::ODD_JOB);
        return view('', [
            'system' => $system,
        ]);
    }

    public function editSetting()
    {
        $data = $this->_post(SettingModel::ODD_JOB);
        SettingModel::setConfigValue($data);
        ajax_return();
    }

    /**
     * 获取数据
     * @param array $param 要获取的数据
     * @return array
     */
    public function _post(array $param)
    {
        $res = [];
        foreach ($param as $v) {
            if (input($v) === '0' || !empty(input($v))) {
                $res[$v] = input($v);
            }
        }

        return $res;
    }
}