<?php
namespace app\api\controller;

use support\Request;
use taoser\facade\Validate;
use app\model\Address as AddressModel;
use hg\apidoc\annotation as Apidoc;

/**
 * 提现地址
 */
class AddressController extends BaseController{
    /**
     * 不需要鉴权的方法
     * @var array
     */
    public $noNeedAuth = ['*'];

    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    public $noNeedLogin = [];
    /**
     * 列表
     * @Apidoc\Method("POST")
     * @Apidoc\Query("network", type="string", require=true, desc="网络")
     * @Apidoc\Query("page", type="int", require=true, desc="页码",default=1)
     * @Apidoc\Query("limit", type="int", require=true, desc="分页大小",default=10)
     */
    public  function list()
    {
        $limit = (int)input('limit',10);
        $page = (int)input('page',1);
        $status = (int)input('status',-1);
        $network = input('network');
        $type = input('type');
        $model = AddressModel::where('user_id',\support\Jwt\JwtToken::getCurrentId());
            //->where('network','BEP-20');
        if($type){
            $model = $model->where('status',1);
        }
        if($network){
            $model = $model->where('network',$network);
        }
        $list = $model->paginate($limit);
        return $this->success(__('successful'),$list->toArray());

    }
    /**
     * 创建
     * @Apidoc\Method("POST")
     * @Apidoc\Param("network", type="string", require=true, desc="网络,BEP-20,TRC-20,ALIPAY,WECHAT",default="BEP-20")
     * @Apidoc\Param("address", type="string", require=true, desc="地址")
     * @Apidoc\Param("title", type="string", require=true, desc="名称")
     * @Apidoc\Param("status", type="string", require=true, desc="状态,可选,1,0,默认1")
     */
    public  function create()
    {
        //captcha_verfiy('image','create_address');
        //* @Apidoc\Param("code", type="string", require=true, desc="图形验证码 event=create_address")
        //$trade_password = input('trade_password');
        //\support\Jwt::verify_trade_password($trade_password);
        //* @Apidoc\Param("trade_password", type="string", require=true, desc="交易密码")
        $data = [
            'network'       => input('network','BEP-20'),
            'title'       => input('title'),
            'address'       => input('address'),
            'img'       => input('img'),
            'is_default'       => input('is_default'),
            'status'       => input('status',0),
            'user_id'       => \support\Jwt\JwtToken::getCurrentId()
        ];
        if(!$data['title']){
            return $this->error(__('Invalid title'));
        }
        if(!$data['address']){
            return $this->error(__('Invalid address'));
        }
        AddressModel::create($data);
        return $this->success(__('successful'));
    }
    /**
     * 编辑
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="string", require=true, desc="id")
     * @Apidoc\Param("title", type="string", require=true, desc="名称")
     * @Apidoc\Param("status", type="string", require=true, desc="状态,可选,1,0,默认1")
     */
    public  function update()
    {
        //captcha_verfiy('image','update_address');
        //$trade_password = input('trade_password');
        //\support\Jwt::verify_trade_password($trade_password);
        $data = [
            'id'        => input('id',''),
            'title'        => input('title',''),
            //'is_default'       => input('is_default'),
            'status'       => input('status',1),
        ];
        if(!$data['id']){
            return $this->error(__('Invalid parameters'));
        }        
        if(!$data['title']){
            return $this->error(__('Invalid title'));
        }
        AddressModel::where('id',$data['id'])->save($data);
        return $this->success(__('successful'));
    }
    /**
     * 详情
     * @Apidoc\Query("id", type="int", require=true, desc="id")
     */
    public function detail(){
        $appid = input('id');
        $vo = AddressModel::where('id',$appid)->find();
        if($vo) {
            return $this->success(__('successful'),$vo->toArray());
        }else{
            return $this->error(__("Address is not exist"));
        }
    }

}