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

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

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

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

//默认值
if ($model->isNewRecord) {
	$model->prompt = 1;
	$model->status = 0;
	$model->registrant =  Yii::$app->user->identity->realname;
	$model->plan_no = Plan::getNo();
    $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;
    }
} 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:'';
    } else {
        $model['contract_name'] = '';
        $model['customer_name'] = '';
    }
	$model->plan_time = date('Y-m-d', $model->plan_time);
}
?>

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

<script>
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>