<?php

session_start(); // starts new or resumes existing session
Doo::loadModelAt('auser', 'admin');
Doo::loadModelAt('ausers', 'admin');
Doo::loadModel('users');
Doo::loadClass('profile');
Doo::loadClass('PasswordHash');
Doo::loadClass('mailer');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// 列表停用 编辑 重置密码
// 管理员权限管理
// 管理员修改密码
class AdminController extends DooController {

    private $data, $users, $user, $profile, $ph, $userz, $mailer;

    public function beforeRun($resource, $action) {
        if (!isset($_SESSION['auid'])) {
            return Doo::conf()->APP_URL . 'manage';
        }
    }

    public function __construct() {
        $this->users = new AUsers();
        $this->user = new AUser();
        $this->userz = new Users();
        $this->profile = new Profile();
        $this->mailer = new Mailer();
        $this->ph = new PasswordHash(8, FALSE);
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
    }

    function addUser() {
        $params = NULL;
        if (isset($_POST['email'])) {
            if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
                $postArray = $_POST;
                $passwdStr = $this->randomPassword();
                $postArray['userid'] = $this->users->createUser($_POST['email'], $passwdStr);
                if (isset($postArray['userid'])) {
                    $this->profile->insertProfile($postArray);
                    $this->mailer->setEmails($_POST['email']);
                    $this->mailer->seteTitle('新账号开通');
                    $this->mailer->setClientName($postArray['realname']);
                    $signupConfigStr = '<p>开通了计量支付云版的帐号：</p><p>登录帐号（邮箱）：<b><a href="mailto:' . $_POST['email'] . '" target="_blank">' . $_POST['email'] . '</a></b></p><p>登录密码：<b>' . $passwdStr . '</b></p><p>请及时登录并修改您的个人信息及密码。</p>';
                    $this->mailer->setContent($signupConfigStr);
                    $this->mailer->send_mail();
                    return Doo::conf()->APP_URL . 'manage/user/list';
                } else {
                    return Doo::conf()->APP_URL . 'manage/user/add';
                }
            } else {
                return Doo::conf()->APP_URL . 'manage/user/add';
            }
        }
        $this->render('admin-addUser', $this->data, TRUE);
    }

    public function randomPassword() {
        $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
        $pass = array(); //remember to declare $pass as an array
        $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
        for ($i = 0; $i < 8; $i++) {
            $n = rand(0, $alphaLength);
            $pass[] = $alphabet[$n];
        }
        return implode($pass); //turn the array into a string
    }

    function editUser() {
        $this->data['users'] = $this->users->getOne(array('where' => 'uid = ?', 'param' => array($this->params['uid']), 'asArray' => TRUE));
        $this->data['profile'] = $this->profile->getProWithUid($this->params['uid']);
        if (isset($_POST['email']) && ($_POST['email'] != $this->data['users']['uemail']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            if (!$this->userz->getOne(array('where' => 'uemail = ?', 'param' => array($_POST['email']), 'asArray' => TRUE))) {
                $this->userz->uemail = $_POST['email'];
                $this->userz->update(array('where' => 'uid = ?', 'param' => array($this->params['uid'])));
            }
        }
        if (isset($_POST['company']) && isset($_POST['jobs']) && isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['mobile'])) {
            $this->profile->upProfile($this->params['uid'], $_POST);
            return Doo::conf()->APP_URL . 'manage/user/list';
        }
        $this->render('admin-editUser', $this->data, TRUE);
    }

    function option() {
        $this->data['auser'] = $this->user->getOne(array('where' => 'auid = ?', 'param' => array($_SESSION['auid']), 'asArray' => TRUE));
        if (isset($_POST['oldpasswd']) && isset($_POST['newpasswd']) && isset($_POST['renewpasswd']) && ($_POST['newpasswd'] == $_POST['renewpasswd']) && $this->ph->CheckPassword($_POST['oldpasswd'], $this->data['auser']['aupass'])) {
            $this->user->upPasswWd($_SESSION['auid'], $this->ph->HashPassword($_POST['newpasswd']));
            return Doo::conf()->APP_URL . 'manage/user/list';
        }
        $this->render('admin-option', $this->data, TRUE);
    }

    function userSwitch() {
        $userzArray = $this->userz->getOne(array('where' => 'uid = ?', 'param' => array($this->params['uid']), 'asArray' => TRUE));
        if (isset($userzArray['uid']) && $userzArray['isstop']) {
            $this->userz->isstop = 0;
        } else {
            $this->userz->isstop = 1;
        }
        $this->userz->update(array('where' => 'uid = ?', 'param' => array($this->params['uid'])));
        return Doo::conf()->APP_URL . 'manage/user/list';
    }

    function userRepasswd() {
        $userzArray = $this->userz->getOne(array('where' => 'uid = ?', 'param' => array($this->params['uid']), 'asArray' => TRUE));
        if (isset($userzArray['uemail'])) {
            $passwdStr = $this->randomPassword();
            $this->userz->upass = $this->ph->HashPassword($passwdStr);
            if ($this->userz->update(array('where' => 'uid=?', 'param' => array($this->params['uid'])))) {
                $proArray = $this->profile->getProWithUid($this->params['uid']);
                $this->mailer->setEmails($userzArray['uemail']);
                $this->mailer->seteTitle('密码重置');
                $this->mailer->setClientName($proArray['name']);
                $signupConfigStr = '<p>重置了计量支付云版的帐号密码：</p><p>登录帐号（邮箱）：<b><a href="mailto:' . $userzArray['uemail'] . '" target="_blank">' . $userzArray['uemail'] . '</a></b></p><p>登录密码：<b>' . $passwdStr . '</b></p><p>请及时登录并修改您的新密码。</p>';
                $this->mailer->setContent($signupConfigStr);
                $this->mailer->send_mail();
                echo $userzArray['uemail'];
            }
        }
    }

    function userList() {
        if (isset($_SESSION['passwd'])) {
            echo '添加用户的密码是：' . $_SESSION['passwd'];
        }
        $this->data['userlist'] = $this->users->getRowAll();
        foreach ($this->data['userlist'] as $key => $value) {
            $proArray = $this->profile->getProWithUid($value['uid']);
            if (isset($proArray)) {
                $this->data['userlist'][$key]['name'] = $proArray['name'];
                $this->data['userlist'][$key]['company'] = $proArray['company'];
                $this->data['userlist'][$key]['jobs'] = $proArray['jobs'];
                $this->data['userlist'][$key]['phone'] = $proArray['phone'];
                $this->data['userlist'][$key]['mobile'] = $proArray['mobile'];
                $this->data['userlist'][$key]['isstop'] = $value['isstop'];
            }
            unset($proArray);
            $this->data['userlist'][$key]['email'] = $value['uemail'];
        }
        $this->data['menu'] = 2;
        $this->render('admin-userlist', $this->data, TRUE);
    }

}
