<?php
namespace backend\modules\user\controllers;

use backend\models\DepositApi;
use backend\models\forms\PayForm;
use backend\models\RateApi;
use backend\models\searches\DepositSearch;
use backend\models\searches\SsacDepositSearch;
use backend\models\SsacDepositApi;
use Yii;

class BankdepositController extends BaseController
{
    public static $chineseNum = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];

    /**
     * @return string 
     */
    public function actionIndex()
    {
        $payMethods = [];
        foreach (PayForm::$payMethods as $name => $payId) {
            $payMethods[$payId] = [
                'id' => $payId,
                'name' => $name,
                'enable' => 0,
            ];
        }
        $api = new DepositApi();
        $result = $api->getDepositConfig();
        $config = $result['data'] ? $result['data'] : [];
        foreach ((array)$config as $key => $row) {
            if (!isset($row['id']) || !isset($payMethods[$row['id']])) {
                continue;
            }
            $payMethods[$row['id']]['label'] = isset($row['label']) ? trim($row['label']) : '';
            $payMethods[$row['id']]['enable'] = isset($row['enable']) && $row['enable'] == 1 ? 1 : 0;
        }
        unset($config);
        $i = 0;
        foreach ($payMethods as $key => $row) {
            if (!isset($row['enable']) || $row['enable'] == 0) {
                unset($payMethods[$key]);
                continue;
            }
            $payMethods[$key]['label'] = '通道' . self::$chineseNum[$i];
            $i++;
        }
        return $this->render('index', [
            'rate' => $this->getRate(),
            'payMethods' => $payMethods,
        ]);
    }

    /**
     * @return \yii\web\Response
     */
    public function actionAjax()
    {
        $searchModel = new DepositSearch();
        $dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams());
        return $this->asJson($searchModel->outResult($dataProvider));
    }

    public function actionGetSsacUserInfo()
    {
        $api = new SsacDepositApi();
        $result = $api->getUserInfo(Yii::$app->getUser()->getId(), Yii::$app->getRequest()->get('addrToken'));
        if ($result['code'] == 1) {
            return $this->asJson(['isSuccess' => true, 'data' => $result['data']]);
        } else {
            return $this->asJson(['isSuccess' => false, 'msg' => $result['message']]);
        }
    }

    public function actionSendPhoneCapt()
    {
        $api = new SsacDepositApi();
        $result = $api->sendPhoneCapt(Yii::$app->getUser()->getId(), Yii::$app->getRequest()->post('addrToken'));
        if ($result['code'] == 1) {
            return $this->asJson(['isSuccess' => true, 'data' => $result['data']]);
        } else {
            return $this->asJson(['isSuccess' => false, 'msg' => $result['message']]);
        }
    }

    public function actionInGold()
    {
        $api = new SsacDepositApi();
        $result = $api->inGold(Yii::$app->getUser()->getId(), Yii::$app->getRequest()->post());
        if ($result['code'] == 1) {
            return $this->render('/pay/succeed');
        } else {
            return $this->render('/pay/failed');
        }
    }

    /**
     * @return \yii\web\Response
     */
    public function actionSsacAjax()
    {
        $searchModel = new SsacDepositSearch();
        $dataProvider = $searchModel->search(Yii::$app->getRequest()->getQueryParams());
        return $this->asJson($searchModel->outResult($dataProvider));
    }

    /**
     * @return array|false
     */
    protected function getRate()
    {
        $api = new RateApi();
        $result = $api->getRate();
        if ($result['code'] == 1) {
            return $result['data'];
        } else {
            return false;
        }
    }
}