<?php
class subjectAction extends action
{
	private $mod = null;
	private $tree = null;
    function __construct(){
        parent::__construct();
		$this->mod = new subjectMod();
		$this->tree = new LibLRTree($this->mod->e['title'],$this->mod->e['table'],$this->mod->e['tree']);
    }

    function __destruct(){
    }
	/**
	 * Method 列表数据
	 */
    function list(){
		$where = '';
		if($_POST['filters']){
			$groups = json_decode(htmlspecialchars_decode($_POST['filters']),true);
			$where = $this->groups2sql($groups);
		}
		$data = $this->mod->list($where, $_POST['page'], $_POST['rows'], $_POST['sidx'], $_POST['sord']);
		foreach($data['rows'] as $k=>$v){
			//余额方向
			$v['s_dict_text'] = mvc::$cfg['subjectDict'][$v['s_dict']];
			if($v['s_dict']=='c') $v['s_dict_text'] = '<span class="label label-info">'.$v['s_dict_text'].'</span>';
			if($v['s_dict']=='d') $v['s_dict_text'] = '<span class="label label-warning">'.$v['s_dict_text'].'</span>';
			//核算对象
			$v['s_object_text'] = $this->mod->object2text($v['s_object']);
			//叶子图标
			$v['icon'] = 'fa fa-dot-circle-o';
			$data['rows'][$k] = $v;
		}
		echo json_encode($data, JSON_UNESCAPED_UNICODE);
	}
	/**
	 * Method 通过关键字取一行数据
	 */
	function getRowByKey($key){
		$row = $this->mod->getRowByKey('*',$key);
		if($row){
			$row['s_dict_text'] = mvc::$cfg['subjectDict'][$row['s_dict']];
			$row['s_object'] = LibBit::xsplit($row['s_object']);
			$row['s_object_text'] = $this->mod->object2text($row['s_object']);
			$this->exitJson(0,$row);
		}else{
			$this->exitJson(2000);
		}
	}

	/**
	 * Method 唯一值检测
	 */
	function onlyCheck(){
		$result = false;
		$key = (empty($_REQUEST[$this->mod->e['key']])) ? 0 : $_REQUEST[$this->mod->e['key']];
		$caption = $_REQUEST[$this->mod->e['caption']];
		if($s_name!=''){
			$where = sprintf("%s<>'%s' AND %s='%s'",$this->mod->e['key'],$key,$this->mod->e['caption'],$caption);
			if($this->mod->getOne('1',$where)){
				$result = true;
			}
		}
		echo ($result) ? 'false' : 'true'; //存在不通过，不存在通过
	}
	/**
	 * Method 添加操作
	 */
	function add(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$key = $this->mod->e['key'];
		$post = $this->mod->checkValueForm($params,[$key]);//添加时,主键可以为空
		if($post['error']){
			$this->exitJson(2001,$post['error']);
		}
		$data = $post['data'];
		$lastID = 0;
		$this->mod->dbWrite->Begin();
		$this->tree->newNode($data[$this->mod->e['tree']['fieldParentId']],function($res)use(&$data,&$lastID){
			$data = array_merge($data,$res);//获取左右值与层级
			$lastID = $this->mod->addRow($data); //创建节点时，需要调用者先添加记录，并返回ID
			return $lastID;
		});
		$this->mod->dbWrite->End();
		if($lastID>0){
			$this->exitJson(0,['key'=>$lastID]);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 修改操作
	 */
	function edit(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		$post = $this->mod->checkValueForm($params);
		if($post['error']){
			$this->exitJson(2001,$post['error']);
		}
		$data = $post['data'];
		$data['s_object'] = LibBit::xor($data['s_object']);
		$data['update_time'] = LibTimer::nowDateTime();
		$key = $data[$this->mod->e['key']];
		$oldRow = $this->mod->getRowByKey('*',$key);
		if($this->mod->setRowByKey($data,$key)){
			$this->tree->restLR(); //重构左右值
			$this->exitJson(0,['key'=>$key]);
		}else{
			$this->exitJson(2000);
		}
	}
	/**
	 * Method 删除操作
	 */
	function delete(){
		$input = file_get_contents('php://input');
		$params = json_decode($input,true);
		if(empty($params['key'])){
			$this->exitJson(2001);
		}
		$result = false;
		$this->tree->deleteNode($params['key'],function()use($params,&$result){
			if(true){ //可以执行其他相关的操作，返回true才执行节点删除
				$result = true;
				return true;
			}else{
				return false;
			}
		});
		if($result){
			$this->exitJson(0);
		}else{
			$this->exitJson(2000);
		}
	}
}