<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\AgentMarket as AgentMarketModel;
use app\common\model\OutRecruit as OutRecruitModel;
use app\common\model\OutRecruitReport as OutRecruitReportModel;

use think\facade\Request;

class Outjob extends BaseController
{
    /**
     * 招聘信息
     */
    public function index()
    {
        if (Request::isAjax()) {
            $limit = input('limit/d', 20);
            $page  = input('page/d', 1);

            $map      = [];
            $keywords = input('keywords/s', "");
            if (!empty($keywords)) {
                $map[] = ['title', 'like', '%' . $keywords . '%'];
            }
            $status = input('status/d', 0);
            if (!empty($status)) {
                $map[] = ['status', '=', $status];
            }
            $source = input('source/s', '');
            if ($source == 'admin') {
                $map[] = ['worker_id', '=', 0];
            } elseif ($source == 'worker') {
                $map[] = ['worker_id', '>', 0];
            }

            $list  = OutRecruitModel::where($map)
                ->order(['priority' => 'desc', 'id' => 'desc'])
                ->limit($limit)
                ->page($page)
                ->append(['status_text'])
                ->select();
            $count = OutRecruitModel::where($map)->count();
            if ($count == 0) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => "未查询到数据",
                ]));
            }
            exit(json_encode([
                'code'  => 0,
                'msg'   => "",
                'count' => $count,
                'data'  => $list,
            ]));

        } else {
            return view('outjob/index');
        }

    }

    public function recruitform()
    {
        $id      = input('id/d, 0');
        $recruit = OutRecruitModel::findOrEmpty($id);

        return view('outjob/recruitform', [
            'recruit' => $recruit,
        ]);
    }

    public function editrecruit()
    {
        $id = input('id/d', 0);

        $data = [
            'title'          => input('title/s', ""),
            'company_name'   => input('company_name/s', ""),
            'num'            => input('num/d', 1),
            'province'       => input('province/s', ""),
            'city'           => input('city/s', ""),
            'district'       => input('district/s', ""),
            'address'        => input('address/s', ""),
            'agegroup'       => input('agegroup/s', ""),
            'tags'           => input('tags/a', []),
            'requirement'    => input('requirement/s', ""),
            'comdetails'     => input('comdetails/s', ""),
            'picall'         => input('picall/a', []),
            'salary'         => input('salary/s', ""),
            'telephone'      => input('telephone/s', ""),
            'remark'         => input('remark/s', ""),
            'status'         => input('status/d', 1),
            'priority'       => input('priority/d', 255),
            'volume'         => input('volume/d', 0),
            'market_content' => input('market_content/s', ""),
            'is_bargain'     => input('is_bargain/d', 1),
            'updatetime'     => time(),
        ];

        if (empty($id)) {
            $data['createtime'] = time();
            OutRecruitModel::create($data);
        } else {
            OutRecruitModel::update($data, ['id' => $id]);
        }

        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function delrecruit()
    {
        $id = input('id/d');

        $res = OutRecruitReportModel::where('recruit_id', $id)->find();
        if (!empty($res)) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "已有报备记录，无法删除",
            ]));
        }

        OutRecruitModel::destroy($id);

        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function report()
    {
        $id = input('id/d', 0);
        if (Request::isAjax()) {
            $limit = input('limit/d', 20);
            $page  = input('page/d', 1);

            $map    = [
                ['recruit_id', '=', $id],
            ];
            $status = input('status/d');
            if (!empty($status)) {
                $map[] = ['status', '=', $status];
            }

            $list  = OutRecruitReportModel::with(['broker'])
                ->where($map)
                ->order(['createtime' => 'desc'])
                ->limit($limit)
                ->page($page)
                ->append(['status_text'])
                ->select();
            $count = OutRecruitReportModel::where($map)->count();
            if ($count == 0) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => "未查询到数据",
                ]));
            }
            exit(json_encode([
                'code'  => 0,
                'msg'   => "",
                'count' => $count,
                'data'  => $list,
            ]));

        } else {
            return view('outjob/report', ['id' => $id]);
        }

    }

    public function dealreport()
    {
        $id = input('id/d', 0);
        if (Request::isAjax()) {
            $data = [
                'status'    => input('status/d', 1),
                'retremark' => input('retremark/s', ''),
            ];

            OutRecruitReportModel::update($data, ['id' => $id]);

            exit(json_encode([
                'code' => 0,
            ]));
        } else {
            $report = OutRecruitReportModel::find($id);
            return view('outjob/dealreport', ['report' => $report]);
        }
    }

    public function market()
    {
        if (Request::isAjax()) {
            $limit = input('limit/d', 20);
            $page  = input('page/d', 1);

            $list  = AgentMarketModel::limit($limit)
                ->page($page)
                ->append(['is_bargain_text'])
                ->select();
            $count = AgentMarketModel::count();
            if ($count == 0) {
                exit(json_encode([
                    'code' => 1,
                    'msg'  => "未查询到数据",
                ]));
            }
            exit(json_encode([
                'code'  => 0,
                'msg'   => "",
                'count' => $count,
                'data'  => $list,
            ]));

        } else {
            return view('outjob/market');
        }
    }
}
