<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\common\model\OutFollow;
use app\common\model\OutResume as OutResumeModel;

class Outresume extends BaseController
{

    // 列表
    public function index()
    {
        $status_list = OutResumeModel::$status;

        return view('outresume/resumelist', [
            'status_list' => $status_list,
        ]);
    }

    public function listresume()
    {
        $limit   = input('limit');
        $page    = input('page');
        $map     = [];
        $keyword = input('keyword/s');
        if (!empty($keyword)) {
            $map[] = ['name|mobile', 'like', "%{$keyword}%"];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $list  = OutResumeModel::with('broker')
            ->where($map)
            ->order(['updatetime' => 'desc'])
            ->limit($limit)
            ->page($page)
            ->append(['status_text', 'gender_text'])
            ->select();
        $count = OutResumeModel::where($map)->count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }

    public function resume()
    {
        $id     = input('get.id');
        $resume = OutResumeModel::with(['broker'])->where('id', $id)->find();
        if (empty($resume)) {
            exit('该数据不存在');
        }
        $status_list = OutResumeModel::$status;

        return view('outresume/resumeform', [
            'status_list' => $status_list,
            'resume'      => $resume,
        ]);
    }

    public function editresume()
    {
        $data = input('post.');

        OutResumeModel::update($data);

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

    // 用户跟进记录
    public function follow()
    {
        $id          = input('id/d');
        $resume      = OutResumeModel::findOrEmpty($id);
        $follow_list = OutFollow::where('resume_id', $id)->order('id', 'desc')->limit(100)->select();
        $status_list = OutResumeModel::$status;
        return view('outresume/follow', [
            'resume'      => $resume,
            'follow_list' => $follow_list,
            'status_list' => $status_list,
        ]);
    }

    public function editFollow()
    {
        $resume_id = input('resume_id/d', 0);
        $resume    = OutResumeModel::findOrEmpty($resume_id);
        if ($resume->isEmpty()) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "信息不存在。",
            ]));
        }
        $status = input('status/d', 1);
        OutFollow::create([
            'resume_id'  => $resume_id,
            'remark'     => input('remark/s', ""),
            'status'     => $status,
            'createtime' => time(),
        ]);
        $resume->save([
            'status'     => $status,
            'updatetime' => time(),
        ]);
        exit(json_encode([
            'code' => 0,
        ]));
    }
}