<?php

namespace App\Http\Controllers\Bank;

use App\Libs\ApiResponse;
use App\Services\Bank\BankService;
use App\Transformer\Bank\BankTransformer;
use App\Exceptions\ApiException;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class BankController extends BaseController
{
    use ApiResponse;

    protected $bankService;

    public function __construct(
        BankService $bankService
    )
    {
        $this->bankService = $bankService;
    }

    /**
     * 下拉银行卡列表
     *
     * @return mixed
     */
    public function bankList()
    {
        $banks = $this->bankService->bankList();
        return $this->success($banks);
    }

    /**
     * 站点银行卡列表
     *
     * @return mixed
     */
    public function channelBankAccounts(Request $request)
    {
        $all = $request->only('is_company');

        $banks = $this->bankService->channelBankAccounts($all);
        return $this->success($banks, [new BankTransformer(), 'buildCashAccounts']);
    }

    /**
     * 新增银行卡账号
     *
     * @param Request $request
     * @return mixed
     * @throws ApiException
     */
    public function addBankAccount(Request $request)
    {
        $all = $request->only('account_name', 'identity_card', 'card_number',
            'account_bank', 'bank_id', 'is_company', 'province', 'sms_code', 'bank');

        $result = $this->bankService->addBankAccount($all);
        return $this->success(['result' => $result ? 1 : 0]);
    }

    /*
     * 删除银行卡账号
     *
     * @param Request $request
     * @return mixed
     * @throws ApiException
     */
    public function delBankAccount(Request $request)
    {
        $all = $request->only('id', 'delete_all');

        $result = $this->bankService->delBankAccount($all);
        return $this->success(['result' => $result ? 1 : 0]);
    }

    /**
     * 发送手机验证码
     *
     * @return mixed
     * @throws ApiException
     */
    public function getBankAccountSms()
    {
        $result = $this->bankService->getBankAccountSms();
        return $this->success(['result' => $result ? 1 : 0]);
    }
}