<?php

namespace app\agent\controller;

use app\agent\BaseController;
use app\common\model\OutResume as OutResumeModel;
use app\common\model\Broker as BrokerModel;
use app\common\model\Config as ConfigModel;
use app\common\service\AgentMoneyService;
use app\common\service\IncomeService;

class Out extends BaseController
{
    /**
     * 简历库
     */
    public function resume()
    {
        $statuslist = OutResumeModel::$confirm_status;
        return view('out/resumelist', [
            'statuslist' => $statuslist,
        ]);
    }

    public function listResume()
    {
        $limit   = input('limit');
        $page    = input('page');
        $map     = [];
        $keyword = input('keywords/s');
        if (!empty($keyword)) {
            $map[] = ['name|mobile', 'like', "%{$keyword}%"];
        }
        $confirm_status = input('confirm_status/d');
        if (!empty($confirm_status)) {
            $map[] = ['confirm_status', '=', $confirm_status];
        }
        $broker_ids = BrokerModel::where('agentid', $this->access_agent['id'])->column('id');
        if (empty($broker_ids)) {
            $map[] = ['brokerid', '=', 0];
        } else {
            $map[] = ['brokerid', 'in', $broker_ids];
        }
        $list  = OutResumeModel::with('broker')
            ->where($map)
            ->order(['confirm_status' => 'asc'])
            ->limit($limit)
            ->page($page)
            ->append(['confirm_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 statusResume()
    {
        $id     = input('id/d', 0);
        $status = input('status/d', 1);

        $resume = OutResumeModel::find($id);
        if (empty($resume)) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "该记录不存在。",
            ]));
        }

        if ($resume['status'] != 1) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "请勿重复操作。",
            ]));
        }

        $resume->confirm_status = $status;
        $resume->save();

        //分佣
        if ($status == 2) {
            $setting = ConfigModel::getConfigValue(['out_resume_broker','out_resume_agent']);
            $incomeService = new IncomeService();
            $incomeService->add($resume['brokerid'], $setting['out_resume_broker'], '录入简历', '省外经纪人录入简历并核验通过');
            $broker = BrokerModel::find($resume['brokerid']);
            $moneyService = new AgentMoneyService();
            $moneyService->add($broker['id'], $setting['out_resume_agent'], '录入简历', '省外经纪人录入简历并核验通过后，门店奖励金额');
        }

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