<?php namespace App\Controllers;
helper('statusCode');

class OrgManageCtr extends BaseController
{

    /****************************************************
     *       添加部门
     /****************************************************/
	public function addOrgSection(){
        $result = array();
        $param = $this->request->getJSON();
        $timest = time();
        $curTime =  date("Y-m-d H:i:s", $timest);
        $item = array();
        $item["org_section_name"] = $param->org_section_name;
        $item["org_section_detail"] = $param->org_section_detail;
        $item["create_time"] = $curTime;
        try{
            $orgManageModel = new \App\Models\OrgManageModel();
            $orgManageModel->insertOrgSection($item);
            $result["status"] = SUCESS;
            $result["message"] = "添加部门成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       删除部门
     /****************************************************/
     public function delOrgSection(){
        $result = array();
        $param = $this->request->getJSON();
        $orgSectionId = $param->org_section_id;
        try{
            $orgManageModel = new \App\Models\OrgManageModel();
            $orgManageModel->delOrgSection($orgSectionId);
            $result["status"] = SUCESS;
            $result["message"] = "删除部门成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       更新部门
     /****************************************************/
     public function updateOrgSection(){
        $result = array();
        $param = $this->request->getJSON();
        $item = array();
        $item["org_section_name"] = $param->org_section_name;
        $item["org_section_detail"] = $param->org_section_detail;
        $item["id"] = $param->org_section_id;
        try{
            $orgManageModel = new \App\Models\OrgManageModel();
            $orgManageModel->updateOrgSection($item);
            $result["status"] = SUCESS;
            $result["message"] = "修改部门成功！";
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }

    /****************************************************
     *       获取所有部门
     /****************************************************/
     public function getAllOrgSections(){
        $result = array();
        $param = $this->request->getJSON();
        try{
            $orgManageModel = new \App\Models\OrgManageModel();
            $data = $orgManageModel->getAllOrgSection();
            $result["status"] = SUCESS;
            $result["data"] = $data;
        }catch (Exception $e){
            $result["status"] = COMMON_FAIL;
            $message = $e->getMessage();
            $result["message"] = $message;
        }
        $this->response->setHeader('Content-Type', 'application/json')
            ->setHeader('charset', 'utf-8');
        $result = json_encode($result);
        echo $result;
    }
}