<?php
ini_set('display_errors', 1);
session_start(); // starts new or resumes existing session
Doo::loadModelAt('aconfig', 'admin');
Doo::loadModelAt('auser', 'admin');
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 SysController extends DooController
{

    private $data, $aconfig, $auser, $profile, $ph, $userz, $mailer;

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

    public function __construct()
    {
        $this->aconfig = new AConfig();
        $this->auser = new AUser();
        $this->mailer = new Mailer();
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
    }

    function sysinfo()
    {
        if (isset($_POST['proname']) || isset($_POST['aemail'])) {
            if ($_POST['proname']) {
                $this->aconfig->proname = $_POST['proname'];
                $this->aconfig->update(array('where' => 'conid = 1'));
            }
            if ($_POST['aemail'] && filter_var($_POST['aemail'], FILTER_VALIDATE_EMAIL)) {
                $this->auser->aemail = $_POST['aemail'];
                $this->auser->update(array('where' => 'auid = 1'));
                $this->mailer->setEmails($_POST['aemail']);
                $this->mailer->seteTitle('管理员邮箱地址已修改');
                $this->mailer->setClientName($_SESSION['aname']);
                $signupConfigStr = '<h3>管理员邮箱地址已修改</h3><p><h2>' . $_POST['aemail'] . '</h2></p>';
                $this->mailer->setContent($signupConfigStr);
                $this->mailer->send_mail();
            }
            return Doo::conf()->APP_URL . 'manage/sys/info';
        }
        $this->data['proName'] = $this->aconfig->getOne(array('select' => 'proName', 'asArray' => TRUE))['proName'];
        $this->data['aemail'] = $this->auser->getOne(array('select' => 'aemail', 'asArray' => TRUE))['aemail'];
        $this->data['ver'] = DOO::conf()->ver;
        $this->data['aname'] = $_SESSION['aname'];
        $this->data['menu'] = 1;
        $this->render('admin-sysinfo', $this->data, TRUE);
    }

    function smsSwitch()
    {
        if (isset($_POST['switch'])) {
            if ($_POST['switch'] == 'off') {
                $this->aconfig->smsswitch = 0;
                $this->aconfig->smssignswitch = 0;
                if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                    exit(json_encode(array('switch' => 'off')));
            }
            if ($_POST['switch'] == 'on') {
                $this->aconfig->smsswitch = 1;
                $this->aconfig->smssignswitch = 1;
                if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                    exit(json_encode(array('switch' => 'on')));
            }
        }

        if (isset($_POST['status']) && isset($_POST['type'])) {
            if($_POST['status'] == 'close'){
                $ststus = 0;
            }else{
                $ststus = 1;
            }
            if ($_POST['type'] == 'sign') {
                $this->aconfig->smssignswitch = $ststus;
                if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                    exit(json_encode(array('switch' => $ststus)));
            }else if ($_POST['type'] == 'switch') {
                $this->aconfig->smsswitch = $ststus;
                if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                    exit(json_encode(array('switch' => $ststus)));
            }
        }

        $this->data['smsSwitch'] = $this->aconfig->getOne(array('select' => 'smsswitch', 'asArray' => TRUE))['smsswitch'];
        $this->data['smsSignSwitch'] = $this->aconfig->getOne(array('select' => 'smssignswitch', 'asArray' => TRUE))['smssignswitch'];
        $this->data['Switch'] = $this->data['smsSwitch'] || $this->data['smsSignSwitch'] ? 1 : 0;
        $this->data['menu'] = 4;
        $this->render('admin-sms', $this->data, TRUE);
    }

    function signSwitch()
    {
        if(isset($_POST['type']) && isset($_POST['status'])){
            if($_POST['status'] == 'close'){
                $ststus = 0;
            }else{
                $ststus = 1;
            }
            switch($_POST['type']){
                case 'report':
                    $this->aconfig->reportswitch = $ststus;
                    if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                        exit('1');
                    break;
                case 'sign':
                    $this->aconfig->signswitch = $ststus;
                    if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                        exit('1');
                    break;
                case 'launch':
                    $this->aconfig->launchsignswitch = $ststus;
                    if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                        exit('1');
                    break;
            }
            exit(0);
        }
        $this->data['reportSwitch'] = $this->aconfig->getOne(array('select' => 'reportswitch', 'asArray' => TRUE))['reportswitch'];
        $this->data['signSwitch'] = $this->aconfig->getOne(array('select' => 'signswitch', 'asArray' => TRUE))['signswitch'];
        $this->data['launchsignSwitch'] = $this->aconfig->getOne(array('select' => 'launchsignswitch', 'asArray' => TRUE))['launchsignswitch'];
        $this->data['menu'] = 6;
        $this->render('admin-sign', $this->data, TRUE);
    }

    function interMediateSwitch()
    {
        if (isset($_POST['switch'])) {
            if ($_POST['switch'] == 'off') {
                $this->aconfig->imediateswitch = 0;
                if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                    exit(json_encode(array('switch' => 'off')));
            }
            if ($_POST['switch'] == 'on') {
                $this->aconfig->imediateswitch = 1;
                if ($this->aconfig->update(array('where' => "conid = 1")) > 0)
                    exit(json_encode(array('switch' => 'on')));
            }
        }
        $this->data['imediateswitch'] = $this->aconfig->getOne(array('select' => 'imediateswitch', 'asArray' => TRUE))['imediateswitch'];
        $this->data['menu'] = 7;
        $this->render('admin-intermediate', $this->data, TRUE);
    }

}
