<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\facade\Db;
use app\admin\model\ActionLog;
use think\facade\Validate;

//系统的组织架构
class Newfill extends Base
{
    public function list()
    {
        $post = $this->request->only(['pid' => '', 'name' => '', 'nickname' => '', 'companyNo' => '','companyName'=>''], 'post', 'trim');

//        $depart = $condition = [['is_del', '=', 0]];
//        if ($post['pid'] !== '') $condition[] = ["pid", "=", $post['pid']];
//        if ($post['name'] != "") $condition[] = ["name", "like", "%{$post['name']}%"];
//        if ($post['nickname'] != '') {
//            $is = Db::name("depart_user")
//                ->where([['nickname', 'like', "%{$post['nickname']}%"]])
//                ->column('itemid');
//
//            if (empty($is)) return json_show(1004, "未找到数据");
//
//            $condition[] = ["id", "in", $is];
//        }
//        if ($post['pid'] === '' && $post['name'] == '' && $post['nickname'] == '') $condition[] = ["pid", "=", 0];
//
//        $pidlist = Db::name("company_item")
//            ->where($condition)
//            ->column("pid");
//        if (!empty($pidlist)) $depart[] = [["itemid", "in", $pidlist]];
//        else {
//            if ($post['pid'] === "") $depart[] = ['itemid', '=', 0];
//            else $depart[] = ['itemid', '=', $post['pid']];
//        }
//
//        $item = Db::name("depart_user")->where($depart)->select()->toArray();
//        $list = Db::name("company_item")->where($condition)->select()->toArray();

        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('ulist', $post);

        return json_show($rs['code'], $rs['message'], $rs['data']);//"获取成功", ["depart" => $list, "item" => $item]);
    }

    /*状态*/
    public function stat()
    {

        $param = $this->request->filter('trim')->post();
        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('stat', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//        $id = $this->request->post('id/d', 0, 'trim');
//        if ($id == 0) return json_show(1004, "参数id 不能为空");
//
//        $s = Db::name('company_item')
//            ->field('id,status')
//            ->where('id', $id)
//            ->findOrEmpty();
//
//        if (empty($s)) return json_show(1005, "未找到部门");
//
//        $var = $s['status'];
//        $di = $s['status'] == 0 ? 1 : 0;
//        $s['status'] = $di;
//        $s['updatetime'] = date("Y-m-d H:i:s");
//        // var_dump($s);
//        $rs = Db::name('company_item')->where('id', $id)->save($s);
//
//        if ($rs) {
//            $orde = ["order_code" => $s['name'], "status" => $var, "action_remark" => '', "action_type" => "edit"];
//            ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $orde, "company_item", $s['status'], $orde);
//            return json_show(0, "更新成功");
//        } else return json_show(1005, '更新失败');

    }

    /*职位*/
    public function userp()
    {
        $param = $this->request->filter('trim')->post();
        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('userp', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
//        $id = isset($this->post['id']) ? intval($this->post['id']) : "";
//        if ($id == "") {
//            return error_show(1004, "参数id不能为空");
//        }
//        $t = Db::name('depart_user')->where(["id" => $id, "is_del" => 0])->find();
//        if (empty($t)) {
//            return error_show(1005, "未找到用户");
//        }
//        $position = isset($this->post['position']) ? intval($this->post['position']) : "";
//        if ($position == "") {
//            return error_show(1004, "职位不能为空");
//        }
//        Db::startTrans();
//        if ($position == 2) {
//            $f = Db::name('depart_user')->where(['itemid' => $t['itemid'], "is_del" => 0, "position" => 2])->find();
//            if (!empty($f) && $f['id'] != $id) {
//                $f['position'] = 1;
//                $f['updatetime'] = date("Y-m-d H:i:s");
//                $m = Db::name('depart_user')->save($f);
//                $orde = ["order_code" => $t['nickname'], "status" => 0, "action_remark" => '', "action_type" => "edit"];
//                ActionLog::logAdd($this->post['token'], $orde, "depart_user", 0, $orde);
//                if ($m == false) {
//                    Db::rollback();
//                    return error_show(1004, "负责人修改失败");
//                }
//            }
//        }
//        $t['position'] = $position;
//        $t['updatetime'] = date("Y-m-d H:i:s");
//        //var_dump($t);
//        $ti = Db::name('depart_user')->save($t);
//        if ($ti == true) {
//            Db::commit();
//            return app_show(0, "修改成功");
//        } else {
//            Db::rollback();
//            return error_show(1004, "修改失败");
//        }
    }

    public function add()
    {
        $param = $this->request->filter('trim')->post();

        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('add', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
//        if ($companyNo == '') return json_show(1004, '所属企业不能为空');
//
//        $item_name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
//        if ($item_name == "") {
//            return error_show(0, "部门名称不能为空");
//        }
//        $repeat_name = Db::name("company_item")
//            ->where(["is_del" => 0, "name" => $item_name, 'companyNo' => $companyNo])
//            ->findOrEmpty();
//        if (!empty($repeat_name)) {
//            return error_show(1004, "部门名称已存在");
//        }
//        $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "";
//        if ($pid === "") {
//            return error_show(1002, "父级id不能为空");
//        }
//
//        $spid = ['depart_link' => ""];
//        if ($pid != 0) {
//            $spid = Db::name('company_item')->where(['id' => $pid, 'is_del' => 0])->find();
//            if (empty($spid)) {
//                return error_show(1004, "父级数据不能为空");
//            }
//        }
//        $level = isset($this->post['level']) && $this->post['level'] !== "" ? trim($this->post['level']) : "1";
//        $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "1";
//        Db::startTrans();
//        try {
//            $data = [
//                "name" => $item_name,
//                "pid" => $pid,
//                "level" => $level,
//                "weight" => $weight,
//                "is_del" => 0,
//                'companyNo' => $companyNo,
//                "addtime" => date("Y-m-d H:i:s"),
//                "updatetime" => date("Y-m-d H:i:s"),
//            ];
//
//            $t = Db::name("company_item")->insert($data, true);
//            if ($t > 0) {
//                $orde = ["order_code" => $repeat_name['name'], "status" => 0, "action_remark" => '', "action_type" => "create"];
//                ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $orde, "company_item", 0, $orde);
//                $depart_link = $spid['depart_link'] . "{$t}-";
//                $level = explode('-', $depart_link);
//                $level = array_filter($level);
//                $level = count($level);
//                $i = ['depart_link' => $depart_link, 'level' => $level];
//                $o = Db::name('company_item')->where(['id' => $t])->update($i);
//                if ($o) {
//                    $stn = ["order_code" => $repeat_name['name'], "status" => 0, "action_remark" => '', "action_type" => "edit"];
//                    ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "company_item", 0, $stn);
//                    Db::commit();
//                    return error_show(0, "添加成功");
//                }
//            }
//            Db::rollback();
//
//        } catch (\Exception $e) {
//            Db::rollback();
//            return error_show(1003, $e->getMessage());
//        }
    }

    public function refresh()
    {

        $param = $this->request->filter('trim')->post();

        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('refresh', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
//        if ($token == "") {
//            return error_show(101, 'token不能为空');
//        }
//        $effetc = VerifyTokens($token);
//        if (!empty($effetc) && $effetc['code'] != 0) {
//            return error_show($effetc['code'], $effetc['message']);
//        }
//        $id = isset($this->post['id']) ? intval($this->post['id']) : "";
//        $items = Db::name("company_item")->where(['id' => $id, 'is_del' => 0])->find();
//        if ($items == false) {
//            return error_show(1003, "部门信息不存在");
//        }
//        $pid = isset($this->post['pid']) && $this->post['pid'] !== "" ? trim($this->post['pid']) : "";
//        if ($pid == "") {
//            return error_show(1002, "父级id不能为空");
//        }
//        $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
//        if ($companyNo == "") return error_show(1002, "所属企业不能为空");
//
//        $spid = ['depart_link' => ""];
//        if ($pid != 0) {
//            $spid = Db::name('company_item')->where(['id' => $pid, 'is_del' => 0])->find();
//            if (empty($spid)) {
//                return error_show(1004, "父级数据不能为空");
//            }
//        }
//        $weight = isset($this->post['weight']) && $this->post['weight'] !== "" ? trim($this->post['weight']) : "1";
//        $itemname = isset($this->post['name']) ? trim($this->post['name']) : "";
//        if ($itemname == "") {
//            return error_show(1002, "部门名称不能为空");
//        }
//        $repeat_name = Db::name("company_item")->where(["is_del" => 0, "name" => $itemname, 'companyNo' => $companyNo])->where('id', '<>', $id)->find();
//        //echo Db::name("company_item")->getLastSql();
//        if (!empty($repeat_name)) {
//            return error_show(1004, "部门名称已存在");
//        }
//        $depart_link = $spid['depart_link'] . "{$id}-";
//        $itemlevel = explode('-', $depart_link);
//        $itemlevel = array_filter($itemlevel);
//        $itemlevel = count($itemlevel);
//
//        $item = [
//            "id" => $id,
//            "name" => $itemname,
//            "updatetime" => date("Y-m-d H:i:s"),
//            "weight" => $weight, "pid" => $pid,
//            'depart_link' => $depart_link,
//            'level' => $itemlevel,
//            'companyNo' => $companyNo
//        ];
//        $re = Db::name("company_item")->save($item);
//        $stn = ["order_code" => $repeat_name['name'], "status" => 0, "action_remark" => '', "action_type" => "edit"];
//        ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "company_item", 0, $stn);
//        return $re ? app_show(0, "信息更新成功") : error_show(1003, "信息更新失败");
    }

    /*查询*/
    public function query()
    {
        $param = $this->request->filter('trim')->post();
        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('query', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//        $companyNo = isset($this->post['companyNo']) ? trim($this->post['companyNo']) : "";
//        if ($companyNo == "") return error_show(1004, '所属企业不能为空');

//        $effetc = VerifyTokens($token);
//        if (!empty($effetc) && $effetc['code'] != 0) {
//            return error_show($effetc['code'], $effetc['message']);
//        }

//        $dati = Db::name("company_item")
//            ->where(['pid' => 0, 'is_del' => 0, 'companyNo' => $companyNo])
//            ->order("weight desc")
//            ->select()
//            ->toArray();
//        $k = [];
//        foreach ($dati as $key => $value) {
        // $temp=[];
        //$temp= Db::name("company_item")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc")->select();
        // $value['child'] =$temp;
        // $k[]=$value;
//            $k[] = crea($value);
//        }
//        return app_show(0, "获取成功", $k);
    }

    public function itemdel()
    {

        $param = $this->request->filter('trim')->post();
        $userCommon = \app\admin\common\User::getIns();
        $rs = $userCommon->handle('itemdel', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);

//        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
//        if ($token == "") {
//            return error_show(101, 'token不能为空');
//        }
//        $effetc = VerifyTokens($token);
//        if (!empty($effetc) && $effetc['code'] != 0) {
//            return error_show($effetc['code'], $effetc['message']);
//        }
//        $id = isset($this->post['id']) ? intval($this->post['id']) : "";
//        $items = Db::name("company_item")
//            ->field('id,is_del,updatetime')
//            ->where(['is_del' => 0, 'id' => $id])
//            ->findOrEmpty();
//        if (empty($items)) {
//            return error_show(1003, "部门信息不存在");
//        }
//        $items['is_del'] = 1;
//        $items['updatetime'] = date("Y-m-d H:i:s");
//        $result = Db::name("company_item")->save($items);
//        $stn = ["order_code" => $items['name'], "status" => 0, "action_remark" => '', "action_type" => "delete"];
//        ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "company_item", 0, $stn);
//        return $result ? app_show(0, "删除成功") : error_show(1003, "删除失败");
    }

}