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

namespace home\modules\customer\controllers;

use Yii;
use home\controllers\Controller;
use yii\data\ActiveDataProvider;
use home\modules\customer\models\Contacter;

/**
 * 联系人控制器
 * 
 * @author zealxd http://blog.u-bo.com
 * @since 0.1
 */
class ContacterController extends Controller
{

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        $actions['create']['layout'] = '@home/views/layouts/modal.php';
        $actions['view']['layout'] = '@home/views/layouts/modal.php';
        $actions['view']['enableScopeAccess'] = false;
        $actions['update']['layout'] = '@home/views/layouts/modal.php';
        $actions['update']['enableScopeAccess'] = false;
        return $actions;
    }

    public function actionIndex()
    {
        //搜索
        $request = Yii::$app->request;
        $keywords = trim($request->get('keywords'));
        $status = $request->get('status');
        $field = $request->get('search_field');

        $this->model === null && $this->model = $this->controller->model;

        $query = call_user_func([$this->model, 'find']);

        //排序，默认按照索引倒序排列
        $query->orderBy(['customer_no' => SORT_ASC, 'sort' => SORT_ASC]);

        //权限判断，除了超级管理员以外的都只能看到自己负责区域的客户
        if (!Yii::$app->session->get('isSuperAdmin')) {
            $result = Contacter::getEligibilityContacter();
            if ($result !== true) {
                $query->where(['in', 'customer_no', $result]);
            }
        }


        if (!empty($keywords) && !empty($field)) {
            switch ($field) {
                case "customer":
                    $customers = \home\modules\customer\models\Customer::find()->where(['like', 'name', $keywords])->select('customer_no')->asArray()->column();
                    $query->andWhere(['customer_no' => $customers]);
                    break;
                case "creater":
                    $creater = \common\models\User::find()->where(['like', 'realname', $keywords])->select('user_id')->asArray()->column();
                    $query->andWhere(['user_id' => $creater]);
                default :
                    $query->andwhere(['like', $field, $keywords]);
            }
        }
        if ($status != '') {
            $query->andWhere(['status' => (int) $status]);
        }

        $provider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 15,
            ],
        ]);

        return $this->render('index',
                [
                'get' => $request->get(),
                'model' => new $this->model,
                'models' => $provider->models,
                'pages' => $provider->pagination,
        ]);
    }

    /**
     * 查看当前联系人详情
     */
    public function actionDetail()
    {
        $id = (int) Yii::$app->request->get('id');
        $model = Contacter::findOne($id);
        $this->layout = "//modal";
        return $this->render('detail', [
                'model' => $model,
        ]);
    }
}