<?php

use home\assets\MainAsset;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use home\modules\contract\models\Contract;
use home\modules\contract\models\Category;
use common\widgets\DatePicker;
use common\widgets\Uploader;
use common\widgets\Editor;
use home\models\PaymentMethod;
use common\assets\My97DatePickerAsset;

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

$attrLabels = $model->attributeLabels();
$form = ActiveForm::begin([
            'id' => 'main-form',
            'options' => ['class' => 'informationform f14'],
            '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="120"><?= $model->getAttributeLabel('name') ?>：</td>
            <?= $form->field($model, 'name', ['options' => ['width' => '30%']])->textInput(['class' => 'form-control']) ?>
            <td><span class="redstar">*</span></td> <!--如果不需要*号,可以删除*号，请勿删除td标签,以此保留原来的格式-->

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

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

            <td align="right"><?= $model->getAttributeLabel('hardware_amount') ?>：</td>
            <?= $form->field($model, 'hardware_amount')->textInput(['class' => 'form-control']) ?>
            <td><span class="redstar"></span></td>
        </tr>
        <tr>
            <td align="right" ><?= $model->getAttributeLabel('real_customer_name') ?>：</td>
            <?=
            $form->field($model, 'real_customer_name')->textInput(['class' => 'form-control select-customer', 'placeholder' => '单击选择', 'data-hidden-id' => 'real_customer-no', 'data-url' => Url::to(['/link/customer']),
                'data-title' => '选择真实客户'])
            ?>
            <?= $form->field($model, 'real_customer_no', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'real_customer-no']) ?>
            <td><span class="redstar"></span></td>

            <td align="right" ><?= $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' => '选择签约客户'])
            ?>
            <?= $form->field($model, 'customer_no', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'customer-no']) ?>
            <td><span class="redstar">*</span></td>

        </tr>
        <tr>
            <td align="right"><?= $model->getAttributeLabel('signing_time') ?>：</td>
            <?=
            DatePicker::widget(['model' => $model, 'form' => $form, 'attribute' => 'signing_time', 'inputConfig' => ['class' => 'form-control', 'id' => 'signing-time'],
                'format' => 'yyyy-MM-dd',])
            ?>
            <td><span class="redstar">*</span></td>

            <td align="right"><?= $model->getAttributeLabel('due_time') ?>：</td>
            <?= DatePicker::widget(['model' => $model, 'form' => $form, 'attribute' => 'due_time', 'inputConfig' => ['class' => 'form-control', 'id' => 'due-time'], 'format' => 'yyyy-MM-dd',]) ?>
            <td><span class="redstar">*</span></td>
        </tr>

        <tr>
            <td align="right"><?= $model->getAttributeLabel('acceptance_time') ?>：</td>

            <td class="field-contract-acceptance_time">
                <input type="text" id="contract-acceptance_time" class="form-control" name="Contract[acceptance_time]" onfocus="WdatePicker({'readOnly': false, 'dateFmt': 'yyyy-MM-dd', dchanging: changeBeginTime, Mchanging: changeBeginTime, ychanging: changeBeginTime, dchanged: changeBeginTime, Mchanged: changeBeginTime, ychanged: changeBeginTime})" value="<?= $model->acceptance_time ?>"/><div class="error"></div>
            </td>

            <td><span class="redstar">*</span></td>

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

        <tr>
            <td align="right"><?= $model->getAttributeLabel('begin_time') ?>：</td>
            <?= $form->field($model, 'begin_time')->textInput(['class' => 'form-control', 'readonly' => true]) ?>

            <td><span class="redstar"></span></td>

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

        <tr>
            <td align="right"><?= $model->getAttributeLabel('category_id') ?>：</td>
            <td>
                <select class="form-control" name="Contract[category_id]" id="category-parent-id">
                    <?php
                    echo Category::getDropDownTree($model['category_id']);
                    ?>
                </select>
                <div class="error"></div>
            </td>
            <td><span class="redstar"></span></td>

            <td align="right"><?= $model->getAttributeLabel('status_id') ?>：</td>
            <?= $form->field($model, 'status_id')->dropDownList(Contract::getContractStatusList(), ['class' => 'form-control']) ?>
            <td><span class="redstar">*</span></td>
        </tr>

        <tr>
            <td align="right"><?= $model->getAttributeLabel('collate_status') ?>：</td>
            <?= $form->field($model, 'collate_status')->radioList(Contract::getCollateList(), ['itemOptions' => ['labelOptions' => ['class' => 'radio-inline']]]) ?>
            <td></td>
            <td align="right"><?= $model->getAttributeLabel('owner_id') ?>：</td>
            <?=
            $form->field($model, 'owner_name')->textInput(['class' => 'form-control select-single-user', 'readonly' => true, 'placeholder' => '单击选择', 'data-hidden-id' => 'owner-id', 'data-url' => Url::to(['/link/single-user']),
                'data-title' => '选择合同所有人'])
            ?>
            <?= $form->field($model, 'owner_id', ['options' => ['tag' => 'div']])->hiddenInput(['id' => 'owner-id']) ?>
            <td><span class="redstar">*</span></td>
        </tr>

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

            <td></td>
        </tr>

        <tr>
            <td align="right"><?= $model->getAttributeLabel('payment_instruction') ?>：</td>
            <td colspan="5">
                <table class="table task-table payment_instruction" id="payment_instruction-table" style="width:97%" align="left">
                    <tbody>
                        <tr class="0">
                            <th width="10%">付款批次</th>
                            <th width="12%" colspan="2">付款比例</th>
                            <th width="20%">付款金额</th>
                            <th>备注</th>
                            <th width="8%">操作</th>
                        </tr>
                        <?php if(empty($model->paymentInstruction)) : ?>
                            <tr class="1 ">
                                <td><input class="form-control sort"  name="instruction[1][sort]" value="1"/></td>
                                <td><input class="form-control percentage"  name="instruction[1][percentage]" value='0'/></td><td>%</td>
                                <td><input class="form-control sum"  name="instruction[1][sum]" readonly="true" value='0'/></td>
                                <td><input class="form-control remark"  name="instruction[1][remark]" /></td>
                                <td><a href='javascript:;' class="delete-tr" >删除</a></td>
                            </tr>
                        <?php else : ?>
                            <?php foreach ($model->paymentInstruction as $instruction) : ?>
                                <tr class="<?= $instruction['sort'] ?>">
                                    <td><input class="form-control sort"  name="instruction[<?= $instruction['sort'] ?>][sort]" value="<?= $instruction['sort'] ?>"/></td>
                                    <td><input class="form-control percentage"  name="instruction[<?= $instruction['sort'] ?>][percentage]" value='<?= $instruction['percentage'] ?>'/></td><td>%</td>
                                    <td><input class="form-control sum"  name="instruction[<?= $instruction['sort'] ?>][sum]" readonly="true" value='<?= $instruction['sum'] ?>'/></td>
                                    <td><input class="form-control remark"  name="instruction[<?= $instruction['sort'] ?>][remark]" value='<?= $instruction['remark'] ?>'/></td>
                                    <td><a href='javascript:;' class="delete-tr" >删除</a></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                        <tr >
                            <td colspan="6" id="all-scope-implement">
                                付款总比例：<span id="total-percentage"><?= '0.00' ?></span>%&nbsp;&nbsp;总额：<span id="sumMoney"><?= '0.00' ?></span>元&nbsp;&nbsp;剩余：<span id="surplus"><?= '0.00' ?></span>%

                            </td>
                        </tr>
                    </tbody>
                </table>
                <p style="text-align:right; padding-right:30px;">
                    <button type="button" class="btn btn-default btn-task add-payment-instruction">
                        <i class="icon-plus"></i>
                    </button>
                </p>
            </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['contract_id']); ?>

<script>
    var messageUrl = "<?= Url::to([$this->context->id . '/get-customer-message']) ?>";
    var changeTimeUrl = "<?= Url::to([$this->context->id . '/get-maint-period']) ?>"
</script>

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