<?php
namespace app\adminapi\controller\v1;

use app\adminapi\ApiController;
use think\facade\Db;

use app\common\lib\Vcard;
use app\common\lib\Aes;

class User extends ApiController
{
    public function getInfo()
    {
        $id=$this->request->uid;
        $result=Db::name('user')
        ->where('id',$id)
        ->find();

        if(!$result){
            $result=[];
        }
        // foreach($result as $key=>$val){
        //     // $rtn=Db::name('goods')->where('gc_id',$val['id'])->select()->toArray();
        //     // if($rtn){
        //     //     $result[$key]['goods']=[];
        //     // }
        //     $result[$key]['goods']=Db::name('goods')->where('gc_id',$val['id'])->select()->toArray();
        // }
        return $this->Success($result);
    }

    // 取得客戶資料
    public function list(){
        $search = input('search');
        $page = input('page')?input('page'):1;
        $size = input('size')?input('size'):10;

        $do=Db::name('users');

        if(!input('search')){
            $result=$do
            ->page($page,$size)
            ->order('user_id','desc')
            ->select()
            ->toArray();

            $total=$do
            ->count();    
        }else{
            $result=$do
            // ->where('mobile_phone','like','%'.input('search').'%')
            // ->whereor('real_name','like','%'.input('search').'%')
            ->page($page,$size)
            ->order('user_id','desc')
            ->select()
            ->toArray();
    
            $total=$do
            // ->where('mobile_phone','like','%'.input('search').'%')
            // ->whereor('real_name','like','%'.input('search').'%')
            ->count();    
        }

        if(!$result){
            $result=[];
        }

        foreach($result as $key => $val){
            $result[$key]['reg_time'] = date('Y-m-d',$val['reg_time']);
        }

        $rtn=[
            'total' => $total,
            'data' => $result
        ];

        return $this->Success($rtn);
    }

    public function getUser(){

        $id=input('id');

        $result=Db::name('users')
        ->where('user_id',$id)
        ->find();

        if(!$result){
            $result=[];
        }
        
        return $this->success($result);
    }

    public function addUser(){

        $data=input('post.');
        unset($data['act']);
        unset($data['cfm_password']);
        $data['reg_time'] = time();

        $result=Db::name('users')
        ->insert($data);

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function updateUser(){

        $data=input('post.');
        unset($data['act']);
        unset($data['cfm_password']);

        $result=Db::name('users')
        ->where('user_id',$data['user_id'])
        ->update($data);

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function deleteUser(){
        $id=input('id');

        $result=Db::name('users')
        ->where('user_id',$id)
        ->delete();

        return $this->success($result);
    }

    public function updateStatus(){
        $id=input('id');
        $status=input('status');

        $result=Db::name('user')
        ->where('id',$id)
        ->update(['status'=>$status]);

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function getUserCard(){

        $id=input('id');

        $result=Db::name('user_card')
        ->field('id,type,title,content,nfc_show,sort_id')
        ->where('user_id',$id)
        ->order('sort_id')
        ->select();


        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function uploadAvatar(){
		$files = request()->file('avatar');
		$savename =  \think\facade\Filesystem::disk('public')->putFile( input('id'), $files);

		$avatar = getUrl().'/storage/'.$savename;
        
        return $this->Success($avatar);        
    }

    public function updateUserCard(){
        $user_id=input('id');
        $cards=input('cards');

        Db::name('user_card')
        ->where('user_id',$user_id)
        ->delete();

        foreach($cards as $key => $val){
            $nfc_show = $val['nfc_show']?1:0;

            Db::name('user_card')
            ->insert([
                'user_id' => $user_id,
                'type' => $val['type'],
                'title' => $val['title'],
                'content' => $val['content'],
                'nfc_show' => $nfc_show,
                'sort_id' => $key,
                'create_time' => time()
            ]);
        }

        // if(!$result){
        //     $result=[];
        // }

        return $this->success(['code'=>200]);
    }

    public function updateVerifyCode(){
        $id=input('id');
        $uniqid=strtoupper(input('code'));

        $result=Db::name('user')
        ->where('id',$id)
        ->update(['uniqid'=>$uniqid]);

        return $this->success('設定成功');
    }

    // 取得預開卡資料
    public function getPrecard(){
        $do=Db::name('precard');

        if(!input('search')){
            $result=$do
            ->page(input('current'),input('size'))
            ->order('id','desc')
            ->select()->toArray();
    
            $total=$do
            ->count();    
        }else{
            $result=$do
            ->where('user_id','like','%'.input('search').'%')
            ->page(input('current'),input('size'))
            ->order('id','desc')
            ->select()->toArray();
    
            $total=$do
            ->where('user_id','like','%'.input('search').'%')
            ->count();    
        }

        if(!$result){
            $result=[];
        }

        foreach($result as $key => $val){
            $aes = new Aes([]);
            $nfc_url = 'user_id='.$val['user_id'].'&verify_code='.$val['verify_code'];            
            $result[$key]['params'] = getUrl().'/card/'.urlencode($aes->encrypt($nfc_url));
        }

        $rtn=[
            'total' => $total,
            'data' => $result
        ];

        return $this->Success($rtn);

    }

    
}
