<?php

use home\assets\MainAsset;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use common\widgets\Editor;
use common\widgets\DatePicker;
use common\widgets\Uploader;
use common\widgets\Tagit;
use yii\helpers\StringHelper;
use home\modules\schedule\models\Category;
use home\assets\ModalAsset;
use common\assets\ArtDialogAsset;
use home\modules\customer\models\Customer;
use home\modules\sale\models\Opportunity;
use common\models\PermissionScope;

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

$linkTypeArray = [];
if (Customer::readAccess('schedule/schedule/link-customer')) {
    $linkTypeArray[] = 'customer';
}
if (Opportunity::readAccess('schedule/schedule/link-sale_opportunity')) {
    $linkTypeArray[] = 'sale_opportunity';
}


$deletePermissions = PermissionScope::getScopeUser('schedule/schedule/delete');
$js = '';
if ($deletePermissions === false || $model->isNewRecord || (!in_array($model->user_id, $deletePermissions) && !empty($deletePermissions))) {
    $js.= <<<JS
    art.dialog.top.$('.aui_buttons button').eq(2).attr("disabled",true);
JS;
}

$js .= <<<JS
    art.dialog.top.$('.aui_buttons button').eq(1).attr("disabled",true);
JS;

if (Yii::$app->request->get('edit')) {
    $js = '';
}
empty($js) || $this->registerJs($js);
?>
<div class="modal-window">
    <?= \home\widgets\Alert::widget(); ?>
    <?php
    $form = ActiveForm::begin([
            'id' => 'main-form',
            'options' => ['class' => 'informationform'],
            '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="12%"><?= $model->getAttributeLabel('title') ?>：</td>
                <?= $form->field($model, 'title', ['options' => ['width' => '40%']])->textInput(['class' => 'form-control']) ?>
                <td width="2%"><span class="redstar">*</span></td>

                <td align="right" width="13%"><?= $model->getAttributeLabel('schedule_time') ?>：</td>
                <?=
                DatePicker::widget(['model' => $model, 'form' => $form, 'format' => 'yyyy-M-d', 'attribute' => 'schedule_time', 'fieldConfig' => ['options' => ['width' => '28%']],
                    'inputConfig' => ['class' => 'form-control'], 'dateConfig' => ['maxDate' => '%y-%M-%d']])
                ?>
                <td width="5%"><span class="redstar">*</span></td>
            </tr>

            <tr>
                <?php if (empty($model->link_name)): ?>
                    <td align="right">关联：</td>
                    <td>
                        <select class="form-control link-select" name="Schedule[link_type]" style="width:30%; display: inline" >
                            <?php foreach (common\models\Common::getLinkTypes($linkTypeArray) as $type) : ?>
                                <option value="<?= $type['type'] ?>" <?= $model->link_type == $type['type'] ? 'selected="selected"' : '' ?> data-class="<?= $type['class'] ?>" data-url="<?=
                                Url::to([$type['url'][0],
                                    'type_permission' => 'create-blog', 'type_module' => 'blog'])
                                ?>" data-title="选择<?= $type['name'] ?>" ><?= $type['name'] ?></option>
                                    <?php endforeach; ?>
                        </select>
                        <input type="input" class="form-control " placeholder="点击选择" value="<?= $model->link_name ?>" data-hidden-id="link-id" data-switch="0" style="width: 68%; display: inline" />

                        <?= $form->field($model, 'link_id', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'link-id']) ?>
                    </td>
                <?php else: ?>
                    <td align="right"><?= \common\models\Common::getLinkTypeData($model->link_type)['name'] ?>：</td>
                    <td title="<?= $model->link_name ?>">
                        <?= StringHelper::truncate($model->link_name, 15) ?>
                        <?= $form->field($model, 'link_type', ['options' => ['tag' => 'div']])->hiddenInput() ?>
                        <?= $form->field($model, 'link_id', ['options' => ['tag' => 'div']])->hiddenInput() ?>
                    </td>
                <?php endif; ?>
                <td><span class="redstar"></span></td>


                <td align="right"><?= $model->getAttributeLabel('contacter_name') ?>：</td>
                <?=
                $form->field($model, 'contacter_name')->textInput(['class' => 'form-control select-multi-user', 'placeholder' => '单击选择', 'data-hidden-id' => 'contacter-id', 'data-url' => Url::to(['/link/contacter',
                        'customer_no' => $model->link_id, 'link_customer_no' => $model->link_customer_no]), 'data-title' => '选择联系人'])
                ?>
                <?= $form->field($model, 'contacter_id', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'contacter-id']) ?>
                <td><span class="c6"><span class="redstar"></span></span></td>


            </tr>

            <tr>
                <td align="right"><?= $model->getAttributeLabel('join_names') ?>：</td>
                <?=
                $form->field($model, 'join_names')->textInput(['class' => 'form-control select-multi-user', 'placeholder' => '请选择执行人', 'data-hidden-id' => 'joins-ids', 'data-url' => Url::to(['/link/multi-user']),
                    'data-title' => '选择执行人'])
                ?>
                <?= $form->field($model, 'join_ids', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'joins-ids']) ?>
                <td><span class="c6"><span class="redstar">*</span></span></td>

                <td align="right"><?= $model->getAttributeLabel('category_id') ?>：</td>
                <td>
                    <select class="form-control" name="Schedule[category_id]" >
                        <?= Category::getDropDownTree($model->category_id, 0, 'true', 'cat_id'); ?>
                    </select>
                </td>
                <td><span class="redstar">*</span></td>
            </tr>

            <tr>
                <td align="right"><?= $model->getAttributeLabel('content') ?>：</td>
                <?=
                Editor::widget(['model' => $model, 'form' => $form, 'attribute' => 'content', 'fieldConfig' => ['options' => ['colspan' => '4']], 'editorType' => 'simple', 'inputConfig' => ['class' => 'act-description'],
                    'width' => '100%', 'height' => 100])
                ?>
                <td><span class="redstar">*</span></td> 
            </tr>

            <tr>
                <td align="right"><?= $model->getAttributeLabel('attach_ids') ?>：</td>
                <td colspan="5"><?= Uploader::widget(['model' => $model, 'form' => $form, 'attribute' => 'attach_ids', 'type' => 'files']) ?></td>
            </tr>

        </tbody>
    </table>

    <?= ($model->isNewRecord) ? '' : Html::hiddenInput('id', $model->schedule_id); ?>
    <?php ActiveForm::end(); ?>
</div>
