<?php
namespace Home\Controller;

use Redis;
use Think\Upload\Driver\Qiniu\QiniuStorage;
use SignedCookie;
use Think\Controller;
use Zhida\OAuthHelperV3;
use Think\Log;

class IndexController extends Controller
{
    private $auth_type = OAuthHelperV3::SCOPE_BASE;
    private $cookie_uid_key = 'mtime-laopao-v1';
    private $debug = 1;
    private $today;
    use ControllerTrait;

    public function _initialize()
    {
        $this->debug = $this->debug && session('X-DEBUG') ? true : false;
        $pub_mode = C('PUBLISH_MODE');
        if (ACTION_NAME != 'test_magic' && ACTION_NAME != 'debug_close' && ACTION_NAME != 'debug_open'
            && $pub_mode != 'production'
            && cookie('X-PUB_MODE') != 'development'
        ) {
            header("Content-type: text/html; charset=utf-8");
            echo '活动尚未开始，敬请期待';
            exit(0);
        }
        $this->today = date('Ymd');
    }
//$user = $this->get_user_info_from_cookie();
//if (!$user) {
//$this->ajaxReturn(array(
//'errcode' => 1,
//'errmsg' => '用户信息错误',
//));
//return;
//}
    public function index()
    {
//        $user = $this->get_user_info_from_cookie();
//        if (!$user) {
//            $this->request_auth($this->get_url());
//            return;
//        }
        $list = glob('Public/static/images/*');

        foreach ($list as $key => $value) {
            $list[$key] = '/' . $value;
        }
        $image = json_encode($list);

        $this->assign('image', $image);
        $this->display();
    }

    public function algorithm()
    {

        $age = $_POST['age'];
        $year = $_POST['year'];
        $type = $_POST['type'];
        if ($year == '100') {
            $parameter = 'paid';
        } elseif ($year == '3') {
            $parameter = 'three';

        } elseif ($year == '5') {
            $parameter = 'five';
        } elseif ($year == '10') {
            $parameter = 'ten';
        } elseif ($year == '15') {
            $parameter = 'fifteen';
        } elseif ($year == '20') {
            $parameter = 'twenty';
        }
        $count = M('Tariff')->where(array(
            'age' => $age,
            'type' => $type

        ))->find();
        if ($count) {

            //        无这个周岁
            $this->ajaxReturn(array(
                'errcode' => 0,
                'sate' => 1,
                'info' => $count[$parameter]
            ));
            return;

        } else {
            //        无这个周岁
            $this->ajaxReturn(array(
                'errcode' => 0,
                'sate' => 0
            ));
            return;

        }


    }

    public function ceshi(){
        $type=$_POST['type'];
        $sex=$_POST['sex'];
        $j_year=$_POST['j_year'];
        $year=$_POST['year'];
        if($year <=10){
            $key=$year;
            $list= M('One')->where(array(
                'type'=>$type,
                'sex'=>$sex,

            ))->select();

        }else if($year >10 &&  $year<=21){
            $key=$year- 11;
            $list= M('Two')->where(array(
                'type'=>$type,
                'sex'=>$sex,

            ))->select();

        }elseif($year >21 && $year <=32){
            $key=$year- 22;
            $list= M('Three')->where(array(
                'type'=>$type,
                'sex'=>$sex,

            ))->select();
        }elseif($year >32 && $year <=44){
            if($year==39){
                $key=0;
                $list= M('Eight')->where(array(
                    'type'=>$type,
                    'sex'=>$sex,
                ))->select();
            }else if($year >=40 ){
                $key=$year- 33-1;
                $list= M('Four')->where(array(
                    'type'=>$type,
                    'sex'=>$sex,
                ))->select();
            }else{
                $key=$year-33;
                $list= M('Four')->where(array(
                    'type'=>$type,
                    'sex'=>$sex,
                ))->select();
            }


        }elseif($year >44 && $year <=55){
            $key=$year- 45;
            $list= M('Five')->where(array(
                'type'=>$type,
                'sex'=>$sex,

            ))->select();
        }elseif($year >55 && $year <=66){
            $key=$year- 56;
            $list= M('Six')->where(array(
                'type'=>$type,
                'sex'=>$sex,
            ))->select();
        }elseif($year >66 && $year <=70){
            $key=$year- 67;
            $list= M('Seven')->where(array(
                'type'=>$type,
                'sex'=>$sex,

            ))->select();
        }
        $this->ajaxReturn(array(
            'errcode' => 0,
            'sate' => 0,
            'key'=>$key,
            'list'=>$list
        ));
        return;
    }
}
