<?php
namespace frontend\controllers;

use backend\helpers\DateTimeHelper;
use frontend\models\Enroll;
use frontend\models\PartnerApply;
use frontend\models\Register;
use Yii;
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;

/**
 * 用户留下联系方式
 */
class RegisterController extends BaseController
{
    public function actionSaveRegister()
    {
        $request = Yii::$app->request;
        $model = new Register();
        $model->name = trim($request->post('name'));
        $model->mobile = trim($request->post('mobile'));
        $model->email = trim($request->post('email'));
        $model->create_time = date('Y-m-d H:i:s');
        
        if ($model->save()) {
            return $this->outJson(1);
        } else {
            $errors = array_values($model->getFirstErrors());
            $message = isset($errors[0]) ? $errors[0] : '';
            return $this->outJson(0, [], $message);
        }
    }

    /**
     * 用户申请合伙人
     */
    public function actionSavePartnerApply()
    {
        $request = \Yii::$app->request;
        $model = new PartnerApply();
        $model->name = trim($request->post('name'));
        $model->mobile = trim($request->post('mobile'));
        $model->email = trim($request->post('email'));
        $model->type = trim($request->post('type'));
        $model->memo = Html::encode($request->post('memo'));
        $model->create_time = date('Y-m-d H:i:s');

        if ($model->save()) {
            return $this->outJson(1);
        } else {
            $errors = array_values($model->getFirstErrors());
            $message = isset($errors[0]) ? $errors[0] : '';
            return $this->outJson(0, [], $message);
        }
    }

    /**
     * 保存原落地页报名信息
     */
    public function actionSaveEnroll()
    {
        $request = Yii::$app->getRequest()->post();
        $model = new Enroll();
        $model->in_time = DateTimeHelper::microtime_float();
        $model->setAttributes($request);
        if ($model->save()) {
            return $this->outJson(1, $model->id);
        } else {
            return $this->outJson(0, [], '保存失败!');
        }
    }
}