<?php

namespace app\admin\controller\family;

use app\common\controller\Backend;
use app\lib\model\FamilyPeopleModel;
use app\lib\model\FamilyTreeMateModel;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class FamilyTreeMate extends Backend
{
    
    /**
     * FamilyTreeMate模型对象
     * @var \app\admin\model\family\FamilyTreeMate
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\family\FamilyTreeMate;
        $this->view->assign("sexList", $this->model->getSexList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index($family_tree_id="")
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $sql="";
            if(!empty($family_tree_id)){
                $sql = "FIND_IN_SET(".$family_tree_id.",family_tree_id)";
            }
            $list = $this->model
                ->with("users")
                    ->where($where)
                    ->where($sql)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $key=> $row) {
                $row->visible(['id','create_user_id','family_tree_id','mother_tree_id','name','sex','identity_card','photo','status','createtime','people_id','users']);
                //$list[$key]['family_tree_name']=$this->model->familyTreeName(['ftm.id'=>$row['id']]);
            }

            $result = array("total" => $list->total(), "rows" => $list->items(),'where'=>$where,'family_tree_id'=>$family_tree_id);

            return json($result);
        }
        $this->assignconfig("family_tree_id",$family_tree_id);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $row = $this->model->get($ids);
            if (!$row) {
                $this->error(__('No Results were found'));
            }
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                if (!in_array($row[$this->dataLimitField], $adminIds)) {
                    $this->error(__('You have no permission'));
                }
            }
            $family_tree_id=$this->request->param("family_tree_id");
            if(empty($family_tree_id)){
                $this->error("参数错误！");
            }

            $tree_ids=explode(",",$row['family_tree_id']);
            if(count($tree_ids) >1){
                $key=array_search($family_tree_id,$tree_ids);
                array_splice($tree_ids,$key,1);
                FamilyTreeMateModel::dataUpdate(['id'=>$ids],['family_tree_id'=>implode(",",$tree_ids)]);
            }else{
                FamilyTreeMateModel::dataDel(['id'=>$ids]);
            }

            $this->success();
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }


}
