<?php

namespace app\mobile\controller;

use app\common\model\OutFollow;
use app\common\model\OutResume as OutResumeModel;
use app\common\model\OutResume;
use app\mobile\MobileBaseController;
use app\mobile\validate\FollowValidate;
use app\mobile\validate\ResumeValidate;
use think\App;
use think\exception\ValidateException;
use think\facade\View;

class Resume extends MobileBaseController
{
    private $_broker = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->_broker = get_broker();
        View::assign('broker', $this->_broker);
    }

    /**
     * 简历列表
     */
    public function index()
    {
        return view('resume/index');
    }

    /**
     * 收益明细列表
     */
    public function listResume()
    {
        $map = $this->dealLikeInput(['searchKey' => 'name|mobile']);

        $map[] = ['brokerid', '=', $this->_broker['id']];
        $list  = OutResumeModel::where($map)
            ->order(['updatetime' => 'desc'])
            ->limit(input('limit', 5))
            ->page(input('page', 1))
            ->append(['status_text'])
            ->select();

        foreach ($list as $v) {
            $log = OutFollow::where('resume_id', $v['id'])->order('id desc')->find();
            if (!empty($log)) {
                $v['last_msg']      = $log['remark'];
                $v['last_msg_time'] = $log['createtime'];
            } else {
                $v['last_msg']      = '未跟进';
                $v['last_msg_time'] = '';
            }
        }

        ajax_success($list);
    }

    /**
     * 表单
     */
    public function form()
    {
        $id   = input('get.id');
        $data = ['form' => '{gender:1}'];

        if (!empty($id)) {
            $form = OutResume::where('id', $id)->find();
            if (!empty($form)) {
                $data['form'] = $form;
            }
        }

        return view('resume/form', $data);
    }

    /**
     * 表单提交
     */
    public function formPost()
    {
        $data = input('post.');
        try {
            validate(ResumeValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        $mobile_check = OutResumeModel::where('mobile', $data['mobile'])->find();
        $mobile_check && ajax_return(1, '该手机号已被登记！');

        $data['brokerid']   = $this->_broker['id'];
        $data['updatetime'] = time();

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

        ajax_return();
    }

    /**
     * 跟进
     */
    public function follow()
    {
        $id     = input('get.id');
        $resume = OutResume::where('id', $id)->append(['gender_text'])->find();
        if (empty($resume)) {
            jump('该简历不存在');
        }

        $follow = OutFollow::where('resume_id', $id)->append(['status_text'])->order('id', 'desc')->select();

        return view('resume/follow', [
            'id'          => $id,
            'resume'      => json_encode($resume),
            'follow'      => $follow,
            'status_list' => json_encode(OutFollow::$status),
        ]);
    }

    /**
     * 跟进提交
     */
    public function followPost()
    {
        $data = input('post.');
        try {
            validate(FollowValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        OutResume::update(['updatetime' => time()], ['id' => $data['resume_id']]);

        $data['createtime'] = time();
        OutFollow::create($data);

        ajax_return();
    }
}
