<?php 
class agentClassAction extends Action
{
	public function defaultAction()
	{
		
	}
	
	public function dataAction()
	{
		$table = 'agent';
		$where = 'and `platid`='.$this->adminid.'';
		$where = '';
		$sql 	= 'select * from [Q]'.$table.' where 1=1 '.$where.'';
		$total	= 0;
		$rows	= $this->db->getall($sql);
		
		$uobj  	= m('users');
		foreach($rows as $k=>$rs){
			$kefulist = '未设置';
			if(!isempt($rs['kefuid'])){
				$kefuls = array();
				$urso = $uobj->getall('`id` in('.$rs['kefuid'].')');
				foreach($urso as $k1=>$rs1)$kefuls[]=$rs1['name'];
				$kefulist = join(',', $kefuls);
			}
			$rows[$k]['kefulist'] = $kefulist;
		}
		
		$barr = array(
			'rows' => $rows,
			'totalCount'=> $total,
			'success' => true
		);
		return $barr;
	}
	
	public function formAction()
	{	
		$id = (int)$this->get('id','0');
		$this->smartydata['id'] = $id;
			
		$fields = c('fields')->getArr('agent');
		$rs 	= $fields['data'];
		$fobj 	= $fields['fieldsobj'];
		$rs['picurlpic'] = 'images/noimg.jpg';
		
		if($id>0){
			$rs 	= m('agent')->getone('`id`='.$id.'');
			if(!isempt($rs['picurl']))$rs['picurlpic'] = $rs['picurl'];
		}
		$userlist = m('users')->getall('`status`=1','`id`,`name`,`ranking`');
		
		$this->assign('fieldsobj', $fobj);
		$this->assign('data', $rs);
		$this->assign('userlist', $userlist);
	}
	
	public function savecheckAction()
	{
		$ming = $this->post('ming');
		$id = (int)$this->post('id','0');
		$uarr= array(
			'name' 		=> $this->post('name'),
			'platid' 	=> $this->adminid,
			'callurl' 	=> $this->post('callurl'),
			'picurl' 	=> $this->post('picurl'),
			'kefuid' 	=> $this->post('kefuid'),
			'status' => 1,
			'optdt' => $this->rock->now
		);
		$where = '`id`='.$id.' and `platid`='.$this->adminid.'';
		if($id==0){
			$where = '';
			$uarr['agentkey'] = $this->db->ranknum('[Q]agent','agentkey',10);
		}
		$bo 	= m('agent')->record($uarr, $where);
		if(!$bo)return $this->db->error();
		
		return 'ok';
	}
	
}