<?php


namespace wechat\controllers;

use wechat\base\CommonController;
use Yii;
use wechat\base\Help;
use common\api\EnrollServer;

class EnrollController extends CommonController
{
    public $enableCsrfValidation = false;
    /*
     * name             姓名
     * source_model     来源模块
     * mobile           手机号
     * intention_house  意向楼盘
     * hid              楼盘ID 不存在:0
     * equipment        设备 2=>pc   1=>移动
     * city             区域
     *
     * */
    public function actionSignup()
    {
        $en = new EnrollServer();
        $cacheResult = $en->Restrict();
        if($cacheResult === false) {
            return Help::JsonCode(Help::ERROR,'您今天的报名次数已超过限制');
        }

        //根据报名判断是否有相应的区域邮箱
        $cityEmail = new \common\models\Email();
        $input = Yii::$app->request->post();
        $en->mobile = $input['mobile'];

        if(!empty($input['intention_house']))
        {
            $en->housename = $input['intention_house'];
        }

        if(!empty($input['budget']))
        {
            $en->budget = $input['budget'];
        }

        if(!empty($input['intention_housetype']))
        {
            $en->houseType = $input['intention_housetype'];
        }


		if(!empty($input['intention_city']))
        {
            $city_rows = \common\models\CategoryCity::findOne(['city_name'=> $input['intention_city']]);
            if($city_rows)
            {
                $city = $city_rows['id'];
            }

            $en->city = $input['intention_city'];
        }


        if(!empty($input['hid'])){
            $houst = new \common\models\House();
            $houseModel  = $houst->FindById($input['hid']);  //楼盘相关信息
            $en->housename = $houseModel['name'];
            $city = $houseModel['city'];
//                $email = $cityEmail->FindByEmail($houseModel['city']);
//                $en->city = $email['city_name'];
        }else if(!empty($input['city'])){
            $city = $input['city'];
//                $email = $cityEmail->FindByEmail($input['city']);
//                $en->city = $email['city_name'];
        }

        //根据区域发送指定邮箱
        if(!empty($city))
        {
            $cityEmail = new \common\models\Email();
            $email = $cityEmail->FindByEmail($city);
            $catCity = ( new \common\models\CategoryCity())->FindById($city);
            $en->city = $catCity['city_name'];
        }


        if(!empty($email))
        {
            $e = $email['email'];
            $en->city = $email['city_name'];
        }
        else
        {
            $e = Yii::$app->params['default_email'];
        }

        $model = new \common\models\Enroll();
        $model->scenario = 'madd';
        $auth = $model->Authenticator($input);

        if(is_object($auth))
        {
            $auth->ip      = Yii::$app->request->userIP;
            $auth->send_email      = $e;
            if($auth->save(false))
            {
                $auth->attributes['id'];
                $en->username = $auth->name;
                //发送邮件
                $event = new \common\event\Event();
                $arr = [];
                $arr['email'] = $e;
                $arr['title'] = '客户报名信息';
                $arr['content'] = $en->TemplateOne();
                $arr['id'] =    $auth->attributes['id'];
                $event->SendEmail($arr);
                return Help::JsonCode(Help::SUCCESS,'提交成功');
            }
        }
        return Help::JsonCode(Help::ERROR,'提交失败',$auth);

    }



}