<?php

namespace app\worker\controller;

use app\worker\BaseController;
use app\common\model\AgentMarket as AgentMarketModel;
use app\common\model\OutRecruit as OutRecruitModel;
use app\common\model\OutRecruitReport as OutRecruitReportModel;
use app\common\model\OutCode as OutCodeModel;

use think\facade\Request;

class Outjob extends BaseController
{
    /**
     * 招聘信息
     */
    public function index()
    {
        $workerid = $this->access_worker['id'];
        if (Request::isAjax()) {
            $limit = input('limit/d', 20);
            $page  = input('page/d', 1);

            $map      = [];
            $map[]    = ['worker_id', '=', $workerid];
            $keywords = input('keywords/s', "");
            if (!empty($keywords)) {
                $map[] = ['title', 'like', '%' . $keywords . '%'];
            }
            $status = input('status/d');
            if (!empty($status)) {
                $map[] = ['status', '=', $status];
            }

            $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 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', ""),
            '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['status'] = 2;
        if (empty($id)) {
            $data['worker_id']  = $this->access_worker['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 recruitform()
    {
        $id      = input('id/d, 0');
        $recruit = OutRecruitModel::findOrEmpty($id);

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

    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');
        }
    }

    public function print()
    {
        return view('outjob/print');
    }

    /**
     * 推广码
     */
    public function qrcode()
    {
        error_reporting(E_ERROR);
        $code = uniqid();
        $url  = 'https://linggong.jinjianghc.com/applet/outactivity/index?code=' . $code;
        OutCodeModel::create([
            'code'        => $code,
            'create_time' => time(),
            'workerid'    => $this->access_worker['id'],
        ]);
        header('Content-Type: image/png');
        ob_clean();
        $errorCorrectionLevel = "L"; // 纠错级别：L、M、Q、H
        $matrixPointSize      = "4"; //生成图片大小 ：1到10
        \phpqrcode\QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize);
        exit();
    }
}
