<?php

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

namespace home\modules\receivable\controllers;

use Yii;
use home\modules\receivable\models\Receivable;
use yii\data\ActiveDataProvider;
use home\modules\contract\models\Contract;
use home\modules\receivable\models\Account;

/**
 * 回款控制器
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class ReceivableController extends \home\controllers\Controller
{

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);
        $actions['create']['layout'] = '//modal';
        $actions['view']['layout'] = '//modal';
        $actions['update']['layout'] = '//modal';
        return $actions;
    }

    public function actionIndex()
    {
        $model = new Receivable();
        $query = Receivable::find();
        $query->orderBy(['rece_id' => SORT_DESC]);

        $request = Yii::$app->request;
        $keywords = trim($request->get('keywords'));
        $field = $request->get('search_field');

        if(!empty($keywords) && !empty($field)){
            switch ($field) {
                case 'contract_name':
                    $contractNos = Contract::find()->where(['like','name', $keywords])->select('contract_no')->asArray()->column();
                    $query->where(['in', 'contract_no', $contractNos]);
                    break;
                default :
                    $query->andwhere(['like', $field,$keywords]);
            }
        }
        $listContractNos = [];
        $listContractNos = Contract::getMyContract();
        if ($listContractNos !== FALSE && is_array($listContractNos)) {
            $query->andWhere(['in', 'contract_no', $listContractNos]);
        }

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

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

    /**
     * 详情
     */
    public function actionDetail()
    {
        $request = Yii::$app->request;
        $id = (int) $request->get('id');
        //当从合同详情页查看计划详情时，传递的参数，以作区别
        $type = $request->get('type');
        $model = Receivable::findOne(['rece_id' => $id]);
        if(empty($model)){
            Yii::$app->getSession()->setFlash('error', '该回款不存在');
            return $this->flash();
        }
        $contract = Contract::findOne(['contract_no' => $model->contract_no]);
        $render = '';
        $render = empty($type) ? 'detail' : 'contract-detail';
        if(!empty($type)){
            $this->layout = "//modal";
        }
        return $this->render($render, [
                    'model' => $model,
                    'contract' => $contract,
        ]);
    }

    /**
     * 获取合同对应的项目以及客户
     */
    public function actionAjax()
    {
        $contract_no = Yii::$app->request->post('contract_no');
        $contract = Contract::findOne(['contract_no' => $contract_no]);

//        $json['project_no'] = isset($contract->project) ? $contract->project['name'] : '';
        $json['customer_name'] = isset($contract->customer) ? $contract->customer['name'] : '';

        return \Yii::createObject([
                    'class' => 'yii\web\Response',
                    'format' => \yii\web\Response::FORMAT_JSON,
                    'data' => $json,
        ]);
    }

    /**
     * 获取对应收款银行下的账号信息
     */
    public function actionGetAccount()
    {
        $bank_id = Yii::$app->request->get('bank_id');
        $json = [
            'data' => '',
            'status' => 0
        ];

        $account = Account::getAccountList(['bank_id' => $bank_id]);
        if(isset($account)){
            $json['data'] = $account;
            $json['status'] = 1;
        }

        return \Yii::createObject([
                    'class' => 'yii\web\Response',
                    'format' => \yii\web\Response::FORMAT_JSON,
                    'data' => $json,
        ]);
    }

}
