<?php
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use home\modules\sale\models\Category;
use home\modules\sale\models\Opportunity;
use common\widgets\DatePicker;

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

$attrLabels = $model->attributeLabels();

$form = ActiveForm::begin([
        'id' => 'main-form',
        'options' => ['class' => 'informationform f14', 'style' => "padding-left:8%; padding-right:8%"],
        'action' => $action,
        'fieldConfig' => [
            'options' => ['tag' => 'td'],
            'template' => "{input}{hint}{error}",
            'errorOptions' => ["class" => "error"]
        ]
    ]);
?>

<table cellpadding="0" cellspacing="10" width="100%">
    <tbody>
        <tr>
            <td align="right" width="10%"><?= $model->getAttributeLabel('name') ?>：</td>
            <?= $form->field($model, 'name', ['options' => ['width' => '30%']])->textInput(['class' => 'form-control']) ?>
            <td width="10%"><span class="redstar">*</span></span></td>

            <td align="right"><?= $model->getAttributeLabel('opportunity_no') ?>：</td>
            <?= $form->field($model, 'opportunity_no')->textInput(['class' => 'form-control']) ?>
            <td><span class="redstar">*</span></td>

        </tr>
        <tr>

            <td align="right" width="12%"><?= $model->getAttributeLabel('customer_name') ?>：</td>
            <?= $form
                ->field($model, 'customer_name')
                ->textInput([
                    'class' => 'form-control select-customer',
                    'placeholder' => '单击选择',
                    'data-hidden-id' => 'customer-no',
                    'data-url' => Url::to(['/link/customer']),
                    'data-title' => '选择客户',
                    'data-callback' => 'relevanceCallback'
                ])
            ?>
            <?= $form->field($model, 'customer_no', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'customer-no']) ?>
            <td width="10%"><span class="redstar">*</span></span></td>

            <td align="right" width="10%"><?= $model->getAttributeLabel('contacter_name') ?>：</td>
            <?= $form
                    ->field($model, 'contacter_name', ['options' => ['width' => '30%']])
                    ->textInput([
                        'class' => 'form-control contacter',
                        'placeholder' => '单击选择',
                        'data-hidden-id' => 'contacter-id',
                        'data-url' => Url::to(['/link/contacter']),
                        'old-url' => Url::to(['/link/contacter']),
                        'data-title' => '选择联系人'
                    ])
                ?>
            <?= $form->field($model, 'contacter_id', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'contacter-id']) ?>
            <td width="10%"><span class="redstar">*</span></span></td>
        </tr>
        <tr>
            <td align="right"><?= $model->getAttributeLabel('category_id') ?>：</td>
            <td>
                <select class="form-control" name="Opportunity[category_id]" >
                    <?= Category::getDropDownTree($model->category_id, 0, 'true', 'cat_id'); ?>
                </select>
            </td>
            <td><span class="redstar">*</span></td>
            <td align="right"><?= $model->getAttributeLabel('status_id') ?>：</td>
            <?= $form->field($model, 'status_id')->dropDownList(Opportunity::getSaleStatusList()) ?>
            <td><span class="redstar">*</span></td>
        </tr>
        <tr>
            <td align="right"><?= $model->getAttributeLabel('origin_id') ?>：</td>
            <?= $form->field($model, 'origin_id')->dropDownList(Opportunity::getOriginList()) ?>
            <td><span class="redstar">*</span></td>

            <td align="right"><?= $model->getAttributeLabel('attention') ?>：</td>
            <?= $form->field($model, 'attention', ['options' => ['width' => '250px']])->radioList(Opportunity::getAttentionList(), ['itemOptions' => ['labelOptions' => ['class' => 'radio-inline']]]) ?>
            <td></td>
        </tr>
        <tr>
            <td align="right"><?= $model->getAttributeLabel('estimate_price') ?>：</td>
            <?= $form->field($model, 'estimate_price')->textInput(['class' => 'form-control']) ?>
            <td><span class="redstar">*</span></td>

            <td align="right"><?= $model->getAttributeLabel('due_date') ?>：</td>
            <?= DatePicker::widget(['model' => $model, 'form' => $form, 'attribute' => 'due_date', 'inputConfig' => ['class' => 'form-control'], 'dateConfig' => ['minDate' => '%y-%M-{%d+1}']]) ?>
            <td><span class="redstar">*</span></td>
        </tr>
        <tr>
            <td align="right" width="12%"><?= $model->getAttributeLabel('discoverer_name') ?>：</td>
            <?= $form
                ->field($model, 'discoverer_name', ['options' => ['width' => '30%']])
                ->textInput([
                    'class' => 'form-control select-single-user',
                    'placeholder' => '点击选择',
                    'data-hidden-id' => 'discoverer-id',
                    'data-url' => Url::to(['/link/single-user']),
                    'data-title' => '选择发现人'
                ])
            ?>
            <?= $form->field($model, 'discoverer_id', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'discoverer-id']) ?>
            <td width="10%"><span class="redstar">*</span></span></td>

            <td align="right" width="12%"><?= $model->getAttributeLabel('leader_name') ?>：</td>
            <?= $form
                ->field($model, 'leader_name', ['options' => ['width' => '30%']])
                ->textInput([
                    'class' => 'form-control select-single-user',
                    'placeholder' => '点击选择',
                    'data-hidden-id' => 'leader-id',
                    'data-url' => Url::to(['/link/single-user']),
                    'data-title' => '选择所有人'
                ])
            ?>
            <?= $form->field($model, 'leader_id', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'leader-id']) ?>
            <td width="10%"><span class="redstar">*</span></span></td>
        </tr>
        <tr>
            <td align="right"><?= $model->getAttributeLabel('nextstep') ?>：</td>
            <?= $form->field($model, 'nextstep', ['options' => ['colspan' => '4']])->textInput(['class' => 'form-control', 'rows' => '5']) ?>
            <td><span class="redstar"></span></td>
        </tr>
        <tr>
            <td align="right"><?= $model->getAttributeLabel('remark') ?>：</td>
            <?= $form->field($model, 'remark', ['options' => ['colspan' => '4']])->textarea(['class' => 'form-control', 'rows' => '5']) ?>
            <td><span class="redstar"></span></td> 
        </tr>
    </tbody>
</table>
<?= ($model->isNewRecord) ? '' : Html::hiddenInput('id', $model->opportunity_id); ?>
<?= ($model->isNewRecord) ? '' : '<span id="only-district" data-set-num="1" ></span>'; ?>
<?= Html::hiddenInput('from', Yii::$app->request->get('from')); ?>


<script>
    function relevanceCallback(input, value){
        $.ajax({
            url : "<?= Url::to([$this->context->id.'/select-link']) ?>",
            type : "post",
            data : "customer_no="+value,
            success: function(json) {
                if (json.status == 1) {
                    $("#opportunity-street").val(json.customer_data.street);
                    $("#opportunity-contacter_name").val(json.contacts_data.name);
                    $("#contacter-id").val(json.contacts_data.contacter_id);
                    $.getJSON(projectParams.selectDistrictUrl, {province_id:json.customer_data.province_id, city_id:json.customer_data.city_id, area_id:json.customer_data.area_id, id:0, type:1}, function(value){
                        $("#only-0").next().remove();
                        //$("#only-0").next().hide("slow");
                        $("#only-0").after(value.data);
                    });
                }
            }
        });
    }
</script>

<?php ActiveForm::end(); ?>
