<?php

namespace app\controller\api;

use app\model\Userbank;
use app\model\User;
use support\Request;

use App\Utils\ApiResponseApp;


use hg\apidoc\annotation as Apidoc;


/**
 * @Apidoc\Title("银行信息")
 */

class UserbankController
{
    
    /**
     * @Apidoc\Title("查询账户信息")
     * @Apidoc\Url("api/userbank/get_bank")
     * @Apidoc\Method("POST")
     */
    public function get_bank(Request $request)
    { 
       $user_id=$request->data['id'];
       $db = Userbank::where('user_id',$user_id);
       $db = $db->get();
       return ApiResponseApp::success($db);
    }

    /**
     * @Apidoc\Title("添加账户信息")
     * @Apidoc\Url("api/userbank/add")
     * @Apidoc\Method("POST")
     */
    public function add(Request $request)
    { 
        $type = $request->post('type');
        $account = $request->post('account');
        $bank_name = $request->post('bank_name');
        $bank_username = $request->post('bank_username');
        $remark = $request->post('remark');
        $password = $request->post('password');
        $user_id=$request->data['id'];
        $user=User::find($user_id);
       if (!password_verify($password, $user->password)) {
            return ApiResponseApp::error('登录密码错误');
        }
       $userbank = Userbank::where('user_id',$user_id)->first();
       if ($userbank) {
        $userbank->update([
            'account' => $account,
            'type' => $type,
            'bank_name' => $bank_name,
            'bank_username' => $bank_username,
            'remark' => $remark,
        ]);
       }else{
        var_dump(22);

        Userbank::create([
            'user_id' => $user_id,
            'account' => $account,
            'type' => $type,
            'bank_name' => $bank_name,
            'bank_username' => $bank_username,
            'remark' => $remark,
        ]);
       }
       return ApiResponseApp::success('操作成功');
    }

}
