<?php
/**
 * Created by PhpStorm.
 * User: Admin 
 * Date: 2018/8/18
 * Time: 0:19
 */

namespace backend\controllers;


use backend\helpers\MailHelper;
use backend\helpers\RandomHelper;
use backend\models\Config;
use backend\models\Member;
use common\helpers\MtKit;
use Yii;

class SameaccountController extends BaseController
{

    /**
     * 开设同步账户
     */
    public function actionAddAccount()
    {
        $member_id = Yii::$app->request->post('member_id');
        $lever = Yii::$app->request->post('lever');

        $mt4_login_pwd = RandomHelper::getRandomStringForMt4();
        $mt4_view_pwd = RandomHelper::getRandomStringForMt4();

        $member = Member::find()->where(['id' => $member_id])->asArray()->one();
        $configData = Config::find()->asArray()->one();
        $mt4ManagerIp = trim($configData['mt4_manager_ip']);
        $mt4ManagerPort = trim($configData['mt4_manager_port']);


        $passwordPhone = $mt4_view_pwd;
        $email = $member['username'];
        $phone = $member['mobile'];
        $leverage = $lever;
        $state = 'hk';
        $country = 'hk';
        $city = 'hk';
        $address = $member['address'];
        $zipCode = '';
        $group = 1;
        $id = $member['id_no'];
        $agentAccount = 0;
        $login = 0;

        $login = MtKit::openUser($mt4ManagerIp, $mt4ManagerPort, $login, $mt4_login_pwd, $mt4_view_pwd, $passwordPhone, $member['name'],
            $group, $email, $phone, $leverage, $country, $state, $city, $address, $zipCode, $id, $agentAccount);

        if (!$login) {
            return $this->outJson(0, $login, '开设同名账户失败');
        }

        $logins = $member['logins'] . ',' . $login;

        Member::updateAll(['logins' => $logins], "id=$member_id");

        $config = Config::findOne(1);
        $paramArray = [
            'name' => $member['name'],
            'mt4Login' => $login,
            'mt4LoginPwd' => $mt4_login_pwd,
            'mt4ViewPwd' => $mt4_view_pwd,
        ];
        // 发送邮件
        MailHelper::sendMail("恭喜您，您的账户已成功开通",  $member['username'], $paramArray, '', $config->mail_same_account);

        return $this->outJson(1, ['login' => $login, 'mt4LoginPwd' => $mt4_login_pwd, 'mt4ViewPwd' => $mt4_view_pwd ], '操作成功');
    }

}