<?php

namespace app\taobao\controller;

use app\agent\model\User as UserModel;
use app\taobao\model\SellCouponUserOrgin as CouponModel;
use alipay\AliverifyPublic as Aliverify;
use extend\alisms\Sms;
use extend\redis\Redisop;

class Index
{

    public function getCode() {
        $phone = json_decode(file_get_contents("php://input"), true)['phone'];
        $redis = new Redisop();
        $user_model = new UserModel();
        $is_exit = $user_model->getOne(['Phone' => $phone]);
        if($is_exit) {
            $return_data = [
                'code' => 0,
                'msg' => '用户已经存在'
            ];
            echo json_encode($return_data);die;
        }


        $getCode = $redis->get($phone);

        if (empty($getCode)) {
            $code = rand(1000,9999);
            $redis->setOfTime($phone,$code);
            $alisms = new Sms;
            $alisms::sendVerify($phone, $code);
            $return_data = [
                'code' => 1,
                'msg' => '验证码已发送'
            ];
            echo json_encode($return_data);die;
        } else {
            $return_data = [
                'code' => 0,
                'msg' => '请稍后再试'
            ];
            echo json_encode($return_data);die;
        }
        
    }
    //---------------

    //推广用户注册
    public function register()
    {
        $data = json_decode(file_get_contents("php://input"), true);
        // dump($data);die;
        $data['LoginCode'] = trim($data['LoginCode']);

        if(empty($data['LoginCode'])){
            $info = [
                'Code' => 10003,
                'Data' => '',
                'Message' => '账号为空',
            ];
            echo json_encode($info);
            die;
        }
        $redis = new Redisop();
        $code = $redis->get($data['LoginCode']);


        if (empty($code)) {
            $return_data = [
                'code' => 0,
                'msg' => '验证码不正确'
            ];
            echo json_encode($return_data);die;
        } 
        if ($data['code'] != $code) {
            $return_data = [
                'code' => 0,
                'msg' => '验证码不正确'
            ];
            echo json_encode($return_data);die;
        }
        //----

        $data['Phone'] = $data['LoginCode'];
        $data['TenantId'] = 0;
        $data['Enabled'] = 1;
        $data['UpdateTime'] = date('Y-m-d H:i:s',time());
        $data['CreateTime'] = date('Y-m-d H:i:s',time());
        $data['CreateType'] = 4;
        $data['ProductAccountCount'] = 0;
        $data['ExpiredProductAccountCount'] = 0;
        $data['RestAmount'] = 0;
        $data['ConsumeAmount'] = 0;
        $data['DeleteTag'] = 0;
        $data['UseTestCount'] = 0;
        $data['Sex'] = 0;
        $data['Wx'] = '';
        $data['QQ'] = '';
        $data['LastLoginDate'] = date('Y-m-d H:i:s',time());
        $data['Password'] = empty($data['Password'])?cToMd5('123654'):cToMd5(trim($data['Password']));

        $user_model = new UserModel;
        unset($data['code']);

        $has_user = $user_model->isExit("LoginCode='".$data['LoginCode']."' or Phone='".$data['Phone']."' or TaoBao='".$data['Phone']."'");
        if ($has_user){
            if ($has_user['is_verify'] == 0) {
                $data_user_old = [
                    'LoginCode' => $has_user['LoginCode'].'-del',
                    'Phone' => $has_user['Phone'].'-del',
                ];

                $user_model->updateOne(['Id'=>$has_user['Id']],$data_user_old);
            }
            // $info = [
            //     'Code' => 20000,
            //     'Data' => '',
            //     'Message' => '该账号或者手机号被注册了',
            // ];
            // echo json_encode($info);
            // die;
        }

        $has_user2 = $user_model->isExit("id_code='".$data['id_code']."' and is_verify=1");
        
        if ($has_user2){
            $info = [
                'Code' => 30000,
                'Data' => '',
                'Message' => '该身份已经进行认证，无法再次进行认证！',
            ];
            echo json_encode($info);
            die;
        }

        if ($user_model->add($data)) {

            echo json_encode($this->aliverify([
                'id_code' =>  $data['id_code'],
                'name' =>  $data['Name'],
                'phone' => $data['LoginCode']
            ]));
        }
        die;
    }

    public function aliverify($temp)
    {
        $info = Aliverify::initVerify2($temp);
        $user = new UserModel;
        $data = [];
        if($info){
            $update_one['certify_id'] = $info['certifyId'];
            $update_one['Name'] = $temp['name'];
            if(isset($temp['phone'])){
                $update_one['Phone'] = $temp['phone'];
            }
            $update_one['id_code'] = $temp['id_code'];
            $data['certify_id'] = $info['certifyId'];
            $data['url'] = $info['url'];
            if($user->updateOne(['LoginCode'=>$temp['phone']],$update_one)){
                $data['Code'] = 10000;
            } else {
                $data = [
                    'Code'=>-10000
                ];
            }
        }  else {
            $data = [
                'Code'=>-10000
            ];
        }
        return $data;
    }

    public function aliGetResult()
    {
        $user_phone = $_GET['user'];
        $user = new UserModel;
        $user_one_info = $user->getOne(['LoginCode'=>$user_phone]);
        $info = Aliverify::getResult2($user_one_info['certify_id']);
        $data = [];
        if($info){
            $user_data['is_verify'] = 1;
            if ($user_one_info['is_verify'] == 1) {
                $data = [
                    'Code'=>10000
                ];
                echo  json_encode($data);
                die;
            }
            if($user->updateOne(['Id'=>$user_one_info['Id']],$user_data)){
                $data = [
                    'Code'=>10000
                ];

                $date1 = date('Y-m-d H:i:s');
                $date2 = date('Y-m-d H:i:s',(30*86400 + time()));

                $coupon_data = [
                    [
                        'TenantId' => 0,
                        'ToUser' => $user_one_info['Id'],
                        'CouponId' => 1,
                        'CouponCount' => 1,
                        'OriginType' => 2,
                        'Remark' => '淘宝下单',
                        'DeleteTag' => 0,
                        'CreateDate' => $date1 ,
                        'StartTime' => $date1 ,
                        'EndTime' => $date2,
                        'Status' => 1,
                        'ToUserRef' => '暂无',
                    ],
                    [
                        'TenantId' => 0,
                        'ToUser' => $user_one_info['Id'],
                        'CouponId' => 8,
                        'CouponCount' => 1,
                        'OriginType' => 2,
                        'Remark' => '淘宝下单',
                        'DeleteTag' => 0,
                        'CreateDate' => $date1 ,
                        'StartTime' => $date1 ,
                        'EndTime' => $date2,
                        'Status' => 1,
                        'ToUserRef' => '暂无',
                    ],
                    [
                        'TenantId' => 0,
                        'ToUser' => $user_one_info['Id'],
                        'CouponId' => 10,
                        'CouponCount' => 1,
                        'OriginType' => 2,
                        'Remark' => '淘宝下单',
                        'DeleteTag' => 0,
                        'CreateDate' => $date1 ,
                        'StartTime' => $date1 ,
                        'EndTime' => $date2,
                        'Status' => 1,
                        'ToUserRef' => '暂无',
                    ],
                    [
                        'TenantId' => 0,
                        'ToUser' => $user_one_info['Id'],
                        'CouponId' => 14,
                        'CouponCount' => 1,
                        'OriginType' => 2,
                        'Remark' => '淘宝下单',
                        'DeleteTag' => 0,
                        'CreateDate' => $date1 ,
                        'StartTime' => $date1 ,
                        'EndTime' => $date2,
                        'Status' => 1,
                        'ToUserRef' => '暂无',
                    ],
                ];

                $coupon = new CouponModel;
                $coupon->addAll($coupon_data);

            }
        } else {
            $data = [
                'Code'=>-10000
            ];
        }
        echo  json_encode($data);
    }
    

    
    public function isverify() {
        $user_phone = $_GET['phone'];
        
        $user_model = new UserModel;

        $userinfo = $user_model->getOne(['LoginCode' => $user_phone],'is_verify');

        echo json_encode($userinfo);
    }


}