<?php

namespace app\worker\controller;

use app\common\model\RensheCode;
use app\common\model\UserTags;
use app\common\model\UserWill;
use app\common\model\WorkerGroup;
use app\common\model\UserGroups as UserGroupsModel;
use app\common\model\WorkerResume as WorkerResumeModel;
use app\common\model\ComjobsCate as ComjobsCateModel;
use app\common\validate\User as UserValidate;
use app\worker\BaseController;
use think\exception\ValidateException;

class Resume extends BaseController
{

    public function grouplist()
    {
        return view('resume/grouplist');
    }

    public function listgroup()
    {
        $workerid = $this->access_worker['id'];
        $limit    = input('limit');
        $page     = input('page');
        $map      = [
            ['workerid', '=', $workerid],
        ];
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] = ['name', 'like', '%' . $keywords . '%'];
        }
        $list  = WorkerGroup::where($map)->limit($limit)->page($page)->select();
        $count = WorkerGroup::count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }

    public function delgroup()
    {
        $idarr    = input('idarr/a');
        $workerid = $this->access_worker['id'];
        WorkerGroup::whereIn('id', $idarr)->where('workerid', $workerid)->delete();
        exit(json_encode([
            'code' => 0,
            'msg'  => "",
        ]));
    }

    public function groupForm()
    {
        $id       = input('id/d, 0');
        $workerid = $this->access_worker['id'];
        $group    = WorkerGroup::where('workerid', $workerid)->where('id', $id)->findOrEmpty();
        return view('resume/groupform', [
            'group' => $group,
        ]);
    }

    public function editgroup()
    {
        $id       = input('id/d');
        $workerid = $this->access_worker['id'];
        if (empty($id)) {
            WorkerGroup::create([
                'name'     => input('name/s'),
                'workerid' => $workerid,
            ]);
        } else {
            $administer = WorkerGroup::find($id);
            $administer->save([
                'name' => input('name/s'),
            ]);
        }
        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function companylist()
    {
        $workerid   = $this->access_worker['id'];
        $groupslist = UserGroupsModel::order(['isdefault' => 'desc', 'id' => 'asc'])->select();
        return view('resume/companylist', [
            'groupslist' => $groupslist,
        ]);
    }

    public function listCompany()
    {
        $workerid = $this->access_worker['id'];
        $limit    = input('limit');
        $page     = input('page');
        $map      = [];
        $map[]    = ['workerid', '=', $workerid];
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] = ['nickname', 'like', '%' . $keywords . '%', 'or'];
        }
        $groupsid = input('groupsid/d');
        if (!empty($groupsid)) {
            $map[] = ['groupsid', '=', $groupsid];
        }
        $list  = WorkerResumeModel::with(['userGroups'])->where($map)->order('id', 'desc')->limit($limit)->page($page)->append(['followstatus_text'])->select();
        $count = WorkerResumeModel::where($map)->count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }

    public function fieldCompany()
    {
        $workerid = $this->access_worker['id'];
        $id       = input('id/d', 0);
        $user     = WorkerResumeModel::where('workerid', '=', $workerid)->findOrEmpty($id);
        if ($user->isEmpty()) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "信息不存在",
            ]));
        } else {
            $user->save([
                input('field/s') => input('value'),
            ]);
        }
        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function delCompany()
    {
        $idarr = input('idarr/a');
        WorkerResumeModel::destroy($idarr);
        exit(json_encode([
            'code' => 0,
            'msg'  => "",
        ]));
    }

    public function companyForm()
    {
        $id            = input('id/d', 0);
        $user          = WorkerResumeModel::findOrEmpty($id);
        $groupslist    = UserGroupsModel::order(['isdefault' => 'desc', 'id' => 'asc'])->select();
        $willlist      = UserWill::select();
        $usertags      = UserTags::select();
        $emptimelist   = RensheCode::getList('emp_time');
        $communitylist = RensheCode::getList('community')->toArray();
        array_push($communitylist, ['code' => 0, 'id' => 0, 'name' => "不限"]);
        $comlist = ComjobsCateModel::select();
        return view('resume/companyform', [
            'groupslist'    => $groupslist,
            'willlist'      => $willlist,
            'usertags'      => $usertags,
            'user'          => $user,
            'emptimelist'   => $emptimelist,
            'communitylist' => $communitylist,
            'comlist'       => $comlist,
        ]);
    }

    public function editCompany()
    {
        $id     = input('id/d');
        $mobile = input('mobile/s');
        $vdata  = [
            'id'     => $id,
            'mobile' => $mobile,
        ];
        try {
            validate(UserValidate::class)->check($vdata);
        } catch (ValidateException $e) {
            exit(json_encode([
                'code' => 1,
                'msg'  => $e->getError(),
            ]));
        }

        //手机号
        $workerid         = $this->access_worker['id'];
        $check_user_where = [['mobile', '=', $mobile], ['workerid', '=', $workerid]];
        if (!empty($id)) {
            $check_user_where[] = ['id', '<>', $id];
        }
        $check_user = WorkerResumeModel::where($check_user_where)->find();
        if (!empty($check_user)) {
            exit(json_encode([
                'code' => 1,
                'msg'  => '手机号已存在',
            ]));
        }

        $data = [
            'groupsid'       => input('groupsid/d', 0),
            'workerid'       => $workerid,
            'nickname'       => input('nickname/s', ""),
            'avatar'         => input('avatar/s', ""),
            'mobile'         => $mobile,
            'idcardzpic'     => input('idcardzpic/s', ""),
            'idcardfpic'     => input('idcardfpic/s', ""),
            'idcard'         => input('idcard/s', ""),
            'gender'         => input('gender/d', 1),
            'birthday'       => input('birthday/s', ""),
            'address'        => input('address/s', ""),
            'education'      => input('education/s', ""),
            'jobintention'   => input('jobintention/s', ""),
            'workexperience' => input('workexperience/s', ""),
            'eduexperience'  => input('eduexperience/s', ""),
            'followstatus'   => input('followstatus/d', 1),
            'bankcard'       => input('bankcard/a', []),
            'emp_time'       => array_values(input('emp_time/a', [])),
            'user_tags'      => array_values(input('user_tags/a', [])),
            'work_place'     => array_values(input('work_place/a', [])),
            'com_cate_type'  => input('com_cate_type/d', 1),
            'com_cate'       => array_values(input('com_cate/a', [])),
            'com_cate_other' => input('com_cate_other/s', ""),
            'comment'        => input('comment/s', ""),
        ];
        if (empty($id)) {
            $data['createtime'] = time();
            WorkerResumeModel::create($data);
        } else {
            $data['id'] = $id;
            WorkerResumeModel::update($data);
        }

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