<?php
namespace App\Sports\Controller;

use BaseController\Controller;
use App\Sports\Controller\Validation;

/**
 * Class Register
 * @package App\Sports\Controller
 * User: tank
 * Date: 2019/3/19
 */
class Register extends Controller{

    /**
     * 新增账号
     */
    public function userAdd(){

        $data = $_REQUEST;
        try {
            if (empty($data['account'])) throw new \Exception(Render([], '4013', lang('Tips','Sports')->get('no_account')));
            if (empty($data['password'])) throw new \Exception(Render([], '4014', lang('Tips','Sports')->get('no_password')));
            if (empty($data['repassword'])) throw new \Exception(Render([], '4015', lang('Tips','Sports')->get('no_repassword')));
            if (empty($data['problemID'])) throw new \Exception(Render([], '4011', lang('Tips','Sports')->get('no_problem')));
            if (empty($data['answer'])) throw new \Exception(Render([], '4012', lang('Tips','Sports')->get('no_answer')));
            if (empty($data['wmpassword'])) throw new \Exception(Render([], '4016', lang('Tips','Sports')->get('no_wmpassword')));
            if ($data['agree'] < 1) throw new \Exception(Render([], '4018', lang('Tips','Sports')->get('no_agree')));
            if ($data['password'] != $data['repassword']) throw new \Exception(Render([], '4017', lang('Tips','Sports')->get('no_pwd_equally')));
            if (strcmp($data['password'],$data['wmpassword']) == 0) throw new \Exception(Render([], '4024', lang('Tips','Sports')->get('agreement_wmpassword_password')));
            if (Validation::isAccount($data['account']) != 1) throw new \Exception(Render([], '4020', lang('Tips','Sports')->get('illegal_account')));
            if (Validation::isAccount($data['password']) != 1) throw new \Exception(Render([], '4021', lang('Tips','Sports')->get('illegal_password')));
            if (Validation::isAccount($data['answer']) != 1) throw new \Exception(Render([], '4022', lang('Tips','Sports')->get('illegal_answer')));
            if (Validation::isAccount($data['wmpassword']) != 1) throw new \Exception(Render([], '4023', lang('Tips','Sports')->get('illegal_wmpassword')));

            $res = lm('Account', 'Sports')->where('username',$data['account'])->select('id')->first();
            if(!empty($res->id)) throw new \Exception(Render([], '4004', lang('Tips','Sports')->get('no_null_account')));

            $obj = [
                'username'=>$data['account'],
                'userpwd'=>$data['password'],
                'qukuanpwd'=>$data['wmpassword'],
                'pwdpoint_problemid'=>$data['problemID'],
                'pwdpoint_answer'=>$data['answer']
            ];
            $inser = lm('Account', 'Sports')->insert($obj);
            if(empty($inser)) throw new \Exception(Render([], '4005', lang('Tips','Sports')->get('fail')));
            Render([], '200', lang('Tips','Sports')->get('success'));
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }


}