<?php

namespace app\index\controller;

use app\index\controller\Base;
use app\index\model\UserModel;

class User extends Base{

    //用户列表
    public function userList()
    {
        $param = input('param.');

        $limit = isset($param['pageSize']) ? $param['pageSize'] : 10;
        $param['pageNumber'] = isset($param['pageNumber']) ? $param['pageNumber'] : 1;
        $offset = ($param['pageNumber'] - 1) * $limit;

        $where = [];
        if (!empty($param['searchText'])) {
            $where['user_name'] = ['like', '%' . $param['searchText'] . '%'];
        }
        $user = new UserModel();
        $selectResult = $user->getUsersByWhere($where, $offset, $limit);

        $status = config('user_status');

        // 拼装参数
        foreach($selectResult as $key=>$vo){
            $selectResult[$key]['status'] = $status[$vo['status']];
        }

        $return['total'] = $user->getAllUsers($where);  //总数据
        $return['rows'] = $selectResult;
        json_return(200 , $return , 'success');
    }

    // 添加用户
    public function userAdd()
    {
        $param = input('post.');

        $validate = new \app\index\validate\UserValidate;
        if (!$validate->check($param)) {
            return json(json_error_exception('1006',$validate->getError()));
        }
        $param['password'] = md5($param['password']);
        $user = new UserModel();
        $flag = $user->insertUser($param);
        if($flag['code'] != 1){
            return json(json_error_exception('1006',$flag['msg']));
        }
        json_return(200,[],'success');
    }

    // 删除用户
    public function userDel()
    {
        $id = input('param.id');

        $role = new UserModel();
        $flag = $role->delUser($id);
        if($flag['code'] != 1){
            return json(json_error_exception('1006',$flag['msg']));
        }
        json_return(200,[],'success');
    }
}