<?php
namespace App\Controllers\manage;

use App\Controllers\BaseController;
use App\Models\manage\DeptModel;

class Dept extends BaseController
{

    private $deptModel;

    public function __construct()
    {
        $this->deptModel = new DeptModel();
    }


    public function dept(): string
    {
        return view("pages/manage/dept/lists", $this->data);
    }

    // 총괄팀장 페이지
    public function getchkuser(): string
    {
        return view("pages/manage/dept/users", $this->data);
    }

    public function getDeptList()
    {
        $start = (int) $this->request->getGet('start') ?: 0;
        $end = (int) $this->request->getGet('length') ?: 10;

        $data = [
            'srchDepth' => $this->request->getGet('srchDepth') ?: '',
            'srcDeptNm' => $this->request->getGet('srcDeptNm') ?: '',
            'srcDeptHead' => $this->request->getGet('srcDeptHead') ?: '',
            'useYn' => $this->request->getGet('useYn') ?: '',
        ];

        $totalCount = $this->deptModel->getTotalCount($data);


        $datas = $this->deptModel->getDeptList($start, $end, $data);


        return $this->response->setJSON(body: [
            'recordsTotal' => $totalCount,
            'recordsFiltered' => $totalCount,
            'data' => $datas,
        ]);
    }


    // 유저목록 조회
    public function getUserList()
    {
        $start = (int) $this->request->getGet('start') ?: 0;
        $end = (int) $this->request->getGet('length') ?: 10;

        $totalCount = $this->deptModel->getUserCount();


        $datas = $this->deptModel->getUserList($start, $end);

        return $this->response->setJSON(body: [
            'recordsTotal' => $totalCount,
            'recordsFiltered' => $totalCount,
            'data' => $datas,
        ]);
    }

    // 상위조직 조회
    public function getPdept()
    {

        $datas = $this->deptModel->getPdept();


        return $this->response->setJSON(body: [
            'data' => $datas,
        ]);

    }

    // 조직정보 저장
    public function saveDept()
    {
        try {

            $type = $this->request->getPost('type');

            if ($type == "create") {
                $data = [
                    $this->request->getPost("addPdept"), // 상위조직
                    $this->request->getPost("addDeptNm"), // 조직명칭
                    $this->request->getPost("addDeptDesc"), // 부서설명
                    $this->request->getPost("addDeptHead"), // 총괄팀장
                    $this->request->getPost(index: "addUseYn"), // 사용여부
                    $this->request->getPost(index: "addDepth"), // 조직구분
                    session('usr_sq'),
                    session('usr_sq'),
                ];

                // INSERT departments
                $this->deptModel->insertDept($data);

            } else if ($type == "update") {

                $data = [
                    $this->request->getPost("pdept_sq"),
                    $this->request->getPost("addDeptNm"), // 조직명칭
                    $this->request->getPost("addDeptDesc"), // 부서설명
                    $this->request->getPost("addDeptHead"), // 총괄팀장
                    $this->request->getPost("addUseYn"), // 사용여부
                    $this->request->getPost("addDepth"), // 조직구분
                    session('usr_sq'),
                    $this->request->getPost("dept_sq"),
                ];

                // UPDATE departments
                $this->deptModel->updateDept($data);
            }

            return $this->response->setJSON([
                'code' => '0',
                'msg' => 'success'
            ]);

        } catch (\Exception $e) {
            return $this->response->setJSON([
                'code' => '9',
                'msg' => $e->getMessage(),
            ]);
        }
    }
}
