<div class="col-md-12">
    <ul class="nav nav-pills">
        <li class="active"><a href="#clientDetail" data-toggle="tab">Client</a></li>
        <li><a href="#jobDetail" data-toggle="tab">Detail</a></li>
        <li><a href="#quoteDetail" data-toggle="tab">Quotation</a></li>
        <li><a href="#faultReport" data-toggle="tab">Fault Report</a></li>
        <li><a href="#materialUsed" data-toggle="tab">Materials</a></li>
        <li><a href="#histories" data-toggle="tab">Visits</a></li>
        <li><a href="#notes" data-toggle="tab">Note</a></li>
        <li><a href="#jobPurchases" data-toggle="tab">Purchase Order</a></li>
        <li><a href="#jobFilesTab" data-toggle="tab">Files</a></li>
    </ul>
    <form id="formUpdateJob" class="panel form-horizontal scrollPanel">
        <div class="tab-content">
            <div class="tab-pane active" id="clientDetail">
                <div class="panel-body">
                    <div class="col-md-6">
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Reference:</label>

                            <div class="col-md-8">
                                <input id="updateReference" type="text" name="reference" class="form-control"
                                       value="<?php echo e($job['reference']); ?>" placeholder=""/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">*Client:</label>

                            <div class="col-md-8">
                                <select id="updateClientSelectBox" name="client_id">
                                    <option></option>
                                    <?php foreach($clients as $client): ?>
                                        <option value="<?php echo e($client['id']); ?>" <?php echo e($client['id']==$job['clientId']?'selected':''); ?>><?php echo e($client['name']); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">*Building:</label>

                            <div class="col-md-8" id="updateBuildingSelectBoxContent">
                                <select id="updateBuildingSelectBox" name="building_id">
                                    <option></option>
                                    <?php foreach($buildings as $building): ?>
                                        <option value="<?php echo e($building['id']); ?>" <?php echo e($building['id']==$job['buildingId']?'selected':''); ?>><?php echo e($building['name']); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Client Reference:</label>

                            <div class="col-md-8">
                                <input id="updateClientReference" type="text" name="clientReference"
                                       class="form-control"
                                       value="<?php echo e($job['clientReference']); ?>" placeholder=""/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Address:</label>

                            <div class="col-md-8">
                                <textarea class="form-control" id="updateBuildingAddress" rows="6" disabled><?php echo e($currentBuilding['address1']); ?>

                                    , <?php echo e($currentBuilding['address2']); ?>

                                    , <?php echo e($currentBuilding['town']); ?> <?php echo e($currentBuilding['postcode']); ?></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Expect Start Time</label>

                            <div class="col-md-8">
                                <input type="text" class="form-control" id="updateExpectStartTime"
                                       name="expectStartTime"
                                       placeholder=""
                                       value="<?php echo e(date('d/m/Y H:i:s', strtotime($job['expectStartTime']))); ?>">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Expect Complete Time</label>

                            <div class="col-md-8">
                                <input type="text" class="form-control" id="updateExpectEndTime"
                                       name="expectEndTime"
                                       placeholder=""
                                       value="<?php echo e(date('d/m/Y H:i:s', strtotime($job['expectEndTime']))); ?>">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Exact Start Time</label>

                            <div class="col-md-8">
                                <input type="text" class="form-control" id="updateExactStartTime"
                                       name="exactStartTime"
                                       placeholder=""
                                       value="<?php echo e(date('d/m/Y H:i:s', strtotime($job['exactStartTime']))); ?>">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Exact Complete Time</label>

                            <div class="col-md-8">
                                <input type="text" class="form-control" id="updateExactEndTime"
                                       name="exactEndTime"
                                       placeholder=""
                                       value="<?php echo e(date('d/m/Y H:i:s', strtotime($job['exactEndTime']))); ?>">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Telephone:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateClientTelephone" class="form-control"
                                       value="<?php echo e($currentBuilding['telephone']); ?>" disabled/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Opening hours:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateClientOpeningHours" class="form-control"
                                       value="<?php echo e($currentBuilding['buildingOpenTime']); ?> - <?php echo e($currentBuilding['buildingCloseTime']); ?>"
                                       disabled/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Client contact:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateClientContact" class="form-control"
                                       value="<?php echo e($currentBuilding['buildingContact']); ?>" disabled/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane" id="jobDetail">
                <div class="panel-body">
                    <div class="col-md-6">
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Discipline:</label>

                            <div class="col-md-8">
                                <select id="updateDisciplineSelectBox" name="discipline">
                                    <option></option>
                                    <?php foreach($disciplines as $discipline): ?>
                                        <option value="<?php echo e($discipline['name']); ?>" <?php echo e($discipline['name']==$job['discipline']?'selected':''); ?>><?php echo e($discipline['name']); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Job type:</label>

                            <div class="col-md-8">
                                <select id="updateJobTypeSelectBox" name="jobType">
                                    <option></option>
                                    <?php foreach($customTypes as $customType): ?>
                                        <?php if($customType['type']=='Job Type'): ?>
                                            <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['jobType']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Out of hours call out?:</label>

                            <div class="col-md-8">
                                <select id="updatePayTypeSelectBox" name="payType">
                                    <option></option>
                                    <option value="callout" <?php echo e($job['payType']=='callout'?'selected':''); ?>>YES</option>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Previous Job:</label>

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

                                            (<?php echo e($aJob['clientReference']); ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">CAPEX No:</label>

                            <div class="col-md-8">
                                <input id="updateCapexNo" type="text" name="capexNo" class="form-control"
                                       value="<?php echo e($job['capexNo']); ?>" placeholder=""/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Response:</label>

                            <div class="col-md-8">
                                <select id="updateResponseTimeSelectBox" name="responseTime">
                                    <option></option>
                                    <?php if(DateTime::createFromFormat('Y-m-d', $job['responseTime']) !== FALSE): ?>
                                        <?php foreach($customTypes as $customType): ?>
                                            <?php if($customType['type']=='Response Time'): ?>
                                                <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']=='Dated'?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <?php foreach($customTypes as $customType): ?>
                                            <?php if($customType['type']=='Response Time'): ?>
                                                <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['responseTime']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label"></label>

                            <div class="col-md-8">
                                <input id="updateResponseDate" type="text" class="form-control" name="responseDate"
                                       placeholder="or select a particular date"
                                       value="<?php echo e(date('d/m/Y',strtotime($job['responseTime']))); ?>">
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Cause:</label>

                            <div class="col-md-8">
                                <select id="updateCauseSelectBox" name="cause">
                                    <option></option>
                                    <?php foreach($customTypes as $customType): ?>
                                        <?php if($customType['type']=='Cause Reason'): ?>
                                            <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['cause']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Assigned to:</label>

                            <div class="col-md-8">
                                <select id="updateAssignedToSelectBox" name="assignedTo" multiple="multiple">
                                    <?php foreach($engineers as $engineer): ?>
                                        <option value="<?php echo e($engineer['id']); ?>" <?php echo e(in_array($engineer['id'], $assignedToEngineers)?'selected':''); ?>><?php echo e($engineer['name']); ?>

                                            (<?php echo e($engineer['email']); ?>)
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Planner Date</label>

                            <div class="col-md-8">
                                <input type="text" class="form-control" id="updatePlannerTime"
                                       name="plannerTime"
                                       placeholder=""
                                       value="<?php echo e($job['plannerTime']?date('d/m/Y H:i:s', strtotime($job['plannerTime'])):''); ?>">
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Spend limit (&pound;):</label>

                            <div class="col-md-8">
                                <input id="updateSpendLimit" type="text" name="spendLimit" class="form-control"
                                       value="<?php echo e($job['spendLimit']); ?>"/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Date logged:</label>

                            <div class="col-md-8">
                                <input id="updateCreatedDate" type="text" name="updateCreatedDate" class="form-control"
                                       value="<?php echo e(date('d/m/Y H:i:s', strtotime($job['createdDate']))); ?>"/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Logged by:</label>

                            <div class="col-md-8">
                                <input id="updateCreatedBy" type="text" name="updateCreatedBy" class="form-control"
                                       value="<?php echo e($job['createdBy']); ?>"/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Status:</label>

                            <div class="col-md-8">
                                <select id="updateStatusSelectBox" name="status">
                                    <option></option>
                                    <?php foreach($customTypes as $customType): ?>
                                        <?php if($customType['type']=='Job Status'): ?>
                                            <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['status']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane" id="quoteDetail">
                <div class="panel-body">
                    <div class="col-md-6">
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Recall:</label>

                            <div class="col-md-8">
                                <select id="updateRecallSelectBox" name="recall">
                                    <option></option>
                                    <?php foreach($customTypes as $customType): ?>
                                        <?php if($customType['type']=='YesOrNo'): ?>
                                            <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['recall']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Warranty:</label>

                            <div class="col-md-8">
                                <select id="updateWarrantySelectBox" name="warranty">
                                    <option></option>
                                    <?php foreach($customTypes as $customType): ?>
                                        <?php if($customType['type']=='YesOrNo'): ?>
                                            <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['warranty']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Compliance:</label>

                            <div class="col-md-8">
                                <select id="updateComplianceSelectBox" name="compliance">
                                    <option></option>
                                    <?php foreach($customTypes as $customType): ?>
                                        <?php if($customType['type']=='YesOrNo'): ?>
                                            <option value="<?php echo e($customType['value']); ?>" <?php echo e($customType['value']==$job['compliance']?'selected':''); ?>><?php echo e($customType['value']); ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Operative Required:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateOperativeRequest" name="operativeRequest"
                                       class="form-control"
                                       value="<?php echo e($job['operativeRequest']); ?>"/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Quote Ref:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateQuoteReference" name="quoteReference" class="form-control"
                                       value="<?php echo e($job['quoteReference']); ?>"/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Quote order no:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateQuoteOrderNo" name="quoteOrderNo" class="form-control"
                                       value="<?php echo e($job['quoteOrderNo']); ?>"/>
                            </div>
                        </div>
                        <div class="row form-group">
                            <label class="col-md-4 control-label">Quote value:</label>

                            <div class="col-md-8">
                                <input type="text" id="updateQuoteValue" name="quoteValue" class="form-control"
                                       value="<?php echo e($job['quoteValue']); ?>"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane" id="faultReport">
                <div class="panel-body">
                    <div class="row form-group">
                        <h6><b>Fault report:</b></h6>

                        <div>
                            <textarea id="updateFaultReport" class="form-control" name="faultReport" id="faultReport"
                                      rows="10"><?php echo e($job['faultReport']); ?></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane" id="materialUsed">
                <div class="panel-body">
                    <div id="materialPanel"></div>
                </div>
            </div>
            <div class="tab-pane" id="histories">
                <div class="panel-body">
                    <div id="historyPanel"></div>
                </div>
            </div>
            <div class="tab-pane" id="notes">
                <div class="panel-body">
                    <div id="notePanel"></div>
                </div>
            </div>

            <div class="tab-pane" id="jobPurchases">
                <div class="panel-body">
                    <div class="col-md-12">
                        <div class="col-md-12 form-group">
                            <select id="serviceProviderSelect" data-placeholder="Select a contractor"
                                    class="form-control <?php echo e(Auth::user()['userType']=='serviceProvider' ? 'hidden' : ''); ?>">
                                <option></option>
                                <?php foreach($serviceProviders as $serviceProvider): ?>
                                    <option value="<?php echo e($serviceProvider['id']); ?>" <?php echo e((Auth::user()['userType']=='serviceProvider' && Auth::user()['userTypeId']==$serviceProvider['id']) ? 'selected' : ''); ?>><?php echo e($serviceProvider['shortName']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="col-md-4 form-group">
                            <select id="supplierSelect" data-placeholder="Select a supplier" class="form-control">
                                <option></option>
                                <?php foreach($suppliers as $supplier): ?>
                                    <option value="<?php echo e($supplier['id']); ?>"><?php echo e($supplier['name']); ?>

                                        :<?php echo e($supplier['branch']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4 form-group">
                            <select id="addPurchaseStatus" name="purchaseStatus" data-placeholder="Select a status"
                                    class="form-control">
                                <option></option>
                                <?php foreach($purchaseStatusOptions as $purchaseStatus): ?>
                                    <option value="<?php echo e($purchaseStatus['value']); ?>"><?php echo e($purchaseStatus['value']); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4 form-group">
                            <a class="btn btn-primary form-control" id="btnCreatePO">Create PO</a>
                        </div>
                    </div>

                    <div class="col-md-12" id="jobPurchaseList">
                    </div>
                </div>
            </div>

            <input type="hidden" id="updateJobId" name="id" value="<?php echo e($job['id']); ?>"/>
            <input type="hidden" id="updateToken" name="_token" value="<?php echo e(csrf_token()); ?>"/>

            <div class="tab-pane" id="jobFilesTab">
                <div id="jobFiles" class="dropzone-box hidden">
                    <div class="dz-default dz-message">
                        <i class="fa fa-cloud-upload"></i>
                        Drop files in here<br><span class="dz-text-small">or click to pick manually</span>
                    </div>
                    <form action="<?php echo e(URL::action('RnwController@uploadFile')); ?>" method="post" class="dropzone-box">
                        <div class="fallback">
                            <input name="aFile" type="file" multiple=""/>
                        </div>
                    </form>
                </div>
                <div>
                    <?php if(!empty($urlPath)): ?>
                        <iframe id="buildingFileList" class="no-border" style="width:100%; height:382px"
                                src="<?php echo e(url('filesys'.'?cd=/'.'&homeRepo='.$urlPath)); ?>"/>
                    <?php endif; ?>
                </div>
            </div>

            <div class="panel-footer text-right">
                <a id="delegateBtn" class="btn btn-info">Delegate</a>
                <div id="popover-head" class="hide">Delegate to ...</div>
                <div id="popover-content" class="hide">
                    <select class="form-control" id="addNotifyUserIds" multiple="multiple">
                        <?php foreach($engineers as $engineer): ?>
                            <option value="<?php echo e($engineer['id']); ?>"><?php echo e($engineer['name']); ?>

                                (<?php echo e($engineer['email']); ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <br/>
                    <textarea class="form-control" id="addNotifyContent"
                              placeholder="type any delegate note here..."></textarea>
                    <hr/>
                    <a id="addDelegateBtn" onclick="javascript:addUserNotification()" class="btn btn-info form-control">Confirm</a>
                </div>
                <a id="updateJobPageJobContact" class="btn btn-success">Update</a>
            </div>

        </div>
    </form>
</div>

<script type="text/javascript">
    $('#jobProfileId').html('<?php echo e($job["id"]); ?>');
    $('#delegateBtn').popover({
        html: true,
        placement: 'left',
        title: function () {
            return $("#popover-head").html();
        },
        content: function () {
            setTimeout(function () {
                $('#addNotifyUserIds').selectize();
            }, 382);
            return $("#popover-content").html();
        }
    });
    $('.scrollPanel').css('max-height', $(window).height() * 0.7);

    $('#updateResponseDate').datepicker({
        format: 'dd/mm/yyyy'
    });
    $('#updateExpectStartTime').datetimepicker({
        format: 'DD/MM/YYYY HH:mm:ss'
    });
    $('#updateExactStartTime').datetimepicker({
        format: 'DD/MM/YYYY HH:mm:ss'
    });
    $('#updateExpectEndTime').datetimepicker({
        format: 'DD/MM/YYYY HH:mm:ss'
    });
    $('#updateExactEndTime').datetimepicker({
        format: 'DD/MM/YYYY HH:mm:ss'
    });

    $('#updateCreatedDate').datetimepicker({
        format: 'DD/MM/YYYY HH:mm:ss'
    });

    $('#updatePlannerTime').datetimepicker({
        format: 'DD/MM/YYYY HH:mm:ss'
    });

    $("#updateClientSelectBox").selectize();
    $("#updateBuildingSelectBox").selectize();
    $("#updateDisciplineSelectBox").selectize();
    $("#updateJobTypeSelectBox").selectize();
    $("#updatePayTypeSelectBox").selectize();
    var updateResponseTimeSelectBox = $("#updateResponseTimeSelectBox").selectize();
    $("#updateRecallSelectBox").selectize();
    $("#updateWarrantySelectBox").selectize();
    $("#updateComplianceSelectBox").selectize();
    $("#updateCauseSelectBox").selectize();
    var updateAssignedToSelectBox = $("#updateAssignedToSelectBox").selectize();
    $("#updatePreviousJobSelectBox").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 updateStatusSelectBox = $("#updateStatusSelectBox").selectize();

    $('#supplierSelect').selectize();
    $('#addPurchaseStatus').selectize();
    $('#serviceProviderSelect').selectize();

    $("#updateAssignedToSelectBox").change(function () {
        if ($('#updateAssignedToSelectBox').val()) {
            if ($('#updateStatusSelectBox').val() == 'ToBeAssigned') {
                updateStatusSelectBox[0].selectize.setValue('Assigned');
            }
        } else {
            updateStatusSelectBox[0].selectize.setValue('ToBeAssigned');
        }
    });

    //    $("#updateStatusSelectBox").change(function () {
    //        if ($('#updateStatusSelectBox').val() == 'ToBeAssigned' || $('#updateStatusSelectBox').val() == 'Assigned' || $('#updateStatusSelectBox').val() == 'Cancelled') {
    //        } else {
    //            updateStatusSelectBox[0].selectize.setValue('');
    //        }
    //    });

    $('#updateResponseDate').change(function () {
        updateResponseTimeSelectBox[0].selectize.setValue('Dated');
    });

    $('#updateClientSelectBox').change(function () {
        $.post('<?php echo e(URL::action("JobController@filterClientBuilding")); ?>', {
            client_id: $(this).val(),
            isUpdate: 'true',
            job_id: '<?php echo e($job["id"]); ?>',
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            $('#updateBuildingSelectBoxContent').html(data);
        });
    });

    if ($('#updateClientSelectBox').val() != '') {
        $.post('<?php echo e(URL::action("JobController@filterClientBuilding")); ?>', {
            client_id: $('#updateClientSelectBox').val(),
            isUpdate: 'true',
            job_id: '<?php echo e($job["id"]); ?>',
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            $('#updateBuildingSelectBoxContent').html(data);
        });
    }

    if ('#JobBuildingsTable') {
        var jobBuildingsTable =
                $('#JobBuildingsTable')
                        .dataTable({
                            bAutoWidth: false,
                            "aoColumns": [
                                {"bSortable": true},
                                {"bSortable": true},
                                {"bSortable": false},
                                {"bSortable": false}
                            ],
                            "aaSorting": [],
                            "iDisplayLength": 10
                        });
        $('#JobBuildingsTable_wrapper .table-caption').text('Buildings');
        $('#JobBuildingsTable_wrapper .dataTables_filter input').attr('placeholder', 'Search...');
    }

    function addUserNotification() {
        loadingOverlay();
        $.post('<?php echo e(URL::action("NotificationController@addNotificationByJobDelegate")); ?>', {
            jobId: $('#updateJobId').val(),
            notifyUserId: $('#addNotifyUserIds').val(),
            notifyContent: $('#addNotifyContent').val(),
            additionalMessage: '(from job page delegate)',
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            loadingRemove();
            new PNotify({
                title: 'Success',
                text: 'Delegate successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
        }).fail(function (data) {
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'Delegate failed!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    }

    // Actions on Job Page
    $('a[id^="updateJobPage"]').click(function () {
        var ahref = $(this);
        ahref.text('loading');
        loadingOverlay();
        $.post('<?php echo e(URL::action("JobController@uiUpdate")); ?>', {
            id: $('#updateJobId').val(),
            _token: $('#updateToken').val(),
            reference: $('#updateReference').val(),
            client_id: $('#updateClientSelectBox').val(),
            building_id: $('#updateBuildingSelectBox').val(),
            capexNo: $('#updateCapexNo').val(),
            clientReference: $('#updateClientReference').val(),
            faultReport: $('#updateFaultReport').val(),
            cause: $('#updateCauseSelectBox').val(),
            assignedTo: $('#updateAssignedToSelectBox').val(),
            spendLimit: $('#updateSpendLimit').val(),
            jobType: $('#updateJobTypeSelectBox').val(),
            discipline: $('#updateDisciplineSelectBox').val(),
            responseTime: $('#updateResponseTimeSelectBox').val(),
            responseDate: changeDateFormatToISO($('#updateResponseDate').val()),
            recall: $('#updateRecallSelectBox').val(),
            warranty: $('#updateWarrantySelectBox').val(),
            compliance: $('#updateComplianceSelectBox').val(),
            operativeRequest: $('#updateOperativeRequest').val(),
            quoteOrderNo: $('#updateQuoteOrderNo').val(),
            quoteValue: $('#updateQuoteValue').val(),
            parentJob: $('#updatePreviousJobSelectBox').val(),
            expectStartTime: changeDateFormat($('#updateExpectStartTime').val()),
            expectEndTime: changeDateFormat($('#updateExpectEndTime').val()),
            exactStartTime: changeDateFormat($('#updateExactStartTime').val()),
            exactEndTime: changeDateFormat($('#updateExactEndTime').val()),
            createdDate: changeDateFormat($('#updateCreatedDate').val()),
            plannerTime: changeDateFormat($('#updatePlannerTime').val()),
            payType: $('#updatePayTypeSelectBox').val(),
            status: $('#updateStatusSelectBox').val()
        }).done(function (data) {
            ahref.text('Update');

            if (typeof refreshJobs == 'function') {
                //function_name is a function
                refreshJobs();
            }

            loadingRemove();
            new PNotify({
                title: 'Success',
                text: 'Job updated successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
        }).fail(function (data) {
            ahref.text('Update');
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'Job updating failed!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    });

    var jobDropzone = new Dropzone('div#jobFiles', {
        url: '<?php echo e(URL::action("RnwController@uploadFile")); ?>',
        paramName: "aFile", // The name that will be used to transfer the file
        maxFilesize: 5, // MB

        addRemoveLinks: true,
        dictResponseError: "Can't upload file!",
        autoProcessQueue: true,
        thumbnailWidth: 138,
        thumbnailHeight: 120,

        previewTemplate: '<div class="dz-preview dz-file-preview"><div class="dz-details"><div class="dz-filename"><span data-dz-name></span></div><div class="dz-size">File size: <span data-dz-size></span></div><div class="dz-thumbnail-wrapper"><div class="dz-thumbnail"><img data-dz-thumbnail><span class="dz-nopreview">No preview</span><div class="dz-success-mark"><i class="fa fa-check-circle-o"></i></div><div class="dz-error-mark"><i class="fa fa-times-circle-o"></i></div><div class="dz-error-message"><span data-dz-errormessage></span></div></div></div></div><div class="progress progress-striped active"><div class="progress-bar progress-bar-success" data-dz-uploadprogress></div></div></div>',

        resize: function (file) {
            var info = {srcX: 0, srcY: 0, srcWidth: file.width, srcHeight: file.height},
                    srcRatio = file.width / file.height;
            if (file.height > this.options.thumbnailHeight || file.width > this.options.thumbnailWidth) {
                info.trgHeight = this.options.thumbnailHeight;
                info.trgWidth = info.trgHeight * srcRatio;
                if (info.trgWidth > this.options.thumbnailWidth) {
                    info.trgWidth = this.options.thumbnailWidth;
                    info.trgHeight = info.trgWidth / srcRatio;
                }
            } else {
                info.trgHeight = file.height;
                info.trgWidth = file.width;
            }
            return info;
        }
    });

    jobDropzone.on("sending", function (file, xhr, formData) {
                formData.append("_token", '<?php echo e(csrf_token()); ?>');
                formData.append("destinationPath", '<?php echo e($filePath); ?>');
            }
    );

    jobDropzone.on("complete", function () {
        $('#clientFileList').attr('src', "<?php echo e(url('filesys'.'?cd=/'.'&homeRepo='.$urlPath)); ?>");
    });

    $('#btnCreatePO').click(function () {
        loadingOverlay();
        $.post('<?php echo e(URL::action("JobController@createPurchaseOrder")); ?>', {
            jobId: '<?php echo e($job['id']); ?>',
            supplierId: $('#supplierSelect').val(),
            invoiceStatus: $('#addPurchaseStatus').val(),
            serviceProviderId: $('#serviceProviderSelect').val(),
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            loadingRemove();
            new PNotify({
                title: 'Success',
                text: 'Purchase order created successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
            generateJobPurchases('<?php echo e(URL::action("JobController@generateJobPurchases")); ?>', '<?php echo e($job['id']); ?>', '<?php echo e(csrf_token()); ?>');
        }).fail(function (data) {
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'Purchase order creating failed!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    });

    generateMaterialsList('<?php echo e(URL::action("JobController@generateMaterialsList")); ?>', '<?php echo e($job['id']); ?>', '<?php echo e(csrf_token()); ?>');
    generateHistoryList('<?php echo e(URL::action("JobController@generateHistoryList")); ?>', '<?php echo e($job['id']); ?>', '<?php echo e(csrf_token()); ?>');
    generateJobPurchases('<?php echo e(URL::action("JobController@generateJobPurchases")); ?>', '<?php echo e($job['id']); ?>', '<?php echo e(csrf_token()); ?>');
    generateNoteList('<?php echo e(URL::action("JobController@generateNoteList")); ?>', '<?php echo e($job['id']); ?>', '<?php echo e(csrf_token()); ?>');
</script>