<?php

namespace app\admin\model\jdgw;

use think\Model;
use fast\Tree;

class Nav extends Model
{

    // 表名
    protected $name = 'jdgw_nav';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'typedata_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $tpl = "";
            switch($row['typedata']){
                case 'article';
                    $tpl = "list_article";
                    break;
                case 'images';
                    $tpl = "list_images";
                    break;
                case 'page';
                    $tpl = "page";
                    break;
                default :
                    $tpl = "page";
                    break;
            }
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk], 'tpl'=>$tpl]);
        });
    }

    public function getTypedataList()
    {
        return ['article' => __('Typedata article'), 'images' => __('Typedata images'), 'page' => __('Typedata page')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getTypedataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['typedata']) ? $data['typedata'] : '');
        $list = $this->getTypedataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function getTree(){
        $list = self::where(['status'=>'normal'])->order('weigh desc')->select();
        $tree = Tree::instance()->init($list, 'pid');
        $list = $tree->getTreeList($tree->getTreeArray(0), 'title');
        return $list;
    }




}
