<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail£ºaspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class salebase extends X {
    protected $cpfile; 
    protected $cptpl; 
    protected $pagesize = 20;
    protected $page = 1;
    protected $gb_c = "";
    protected $gb_a = "";

    
    public function __construct() {
        $this->gb_c = GB_LJCMS_C;
        $this->gb_a = GB_LJCMS_A;
        if ($this->gb_c != "Public") {   
           $this->checkLogin();   
        }
        //$this->checkAuth($this->gb_a);       
        $this->cpfile = 'sale.php';
        $this->cptpl = 'tpl/sale/';
       
        $this->page = intval(XRequest::getArgs('page'));
        if ($this->page<1) {
            $this->page = 1;
        }
        $config=$this->get_system_config();
        $_var_array = array(
            "c"=>$this->gb_c,
            "a"=>$this->gb_a,
            "time"=>time(),
            "cpfile"=>$this->cpfile,
            'salepath'=>'tpl/sale/',
            'session'=>$_SESSION,
            'config'=>$config,
        );
        TPL::assign($_var_array);
//        if (isset($_GET['ljcms'])) {
//            X::poweredBy();
//        }
       // echo 123;die;
    }
    
    public function checkLogin() {
        if(false === parent::model('login', 'sa')->checkLogin()){
            XHandle::halt('对不起，您还没有登录！', $this->cpfile.'?c=Public&a=login', 4);
        //    XHandle::redirect($this->cpfile.'?c=login');
        }
  //       $status = false;
		// parent::loadUtil('cookie');
		// $sid = XCookie::get('sale_userinfo');
  //       $code = parent::import('code', 'util');
  //       $sid = $code->authCode($sid, 'DECODE', LJCMS_RANDKEY);
  //       unset($code);
  //       var_dump($sid);die;
  //       if (empty($sid)) {
  //            XHandle::halt('¶Ô²»Æð£¬Äú»¹Ã»ÓÐµÇÂ¼£¡', $this->cpfile.'sale.php?c=Public&a=login', 4);
  //       }
  //       else {
  //           $adminInfo = explode('\t', $sid);
  //           $_SESSION['sale_id']=$adminInfo[0];
  //           $_SESSION['sale_name']=$adminInfo[1];
  //           $_SESSION['sale_role']=$adminInfo[2];
  //           $_SESSION['sale_role_name']=$adminInfo[3];            
  //       }              
    }

    public function saveLoginLog($username, $content, $success=0) {
        $logid = parent::$obj->fetch_newid("SELECT MAX(logid) FROM ".DB_PREFIX."log", 1);
        $log_array = array(
            'logid'=>$logid,
            'username'=>$username,
            'ip'=>XRequest::getip(),
            'content'=>$content,
            'logtype'=>1,
            'timeline'=>time(),
            'success'=>$success,
        );
        parent::$obj->insert(DB_PREFIX.'log', $log_array);
        unset($username, $content, $success, $logid, $log_array);
    }

    
    public function checkAuth($value) {
        $auth = parent::model('login', 'am');
        $auth->checkAuth($value);
        unset($auth);
    }


        public function log($em, $oplog = '', $result = 1) {
        
        require_once BASE_ROOT.'source/include/elements.php';
        $emarray = array();
        $emarray = auth_get_em_array();

        
        $log_string = '';
        if (!empty($em)) {
            $log_string = $emarray[$em];
        }
        if (!empty($oplog)) {
            $log_string .= !empty($log_string) ? '-'.$oplog : $oplog;
        }

        
        if (!empty($log_string)) {
            $logid = parent::$obj->fetch_newid("SELECT MAX(logid) FROM ".DB_PREFIX."log", 1);
            $log_array = array(
                'logid'=>$logid,
                'username'=>parent::$wrap_admin['adminname'],
                'ip'=>XRequest::getip(),
                'content'=>$log_string,
                'logtype'=>1,
                'timeline'=>time(),
                'success'=>$result,
            );
            parent::$obj->insert(DB_PREFIX.'log', $log_array);
            unset($log_array);
        }
    }

    public function get_system_config() {
	$model = parent::model('system', 'sa');
        $system_data=$model->getconfig("system");
        $data=array();
        foreach($system_data as $val){
            $data[$val['code']]=$val['val'];
        }
        return $data;
}

 
  
    protected function c() { 
        return $this->gb_c;
    }
    

    protected function url($url_items=null) { 
        $_url = $this->cpfile."?c=".$this->c();
        if (!empty($url_items)) {
            $_url .= "&".$url_items;
        }
        return $_url;
    }

}
?>
