<?php 
class wentiClassAction extends Action
{
	public function defaultAction()
	{
		
	}
	
	public function dataAction()
	{
		$limit= (int)$this->get('limit','1');
		$sort = $this->get('sort');
		$dir  = $this->get('dir');
		$order= '`id` desc';
		if($sort && $dir)$order= '`'.$sort.'` '.$dir.'';
		$barr = m('wenti')->getlimit('1=1', (int)$this->get('page','1'),'*',$order, $limit);
		$db1  = m('agent');
		
		$rows  = $barr['rows'];
		foreach($rows as $k=>$rs){
			$ars = $db1->getXinxi($rs['agentid']);
			$rows[$k]['agentid'] = arrvalue($ars,'name');
		}
		$total = count($rows);
		$barr = array(
			'rows' => $rows,
			'totalCount'=> $barr['count'],
			'success' => true,
		);
		return $barr;
	}
	
	public function formAction()
	{
		$id = (int)$this->get('id','0');
		$this->smartydata['id'] = $id;
			
		$fields = c('fields')->getArr('wenti');
		$rs 	= $fields['data'];
		$fobj 	= $fields['fieldsobj'];
		
		if($id>0){
			$rs 	= m('wenti')->getone('`id`='.$id.'');
		}
		$agentrows 	= m('agent')->getall('id>0');
		
		$this->assign('fieldsobj', $fobj);
		$this->assign('data', $rs);
		$this->assign('agentrows', $agentrows);
	}
	
	public function savecheckAction()
	{
		
		$id = (int)$this->post('id','0');

		$uarr= array(
			'title' 	=> $this->post('title'),
			'url' 		=> $this->post('url'),
			'targ' 		=> $this->post('targ'),
			'content' 	=> $this->post('content'),
			'color' 	=> $this->post('color'),
			'type' 		=> (int)$this->post('type'),
			'sort' 		=> (int)$this->post('sort'),
			'status' 	=> (int)$this->post('status'),
			'agentid' 	=> (int)$this->post('agentid'),
		);
		$where = '`id`='.$id.'';
		if($id==0)$where='';

		$bo 	= m('wenti')->record($uarr, $where);
		if(!$bo)return $this->db->error();
		
		return 'ok';
	}
	
}