<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\CompanyT;
use common\models\DepartmentT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class CompanyController extends \yii\web\Controller
{
    public $my = null;
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = UserT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $items = CompanyT::find()
            ->orderBy('order_id asc')
            ->all();

        $data = [];
        $data['total'] = count($items);
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $data['rows'][] = $row;
        }

        return $data;
    }

    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = CompanyT::findOne(['id'=>$id]);
        } else {
            $info = new CompanyT();
        }

        return $this->render('edit',[
            'info' => $info
        ]);
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $id = $request->post('id',0);
            $name = $request->post('name');

            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }

            $row = null;
            if($id > 0) {
                $row = CompanyT::findOne(['id'=>$id]);
            } else {
                $row = new CompanyT();
            }
            $row->name = $name;
            $row->save();

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        return $result;
    }

    public function actionDelete()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '删除失败';

        if($request->isPost) {
            $id = $request->post('id',0);

            if($id > 0) {
                $row = CompanyT::findOne(['id'=>$id]);
                $row->delete();

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }

    public function actionDpIndex()
    {
        $request = Yii::$app->request;
        $company_id = $request->get('company_id',0);
        $company_info = null;
        if($company_id > 0) {
            $company_info = CompanyT::findOne(['id'=>$company_id]);
        }

        $items = DepartmentT::getTree($company_id);
        return $this->render('dp-index',[
            'items' => $items,
            'company_id' => $company_id,
            'company_info' => $company_info
        ]);
    }

    public function actionDpEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $company_id = $request->get('company_id',0);
        $company_info = null;

        if($company_id > 0) {
            $company_info = CompanyT::findOne(['id'=>$company_id]);
        }
        if($id > 0) {
            $info = DepartmentT::findOne(['id'=>$id,'company_id'=>$company_id]);
        } else {
            $info = new DepartmentT();
            $info->company_id = $company_id;
        }

        $group_items = DepartmentT::getTree($company_id);

        return $this->render('dp-edit',[
            'info' => $info,
            'group_items' => $group_items,
            'company_info' => $company_info
        ]);
    }

    public function actionDpSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $id = $request->post('id',0);
            $company_id = $request->post('company_id',0);
            $name = $request->post('name');
            $parent_id = intval($request->post('parent_id',0));

            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }
            if($id > 0 && $parent_id == $id) {
                $result['msg'] = '上级不能选择自己!';
                return $result;
            }

            $tran = DepartmentT::getDb()->beginTransaction();

            try {
                $row = null;
                if($id > 0) {
                    $row = DepartmentT::findOne(['id'=>$id]);
                    if($row->hasChild($parent_id)) {
                        $result['msg'] = '上级不能选择自己下级岗位!';
                        return $result;
                    }
                } else {
                    $row = new DepartmentT();
                }
                $row->name = $name;
                $row->parent_id = $parent_id;
                $row->company_id = $company_id;
                $row->save();

                $result['success'] = true;
                $result['msg'] = '保存成功';

                $tran->commit();
            } catch(\Exception $e) {
                $tran->rollBack();
                throw $e;
            }
        }
        return $result;
    }

    public function actionDpDelete()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '删除失败';

        if($request->isPost) {
            $id = $request->post('id',0);

            if($id > 0) {
                $tran = DepartmentT::getDb()->beginTransaction();
                try {
                    $row = DepartmentT::findOne(['id'=>$id]);
                    $children = $row->departments;
                    if(count($children) > 0) {
                        $result['msg'] = '请先删除子项';
                        return $result;
                    }
                    $row->delete();

                    $tran->commit();
                } catch(\Exception $e) {
                    $tran->rollBack();
                    throw $e;
                }

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }

    public function actionDepartments()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $company_id = $request->get('company_id',0);

        $items = DepartmentT::getTree($company_id);
        $html = '<option value="0">---请选择---</option>';
        foreach($items as $item) {
            $html .= '<option value='.$item['id'].'>'.$item['name'].'</option>';
        }
        $result['success'] = true;
        $result['html'] = $html;
        return $result;
    }
}
