<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/6/11
 * Time: 上午9:28
 */

namespace common\api;

use Yii;
use linslin\yii2\curl\Curl;
use backend\base\Help;

class EnrollServer
{

    public $housename;
    public $mobile;
    public $city;
    public $username;
    public $houseType;
    public $budget;

    /*
     * 计算当天剩余时间 返回秒
     * */
    public function Remainingime()
    {
        $year = date("Y");
        $month = date("m");
        $day = date("d");
        $end = mktime(23, 59, 59, $month, $day, $year);//当天结束时间戳
        return (date('H', $end) - date('H', time())) * 60 * 60;//剩余小时数,
    }

    /*
     * 限制一天报名次数
     * */
    public function Restrict($input = null)
    {
        $data = (new Curl())->setPostParams([
            'ucode' => 'http://ygadmin.yigouf.com',
            'phone' => $input['mobile']
        ])->post('http://ossimg.pinfangw.com/public/signrestrict');
        $data = json_decode($data, true);
        if ($data['code'] == 200) {
            if ($data['data']['is_test']) {
                return $data['data'];
            }
            $ipinfo = new IpInfoServer();
            $ip = $ipinfo->getUserIp();
            $cache = \common\api\CacheServer::Cache();
            $cacheName = md5($ip);
            $CacheContent = $cache->get($cacheName);
            $data['data']['count'] = $CacheContent;
            if ($CacheContent === false) {
                $data['data']['sign'] = false;
                $cache->set($cacheName, 1, $this->Remainingime());
            } else {
                if ($data['data']['sign'] != 0 && $CacheContent >= $data['data']['sign']) {
                    $data['data']['sign'] == true;//达到指定次数进行限制
                } else {
                    $data['data']['sign'] = false;
                    $cache->set($cacheName, ++$CacheContent, $this->Remainingime());
                }
            }
        } else {
            $data['data']['is_test'] = false;
            $data['data']['sign'] == fasle;
        }
        return $data['data'];
    }

    /*
     * 有归属楼盘 模板
     * */
    public function TemplateOne()
    {
        $str = '';
        $str .= ' <table class="table">';
        $str .= '<tr><td>信息订阅!</td></tr>';
        if (!empty($this->username)) {
            $str .= ' <tr><td>姓名:</td><td>' . $this->username . '</td></tr>';
        }

        if (!empty($this->mobile)) {
            $str .= '<tr><td>电话:</td><td>' . $this->mobile . '</td></tr>';
        }

        if (!empty($this->city)) {
            $str .= '<tr><td>区域:</td><td>' . $this->city . '</td></tr>';
        }

        if (!empty($this->housename)) {
            $str .= '<tr><td>意向楼盘:</td><td>' . $this->housename . '</td></tr>';
        }

        if (!empty($this->houseType)) {
            $str .= '<tr><td>意向户型:</td><td>' . $this->houseType . '</td></tr>';
        }

        if (!empty($this->city)) {
            $str .= '<tr><td>意向区域:</td><td>' . $this->city . '</td></tr>';
        }

        if (!empty($this->budget)) {
            $str .= '<tr><td>预算:</td><td>' . $this->budget . '</td></tr>';
        }

        $str .= ' </table>';
        return $str;

        $msgemail = <<<Eof
                                   <!doctype html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport"
                      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
                <meta http-equiv="X-UA-Compatible" content="ie=edge">
                <title>邮件</title>
            </head>
            <body>
                    <table class="table">
                        <tr>
                            <td>信息订阅!</td>
                        </tr>
                        <tr>
                            <td>姓名:</td>
                            <td>{$this->username}</td>
                        </tr>
                           
                        <tr>
                            <td>电话:</td>
                            <td>{$this->mobile}</td>
                        </tr>
                        <tr>
                            <td>区域:</td>
                            <td>{$this->city}</td>
                        </tr>
                        <tr>
                            <td>意向楼盘:</td>
                            <td>{$this->housename}</td>
                        </tr>
                    </table>
            </body>
            </html>
Eof;
        return $msgemail;
    }

    /*
     *公共 模板
     * */
    public function TemplateTwo()
    {

    }

    public function TabelEmail($input)
    {
        $str = '';
        $str .= ' <table class="table">';
        $str .= '<tr><td>信息订阅!</td></tr>';
        if (!empty($input['name'])) {
            $str .= ' <tr><td>姓名:</td><td>' . $input['name'] . '</td></tr>';
        }

        if (!empty($input['mobile'])) {
            $str .= '<tr><td>电话:</td><td>' . $input['mobile'] . '</td></tr>';
        }

        if (!empty($input['city_name'])) {
            $str .= '<tr><td>区域:</td><td>' . $input['city_name'] . '</td></tr>';
        }

        if (!empty($input['house_name'])) {
            $str .= '<tr><td>意向楼盘:</td><td>' . $input['house_name'] . '</td></tr>';
        }

        if (!empty($input['intention_housetype'])) {
            $str .= '<tr><td>意向户型:</td><td>' . $input['intention_housetype'] . '</td></tr>';
        }

        if (!empty($input['intention_city'])) {
            $str .= '<tr><td>意向区域:</td><td>' . $input['intention_city'] . '</td></tr>';
        }

        if (!empty($input['budget'])) {
            $str .= '<tr><td>预算:</td><td>' . $input['budget'] . '</td></tr>';
        }

        $str .= ' </table>';
        return $str;
    }

    public function GetArr($input, $str)
    {
        $arr = [];
        $arr['email'] = $input['send_email'];
        $arr['title'] = Yii::t('app', 'enter_email');
        $arr['content'] = $str;
        $arr['id'] = $input['id'];
        return $arr;
    }

    public function ChacPhone($mobile)
    {
        //限制电话号码
        $new_time = time();
        $astrict_phone = \common\models\AstrictPhone::find()
            ->where(['mobile'=>$mobile,'state'=>1])
            ->one();
        if(!empty($astrict_phone)){
            if(($astrict_phone['astrict_start'] == 0 && $astrict_phone['astrict_start'] == 0)|| ($astrict_phone['astrict_start'] <= $new_time && $astrict_phone['astrict_start'] == 0)||($astrict_phone['astrict_start'] <= $new_time && $astrict_phone['astrict_start'] >= $new_time)){
                return ['code'=>true,'msg'=>$astrict_phone['astrict_msg']];
            }
        }
        //今天是否已经报名过
        $day_start_time =strtotime(date("Y-m-d",time()));
        $rows = \common\models\Enroll::find()
            ->where(['mobile'=>$mobile])
            ->andWhere(['>','create_at',$day_start_time])
            ->orderBy(['create_at'=>SORT_DESC,'id'=>SORT_DESC])
            ->asArray()->one();
        if(!empty($rows)) return ['code'=>true,'msg'=>'电话号码已经报过名!'];
    }
}