<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use think\Db;
use app\common\library\Sms;
/**
 * 银行卡
 */
class Userbank extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    //列表
    public function lists(){
        $list = Db::name('company_bank')->order('id desc')->select();
        $this->success(1,$list);
    }

    //提现账号添加
    public function bankadd() {

        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置银行卡');
        }
        //验证手机和验证码
        $captcha    = input('captcha', '', 'trim'); //账户真实姓名
        if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
            $this->error('验证码错误');
        }

        //
        $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('company_bank')->where(['company_id' => $this->auth->company_id])->count('id');
        if ($count) {
            $this->error('您已经拥有该类型账号，不能再添加');
        }

        $data['company_id'] = $this->auth->company_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('company_bank')->insertGetId($data);
        if (!$rs) {
            $this->error('设置失败');
        }

        $this->success('设置成功');
    }

    //提现账号编辑
    public function bankedit(){

        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置银行卡');
        }
        //验证手机和验证码
        $captcha    = input('captcha', '', 'trim'); //账户真实姓名
        if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
            $this->error('验证码错误');
        }

        //
        $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('company_bank')->where(['id' => $id, 'company_id' => $this->auth->company_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('company_bank')->where(['id' => $id])->update($data);
        if ($rs === false) {
            $this->error('设置失败');
        }

        $this->success('设置成功');
    }

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

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

        $rs = [
            'bank_info' => $info,
            'set_mobile'=> Db::name('company_staff')->where('company_id',$this->auth->company_id)->where('type',1)->value('mobile'),
        ];

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

}
