<?php

namespace app\agent\controller;

use fastphp\base\Jwt;
use app\agent\model\AgentUser;
use app\agent\model\User as UserModel;

class Index
{

    //代理登录
    public function login()
    {
        $data = json_decode(file_get_contents("php://input"), true);
        $jwt = new Jwt('etor_yh_lzh_20f_2020_YES');
        $agent_user_model = new AgentUser;
        $userinfo = $agent_user_model->getOne(['username'=>$data['Logincode']]);
        $userinfo['ID'] = $userinfo['id'];

        $payload = [
            'LoginName' => $userinfo['realname'],
            'RoleName' => '',
            'OperaterID' => $userinfo['id'],
            'TenantId' => 1157,
            'iat' => time(),
            'exp' => time() + 180 * 60
        ];

        $token = $jwt::getToken($payload);

        $return_data = [
            'Manager'=>$userinfo,
            'Token'=>$token
        ];

        if (!empty($userinfo)){
            if (cToMd5($data['Password']) == $userinfo['password']){
                
                $info = [
                    'Code'=>10000,
                    'Data'=>$return_data
                ];
                echo  json_encode($info);
                
            }
        }

    }

    //推广用户注册
    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;
        }

        $data['agent_id'] = base64_decode($data['agent_id']);
        // dump($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['Wx'];
        $data['QQ'] = $data['QQ'];
        $data['LastLoginDate'] = date('Y-m-d H:i:s',time());
        $data['Password'] = empty($data['Password'])?cToMd5('1234'):cToMd5(trim($data['Password']));

        $user_model = new UserModel;
        $manager_list = $user_model->getManagerList([],'ManagerId,ManagerName,count(1) as num',['ManagerId'],'num asc');
        $data['ManagerId'] = $manager_list[0]['ManagerId'];
        $data['ManagerName'] = $manager_list[0]['ManagerName'];

        $has_user = $user_model->isExit("LoginCode='".$data['LoginCode']."' or Phone='".$data['Phone']."' or TaoBao='".$data['Phone']."'");
        if ($has_user){
            $info = [
                'Code' => 10007,
                'Data' => '',
                'Message' => '该账号或者手机号被注册了',
            ];
            echo json_encode($info);
            die;
        }

        if ($user_model->add($data)) {
            $info = [
                'Code' => 10000,
                'Data' => '',
                'Message' => '',
            ];
            echo json_encode($info);
            die;
        }
        die;
    }
}