<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 会员银行卡
 */
class Usercenter extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //提现账号添加
    public function bankadd() {
        $bank_account    = input('bank_account', '', 'trim'); //账户真实姓名
        $bank_card       = input('bank_card', '', 'trim'); //卡号或账号
        $bank_name       = input('bank_name', '', 'trim'); //银行名称
        $bank_branchname = input('bank_branchname', '', 'trim'); //支行名称

        if ($bank_account === '' || iconv_strlen($bank_account, 'utf-8') > 30) {
            $this->error('账号姓名1-30位');
        }
        if ($bank_card === '' || iconv_strlen($bank_card, 'utf-8') > 50) {
            $this->error('账号1-50位');
        }
        if ($bank_name === '' || iconv_strlen($bank_name, 'utf-8') > 50) {
            $this->error('银行名称1-50位');
        }
        if ($bank_branchname === '' || iconv_strlen($bank_branchname, 'utf-8') > 50) {
            $this->error('支行名称1-50位');
        }

        //添加
        $count = Db::name('user_bank')->where(['user_id' => $this->auth->id])->count('id');
        if ($count) {
            $this->error('您已经拥有该类型账号，不能再添加');
        }

        $data['user_id'] = $this->auth->id;
        $data['bank_account'] = $bank_account;
        $data['bank_card'] = $bank_card;
        $data['bank_name'] = $bank_name;
        $data['bank_branchname'] = $bank_branchname;
        $data['createtime'] = time();

        $rs = Db::name('user_bank')->insertGetId($data);
        if (!$rs) {
            $this->error('添加失败');
        }

        $this->success('添加成功');
    }

    //提现账号编辑
    public function bankedit(){
        $id = input('id', 0, 'intval'); //账号id
        $bank_account    = input('bank_account', '', 'trim'); //账户真实姓名
        $bank_card       = input('bank_card', '', 'trim'); //卡号或账号
        $bank_name       = input('bank_name', '', 'trim'); //银行名称
        $bank_branchname = input('bank_branchname', '', 'trim'); //支行名称

        if ($bank_account === '' || iconv_strlen($bank_account, 'utf-8') > 30) {
            $this->error('账号姓名1-30位');
        }
        if ($bank_card === '' || iconv_strlen($bank_card, 'utf-8') > 50) {
            $this->error('账号1-50位');
        }
        if ($bank_name === '' || iconv_strlen($bank_name, 'utf-8') > 50) {
            $this->error('银行名称1-50位');
        }
        if ($bank_branchname === '' || iconv_strlen($bank_branchname, 'utf-8') > 50) {
            $this->error('支行名称1-50位');
        }

        //查询账号是否存在
        $info = Db::name('user_bank')->where(['id' => $id, 'user_id' => $this->auth->id])->find();
        if (!$info) {
            $this->error('账号不存在');
        }

        $data['bank_account'] = $bank_account;
        $data['bank_card'] = $bank_card;
        $data['bank_name'] = $bank_name;
        $data['bank_branchname'] = $bank_branchname;
        $data['updatetime'] = time();

        $rs = Db::name('user_bank')->where(['id' => $id])->update($data);
        if ($rs === false) {
            $this->error('修改失败');
        }

        $this->success('修改成功');
    }

    //提现账号信息
    public function bankinfo() {

        $info = Db::name('user_bank')->where(['user_id' => $this->auth->id])->find();
        if (!$info) {
            $info = (object)[];
        }

        $this->success('账户信息', $info);
    }

}
