<?php

namespace app\mainapp\controller;

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

class Woutjobs extends BaseController
{
    public function listjobs()
    {
        $status   = input('status/d', 1);
        $workerid = input('workerid/d', 0);
        $ppage    = input('ppage/d', 1);
        $psize    = input('psize/d', 20);
        $map      = [];
        $map[]    = ['worker_id', '=', $workerid];
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $plist = OutRecruitModel::where($map)
            ->order(['priority' => 'desc', 'id' => 'desc'])
            ->limit($psize)
            ->page($ppage)
            ->append(['status_text'])
            ->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function delrecruit()
    {
        $id        = input('id/d', 0);
        $worker_id = input('workerid/d', 0);

        $res = OutRecruitReportModel::where('recruit_id', $id)->find();
        if (!empty($res)) {
            page_result(1, "已有报备记录，无法删除");
        }
        OutRecruitModel::where('worker_id', $worker_id)->where('id', $id)->delete();

        page_result();
    }

    public function getmarket()
    {
        $list = AgentMarketModel::where(1)->append(['is_bargain_text'])
            ->select();
        page_result(0, "", [
            'list' => $list,
        ]);
    }

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

        $data = [
            'title'          => input('title/s', ""),
            'worker_id'      => input('worker_id/d', 0),
            '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/s', ""),
            'requirement'    => input('requirement/s', ""),
            'comdetails'     => input('comdetails/s', ""),
            'picall'         => input('picall/s', ""),
            'salary'         => input('salary/s', ""),
            'telephone'      => input('telephone/s', ""),
            'remark'         => input('remark/s', ""),
            '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(),
        ];

        $data['picall'] = json_decode($data['picall'], true);
        $data['tags']   = explode(' ', $data['tags']);
        $data['status'] = 2;
        if (empty($id)) {
            $data['createtime'] = time();
            OutRecruitModel::create($data);
        } else {
            OutRecruitModel::update($data, ['id' => $id]);
        }

        page_result();
    }

    public function getrecruit()
    {
        $id = input('id/d', '0');
        if (empty($id)) {
            page_result(0, "", []);
        }

        $recruit         = OutRecruitModel::findOrEmpty($id);
        $recruit['tags'] = implode(' ', $recruit['tags']);

        page_result(0, "", $recruit);
    }
}