<?php
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use home\assets\MainAsset;
use home\modules\sale\models\Opportunity;
use home\assets\ModalAsset;
use common\assets\ArtDialogAsset;
use home\modules\customer\models\Contacter;

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

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

//默认值
if ($model->isNewRecord) {
	$this->title = '添加销售机会';
	$model->contacter_id = 3;
	$model->sort = 0;
    $model->attention = 0;
    $model->category_id = 2;
    $model->leader_name = Yii::$app->user->identity->realname;
    $model->leader_id = Yii::$app->user->identity->user_id;
    $model->discoverer_name = Yii::$app->user->identity->realname;
    $model->discoverer_id = Yii::$app->user->identity->user_id;
    $model->opportunity_no = Opportunity::getNo();
    $customer_no = Yii::$app->request->get('customer_no');
    if (!empty($customer_no)) {
        $model->customer_name = Yii::$app->request->get('customer_name');
        $model->customer_no = $customer_no;
        $contacterInfo=Contacter::findOne(['customer_no'=>$customer_no]);
        $model->contacter_id=ArrayHelper::getValue($contacterInfo, 'contacter_id');
        $model->contacter_name = ArrayHelper::getValue($contacterInfo, 'name');
    }

} else {
	$this->title = '修改销售机会';
    $model->leader_name = ArrayHelper::getValue(\common\api\User::getOne($model->leader_id), 'realname');
    $model->discoverer_name = ArrayHelper::getValue(\common\api\User::getOne($model->discoverer_id), 'realname');
    $model->customer_name = isset($model->customer->name) ? $model->customer->name : '';
    $model->contacter_name = ArrayHelper::getValue(Contacter::findOne($model->contacter_id), 'name');
    $model->due_date = date('Y-m-d', $model->due_date);
}
?>

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

</div>
<script>
    var projectParams = {
        selectDistrictUrl : "<?= Url::to(['/customer/district/select-opportunity']) ?>",
        cityUrl : "<?= Url::to(['/customer/district/select-city']) ?>",
        areaUrl : "<?= Url::to(['/customer/district/select-area']) ?>",
        dateType : "1"
    }
</script>
