<?php

!defined('IN_ONEZ') && exit('Access Denied');



$groupid=(int)onez()->gp('groupid');

$uid=(int)onez()->gp('uid');

if(!$uid){

  $A['title']='请选择要转账的用户';

  $A['tpl']='refresh_more';

  $type=onez()->gp('type');

  $kw=onez()->gp('kw');

  $xxx='';

  if($kw){

    $id=intval($kw);

    $xxx.=" and (key8 like '%$kw%' or id='$id')";

  }else{

    $xxx.=" and 1=0";

  }

  $T=$G['this']->data()->open('member')->record("1$xxx order by id desc $G[pagelimit]");

  if(count($T)<12){

    $A['tpl']='refresh';

  }

  if($G['page']<=1 && !isset($_POST['kw'])){

    $record[]=array(

      'type'=>'search',

      'hint'=>'请填写用户昵称或UID',

      'token'=>'search',

      'action'=>$this->action('turn2&type=search'),

    );

  }

  foreach($T as $rs){

    $avatar=onez('chat.group.common')->avatar($rs['id']);

    $record[]=array(

      'type'=>'pictext',

      'classname'=>'pictext-users',

      'icon'=>$avatar,

      'name'=>$rs['nickname'],

      'token'=>'page',

      'action'=>$this->action('turn2&uid='.$rs['id']),

    );

  }

  return $A;

}

$A['close']=$this->action('turn2&type=search');



$item=$G['this']->data()->open('product')->one("id='$id'");

$mg=$G['this']->data()->open('member.group')->one("upid='$groupid' and userid='$G[userid]'");

if(!$mg){

  $new=array();

  $new['upid']=$groupid;

  $new['userid']=$G['userid'];

  $id=$G['this']->data()->open('member.group')->insert($new);

  $mg=$G['this']->data()->open('member.group')->one("upid='$groupid' and userid='$G[userid]'");

}

$mycredit=intval($mg['money1']);



if(defined('IS_POST')){

  ob_clean();

  

  $fee1=$fee2=0;

  

  $num=(int)onez()->gp('num');

  

  $money=onez('chat.group.plugin.credit')->format($num);

  if($money<=0){

    $A['tip']='转账积分有误';

    return $A;

  }

  

  $total=$money;

  $fee=0;

  if($fee1>0){

    $total+=$money*$fee1/100;

    $fee+=$money*$fee1/100;

  }

  if($fee2>0){

    $total+=$fee2;

    $fee+=$fee2;

  }

  $fee=onez('chat.group.plugin.credit')->format($fee);

  

  $total=onez('chat.group.plugin.credit')->format($total);

  if($total<=0){

    $A['tip']='积分数量有误';

    return $A;

  }

  

  if($total>onez('chat.group.plugin.credit')->value($G['userid'])){

    $A['tip']='账户积分不足，请充值！';

    return $A;

  }

  

  $u=$G['this']->data()->open('member')->one("id='$uid'");

  $G['user']=$G['this']->data()->open('member')->one("id='$G[userid]'");

  

  

  onez('chat.group.plugin.credit')->add($G['userid'],'money',-$money,'turn',"转账给".$u['nickname']);

  if($fee>0){

    onez('chat.group.plugin.credit')->add($G['userid'],'money',-$fee,'turn',"转账手续费");

  }

  onez('chat.group.plugin.credit')->add($uid,'money',$money,'turn',"来自".$G['user']['nickname']."的转账");

  

  $A['tip']='转账成功';

  $A['update']='group.info';

  $A['exit']='1';

  $A['token']='page';

  $A['action']=$this->action('creditlog&groupid='.$groupid);

  return $A;

}



$A['title']='转账';





if($item['token']=='charge'){

  $rt='<code class="price">'.$item['price'].'</code> 元';

}elseif($item['token']=='cash'){

  $rt='<code class="credit">'.$item['credit2'].'</code> 积分';

}

$u=$G['this']->data()->open('member')->one("id='$uid'");

$A['header']='

<div class="pictext2 itemline space-before" style="width:100%">

  <img class="icon pull-left" src="'.onez('chat.group.common')->avatar($uid).'">

  <div class="pull-right">ID: '.$uid.'</div><div> 转账给<span style="color:orange">'.$u['nickname'].'</span>  </div>

</div>

';





$buttons='';



$buttons.='<button type="button" class="btn btn-lg btn-primary flex1" style="margin:5px" data-token="submit">确定转账</button>';



if(!$G['userid']){

  $buttons='<button type="button" class="btn btn-lg btn-primary flex1" data-token="page" data-action="login">请先登录</button>';

}

if($buttons){

  $A['footer']='<div class="product-btns flex" style="width:100%">'.$buttons.'</div>';

}else{

  $A['footer']='<div></div>';

}





$record[]=array(

  'type'=>'space',

  'height'=>'5',

);





$record[]=array(

  'type'=>'input',

  'label'=>'转账积分',

  'inputype'=>'number',

  'hint'=>'请填写您要转账的积分金额',

  'value'=>'',

  'key'=>'num',

);



$record[]=array(

  'type'=>'info',

  'label'=>'我的积分',

  'value'=>'<span class="totalprice"><code class="credit">'.onez('chat.group.plugin.credit')->value($G['userid']).'</code></span>',

);

$record[]=array(

  'type'=>'space',

  'height'=>'5',

);





return $A;