<?php
declare (strict_types = 1);

namespace app\model\admin;

use library\basic\BaseModel;

/**
 * @mixin \think\Model
 */
class Category extends BaseModel
{

    private $_data;


    /**
     * 设置显示状态
     * @param $id
     * @param $status
     */
    public function setStatus($id,$status) {
        self::beginTrans();
        try{
            $this->where('id', $id)->save(['is_show'=>$status]);
            $this->where('pid',$id)->save(['is_show'=>$status]);
            self::commitTrans();
            return true;
        }catch (DbException $db) {
            self::rollbackTrans();
            return false;
        }

    }

    /**
     * 保存分类
     * @param $post
     * @return bool
     */
    public function saveMenu($post){
        if(!empty($post['id'])) {
            $this->where('id',$post['id'])->save($post);
            return true;
        } else {
            unset($post['id']);
            $post['add_time'] = time();
            $bool = $this->insert($post);
            return $bool;
        }
    }


    /**
     * 删除栏目
     * @param $id
     */
    public function delMenu($id){
        $this->where('pid',$id)->delete();
        $this->where('id',$id)->delete();
        return true;
    }



    /**
     * 获取分类数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArMenu($cate_name = '',$pid = '',$show =  0){
        $menus = $this
            ->when(!empty($cate_name),function ($query) use($cate_name){
                $query->whereLike('cate_name',"%{$cate_name}%");
            })
            ->when(!empty($pid),function ($query) use($pid){
                $query->where('pid',$pid);
            })
            ->order("sort","desc")
            ->select();
        $data = [];
        foreach ($menus as $item) {
            $data[] = $item->getData();
        }
        $menuAr = self::getTree($data);
        return $menuAr;
    }

    /**
     * 获取树型菜单
     * @param $data
     * @param int $pid
     * @param int $level
     * @return array
     */
    public static function getTree($data, $pid = 0, $level = 0)
    {
        $childs = self::getChild($data, $pid, $level);
        array_multisort(array_column($childs, 'sort'), SORT_DESC, $childs);
        foreach ($childs as $key => $navItem) {
            $resChild = self::getTree($data, $navItem['id']);
            if (null != $resChild) {
                $childs[$key]['children'] = $resChild;
            }
        }
        return $childs;
    }

    /**
     * 获取子菜单
     * @param $arr
     * @param $id
     * @param $lev
     * @return array
     */
    private static function getChild(&$arr, $id, $lev)
    {
        $child = [];
        foreach ($arr as $k => $value) {
            if ($value['pid'] == $id) {
                $value['level'] = $lev;
                $child[] = $value;
            }
        }
        return $child;
    }







}