<?php
/**
 * File UserController.php
 *
 * @author Tuan Duong <bacduong@gmail.com>
 * @package Laravue
 * @version 1.0
 */

namespace App\Http\Controllers;

use App\Laravue\Models\wxUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Validator;

class WxuserController extends Controller
{
    //获取wx用户列表
    public function wxlist(Request $request){
        /*
         * get请求返回wx用户列表
         * */
        $pagenNum=$request->input('page')-1;//页数
        $limit = $request->input('limit');

        if($pagenNum === '' || $limit == ''){
            return $this->jsonErrorData(105,'页数或limit不能为空');
        }
        $cont =  Wxuser::count();
        $Wxuser = Wxuser::select('nickname','id','openid','sex','province','city','mobile','country','headimgurl','created_at','state')
            ->offset($pagenNum)
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->get();
        $data = ['Wxuser'=>$Wxuser,'cont'=>$cont];
        if ($Wxuser){
            return $this->jsonSuccessData($data);
        }else{
            return $this->jsonErrorData('105','获取数据失败');
        }
    }
    //用户搜索
    public  function seek (Request $request){
        $keys = $request->all();
        $handle = DB::table('wx_User');
        foreach ($keys as $key => $val) {
            if ($key == 'page') {
                unset($keys[$key]);
            } elseif ($key == 'limit'){
                unset($keys[$key]);
            }
        }
        foreach ($keys as $k => $v) {
            if($k == 'sex'){
                if ($v == '男'){
                    $v = 1;
                }else if ($v == '女'){
                    $v =2;
                }
            }else if ($k == 'state'){
                if ($v == '正常'){
                    $v = 2;
                }else if ($v == '禁用'){
                    $v = 1;
                }
            }
            $keys[$key] && $handle->where($k, 'like', '%' . $v . '%');
        }

        $datas = $handle->get();
        $cont = $handle->count();
        $data = ['Wxuser'=>$datas,'cont'=>$cont];
        return $this->jsonSuccessData($data);
    }
}