<div class="col-md-12">
    <div class="col-md-12">
        <div class="row form-group">
            <select id="addQuotationJob" class="form-control" placeholder="- Select a job">
                <option></option>
                <?php foreach($jobs as $job): ?>
                    <option value="<?php echo e($job['id']); ?>"><?php echo e($job['reference']); ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <div class="col-md-6 ">
        <div class="row form-group <?php echo e(Auth::user()['userType']=='serviceProvider'?'hidden':''); ?>">
            <label class="col-md-4 control-label">Contractor:</label>

            <div class="col-md-8">
                <select id="addJobServiceProvider" placeholder="Select a contractor"
                        class="form-control">
                </select>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Customer Ref:</label>

            <div class="col-md-8">
                <input type="text" id="addCustomerRef" class="form-control"/>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Quotation Date:</label>

            <div class="col-md-8">
                <input type="text" id="addQuotationDate"
                       value="<?php echo e(date('d/m/Y', strtotime('now'))); ?>" class="form-control"/>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Quotation Type:</label>

            <div class="col-md-8">
                <select id="addQuotationType" class="form-control">
                    <option></option>
                    <?php foreach($quotationTypes as $quotationType): ?>
                        <option value="<?php echo e($quotationType['value']); ?>"><?php echo e($quotationType['value']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="row form-group">
            <label class="col-md-4 control-label">Lead-in Time (days):</label>

            <div class="col-md-8">
                <input type="text" id="addLeadInTime" class="form-control"/>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Work duration (days):</label>

            <div class="col-md-8">
                <input type="text" id="addWorkDuration" class="form-control"/>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Discipline:</label>

            <div class="col-md-8">
                <select id="addDiscipline" class="form-control">
                    <option></option>
                    <?php foreach($disciplines as $discipline): ?>
                        <option value="<?php echo e($discipline['name']); ?>"><?php echo e($discipline['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </div>
</div>
<div class="col-md-12">
    <hr/>
</div>
<div class="col-md-12">
    <div class="col-md-6">
        <div class="row form-group">
            <label class="col-md-4 control-label">Client Name:</label>

            <div class="col-md-8">
                <input type="text" id="addClientName" class="form-control"/>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Building Name:</label>

            <div class="col-md-8">
                <input type="text" id="addSiteName" class="form-control"/>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">Building address:</label>

            <div class="col-md-8">
                <textarea type="text" id="addSiteAddress" rows="4" class="form-control"></textarea>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="row form-group">
            <label class="col-md-4 control-label">Out of hours work required:</label>

            <div class="col-md-8">
                <select id="addOutOfHoursWork">
                    <option></option>
                    <?php foreach($yesOrNos as $yesOrNo): ?>
                        <option value="<?php echo e($yesOrNo['value']); ?>"><?php echo e($yesOrNo['value']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="row form-group">
            <label class="col-md-4 control-label">We require another discipline contractor for
                these works:</label>

            <div class="col-md-8">
                <select id="addDisciplineContractor">
                    <option></option>
                    <?php foreach($yesOrNos as $yesOrNo): ?>
                        <option value="<?php echo e($yesOrNo['value']); ?>"><?php echo e($yesOrNo['value']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </div>
</div>
<div class="col-md-12">
    <hr/>
</div>
<div class="col-md-12">
    <div class="row control-label form-group">
        <h4>Work detail</h4>
        <textarea id="addWorkDetail" rows="10" class="form-control"></textarea>
    </div>
    <hr/>
</div>
<div class="col-md-12">
    <div class="row control-label form-group">
        <h4>Quotation Detail</h4>
        <ul class="nav nav-pills">
            <li class="active"><a href="#engineerQuotationPanel" data-toggle="tab">Labour</a>
            </li>
            <li><a href="#materialQuotationPanel" data-toggle="tab">Materials</a></li>
            <li><a href="#otherQuotationPanel" data-toggle="tab">Others</a></li>
        </ul>
        <div class="tab-content">
            <div id="engineerQuotationPanel" class="tab-pane active">
                <div class="col-md-4">
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Engineer Qty:</label>

                        <div class="col-md-8">
                            <input type="text" id="addEngineerQuantity" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Hourly Rate:</label>

                        <div class="col-md-8">
                            <input type="text" id="addDisciplineHourlyRate" class="form-control"
                                   value="0"/>
                            <input type="hidden" id="addDisciplineCallOut" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Hours per engineer:</label>

                        <div class="col-md-8">
                            <input type="text" id="addEngineerHours" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Line Total:</label>

                        <div class="col-md-8">
                            <input type="text" id="addEngineerPriceTotal" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Description:</label>

                        <div class="col-md-8">
                                                <textarea id="addEngineerPriceDescription"
                                                          class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-8">
                            <select id="addCallOut" data-placeholder="Select an option">
                                <option></option>
                                <option value="callOut">Call out applied</option>
                                <?php /*<option value="arrivalFee">Arrival fee applied</option>*/ ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <a class="btn btn-primary form-control" id="addQuotationEngineerButton"><i
                                        class="fa fa-plus"></i>&nbsp;ADD</a>
                        </div>
                    </div>
                </div>
                <div class="col-md-8" id="quotationItemEngineerList">
                </div>
                <div class="col-md-4"></div>
                <div class="row form-group col-md-8">
                    <label class="col-md-4 control-label text-right">Sub total:</label>

                    <div class="col-md-8">
                        <input type="text" id="addLabourPriceTotal" class="form-control"
                               value="0"/>
                    </div>
                </div>
            </div>
            <div id="materialQuotationPanel" class="tab-pane">
                <div class="col-md-4">
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Material Qty:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationMaterialQuantity"
                                   class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Name:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationMaterialName" class="form-control"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Part no:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationMaterialPartNo" class="form-control"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Unit price:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationMaterialPrice" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Mark up %:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationMaterialMargin" class="form-control"
                                   value="15"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Description:</label>

                        <div class="col-md-8">
                                                <textarea id="addQuotationMaterialDescription"
                                                          class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Line Total:</label>

                        <div class="col-md-8">
                            <input type="text" id="addMaterialPriceTotal" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label"></label>

                        <div class="col-md-8">
                            <input type="hidden" id="addQuotationMaterialId" class="form-control"
                                   value="0"/>
                            <a class="btn btn-primary form-control" id="addQuotationMaterialButton"><i
                                        class="fa fa-plus"></i>&nbsp;ADD</a>
                        </div>
                    </div>
                </div>
                <div id="quotationItemMaterialList" class="col-md-8">
                </div>
                <div class="col-md-4"></div>
                <div class="row form-group col-md-8">
                    <label class="col-md-4 control-label text-right">Sub total:</label>

                    <div class="col-md-8">
                        <input type="text" id="addMaterialPriceSubTotal" class="form-control"
                               value="0"/>
                    </div>
                </div>
            </div>
            <div id="otherQuotationPanel" class="tab-pane">
                <div class="col-md-4">
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Qty:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationOtherQuantity" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Name:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationOtherName" class="form-control"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Price:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationOtherUnitPrice" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Mark up %:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationOtherUnitMargin" class="form-control"
                                   value="15"/>
                        </div>
                    </div>
                    <div class="row form-group hidden">
                        <label class="col-md-4 control-label">Duration:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationOtherDuration" class="form-control"
                                   value="1"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Description:</label>

                        <div class="col-md-8">
                                                <textarea id="addQuotationOtherDescription"
                                                          class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label">Line Total:</label>

                        <div class="col-md-8">
                            <input type="text" id="addQuotationOtherTotalPrice" class="form-control"
                                   value="0"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <label class="col-md-4 control-label"></label>

                        <div class="col-md-8">
                            <a class="btn btn-primary form-control" id="addQuotationOtherButton"><i
                                        class="fa fa-plus"></i>&nbsp;ADD</a>
                        </div>
                    </div>
                </div>
                <div id="quotationItemOtherList" class="col-md-8">
                </div>
                <div class="col-md-4"></div>
                <div class="row form-group col-md-8">
                    <label class="col-md-4 control-label text-right">Sub total:</label>

                    <div class="col-md-8">
                        <input type="text" id="addOtherPriceTotal" class="form-control"
                               value="0"/>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="col-md-12">
    <hr/>
</div>
<div class="col-md-6">
    <div class="control-label form-group">
        <h4>General Qualifications to This Offer</h4>
            <textarea id="addGeneralQualifications" rows="7" class="form-control">1. Costs shown above are fixed for a period of 30 days only
2. This estimate assumes that all Local Authority approvals, Landlord's consent, Planning Applications etc. are the responsibility to the client. No provision has been made within this estimate for such work, unless specifically mentioned above
3. All works, unless specifically mentioned above, are priced to be undertaken in "Normal Hours" in accordance with the Client's "Normal Working Hours" policy
4. If a test certificate is required, this will be submitted no later than 14 days after completion of the works
5. No works or procurement of products will be undertaken without formal authorisation to proceed with this offer
6. Unless mentioned above, we have made no provision on specialist attendances that may be required, such as third party organisations</textarea>
    </div>
</div>
<div class="col-md-6">
    <div class="control-label form-group">
        <h4>Special Terms</h4>
        <textarea id="addAdditionalTerms" rows="7" class="form-control"></textarea>
    </div>
</div>

<div class="col-md-12">
    <hr/>
</div>
<div class="col-md-12">
    <div class="col-md-12 text-right">
        <div class="row form-group">
            <label class="col-md-4 control-label">Quotation Total &pound;:</label>

            <div class="col-md-4">
                <input type="text" id="addQuotationTotal" class="form-control"/>
            </div>
            <a class="btn btn-primary col-md-4" id="addQuotationButton"><i
                        class="fa fa-plus"></i>&nbsp;Save this quotation</a>
        </div>
    </div>
</div>

<script type="text/javascript">
    $('#addLeadInTime').numeric();
    $('#addWorkDuration').numeric();

    var presetData = {};

    $('#addQuotationDate').datepicker({
        format: 'dd/mm/yyyy'
    });

    $('#addQuotationJob').selectize();

    $('#addQuotationJob').change(function () {
        $.post('<?php echo e(URL::action("QuotationController@presetAfterSelectJob")); ?>', {
            jobId: $('#addQuotationJob').val(),
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            presetData = data;
            $('#addCustomerRef').val(presetData['customerRef']);
            $('#addClientName').val(presetData['clientName']);
            $('#addSiteName').val(presetData['siteName']);
            $('#addSiteAddress').val(presetData['siteAddress']);
            $('#addDiscipline')[0].selectize.setValue(presetData['discipline']);

            var selectizeAddJobServiceProvider = $('#addJobServiceProvider').selectize({
                valueField: 'id',
                labelField: 'name',
                searchField: ['name'],
                options: presetData['serviceProviders'],
                onChange: function (value) {
                    $('#addDisciplineHourlyRate').val(presetData['serviceProvider' + value]['hourlyRate']);
                    $('#addDisciplineCallOut').val(presetData['serviceProvider' + value]['callOut']);
                }
            });

            <?php if(session('currentServiceProvider')): ?>
                    selectizeAddJobServiceProvider[0].selectize.setValue('<?php echo e(session("currentServiceProvider")["id"]); ?>');
            <?php endif; ?>

            <?php if(Auth::user()['userType']=='serviceProvider'): ?>
                    selectizeAddJobServiceProvider[0].selectize.setValue('<?php echo e(Auth::user()["userTypeId"]); ?>');
            <?php endif; ?>
        });
    });

    $('#addOutOfHoursWork').change(function () {
        if ($('#addOutOfHoursWork').val().toLowerCase() === ('Yes').toLowerCase()) {
            if ($('#addJobServiceProvider').val()) {
                $('#addDisciplineHourlyRate').val(presetData['serviceProvider' + $('#addJobServiceProvider').val()]['overtimeHourlyRate']);
                $('#addDisciplineCallOut').val(presetData['serviceProvider' + $('#addJobServiceProvider').val()]['overtimeCallOut']);
            }
        } else {
            if ($('#addJobServiceProvider').val()) {
                $('#addDisciplineHourlyRate').val(presetData['serviceProvider' + $('#addJobServiceProvider').val()]['hourlyRate']);
                $('#addDisciplineCallOut').val(presetData['serviceProvider' + $('#addJobServiceProvider').val()]['callOut']);
            }
        }
    });

    $('#addQuotationType').selectize();

    $('#addCallOut').selectize();

    $('#addDiscipline').selectize();

    $('#addOutOfHoursWork').selectize();

    $('#addDisciplineContractor').selectize();

    function getQuotationTypeAheadMaterialsData() {
        $.post('<?php echo e(URL::action("QuotationController@getTypeAheadMaterials")); ?>', {
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (result) {
            $('#addQuotationMaterialName').typeahead({
                source: function (query, process) {
                    objects = [];
                    map = {};
                    var data = result;
                    $.each(data, function (i, object) {
                        map[object.label] = object;
                        objects.push(object.label);
                    });
                    process(objects);
                },
                updater: function (item) {
                    $('#addQuotationMaterialId').val(map[item].id);
                    setTimeout(function () {
                        $('#addQuotationMaterialName').val(map[item].name);
//                        $('#addQuotationMaterialPrice').val(map[item].sellPrice);
                        $('#addQuotationMaterialPartNo').val(map[item].serial);
//                        var total = $('#addQuotationMaterialQuantity').val() * $('#addQuotationMaterialPrice').val() * ((100 + Number($('#addQuotationMaterialMargin').val())) / 100);
//                        $('#addMaterialPriceTotal').val(total.toFixed(2));
                    }, 382);
                    return item;
                }
            });
        });
    }
    getQuotationTypeAheadMaterialsData();

    $('#addQuotationMaterialPrice').change(function () {
        var total = $('#addQuotationMaterialQuantity').val() * $('#addQuotationMaterialPrice').val() * ((100 + Number($('#addQuotationMaterialMargin').val())) / 100);
        $('#addMaterialPriceTotal').val(total.toFixed(2));
    });

    $('#addQuotationMaterialQuantity').change(function () {
        var total = $('#addQuotationMaterialQuantity').val() * $('#addQuotationMaterialPrice').val() * ((100 + Number($('#addQuotationMaterialMargin').val())) / 100);
        $('#addMaterialPriceTotal').val(total.toFixed(2));
    });

    $('#addQuotationMaterialMargin').change(function () {
        var total = $('#addQuotationMaterialQuantity').val() * $('#addQuotationMaterialPrice').val() * ((100 + Number($('#addQuotationMaterialMargin').val())) / 100);
        $('#addMaterialPriceTotal').val(total.toFixed(2));
    });

    $('#addDisciplineHourlyRate').change(function () {
        $('#addEngineerPriceTotal').val($('#addDisciplineHourlyRate').val() * $('#addEngineerQuantity').val() * $('#addEngineerHours').val());
    });

    $('#addEngineerQuantity').change(function () {
        $('#addEngineerPriceTotal').val($('#addDisciplineHourlyRate').val() * $('#addEngineerQuantity').val() * $('#addEngineerHours').val());
    });

    $('#addEngineerHours').change(function () {
        $('#addEngineerPriceTotal').val($('#addDisciplineHourlyRate').val() * $('#addEngineerQuantity').val() * $('#addEngineerHours').val());
    });

    $('#addQuotationOtherQuantity').change(function () {
        var total = $('#addQuotationOtherQuantity').val() * $('#addQuotationOtherUnitPrice').val() * $('#addQuotationOtherDuration').val() * ((100 + Number($('#addQuotationOtherUnitMargin').val())) / 100);
        $('#addQuotationOtherTotalPrice').val(total.toFixed(2));
    });

    $('#addQuotationOtherUnitPrice').change(function () {
        var total = $('#addQuotationOtherQuantity').val() * $('#addQuotationOtherUnitPrice').val() * $('#addQuotationOtherDuration').val() * ((100 + Number($('#addQuotationOtherUnitMargin').val())) / 100);
        $('#addQuotationOtherTotalPrice').val(total.toFixed(2));
    });

    $('#addQuotationOtherDuration').change(function () {
        var total = $('#addQuotationOtherQuantity').val() * $('#addQuotationOtherUnitPrice').val() * $('#addQuotationOtherDuration').val() * ((100 + Number($('#addQuotationOtherUnitMargin').val())) / 100);
        $('#addQuotationOtherTotalPrice').val(total.toFixed(2));
    });

    $('#addQuotationEngineerButton').click(function () {
        var ahref = $(this);
        ahref.text('adding...');
        ahref.bind('click', false);
        loadingOverlay();
        $.post('<?php echo e(URL::action("QuotationController@addQuotationItemEngineer")); ?>', {
            customerRef: $('#addCustomerRef').val(),
            quoteDate: $('#addQuoteDate').val(),
            discipline: $('#addDiscipline').val(),
            quotationType: $('#addQuotationType').val(),
            siteName: $('#addSiteName').val(),
            siteAddress: $('#addSiteAddress').val(),
            jobId: $('#addQuotationJob').val(),
            leadInTime: $('#addLeadInTime').val(),
            workDuration: $('#addWorkDuration').val(),
            outOfHoursWork: $('#addOutOfHoursWork').val(),
            disciplineContractor: $('#addDisciplineContractor').val(),
            workDetail: $('#addWorkDetail').val(),
            itemQuantity: $('#addEngineerQuantity').val(),
            itemUnitPrice: $('#addDisciplineHourlyRate').val(),
            callOutPrice: $('#addDisciplineCallOut').val(),
            itemDuration: $('#addEngineerHours').val(),
            itemTotalPrice: $('#addEngineerPriceTotal').val(),
            itemDescription: $('#addEngineerPriceDescription').val(),
            callOut: $('#addCallOut').val(),
            quotationId: $('#addQuotationId').val(),
            serviceProviderId: $('#addJobServiceProvider').val(),
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            ahref.text('ADD');
            ahref.unbind('click', false);
            generateQuotationItemEngineer('<?php echo e(URL::action("QuotationController@generateQuotationItemEngineer")); ?>', data, '<?php echo e(csrf_token()); ?>');
            calculateQuotationLabourSubTotal('<?php echo e(URL::action("QuotationController@calculateQuotationLabourSubTotal")); ?>', data, '<?php echo e(csrf_token()); ?>');
            calculateQuotationTotal('<?php echo e(URL::action("QuotationController@calculateQuotationTotal")); ?>', data, '<?php echo e(csrf_token()); ?>');
            loadingRemove();
            new PNotify({
                title: 'Success',
                text: 'Engineer for quotation add successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
            $('#addEngineerQuantity').val(0);
            $('#addEngineerHours').val(0);
            $('#addEngineerPriceTotal').val(0);
            $('#addEngineerPriceDescription').val('');
            $('#addQuotationId').val(data);
        }).fail(function (data) {
            ahref.text('ADD');
            ahref.unbind('click', false);
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'Engineer for quotation add failed, please re-check quotation inputs!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    });

    $('#addQuotationMaterialButton').click(function () {
        var ahref = $(this);
        ahref.text('adding...');
        ahref.bind('click', false);
        loadingOverlay();
        $.post('<?php echo e(URL::action("QuotationController@addQuotationItemMaterial")); ?>', {
            customerRef: $('#addCustomerRef').val(),
            quoteDate: $('#addQuoteDate').val(),
            discipline: $('#addDiscipline').val(),
            quotationType: $('#addQuotationType').val(),
            siteName: $('#addSiteName').val(),
            siteAddress: $('#addSiteAddress').val(),
            jobId: $('#addQuotationJob').val(),
            leadInTime: $('#addLeadInTime').val(),
            workDuration: $('#addWorkDuration').val(),
            outOfHoursWork: $('#addOutOfHoursWork').val(),
            disciplineContractor: $('#addDisciplineContractor').val(),
            workDetail: $('#addWorkDetail').val(),
            itemName: $('#addQuotationMaterialName').val(),
            itemSerial: $('#addQuotationMaterialPartNo').val(),
            itemQuantity: $('#addQuotationMaterialQuantity').val(),
            itemUnitPrice: $('#addQuotationMaterialPrice').val(),
            itemUnitMargin: $('#addQuotationMaterialMargin').val(),
            itemTotalPrice: $('#addMaterialPriceTotal').val(),
            itemDescription: $('#addQuotationMaterialDescription').val(),
            quotationId: $('#addQuotationId').val(),
            serviceProviderId: $('#addJobServiceProvider').val(),
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            ahref.text('ADD');
            ahref.unbind('click', false);
            generateQuotationItemMaterial('<?php echo e(URL::action("QuotationController@generateQuotationItemMaterial")); ?>', data, '<?php echo e(csrf_token()); ?>');
            calculateQuotationMaterialSubTotal('<?php echo e(URL::action("QuotationController@calculateQuotationMaterialSubTotal")); ?>', data, '<?php echo e(csrf_token()); ?>');
            calculateQuotationTotal('<?php echo e(URL::action("QuotationController@calculateQuotationTotal")); ?>', data, '<?php echo e(csrf_token()); ?>');
            loadingRemove();
            new PNotify({
                title: 'Success',
                text: 'Add successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
            $('#addQuotationMaterialQuantity').val(0);
            $('#addQuotationMaterialName').val('');
            $('#addQuotationMaterialPrice').val(0);
            $('#addQuotationMaterialMargin').val(15);
            $('#addQuotationMaterialDescription').val('');
            $('#addMaterialPriceTotal').val(0);
            $('#addQuotationId').val(data);
        }).fail(function (data) {
            ahref.text('ADD');
            ahref.unbind('click', false);
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'Add failed, please re-check quotation inputs!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    });

    $('#addQuotationOtherButton').click(function () {
        var ahref = $(this);
        ahref.text('adding...');
        ahref.bind('click', false);
        loadingOverlay();
        $.post('<?php echo e(URL::action("QuotationController@addQuotationItemOther")); ?>', {
            customerRef: $('#addCustomerRef').val(),
            quoteDate: $('#addQuoteDate').val(),
            discipline: $('#addDiscipline').val(),
            quotationType: $('#addQuotationType').val(),
            siteName: $('#addSiteName').val(),
            siteAddress: $('#addSiteAddress').val(),
            jobId: $('#addQuotationJob').val(),
            leadInTime: $('#addLeadInTime').val(),
            workDuration: $('#addWorkDuration').val(),
            outOfHoursWork: $('#addOutOfHoursWork').val(),
            disciplineContractor: $('#addDisciplineContractor').val(),
            workDetail: $('#addWorkDetail').val(),
            itemQuantity: $('#addQuotationOtherQuantity').val(),
            itemName: $('#addQuotationOtherName').val(),
            itemUnitPrice: $('#addQuotationOtherUnitPrice').val(),
            itemUnitMargin: $('#addQuotationOtherUnitMargin').val(),
            itemDuration: $('#addQuotationOtherDuration').val(),
            itemTotalPrice: $('#addQuotationOtherTotalPrice').val(),
            itemDescription: $('#addQuotationOtherDescription').val(),
            quotationId: $('#addQuotationId').val(),
            serviceProviderId: $('#addJobServiceProvider').val(),
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            ahref.text('ADD');
            ahref.unbind('click', false);
            generateQuotationItemOther('<?php echo e(URL::action("QuotationController@generateQuotationItemOther")); ?>', data, '<?php echo e(csrf_token()); ?>');
            calculateQuotationOtherSubTotal('<?php echo e(URL::action("QuotationController@calculateQuotationOtherSubTotal")); ?>', data, '<?php echo e(csrf_token()); ?>');
            calculateQuotationTotal('<?php echo e(URL::action("QuotationController@calculateQuotationTotal")); ?>', data, '<?php echo e(csrf_token()); ?>');
            loadingRemove();
            new PNotify({
                title: 'Success',
                text: 'Add successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
            $('#addQuotationOtherQuantity').val(0);
            $('#addQuotationOtherName').val('');
            $('#addQuotationOtherUnitPrice').val(0);
            $('#addQuotationOtherTotalPrice').val(0);
            $('#addQuotationOtherDescription').val('');
            $('#addQuotationId').val(data);
        }).fail(function (data) {
            ahref.text('ADD');
            ahref.unbind('click', false);
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'Add failed, please re-check quotation inputs!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    });

    $('#addQuotationButton').click(function () {
        var ahref = $(this);
        ahref.text('adding...');
        ahref.bind('click', false);
        loadingOverlay();
        $.post('<?php echo e(URL::action("QuotationController@uiAdd")); ?>', {
            quotationId: $('#addQuotationId').val(),
            customerRef: $('#addCustomerRef').val(),
            quoteDate: $('#addQuoteDate').val(),
            discipline: $('#addDiscipline').val(),
            quotationType: $('#addQuotationType').val(),
            clientName: $('#addClientName').val(),
            siteName: $('#addSiteName').val(),
            siteAddress: $('#addSiteAddress').val(),
            jobId: $('#addQuotationJob').val(),
            leadInTime: $('#addLeadInTime').val(),
            workDuration: $('#addWorkDuration').val(),
            outOfHoursWork: $('#addOutOfHoursWork').val(),
            disciplineContractor: $('#addDisciplineContractor').val(),
            workDetail: $('#addWorkDetail').val(),
            generalQualifications: $('#addGeneralQualifications').val(),
            additionalTerms: $('#addAdditionalTerms').val(),
            labourPriceTotal: $('#addLabourPriceTotal').val(),
            materialPriceTotal: $('#addMaterialPriceSubTotal').val(),
            otherPriceTotal: $('#addOtherPriceTotal').val(),
            quotationTotal: $('#addQuotationTotal').val(),
            serviceProviderId: $('#addJobServiceProvider').val(),
            _token: '<?php echo e(csrf_token()); ?>'
        }).done(function (data) {
            ahref.text('SAVE');
            ahref.unbind('click', false);

            refreshQuotations();

            loadingRemove();
            $('#addQuotationId').val(0);
            new PNotify({
                title: 'Success',
                text: 'New quotation created successfully!',
                type: 'success',
                icon: 'fa fa-check'
            });
            generateFormAddQuotation('<?php echo e(URL::action("QuotationController@generateFormAddQuotation")); ?>', '<?php echo e(csrf_token()); ?>');
        }).fail(function (data) {
            ahref.text('SAVE');
            ahref.unbind('click', false);
            loadingRemove();
            new PNotify({
                title: 'Fail',
                text: 'New quotation created failed!',
                type: 'error',
                icon: 'fa fa-times'
            });
        });
    });
</script>