<?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 Card extends ApiController
{
    public function addPrecard(){
        $req=input();
        //取得prefix
        $agent = Db::name('agent')
        ->where('id',$req['agent_id'])
        ->find();

        $aes = new Aes([]);

        for($i=0;$i<input('number');$i++){

            // $user_id=genUniqid($agent['prefix']);
            // $verify_code = genSerialNo();

            // $params = urlencode($aes->encrypt('user_id='.$user_id.'&verify_code='.$verify_code));
            // $nfcUrl = genQrCode(getUrl().'/card/?params='.$params,$user_id,'nfc');

            $data[]=[
                // 'user_id'=>$user_id,
                'agent_id'=>$req['agent_id'],
                'try_days'=>7,
                'verify_code'=>'',
                'expire_time'=>$req['expire_time'],
                'status'=>0,
            ];
            
        }

        try{
            Db::name('precard')
            ->insertAll($data);    
        }catch(\Exception $e){
            print_r($e);
            return $this->error('新增失敗');
        }

        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));
            $result[$key]['agent_name'] = Db::name('agent')->where('id',$val['agent_id'])->value('name');
            $result[$key]['expire'] = date('Y-m-d',$val['expire_time']);
            switch($val['status']){
                case 0:
                    $result[$key]['status_name']='未制卡';
                    break;
                case 1:
                    $result[$key]['status_name']='已制卡';
                    break;
                case 2:
                    $result[$key]['status_name']='已開通';
                    break;
                case 3:
                    $result[$key]['status_name']='已作癈';
                    break;
            }

            $aes = new Aes([]);
            $params = urlencode($aes->encrypt('verify_code='.$val['verify_code']));
            $result[$key]['nfcurl']= getUrl().'/card/?params='.$params;
            // $nfcUrl = genQrCode('https://'.$_SERVER['HTTP_HOST'].'/card/?params='.$params,$data['user_id'],'nfc');
        }

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

        return $this->Success($rtn);

    }

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

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

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

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

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

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

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

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

        $is_user = Db::name('user')
        ->where('uniqid',$verify_code)
        ->count();

        if($is_user){
            return $this->error('卡片已綁定會員');
        }

        $is_precard = Db::name('precard')
        ->where('verify_code',$verify_code)
        ->count();

        if($is_precard){
            return $this->error('已存在預製卡');
        }

        try{
            $result=Db::name('precard')
            ->where('id',$id)
            ->update(['verify_code'=>$verify_code,'status'=>1]);
    
            return $this->success('設定成功');    
        }catch(\Exception $e){
            return $this->error('系統錯誤');    
        }
    }

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

        $pc=Db::name('precard')
        ->where('id',$id)
        ->find();

        if(strlen($pc['nfc_qrcode'])>0){
            $nfc_qrcode = $pc['nfc_qrcode'];
        }else{

        }

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