<?php

namespace App\Http\Controllers\Web\Talent;

use App\Http\Controllers\Web\WebBaseController;
use App\Models\MemberShuobo;
use App\Services\Common\SmsService;
use App\Models\Member;
use Illuminate\Http\Request;
use App\Services\Common\SearchService;
use App\Models\C2M;
use Illuminate\Support\Facades\DB;

class ShuoboController extends WebBaseController
{

    private $smsService;
    protected $searchService;
    protected $c2m;

    public function __construct(SmsService $smsService,SearchService $searchService)
    {
        $this->smsService = $smsService;
        $this->searchService = $searchService;
        $this->c2m = new C2M();
    }

    public function talent()
    {

        return view('app.shuobo.talent');
    }

    public function getCompanyToShuobo(Request $request){
        $user = auth('web-company')->user();
        if($user){
            $info = $this->c2m->where("company_id",$user->id)->where('status',0)->first();
            if($info){
                $data = [
                    'number' => 1,
                    'shuobo_id' => $info->shuobo_id,
                    'info' => MemberShuobo::where('id',$info->shuobo_id)->first()
                ];
            }else{
                $data = [
                    'number' => 0
                ];
            }

            return json_encode(['status' => 1,'data' => $data,'msg' => '','login' => 1]);
        }else{
            return json_encode(['status' => 1,'data' => '','msg' => '未登录','login' => 0]);
        }

    }

    public function getTalentData(Request $request)
    {
        $keyword = $request->input('keyword');
        $page = $request->input('page');
        $list = $this->searchService->search('Shuobo', [], ['sort'=>'desc','updated_at' => 'desc','id'=>'desc'], $keyword,10,$page);
        $list_data =[];
        $has_shuobo_id = $this->c2m->whereRaw('status = 0 or status = 1')->select('shuobo_id')->get()->toArray();
        $no_show_shuobo = [];
        if(is_array($has_shuobo_id)){
            foreach ($has_shuobo_id as $v){
                array_push($no_show_shuobo,$v['shuobo_id']);
            }
        }
        $user = auth('web-company')->user();
        $notice_show_shuobo = [];
        if($user){
            //如果有登录,查找之前有跟进过的人才并标记
            $notice_shobo_id = $this->c2m->where('company_id',$user->id)->select('shuobo_id')->get()->toArray();
            if(is_array($notice_shobo_id)){
                foreach ($notice_shobo_id as $v){
                    array_push($notice_show_shuobo,$v['shuobo_id']);
                }
            }
        }

        foreach ($list->items() as $k => $v){
            if(in_array($v->id,$no_show_shuobo)){
                continue;
            }
            $item = [
                'id' => $v->id,
                'name' => $this->splitName($v->realname)[0] . ($v->sex == '男' ? '先生' : '女士'),
                'sex' => $v->sex,
                'birthday' => date('Y-m-d H:i:s',$v->birthday),
                'education' => $v->education,
                'school' => $v->school,
                'pro' => $v->pro,
                'trade_type' => $v->trade_type,
                'speciality' => $v->speciality,
                'notice' => in_array($v->id,$notice_show_shuobo) ? 1 : 0
            ];
            array_push($list_data,$item);
        }

        $data = array(
            'total' => $list->total(),
            'list' => $list_data
        );


        return json_encode($data);
    }

    public function getTalentInfo(Request $request)
    {
        $user = auth('web-company')->user();
        if(!$user){
            return json_encode(['status' => 1,'data' => '','msg' => '未登录']);
        }
        $id = $request->input('id');
        $info = MemberShuobo::where('id',$id)->first();
        if(!$info){
            return json_encode(['status' => 1,'data' => '','msg' => '找不到该人才信息']);
        }
        $c2m_data = [
            'company_id' => $user->id,
            'company_name' => $user->companyname,
            'shuobo_id' => $id,
            'shuobo_name' => $info->realname,
            'status' => 0
        ];

        $scid = $this->c2m->create($c2m_data);

        return json_encode(['status' => 1,'data' => $info,'msg' => '']);

    }

    public function updateCompanyToShuobo(Request $request)
    {
        $user = auth('web-company')->user();
        if($user){
            $id = $request->input('id');
            if(!$id){
                return json_encode(['status' => 1,'data' => '','msg' => '必要参数缺失!']);
            }
            $info = $this->c2m->where("company_id",$user->id)->where('shuobo_id',$id)->where('status',0)->first();
            if(!$info){
                return json_encode(['status' => 1,'data' => '','msg' => '这条硕博跟进记录不存在。']);
            }
            $status = $request->input('status');
            $remark = $request->input('remark');
            if(!in_array($status,[-1,0,1,2])){
                return json_encode(['status' => 1,'data' => '','msg' => '状态不正确。']);
            }
            $this->c2m->where("company_id",$user->id)->where('shuobo_id',$id)->update(['status' => $status,'remark' => $remark]);
            $ret = [
                'number' => 0
            ];
            return json_encode(['status' => 1,'data' => $ret,'msg' => '更新成功。']);
        }
        return json_encode(['status' => 1,'data' => '','msg' => '未登录','login' => 0]);
    }

    public function enterprise(Request $request)
    {
        return view('app.shuobo.enterprise');
    }

    public function getShuoboJob(Request $request)
    {
        $keyword = $request->input('keyword');
        $page = $request->input('page');
        if(!empty($keyword)){
            $list = DB::table("shuobo_jobs")->where("company_name","like","%{$keyword}%")->orWhere("job",'like',"%{$keyword}%")->orderBy('id','desc')->get()->toArray();
        }else{
            $list = DB::table("shuobo_jobs")->orderBy('id','desc')->get()->toArray();
        }

        $data = array(
            'total' => count($list),
            'list' => array_slice($list,($page-1)*10,10)
        );

        return json_encode($data);
    }

    function splitName($fullname){
        $hyphenated = array('欧阳','太史','端木','上官','司马','东方','独孤','南宫','万俟','闻人','夏侯','诸葛','尉迟','公羊','赫连','澹台','皇甫',
            '宗政','濮阳','公冶','太叔','申屠','公孙','慕容','仲孙','钟离','长孙','宇文','城池','司徒','鲜于','司空','汝嫣','闾丘','子车','亓官',
            '司寇','巫马','公西','颛孙','壤驷','公良','漆雕','乐正','宰父','谷梁','拓跋','夹谷','轩辕','令狐','段干','百里','呼延','东郭','南门',
            '羊舌','微生','公户','公玉','公仪','梁丘','公仲','公上','公门','公山','公坚','左丘','公伯','西门','公祖','第五','公乘','贯丘','公皙',
            '南荣','东里','东宫','仲长','子书','子桑','即墨','达奚','褚师');
        $vLength = mb_strlen($fullname, 'utf-8');
        $lastname = '';
        $firstname = '';//前为姓,后为名
        if($vLength > 2){
            $preTwoWords = mb_substr($fullname, 0, 2, 'utf-8');//取命名的前两个字,看是否在复姓库中
            if(in_array($preTwoWords, $hyphenated)){
                $lastname = $preTwoWords;
                $firstname = mb_substr($fullname, 2, 10, 'utf-8');
            }else{
                $lastname = mb_substr($fullname, 0, 1, 'utf-8');
                $firstname = mb_substr($fullname, 1, 10, 'utf-8');
            }
        }else if($vLength == 2){//全名只有两个字时,以前一个为姓,后一下为名
            $lastname = mb_substr($fullname ,0, 1, 'utf-8');
            $firstname = mb_substr($fullname, 1, 10, 'utf-8');
        }else{
            $lastname = $fullname;
        }
        return array($lastname, $firstname);

    }
}