<?php


namespace app\Admin\controller;

use think\Db;


class System extends Base
{
    protected $role = ["0" => '系统', "1" => "超级管理员", "2" => "运营", "3" => "用户","4"=>'库管','5'=>"管理员"];
    protected $menu = [];
    protected $action = [];

    public function __construct()
    {
        parent::__construct();
        $this->menu = [
            "login" => ["label" => "后台登录模块", "action" => []],
            "homelogin" => ["label" => "用户登录模块", "action" => []],
            "menu" => ["label" => "菜单管理模块", "action" => [
//                ['value'=>"add","label"=>'新建'],
//                ['value'=>"edit","label"=>'编辑'],
//                ['value'=>"status","label"=>'状态'],
//                ['value'=>"del","label"=>'删除'],
                ]],
            "menuaction" => ["label" => "菜单功能模块", "action" => [
//                ['value'=>"add","label"=>'新建'],
//                ['value'=>"edit","label"=>'编辑'],
//                ['value'=>"status","label"=>'状态'],
//                ['value'=>"del","label"=>'删除']
            ]
            ],
            "order" => ["label" => "订单管理模块", "action" => [
//                ['value'=>"edit","label"=>'编辑'],
//                ['value'=>"status","label"=>'状态']
            ]],
            "stock" => ["label" => "库存管理模块", "action" => [
//                ['value'=>"add","label"=>'新建'],
//                ['value'=>"edit","label"=>'编辑']
            ]],
            "account" => ["label" => "用户管理模块", "action" => [
//                ['value'=>"add","label"=>'新建'],
//                ['value'=>"edit","label"=>'编辑'],
//                ['value'=>"status","label"=>'状态'],
//                ['value'=>"del","label"=>'删除']
            ]],
            "version" => ["label" => "版本管理模块", "action" => [
//                ['value'=>"add","label"=>'新建'],
//                ['value'=>"edit","label"=>'编辑']
            ]
        ]];
        $this->action = [
            "add" => "新建",
            "edit" => "编辑",
            "status" => "状态",
            "del" => "删除"
        ];
    }

    public function list()
    {
        $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
        $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
        $where = [];
        $role = isset($this->post['role']) && $this->post['role'] != "" ? $this->post['role'] : "";
        if ($role != "") {
            if($role==2){
                $where['role'] = ['in',[2,4,5]];
            }else{
                $where['role'] = $role;
            }

        }
        $lowtime = isset($this->post['lowtime']) && $this->post['lowtime'] != "" ? $this->post['lowtime'] : "";
        if ($lowtime != "") {
            $where['addtime'] = [">=", $lowtime];
        }
        $uptime = isset($this->post['uptime']) && $this->post['uptime'] != "" ? $this->post['uptime'] : "";
        if ($uptime != "") {
            $where['addtime'] = ["<=", $uptime];
        }
        $username = isset($this->post['username']) && $this->post['username'] != "" ? $this->post['username'] : "";
        if ($username != "") {
            $where['username'] = ["like", "%{$username}%"];
        }
        $moudel = isset($this->post['moudel']) && $this->post['moudel'] != "" ? $this->post['moudel'] : "";
        if ($moudel != "") {
            $where['module|action'] =$moudel;
        }
        $count = Db::name("system_log")->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name("system_log")->where($where)->page($page, $size)->order("addtime desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['module'] = key_exists($value['module'], $this->menu) ?
                $this->menu[$value['module']]['label'] :
                $value['module'];
            $list[$key]['action'] = key_exists($value['action'], $this->action) ? $this->action[$value['action']] : $value['action'];
            $list[$key]['role'] = key_exists($value['role'], $this->role) ? $this->role[$value['role']] : $value['role'];
        }
        return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
    }

    public function GetMenu()
    {
        $data = [];
        foreach ($this->menu as $key => $value) {
            $temp = [];
            $temp["label"] = $value['label'];
            $temp["value"] = $key;
            $temp["children"] = $value['action'];
            $data[] = $temp;
        }
        return app_show(0, "获取成功", $data);
    }

    public function GetAction()
    {
        $data = [];
        $moudel=isset($this->post['moudel'])&&$this->post['moudel']!="" ?  trim($this->post['moudel']):"";
        if($moudel!=""){
            $this->action = key_exists($moudel, $this->menu) ? $this->menu[$moudel]['action'] :[];
        }
        foreach ($this->action as $key => $value) {
            $temp = [];
            $temp["value"] = $value;
            $temp["lable"] = $key;
            $data[] = $temp;
        }
        return app_show(0, "获取成功", $data);
    }
}