<?php
use yii\helpers\Url;
use home\assets\MainAsset;
use home\modules\receivable\models\Receivable;
use common\models\User;
use home\modules\contract\models\Contract;

/* @var $this \yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \home\modules\receivable\models\receivable */

$assetUrl = $this->getAssetManager()->getBundle(MainAsset::className())->baseUrl;
$this->registerJsFile($assetUrl . '/js/link.js', ['depends' => MainAsset::className()]);
$this->registerJsFile($assetUrl . '/js/receivable.js', ['depends' => MainAsset::className()]);

$this->title = '新增回款';

//默认值
if ($model->isNewRecord) {
	$model->user_id =  Yii::$app->user->identity->realname;
	$model->rece_no = Receivable::getNo();
    $model->to_date=date('Y-m-d');
    $contract_no = Yii::$app->request->get('contract_no');
    if (!empty($contract_no)) {
        $contract = Contract::findOne(['contract_no' => $contract_no]);
        $model->contract_no = $contract_no;
        $model->contract_name = $contract->name;
        $model->customer_name = empty($contract->customer->name)?'':$contract->customer->name;
//        $model->project_name = empty($contract->project->name)?'':$contract->project->name;
    }
} else {
	$contract = Contract::findOne(['contract_no' => $model->contract_no]);
    if (isset($contract)) {
        $model['contract_name'] = $contract->name;
        $model['customer_name'] = isset($contract->customer->name)?$contract->customer->name:'';
//        $model['project_name'] = isset($contract->project->name)?$contract->project->name:'';
    } else {
        $model['contract_name'] = '';
        $model['customer_name'] = '';
//        $model['project_name'] = '';
    }
	$model->user_id = User::findById($model->user_id)->realname;
    $model->to_date = date('Y-m-d', $model->to_date);
}
?>

<div class="modal-window">
	<?= \home\widgets\Alert::widget(); ?>
	<?= $this->render('_form', ['model' => $model, 'action' => $action]);  ?>
</div>

<script>
var getAccount = "<?= Url::to(['get-account']) ?>";

function linkSelectCallback(input, value){
    $.ajax({
        url : "<?= Url::to([$this->context->id.'/ajax']) ?>",
        type : "post",
        data : "contract_no="+value,
        success: function(res) {
        	$(".customer_name").val(res.customer_name);
//        	$(".project_name").val(res.project_no)
    	}
	});
}
</script>