<?php

namespace app\admin\controller\sys_manage;

use app\admin\service\sys_manage\DeptService;
use app\admin\validate\sys_manage\DeptValidate;
use app\common\Tree;
use app\controller\Curd;
use app\model\SysDept;
use support\Request;
use support\Response;

class DeptController extends Curd
{
    public function __construct()
    {
        $this->model = new SysDept();
    }
    /**
     * @Desc 部门列表
     * @Author Gorden
     * @Date 2024/2/21 10:29
     *
     * @param Request $request
     * @return \support\Response
     */
    public function deptList(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $keywords = format_string($request->get('keywords', ''));

        return DeptService::deptList($page, $limit, $keywords);
    }

    public function select(Request $request): Response
    {
        
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $format = 'tree';
        $order = $request->get('order', 'desc');
        $field = $field ?? 'dept_addtimes';
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, 1000);
    }

    protected function formatTree($items): Response
    {
        $format_items = [];
        foreach ($items as $item) {
            $format_items[] = [
                'name' => $item->dept_name,
                'value' => (string)$item->dept_id,
                'id' => $item->id ?? $item->dept_id,
                'pid' => $item->pid ?? $item->dept_super_id,
                'dept_id' => $item->dept_id,
                'dept_name' => $item->dept_name,
                'dept_super_id' => $item->dept_super_id,
                'dept_status' => $item->dept_status,
                'dept_category' => $item->dept_category,
                'dept_code' => $item->dept_code,
                'dept_telephone' => $item->dept_telephone,
                'category_addtimes' => $item->category_addtimes,
            ];
        }
        $tree = new Tree($format_items);
        return json_success('success', $tree->getTree());
    }

    public function selectList(Request $request)
    {
        return DeptService::selectList($request);
    }

    /**
     * @Desc 部门详情
     * @Author Gorden
     * @Date 2024/2/21 10:35
     *
     * @param $id
     * @return \support\Response
     */
    public function deptInfo($id)
    {
        return DeptService::deptInfo($id);
    }

    /**
     * @Desc 创建部门
     * @Author Gorden
     * @Date 2024/2/21 9:22
     *
     * @param Request $request
     * @return \support\Response
     */
    public function addDept(Request $request)
    {
        $validate = new DeptValidate();
        if (!$validate->scene('add')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return DeptService::insertDept($request->post());
    }

    /**
     * @Desc 修改部门
     * @Author Gorden
     * @Date 2024/2/21 10:15
     *
     * @param $id
     * @param Request $request
     * @return \support\Response
     */
    public function updateDept($id, Request $request)
    {
        $validate = new DeptValidate();
        if (!$validate->scene('update')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return DeptService::updateDept($id, $request->post());
    }

    /**
     * @Desc 修改部门状态
     * @Author Gorden
     * @Date 2024/2/21 10:41
     *
     * @param $id
     * @param $status
     * @return \support\Response
     */
    public function updateStatus($id, Request $request)
    {
        $validate = new DeptValidate();
        if (!$validate->scene('update_status')->check($request->post())) {
            return json_fail($validate->getError());
        }

        return DeptService::updateStatus($id, $request->post('dept_status'));
    }

    public function delDept($id)
    {
        return DeptService::delDept($id);
    }

    /**
     * 场所名获取打印机
     */
    public function getPrinterByPremiseName(Request $request)
    {
        $premiseName = $request->get('premise','');

        $printerList = [];
        try{

            $premise = SysDept::where('dept_name',$premiseName)->first();
            if (!empty($premise->dept_extend_json)){
                $deptExtendJson = json_decode($premise->dept_extend_json,true);
                if (isset($deptExtendJson['printer'])){
                    $printerList = $deptExtendJson['printer'];
                }
            }
        }catch(\Exception $e){}

        return json_success('',$printerList);
    }
}