<?php


namespace app\wechat\controller;


use app\common\Tree;
use app\wechat\IndexController;
use app\wechat\service\UserService;
use support\Request;
use support\Response;

class UserController extends IndexController
{
    public $notNeedLogin = ['login'];

    public function index(Request $request)
    {
        $params = $request->post();
        return UserService::getAll($params);
    }

    /**
     * Notes:
     * User: yb
     * Date: 2024/8/8
     * Time: 9:19
     * @param Request $request
     * @return \support\Response
     */
    public function login(Request $request)
    {
        $params = $request->post();
        if (empty($params['mobile'])) {
            return json_fail('手机号不能为空');
        }
        if (empty($params['password'])) {
            return json_fail('密码不能为空');
        }
        return UserService::login($params);
    }

    /**
     * Notes: 验证身份
     * User: yb
     * Date: 2024/8/8
     * Time: 10:08
     * @param Request $request
     */
    public function auth()
    {
        return UserService::auth();
    }

    /**
     * Notes: 统计分析
     * User: yb
     * Date: 2024/8/14
     * Time: 10:22
     */
    public function statistics(Request $request)
    {
        $params = $request->post();
        return UserService::statistics($params);
    }

    /**
     * Notes: 客户数量走势
     * User: yb
     * Date: 2024/8/17
     * Time: 17:51
     */
    public function customTrend(Request $request)
    {
        $params = $request->post();
        return UserService::customTrend($params);
    }

    /**
     * Notes: 用户详情
     * User: yb
     * Date: 2024/8/14
     * Time: 13:34
     */
    public function info()
    {
        return UserService::info();
    }

    /**
     * Notes: 成员详情
     * User: yb
     * Date: 2024/8/15
     * Time: 10:22
     * @param Request $request
     * @return Response
     */
    public function infoById(Request $request)
    {
        $params = $request->post();
        if (empty($params['id'])) {
            return json_fail('成员id不能为空');
        }
        return UserService::getInfoById($params['id']);
    }

    /**
     * Notes: 编辑成员
     * User: yb
     * Date: 2024/8/15
     * Time: 10:27
     * @param Request $request
     */
    public function updateUserById(Request $request)
    {
        $params = $request->post();
        if (empty($params['mobile'])) {
            return json_fail('手机号不能为空');
        }
        if (empty($params['name'])) {
            return json_fail('姓名不能为空');
        }
        if (empty($params['gender'])) {
            return json_fail('性别不能为空');
        }
        if (empty($params['dept_id'])) {
            return  json_fail('所属部门不能为空');
        }
        if (empty($params['status'])) {
            return  json_fail('状态不能为空');
        }
        return UserService::updateById($params);
    }

    /**
     * Notes: 更新用户
     * User: yb
     * Date: 2024/8/14
     * Time: 14:29
     */
    public function updateUser(Request $request)
    {
        $params = $request->post();
        if (empty($params['mobile'])) {
            return json_fail('手机号不能为空');
        }
        if (empty($params['name'])) {
            return json_fail('姓名不能为空');
        }
        if (empty($params['gender'])) {
            return json_fail('性别不能为空');
        }
        return UserService::update($params);
    }

    /**
     * Notes: 删除成员
     * User: yb
     * Date: 2024/8/15
     * Time: 11:38
     * @param Request $request
     */
    public function deleteUser(Request $request)
    {
        $params = $request->post();
        if (empty($params['id'])) {
            return json_fail('成员id不能为空');
        }
        return UserService::del($params['id']);
    }

    /**
     * Notes: 设置密码
     * User: yb
     * Date: 2024/8/14
     * Time: 14:44
     */
    public function setPassword(Request $request)
    {
        $params = $request->post();
        if (empty($params['old_password'])) {
            return json_fail('旧密码不能为空');
        }
        if (empty($params['new_password'])) {
            return json_fail('新密码不能为空');
        }
        return UserService::editPassword($params);
    }

    /**
     * Notes: 我的成员
     * User: yb
     * Date: 2024/8/14
     * Time: 15:24
     */
    public function userList(Request $request)
    {
        $params = $request->post();
        return UserService::index($params);
    }

    /**
     * Notes: 添加员工
     * User: yb
     * Date: 2024/8/15
     * Time: 9:54
     * @param Request $request
     * @return Response
     */
    public function addUser(Request $request)
    {
        $params = $request->post();
        if (empty($params['mobile'])) {
            return json_fail('手机号不能为空');
        }
        if (empty($params['name'])) {
            return json_fail('姓名不能为空');
        }
        if (empty($params['gender'])) {
            return json_fail('性别不能为空');
        }
        if (empty($params['dept_id'])) {
            return  json_fail('所属部门不能为空');
        }
        if (empty($params['status'])) {
            return  json_fail('状态不能为空');
        }
        return UserService::add($params);
    }

    /**
     * Notes: 我的团队
     * User: yb
     * Date: 2024/8/14
     * Time: 16:38
     * @return \support\Response
     */
    public function teamList()
    {
        return $this->formatTree(UserService::getTeamList());
    }

    protected function formatTree($items): Response
    {
        $format_items = [];
        foreach ($items as $item) {
            $format_items[] = [
                'label' => $item->dept_name,
                'value' => (string)$item->dept_id,
                'id' => $item->id ?? $item->dept_id,
                'pid' => $item->pid ?? $item->dept_super_id
            ];
        }
        $tree = new Tree($format_items);
        return json_success('success', $tree->getTree());
    }

}