<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/11/6
 * Time: 下午6:07
 */

namespace backend\modules\user\controllers;


use backend\models\searches\TransferSearch;
use backend\models\TransferApi;
use backend\models\MemberIdentity;

class TransferController extends BaseController
{
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $member_id = $this->getMemberId();
        $transfer = new TransferApi();
        $rs = $transfer->transfer($member_id);
        return $this->render('index', [
            'data' => $rs['data'],
        ]);
    }

    public function actionOpen()
    {
        return $this->render('open', [
            'data' => [],
        ]);
    }

    /**
     * 获取同名转账申请记录
     */
    public function actionTransferAjax()
    {
        $searchModel = new TransferSearch();
        $request = \Yii::$app->getRequest()->getQueryParams();
        $request['member_id'] = $this->getMemberId();
        $dataProvider = $searchModel->search($request);
        return $this->asJson($searchModel->outResult($dataProvider));
    }

    /**
     * 同名转账申请
     */
    public function actionTransferSave()
    {
        $request = \Yii::$app->request;
        if ($request->isPost) {
            $params = $request->post();
            $member_id = $this->getMemberId();
            $params['member_id'] = isset($member_id) ? $member_id : '';
            $params['from_login'] = isset($params['flogin']) ? trim($params['flogin']) : '';
            $params['to_login'] = isset($params['tlogin']) ? trim($params['tlogin']) : '';
            $params['amount'] = isset($params['amount']) ? trim($params['amount']) : '';
            $params['type'] = MemberIdentity::MEMBER_TYPE_USER;
            $transferApi = new TransferApi();
            $rs = $transferApi->addTransfer($params);
            return $this->asJson($rs);
        }
    }

    /**
     * 验证转入账户是否为转出账户的名下用户
     */
    public function actionIsIbUser()
    {
        $data = \Yii::$app->request->get();
        $api = new TransferApi();
        $data['type'] = MemberIdentity::MEMBER_TYPE_USER;
        $rs = $api->isIbUser($data);
        return $this->asJson($rs['data']);
    }
}

