<div id="previousJobsWidget" class="panel widget-notifications">
    <div class="panel-body padding-sm">
        <?php foreach($jobs as $aJob): ?>
            <div class="notifications-list">
                <div class="notification">
                    <div class="notification-title">
                        <a class="btn btn-xs btn-success edit-job" data-url="<?php echo e(URL::action("JobController@generateJobDetail")); ?>" data-id="<?php echo e($aJob['id']); ?>"><?php echo e($aJob['reference']); ?></a>
                    </div>
                    <div class="notification-description"><strong>Created</strong>: <?php echo e(date('d/m/Y H:i', strtotime($aJob['createdDate']))); ?></div>
                    <div class="notification-description"><strong>Discipline</strong>: <?php echo e($aJob['discipline']); ?></div>
                    <div class="notification-description"><strong>Response Time</strong>: <?php echo e(\DateTime::createFromFormat('Y-m-d', $aJob['responseTime']) !== FALSE ? date('d/m/Y', strtotime($aJob['responseTime'])) : $aJob['responseTime']); ?></div>
                    <div class="notification-description"><strong>Fault Report</strong>: <?php echo e($aJob['faultReport']); ?></div>
                    <?php if(!empty($aJob->lastVisit)): ?>
                        <div class="notification-description"><strong>Last engineer attended</strong>: <?php echo e(date('d/m/Y H:i', strtotime($aJob->lastVisit['checkoutTime']))); ?></div>
                        <div class="notification-description"><strong>Last engineer job notes</strong>: <?php echo e($aJob->lastVisit['note']); ?></div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

<select id="addPreviousJobSelectBox" name="parentJob">
    <option></option>
    <?php foreach($jobs as $job): ?>
        <option value="<?php echo e($job['id']); ?>" data-data='<?php echo e(json_encode(['discipline' => $job['discipline'], 'responseTime' => \DateTime::createFromFormat('Y-m-d', $job['responseTime']) !== FALSE ? date('d/m/Y', strtotime($job['responseTime'])) : $job['responseTime'], 'faultReport' => $job['faultReport']])); ?>'><?php echo e($job['reference']); ?>

            (<?php echo e($job['clientReference']); ?>)
        </option>
    <?php endforeach; ?>
</select>
<div id="addPreviousJobDetail">
    <a class="btn btn-xs btn-success col-md-12 previous-job-info-update edit-job" data-url="<?php echo e(URL::action("JobController@generateJobDetail")); ?>" style="display:none"></a>
</div>
<script type="text/javascript">

    $('#addPreviousJobDetail, #previousJobsWidget').on('click', 'a.edit-job', function () {
        generateJobDetail($(this).data("url"), $(this).data("id"), $_token)
    });

    if ($('#addPreviousJobSelectBox').length > 0) {
        $("#addPreviousJobSelectBox").selectize({
            render: {
                item: function(item) {
                    return '<div>' + item.text + '</div>';
                },
                option: function(item) {
                    return '<div class="previous-job-option">' +
                            '<div class="pjo-text">' + item.text + '</div>' +
                            '<div class="pjo-details"><span>Discipline: ' + item.discipline + '</span><span>Response: ' + item.responseTime + '</span>' + (item.faultReport.length ? '<span class="full">Fault Report: ' + item.faultReport + '</span>' : '') + '</div>' +
//                            '<div class="pjo-details"><span>Discipline: ' + item.discipline + '</span><span>Response: ' + item.responseTime + '</span><span>Recall: ' + item.recall + '</span><span>Warranty: ' + item.warranty + '</span><span>Compliance: ' + item.compliance + '</span><span>Pay type: ' + item.payType + '</span></div>' +
                            '</div>';
                }
            }
        });

        var previousJob = $("#addPreviousJobSelectBox")[0].selectize.getValue();

        if (previousJob) {
            $('.previous-job-info-update').text(previousJob + ' - See details').data('id', previousJob).show();
        }

        $("#addPreviousJobSelectBox").change(function(){
            previousJob = $(this)[0].selectize.getValue();

            $('.previous-job-info-update').text(previousJob + ' - See details').data('id', previousJob).show();
        });
    }

    $('#buildingAddress').val("<?php echo e($building['address1']!=='' && $building['postcode']!==''?$building['address1'] . ', ' . $building['address2'] . ', ' . $building['town'] . ' ' . $building['postcode']: $client['address1'] . ', ' . $client['address2'] . ', ' . $client['town'] . ' ' . $client['postcode']); ?>");

    $('#clientTelephone').val("<?php echo e($building['telephone']!==''?$building['telephone']:$client['telephone']); ?>");
    $('#clientOpeningHours').val("<?php echo e($building['buildingOpenTime']!==''?$building['buildingOpenTime'] . '-' . $building['buildingCloseTime']:$client['openingHours']); ?>");
    $('#clientContact').val("<?php echo e($building['buildingContact']!==''?$building['buildingContact']:$client['clientContact']); ?>");

</script>