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

namespace wap\controllers;

use Yii;
use wap\controllers\Controller;
use wap\models\Application;
use yii\db\Query;
use common\models\Common;
use yii\web\NotFoundHttpException;
use yii\web\ForbiddenHttpException;
use yii\data\ActiveDataProvider;
use yii\helpers\StringHelper;
use yii\helpers\Url;
use common\models\PermissionScope;
use home\modules\reimbursement\models\Verify;

/**
 * 申请单控制器
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class ApplicationController extends Controller
{
    /**
     * 选择申请单
     */
    public function actionChooseAppli()
    {
        $request = Yii::$app->request;
        $userId = Yii::$app->user->id;
        $keywords = $request->get('keywords');

        $query = (new Query())
            ->select('ap.*')
            ->orderBy(['appli_id' => SORT_DESC])
            ->from('{{%reimbursement_application}} ap');

        $query->where(['isverify' => 1, 'isused' => 0]);

        //权限范围过滤
        /**
        $scopeUser = PermissionScope::getScopeUser('reimbursement/application/index');
        if (!empty($scopeUser)) {
            $findArr = [];
            foreach ($scopeUser as $u) {
                $findArr[] = 'ap.user_id = ' . (int) $u;
            }
            $applicationWhere = implode(' or ', $findArr);
        }
        if (!empty($applicationWhere)) {
            $query->andWhere($applicationWhere);
        }
         */
        $query->andWhere(['ap.user_id' => Yii::$app->user->id]);

        if (!empty($keywords)) {
            $query->andWhere(['like', 'ap.name', trim($keywords)]);
        }
        $models = $query->all();

        return $this->render('choose-appli', [
            'models' => $models,
        ]);
    }

    /**
     * 新增报销
     */
    public function actionCreate()
    {
        $this->model = new Reimbursement();
        $request = Yii::$app->request;
        if ($this->model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            if ($this->model->save()) {
                $data = ['status' => 1, 'info' => '新增成功', 'url' => Url::to(['index'])];
            }
            elseif($this->model->hasErrors()) {
                $firstError = reset($this->model->getFirstErrors());
                $data = ['status' => 0, 'info' => $firstError];
            }
            return $data;
        }
        $linkId = $request->get('link_id');
        $linkType = $request->get('link_type');
        $linkData = Common::getLinkData($linkType, $linkId);

        if (empty($linkData)) {
            throw new NotFoundHttpException('关联信息不存在');
        }


        return $this->render('create', [
            'linkData' => $linkData,
            'linkId' => $linkId,
            'linkType' => $linkType,
        ]);
    }

    /**
     * 详情
     */
    public function actionDetail()
    {
        $request = Yii::$app->request;
        $id = (int) $request->get('id');
        $model = Application::findOne(['appli_id' => $id]);
        if (empty($model)) {
            throw new NotFoundHttpException('申请单不存在或已被删除');
        }

        //权限检测
        $scopeUser = PermissionScope::getScopeUser('reimbursement/application/detail');
        if (!empty($scopeUser) && !in_array($model['user_id'], $scopeUser)) {
            throw new ForbiddenHttpException('当前没有浏览该申请单详情的权限');
        }

        $verifys = Verify::getList(['link_type' => 'appli', 'link_id' => $id]);
        return $this->render('detail', [
            'model' => $model,
            'verify' => new Verify(),
            'verifys' => $verifys,
        ]);
    }

    /**
     * 选择申请单详情
     */
    public function actionChooseDetail()
    {
        $request = Yii::$app->request;
        $id = (int) $request->get('id');
        $model = Application::findOne(['appli_id' => $id]);
        if (empty($model)) {
            throw new NotFoundHttpException('申请单不存在或已被删除');
        }

        //权限检测
        $scopeUser = PermissionScope::getScopeUser('reimbursement/application/detail');
        if (!empty($scopeUser) && !in_array($model['user_id'], $scopeUser)) {
            throw new ForbiddenHttpException('当前没有浏览该申请单详情的权限');
        }

        $verifys = Verify::getList(['link_type' => 'appli', 'link_id' => $id]);
        return $this->render('choose-detail', [
            'model' => $model,
            'verify' => new Verify(),
            'verifys' => $verifys,
        ]);

    }


}
