<?php 
use yii\helpers\{ArrayHelper, Html, Json, Url};
use yii\bootstrap4\Modal;
use yii\db\Query;
use yii\web\JsExpression;

use kartik\widgets\{ActiveForm, ActiveField, FileInput};
use kartik\date\DatePicker;
use kartik\datetime\DateTimePicker;
use kartik\select2\Select2;

use common\widgets\TinyMce;
use backend\models\Attributesvalues;
use modules\custom\models\{Accounts, Contacts};
use frontend\models\{Dtwps, Settings, Uploads, User};

$model->sdate = empty($model->sdate) || $model->sdate == '01/01/1970 01:00' ? null : $model->sdate;
$model->edate = empty($model->edate) || $model->edate == '01/01/1970 01:00' ? null : $model->edate;
$settings = Settings::find()->one();

?>

<?= $form->field($model, 'inline')->hiddenInput([
    'id' => 'inline',
    'value' => "$settings->address1, $settings->address2, $settings->town, $settings->county, $settings->postcode"
])->label(false) ?>

<?= $form->field($model, 'prevjobno')->hiddenInput(['id' => 'prevjobno'])->label(false) ?>

<script>
function getRoute() {
    dest = $('#postcode').val();
    $.ajax({
        type: 'get',
        url: '/activities/getroute',
        data: {
            id: "<?= $model->id ?>"
        },
        success: function(result){
            $('#routejsond').html(result);
        }
    });
}
</script>
<div class="row">
    <div class="col-12 col-md-4">
        <div class="card">
            <div class="card-header">
                Time
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, "sdate")->widget(DateTimePicker::class, [
                            'id' => 'starttime',
                            'pluginOptions' => [
                                'showMeridian' => false,
                                'format' => 'dd/mm/yyyy hh:ii'
                            ]
                        ])->label('Start') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'shiftlength')->textInput([
                            'id' => 'shiftlength',
                            'onchange' => 'shift()'
                        ])->label('Shift Length') ?>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, "edate")->widget(DateTimePicker::class, [
                            'id' => 'finishtime',
                            'pluginOptions' => [
                                'showMeridian' => false,
                                'format' => 'dd/mm/yyyy hh:ii'
                            ]
                        ])->label('Finish') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'day')->textInput(['id' => 'day'])->label('Day') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'shift')->dropDownList(
                            Attributesvalues::formArray('shift'),
                            ['prompt' => 'Please Select']
                        )->label('Shift <span class="fa fa-plus btn-attributes float-right" data-value="shift"></span>') ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="col-12 col-md-4">
        <div class="card">
            <div class="card-header">
                Travel
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'location')->textInput(['id' => 'location'])->label('Location') ?>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'accesspoint')->textInput()->label('Access Point') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-6">
                        <?= $form->field($model, 'googletime')->textInput([
                            'disabled' => true
                        ])->label('Google Time') ?>
                    </div>
                    <div class="col-6">
                        <?= $form->field($model, 'googledistance')->textInput([
                            'disabled' => true
                        ])->label('Google Distance from Office') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'journey')->dropDownList(
                            Attributesvalues::formarray('journey'),
                            [
                                'id' => 'journey',
                                'onchange' => 'calculateDistances()',
                                'prompt' => 'Please Select'
                            ]
                        )->label('Journey Type <span class="fa fa-plus btn-attributes float-right" data-value="journey"></span>') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12">
                        <?= $form->field($model, 'travelstarttime')->textInput([
                            'id' => 'totalshiftlength'
                        ])->label('Total Shift Length') ?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12 col-md-6">
                        <?= $form->field($model, 'actualshifttime')->textInput([
                            'disabled' => true
                        ])->label() ?>
                    </div>
                    <div class="col-12 col-md-6">
                        <?= $form->field($model, 'predictedshifttime')->textInput([
                            'disabled' => true
                        ])->label('Operatives - Total Time') ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php if(empty($model->type) || $model->type == 'welding'){ ?>
        <div class="col-12 col-md-4">
            <div class="card">
                <div class="card-header">
                    Job Details
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-12">
                            <?= $form->field($model, 'elr')->textInput(['id' => 'elr'])  ->label('ELR') ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-4">
                            <?= $form->field($model, 'noofshifts')->textInput(['id' => 'noofshifts'])  ->label('Number of Shifts') ?>
                        </div>
                        <div class="col-4">
                            <?= $form->field($model, 'noofteams')->textInput(['id' => 'noofteams'])  ->label('Number of Teams') ?>
                        </div>
                        <div class="col-4">
                            <?= $form->field($model, 'noofwelds')->textInput(['id' => 'noofwelds'])  ->label('Number of Welds') ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12">
                            <?= $form->field($model, 'weekno')->textInput(['id' => 'weekno'])  ->label('Week Number') ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12">
                            <?= $form->field($model, 'customerpono')->textInput(['id' => 'customerpo'])  ->label('Purchase Order Number') ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12">
                            <?= $form->field($model, 'ordertitle')->textInput([])  ->label('Job Costing No (Client)') ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12">
                            <?= $form->field($model, 'travelfinishtime')->textInput(['id' => 'travelfinishtime'])  ->label('Issuing Depot') ?>
                        </div>
                    </div>
                </div>
            </div>   
        </div>
    <?php } elseif($model->type == 'administration') {?>
        <div class="col-12 col-md-4">
        <?= $this->render('_administration', [
                'dataModel' => $model->data['Administration'],
                'model' => $model,
                'form' => $form
            ])?>
        </div>
    <?php } elseif($model->type == 'training') {?>
        <div class="col-12 col-md-4">
        <?= $this->render('_training', [
                'dataModel' => $model->data['Training'],
                'model' => $model,
                'form' => $form
            ])?>
        </div>
    <?php } ?>
</div>
<?php if (empty($model->type) || $model->type == 'welding') {?>
<div class="row mt-3">
    <div class="col-12 col-md-12">
            <?=  $this->render('_welding', [
                'model' => $model,
                'users' => $users,
                'form' => $form
            ]) ?>
        </div>
    </div>
<?php } ?>
<div class="row mt-3">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                Job Status
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-6 text-center">
                        <?= $form->field($model, 'jobstatus')->dropDownList(Attributesvalues::formarray('jobstatus'), ['prompt' => 'Please Select']) ->label('Job Status <span class="fa fa-plus btn-attributes float-right" data-value="jobstatus"></span>')?>
                    </div>
                    <div class="col-6 text-center">
                        <?= $form->field($model, 'billingstatus')->dropDownList(Attributesvalues::formarray('billingstatus'), ['prompt' => 'Please Select']) ->label('Billing Status <span class="fa fa-plus btn-attributes float-right" data-value="billingstatus"></span>')?>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-12 col-md-3">
                        <label class="control-label">Created by:</label> <?= $model->createdby_name ?>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="control-label">Created At:</label> <?= $model->created_at ?>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="control-label">Updated by:</label> <?= $model->updatedby_name ?>
                    </div>
                    <div class="col-12 col-md-3">
                        <label class="control-label">Updated At:</label> <?= $model->updated_at ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="routejsond"></div>