<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\UserFollowModel;
use app\common\model\UserModel;
use app\admin\validate\UserValidate;
use think\exception\ValidateException;

class User extends AdminBaseController
{
    /**
     * 用户列表
     */
    public function index()
    {
        $gender_list    = UserModel::GENDER;
        $gender_list[0] = '全部';
        return view('', [
            'status_list' => UserModel::STATUS,
            'gender_list' => $gender_list,
        ]);
    }

    public function listUser()
    {
        $map   = $this->dealEqualInput(['gender', 'status'], $this->dealLikeInput(['keywords' => 'account|nickname|realname', 'mobile']));
        $list  = UserModel::where($map)
            ->order('id', 'desc')
            ->limit(input('limit'))
            ->page(input('page'))
            ->append(['status_text', 'gender_text'])->select();
        $count = UserModel::where($map)->count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    /**
     * 用户跟进记录
     */
    public function follow()
    {
        $id   = input('id/d');
        $user = UserModel::find($id);
        $list = UserFollowModel::where('user_id', $id)->order('id', 'desc')->limit(100)->select();
        return view('', [
            'user'      => $user,
            'list'      => $list,
            'type_list' => UserFollowModel::TYPE,
        ]);
    }

    public function editFollow()
    {
        $id   = input('id/d', 0);
        $user = UserModel::find($id);
        if (empty($user)) {
            ajax_return(1, '用户信息不存在。');
        }
        UserFollowModel::create([
            'user_id' => $id,
            'type'    => input('type/d', 1),
            'remark'  => input('remark/s', ""),
        ]);
        ajax_return();
    }

    /**
     * 编辑用户
     */
    public function userForm()
    {
        $id   = input('id/d', 0);
        $info = UserModel::find($id);
        return view('', [
            'info'        => $info,
            'gender_list' => UserModel::GENDER,
            'status_list' => UserModel::STATUS,
        ]);
    }

    public function editUser()
    {
        $data = input('post.');
        try {
            validate(UserValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        //用户名
        $check_account_where = [['account', '=', $data['account']]];
        if (!empty($data['id'])) {
            $check_account_where[] = ['id', '<>', $data['id']];
        }
        $check_account = UserModel::where($check_account_where)->find();
        if (!empty($check_account)) {
            ajax_return(1, '用户名已存在');
        }

        //手机号
        $check_mobile_where = [['mobile', '=', $data['mobile']]];
        if (!empty($data['id'])) {
            $check_mobile_where[] = ['id', '<>', $data['id']];
        }
        $check_mobile = UserModel::where($check_mobile_where)->find();
        if (!empty($check_mobile)) {
            ajax_return(1, '手机号已存在');
        }

        //密码
        if (empty($data['id']) && empty($data['password'])) {
            ajax_return(1, '请输入一个初始密码');
        }
        if (empty($data['password'])) {
            unset($data['password']);
        } else {
            $data['salt']     = rand_str();
            $data['password'] = md5(md5($data['salt']) . $data['password']);
        }

        if (empty($data['id'])) {
            UserModel::create($data);
        } else {
            UserModel::update($data, ['id' => $data['id']]);
        }

        ajax_return();
    }
}