<?php

namespace admin\action;



use \model\collocation;

/**
 * Class collocationAction
 * @package admin\action
 */
class collocationAction  extends \action
{
	function actList()
	{
		$req = \mvc::$req;
		\view::put();
    }

	private $lrtree = null;
    /**
     * @param int $id
     */
	public function list($id = 0)
    {
		$req = \mvc::$req;
		/*$where = [];
        if($id > 0)
        {
            $node = $this->lrtree->getNode($id);
            $req['filters'] = '{"groupOp":"AND","rules":[{"field":"o_lft","op":"gt","data":"'.$node['o_lft'].'"},{"field":"o_rgt","op":"lt","data":"'.$node['o_rgt'].'"}]}';
        }*/
        $collocation = new collocation();
		$data = $collocation->list($id, $req['filters'], $req['page'], $req['rows'], $req['sidx'], $req['sord']);
		$this->successJson($data);
	}

    /**
     * 重构树的左右值
     * http://127.0.0.1:8087/qgmvc/gsp/admin/collocation/restructure
     */
	function restructure(){
		set_time_limit(0);
        $collocation = new collocation();
        if($collocation->lrtree->restructure()){
			exit('Success');
		}else{
			exit('Fail');
		}
    }

	function getChilds($id){
        $collocation = new collocation();
        $rows = $collocation->lrtree->getChilds($id);
        $this->successJson($rows);
    }

	function getItems($id){
		$req = \mvc::$req;
        $collocation = new collocation();
        $rows = $collocation->lrtree->getChilds($id);
        $data = [];
        $regi=0;
		$firstkey = isset($req['firstkey'])? $req['firstkey']: 0;
        if(isset($req['hasFirst']) && $req['hasFirst']){
            $data[0] = ['key'=>$firstkey, 'value'=>'' ];
            $regi = 1;
        }
        foreach($rows as $k=>$v){
            $data[$k+$regi]=[ 'key'=>$v['o_id'], 'value' => $v[LG_NAME] ];
        }
       $this->successJson($data);
    }

	function get_s_apply_type(){
		$req = \mvc::$req;
        $collocation = new collocation();
        $rows = $collocation->lrtree->getChilds(5);
        $data = [];
        $regi=0;
		$firstkey = isset($req['firstkey'])? $req['firstkey']: 0;
        if(isset($req['hasFirst']) && $req['hasFirst']){
            $data[0] = ['key'=>$firstkey, 'value'=>'' ];
            $regi = 1;
        }
        foreach($rows as $k=>$v){
            if($v['o_id']!=10062){
                $data[$k+$regi]=[ 'key'=>$v['o_id'], 'value' => $v[LG_NAME] ];
            }
        }
       $this->successJson($data);
    }

	function getCheckBox($id,$values){
        $collocation = new collocation();
        $rows = $collocation->lrtree->getChilds($id);
        $checks = explode(',',$values);
		\view::set('checks', $checks);
		\view::set('data',$rows);
		\view::put($this->activeView().substr(__METHOD__,strpos(__METHOD__,':')+2));
    }

    /**
     * 是否需要code
     * @param $parentId
     */
	public function hasCode($parentId)
    {
        $collocation = new collocation();
        $this->successJson(['has_code' => $collocation->lrtree->hasCode($parentId)]);
    }

	public function add()
    {
        $param = \mvc::$req;
        $data = ['cn_name' => $param['cn_name'],"en_name"=> $param['en_name'],"o_sort"=> $param['o_sort']];
        if(isset($param['code'])) $data['code'] = $param['code'];
        $collocation = new collocation();
		if($collocation->lrtree->addNode($param['o_parent_id'],$data)){
			$this->successJson();
		}
	}

	public function edit()
    {
        $collocation = new collocation();
        $param = \mvc::$req;
        $data = ['cn_name' => $param['cn_name'],"en_name"=> $param['en_name'],"o_sort"=> $param['o_sort']];
        if(isset($param['code'])) $data['code'] = $param['code'];
        $node =  $collocation->lrtree->getNode($param['o_id']);
        $param['o_parent_id'] = $node['o_parent_id'];
        if($node['o_is_init']=='0'){
            if($collocation->lrtree->editNode($param['o_id'], $param['o_parent_id'],$data)){
                $this->successJson();
            }
        }else{
            //系统特定项，只能修改序号
            unset($data['cn_name']);
            unset($data['en_name']);
            unset($data['code']);
            if($collocation->lrtree->editNode($param['o_id'], $param['o_parent_id'],$data)){
                $this->successJson();
            }
        }
	}

    /**
     * @param $prevKey
     * @param $key
     * @param $nextKey
     */
	public function move($prevKey,$key,$nextKey)
    {
        $collocation = new collocation();
		if($collocation->lrtree->moveNode($prevKey,$key,$nextKey)){
			$this->successJson();
		}
	}

    /**
     * @param $id
     */
	public function delete($id)
    {
        $collocation = new collocation();
        $item = $collocation->getItem($id);
        if($item['o_is_init']=='0'){
            if($collocation->lrtree->deleteNode($id)){
                $this->successJson();
            }
        }else{
            $this->msgJson(lg('系统特定项，不能删除'));
        }
	}

    /**
     * @param $id
     */
    public function getNode($id)
    {
        $collocation = new collocation();
        $data = $collocation->lrtree->getNode($id);
        $this->successJson($data);
    }

    /**
     * @param $id
     * @param $hasSelf
     */
    public function getParents($id,$hasSelf)
    {
        $collocation = new collocation();
        $data = $collocation->lrtree->getParents($id,$hasSelf);
        $this->successJson($data);
    }
}