<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace qcbase\controllers;

use Yii;
use qcbase\models\Department;
use common\models\User;
use qcbase\models\UserDepartment;
use yii\helpers\ArrayHelper;

/**
 * 部门控制器
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class DepartmentController extends Controller
{

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        return $actions;
    }

    public function actionIndex()
    {
        $models = Department::getList('*', [], [], 'leader');
        $deptUsers = User::getListByDepartment();

        foreach ($models as &$model) {
            $model['allUserStr'] = '';
            if (isset($deptUsers[$model['dept_id']]) && !empty($deptUsers[$model['dept_id']]['user'])) {
                $nameArr = ArrayHelper::getColumn($deptUsers[$model['dept_id']]['user'], 'realname');
                $model['allUserStr'] = implode(',', $nameArr);
            }
        }

        return $this->render('index', [
            'get' => \Yii::$app->request->get(),
            'model' => new $this->model,
            'models' => $models,
        ]);
    }

    /**
     * 获取当前部门以及父部门下所有的用户下拉列表数据
     */
    public function actionGetUser()
    {
        $deptId = Yii::$app->request->post('deptId');
        $parentId = Yii::$app->request->post('parentId');
        $leadUserId = Yii::$app->request->post('leadUserId');
        $json = [
            'data' => '',
            'status' => 0
        ];
        $data = UserDepartment::getUserByDeptAndParent($deptId, $parentId, $leadUserId);
        $data != '' ? $json['data'] = $data : $json['data'] = '';
        $json['status'] = 1;
        return \Yii::createObject([
                'class' => 'yii\web\Response',
                'format' => \yii\web\Response::FORMAT_JSON,
                'data' => $json,
        ]);
    }
}
