<?php
!defined('IN_ONEZ') && exit('Access Denied');
#名称：仿QQ群内部交流系统 - 积分系统
#标识：chat.group.plugin.credit

class onezphp_chat_group_plugin_credit extends onezphp{
  var $users=array();
  function __construct(){
    
  }
  function href($href,$inplugin=0){
    global $G;
    if($G['url.grade']=='admin'){
      return parent::href($href,$inplugin);
    }
    if(strpos($href,'?')===false){
      $href.='?';
    }else{
      $href.='&';
    }
    return onez()->href($href.'_p='.$this->token.'&_m=pageshow&sitetoken='.$G['this']->token);
  }
  function pageshow(){
    global $G;
    $sitetoken=onez()->gp('sitetoken');
    if($sitetoken && onez()->exists($sitetoken)){
      $groupid=(int)onez()->gp('groupid');
      $G['this']=onez($sitetoken);
      if(defined('IS_ONEZ_WWW')){
        $G['this']->data_alias=$G['this']->cname;
      }
      $login=onez('login')->init('chat_group',0);
      include_once(onez('chat.group.common')->path.'/theme/default/www/init.php');
      onez()->start(dirname(__FILE__));
    }
  }
  function name(){
    global $G;
    $name=$this->option('name');
    !$name && $name='积分';
    return $name;
  }
  function unit(){
    global $G;
    $name=$this->option('unit');
    !$name && $name='个';
    return $name;
  }
  function value($userid,$type='money'){
    global $G;
    $value=0;
    if(!isset($this->users[$userid])){
      $this->users[$userid]=$G['this']->data()->open('money')->one("userid='$userid'");
    }
    if($this->users[$userid]){
      $value=$this->users[$userid][$this->getkey($type)];
    }
    return $this->format($value);
  }
  function add($userid,$type,$value,$token='',$subject=''){
    global $G;
    $this->users[$userid]=$G['this']->data()->open('money')->one("userid='$userid'");
    if(!$this->users[$userid]){
      $item=array();
      $item['userid']=$userid;
      $id=$G['this']->data()->open('money')->insert($item);
      $this->users[$userid]=$G['this']->data()->open('money')->one("userid='$userid'");
    }
    $key=$this->getkey($type);
    $update=array();
    $update[$key]=$this->users[$userid][$key]+$value;
    $G['this']->data()->open('money')->update($update,"userid='$userid'");
    
    
    $item=array();
    $item['token']=$token;
    $item['userid']=$userid;
    $item['key1']=$subject;
    $item['key2']=$value;
    $G['this']->data()->open('credit.log')->insert($item);
    
    $this->users[$userid]=$G['this']->data()->open('money')->one("userid='$userid'");
    return $this;
  }
  function format($value){
    $format=(int)$this->option('format');
    if($value>0){
      $value-=1/pow(10,$format+1)*5;
    }elseif($value<0){
      $value+=1/pow(10,$format+1)*5;
    }else{
    }
    $value=number_format($value,$format,'.','');
    return $value;
  }
  function getkey($type){
    if($type=='money'){
      return 'money1';
    }
  }
  function option($key=false){
    global $G;
    if(!$this->option){
      $this->option=$G['this']->option();
    }
    if($key===false){
      return $this->option;
    }
    return $this->option['credit_'.$key];
  }
  function bind_admin_menu($grade){
    $Menu=onez('call')->get('admin_menu');
    if($grade=='admin'){
      $Menu[]=array (
        'name' => '积分系统',
        'href' => '',
        'icon' => '',
        'group' => 'chat.group.plugin.credit',
      );
      $Menu[]=array (
        'name' => '积分设置',
        'href' => $this->href('/pages/setting.php',0),
        'icon' => '',
        'group' => 'chat.group.plugin.credit',
      );
      $Menu[]=array (
        'name' => '积分余额',
        'href' => $this->href('/pages/credits.php',0),
        'icon' => '',
        'group' => 'chat.group.plugin.credit',
      );
      $Menu[]=array (
        'name' => '交易明细',
        'href' => $this->href('/pages/log.php',0),
        'icon' => '',
        'group' => 'chat.group.plugin.credit',
      );
      if($this->option('charge_user')){
        $Menu[]=array (
          'name' => '手动充值审核',
          'href' => $this->href('/pages/charge.wait.php',0),
          'icon' => '',
          'group' => 'chat.group.plugin.credit',
        );
      }
      if($this->option('cash_enabled')){
        $Menu[]=array (
          'name' => '提现申请',
          'href' => $this->href('/pages/cash.wait.php',0),
          'icon' => '',
          'group' => 'chat.group.plugin.credit',
        );
      }
    }
    onez('call')->set('admin_menu',$Menu);
  }
  function bind_pc_nav(){
    global $G;
    $groupid=(int)onez()->gp('groupid');
    $btns='';
    if($G['userid']){
      $btns.='<li data-key="notice" data-url="'.$this->href('/group/credit.php?groupid='.$groupid).'">账户</li>';
    }
    return $btns;
  }
  function bind_chat_group_userlist(){
    global $G,$A,$record;
    
    $action=onez()->gp('action');
    if($action=='group.info'){
      $record[]=array(
        'type'=>'pictext',
        'name'=>'我的积分',
        'rightinfo'=>$this->value($G['userid']),
      );
      $record[]=array(
        'type'=>'pictext',
        'name'=>'转让积分',
        'link'=>'1',
        'token'=>'page',
        'action'=>$this->action('turn&groupid='.$G['group']['id']),
      );
      $record[]=array(
        'type'=>'pictext',
        'name'=>'积分记录',
        'link'=>'1',
        'token'=>'page',
        'action'=>$this->action('creditlog&groupid='.$G['group']['id']),
      );
      $record[]=array(
        'type'=>'space',
        'height'=>'5',
      );
    }
  }
  function bind_m_tmpls(){
    global $G,$A,$record;
    $myrecord=array();
    foreach($record as $v){
      $myrecord[]=$v;
      if($v['action']=='group.history'){
        $myrecord[]=array(
          'type'=>'pictext',
          'icon'=>'http://cdn.onez.cn/icons/2017/1227/2017122705361996840001.png',
          'name'=>'我的积分',
          'myhtml'=>'<div class="right-info-link">'.$this->value($G['userid']).' </div>',
          'link'=>'1',
          'token'=>'page',
          'action'=>$this->action('credit'),
        );

        $myrecord[]=array(
          'type'=>'space',
          'height'=>'5',
        );
      }
    }
    
    $record=$myrecord;
  }
}