<script src="<?php echo THEME_PATH; ?>plugins/jquery-validation/js/jquery.validate.min.js"></script>
<div class="modal fade <?php echo $IsEdit ? "DontReset" : ""; ?>" id="EditVehicle" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog ">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label bold">
                    <?php echo CommonLanguage::getMessage($IsEdit ? 'edit_vehicle' : 'new_vehicle'); ?></h5>
                <div id="VehicleValidationAlert"></div>

            </div>
            <div class="modal-body">
                <form id="EditVehiclePostForm" method="POST" onsubmit="return false">
                    <input type="hidden"  id="Vehicle_ID" name="Vehicle_ID" class="form-control " <?php echo $IsEdit ? ('value="' . $Vehicle_ID . '"') : ''; ?>>
                    <input type="hidden"  id="Is_Edit" name="Is_Edit" class="form-control " value="<?php echo $IsEdit ? true : false; ?>">
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('Code'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('Code'); ?>" id="Vehicle_Code" name="Vehicle_Code" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Code"] . '"') : ''; ?> required>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('Name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('Name'); ?>" id="Vehicle_Name" name="Vehicle_Name" class="form-control " value="<?php echo $IsEdit ? $VehicleInfo[0]["Vehicle_Name"] : ''; ?>"  required>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('insurance_next_date'); ?></label>
                            <input type="text"   placeholder="<?php print CommonLanguage::getMessage('insurance_next_date'); ?>" id="insurance_date" name="insurance_date" class="form-control " value="<?php echo $IsEdit ? $VehicleInfo[0]["insurance_date"] : ''; ?>"  >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('insurance_company'); ?></label>
                            <input type="text" data-date-  placeholder="<?php print CommonLanguage::getMessage('insurance_company'); ?>" id="insurance_company" name="insurance_company" class="form-control " value="<?php echo $IsEdit ? $VehicleInfo[0]["insurance_company"] : ''; ?>"  >
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('vehicle_make_name'); ?></label>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=9" style="color:#ed9421;cursor:pointer"></span>
                            <input type="hidden"  value="<?= $IsEdit ? ($VehicleInfo[0]['Vehicle_Make_Name_ID']): '' ?>" data-select-name="<?= $IsEdit ? ($VehicleInfo[0]['Vehicle_Type_Name']): '' ?>"   class="full-width Vehicle_Make_Name_ID" id="Vehicle_Make_Name_ID" name="Vehicle_Make_Name_ID"/>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('model'); ?></label>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=7" style="color:#ed9421;cursor:pointer"></span>
                            <input type="hidden" class="full-width Vehicle_Model_ID" id="Vehicle_Model_ID" name="Vehicle_Model_ID"
                                   value="<?php echo $IsEdit ? $VehicleInfo[0]['Vehicle_Model_ID']: '' ?>" data-select-name="<?php echo $IsEdit ? ($VehicleInfo[0]['Model_Name']): '' ?>"
                                   />
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('year'); ?></label>
                            <input type="text" class="form-control vehicle_year" id="vehicle_year" name="vehicle_year"
                                   value="<?php echo $IsEdit ? $VehicleInfo[0]['vehicle_year']: '' ?>" 
                                   />
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('color'); ?></label>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=1" style="color:#ed9421;cursor:pointer"></span>
                            <input type="hidden"  value="<?php echo $IsEdit ? $VehicleInfo[0]['Vehicle_Color_ID']: '' ?>" data-select-name="<?php echo $IsEdit ? ($VehicleInfo[0]['Color_Name']): '' ?>" class="full-width Vehicle_Color_ID" id="Vehicle_Color_ID" name="Vehicle_Color_ID"/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('reg_nb'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('reg_nb'); ?>" id="Vehicle_Registration_Number" name="Vehicle_Registration_Number" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Registration_Number"] . '"') : ''; ?> required>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('driver_name'); ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('driver_name'); ?>" id="driver_name" name="driver_name" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["driver_name"] . '"') : ''; ?> >
                        </div> 
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('driver_phone'); ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('driver_phone'); ?>" id="driver_phone" name="driver_phone" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["driver_phone"] . '"') : ''; ?> >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('owner_name'); ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('owner_name'); ?>" id="owner_name" name="owner_name" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["owner_name"] . '"') : ''; ?> >
                        </div>

                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('fuel_tank_volume'); ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('fuel_tank_volume'); ?>" id="Vehicle_Fuel_Tank_Volume" name="Vehicle_Fuel_Tank_Volume" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Fuel_Tank_Volume"] . '"') : ''; ?> >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('fuel_type'); ?></label>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=5" style="color:#ed9421;cursor:pointer"></span>

                            <input type="hidden" value="<?php echo $IsEdit ? $VehicleInfo[0]['Vehicle_Fuel_Type_ID']: '' ?>" data-select-name="<?php echo $IsEdit ? ($VehicleInfo[0]['Fuel_Type_Name']): '' ?>" class="full-width Vehicle_Fuel_Type_ID" id="Vehicle_Fuel_Type_ID" name="Vehicle_Fuel_Type_ID"/>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('vehicle_usage_type'); ?></label>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=6" style="color:#ed9421;cursor:pointer"></span>
                            <input type="hidden" value="<?php echo $IsEdit ? $VehicleInfo[0]['Vehicle_Usage_Type_ID']: '' ?>" data-select-name="<?php echo $IsEdit ? ($VehicleInfo[0]['Usage_Type_Name']): '' ?>" class="full-width Vehicle_Usage_Type_ID" id="Vehicle_Usage_Type_ID" name="Vehicle_Usage_Type_ID"/>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('start_usage_date'); ?></label>
                            <span class="red-star">*</span>
                            <input placeholder="<?php print CommonLanguage::getMessage('start_usage_date'); ?>" id="Vehicle_Start_Usage_Date" name="Vehicle_Start_Usage_Date" class="datepicker form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Start_Usage_Date"] . '"') : ''; ?>required>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('oil_exchange') . ' (' . CommonLanguage::getMessage('km') . ' )'; ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('oil_exchange'); ?>" id="Vehicle_Oil_Change" name="Vehicle_Oil_Change" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Oil_Change"] . '"') : ''; ?> >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('tracking_nb'); ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('tracking_nb'); ?>" id="Vehicle_Tracking_Number" name="Vehicle_Tracking_Number" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Tracking_Number"] . '"') : ''; ?> >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('fuel_per_km'); ?></label>
                            <input placeholder="<?php print CommonLanguage::getMessage('fuel_consumption'); ?>" id="Vehicle_Fuel_Consumption" name="Vehicle_Fuel_Consumption" class=" form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Fuel_Consumption"] . '"') : ''; ?>>
                        </div>

                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('maintenance_period') . ' (' . CommonLanguage::getMessage('km') . ' )'; ?></label>
                            <input type="text" placeholder="<?php print CommonLanguage::getMessage('maintenance_period'); ?>" id="Vehicle_Maintenance_Period" name="Vehicle_Maintenance_Period" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["Vehicle_Maintenance_Period"] . '"') : ''; ?> >
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('mecanique') . ' (' . CommonLanguage::getMessage('month') . ' )'; ?></label>
                            <span class="red-star">*</span>
                            <input type="text" data-date-format="mm" placeholder="<?php print CommonLanguage::getMessage('mecanique'); ?>" id="Vehicle_Mecanique" name="Vehicle_Mecanique" class="form-control " <?php echo $IsEdit ? ('value="' . $VehicleInfo[0]["mecanique_month"] . '"') : ''; ?> required>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('zone'); ?></label>
                            <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/GeneralSetting?is_pop=1&ID=14" style="color:#ed9421;cursor:pointer"></span>
                            <input type="hidden" value="<?php echo $IsEdit ? $VehicleInfo[0]['zone_id']: '' ?>" data-select-name="<?php echo $IsEdit ? ($VehicleInfo[0]['Zone_Name']): '' ?>" class="full-width Vehicle_Zone_ID" id="Vehicle_Zone_ID" name="Vehicle_Zone_ID"/>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('driver'); ?>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="HRM/viewEmployees?is_pop=1" style="color:#ed9421;cursor:pointer"></span>
                            </label>
                            <input type="hidden"  value="<?php echo $IsEdit ? $VehicleInfo[0]['driver_emp_id']: '' ?>" data-select-name="<?php echo $IsEdit ? ($VehicleInfo[0]['Employee_Name']): '' ?>" id="driver_emp_id" name="driver_emp_id" class="full-width driver_emp_id" >
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('rent_type'); ?></label>

                            <input type="hidden" id="rent_type_id" name="rent_type_id" class="full-width " value="<?php  echo $IsEdit ? ($VehicleInfo[0]["rent_type_id"] ? $VehicleInfo[0]["rent_type_id"] : '') :'';?>" data-select-name="<?php  echo $IsEdit ? ($VehicleInfo[0]["rent_type_name"] ? $VehicleInfo[0]["rent_type_name"] : '') : '';?>"  >
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('note'); ?></label>
                            <textarea type="text"  id="note" name="note" class="form-control "  ><?php echo $IsEdit ? $VehicleInfo[0]["note"] : '' ;?></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal" id="CloseUpdateVehicle"   > <?php print CommonLanguage::getMessage('Close'); ?> </button>
                <button class="btn btn-bg-success-darker" id="SubmitUpdateVehicle"  > <?php print CommonLanguage::getMessage('Save'); ?>   </button>
            </div>
            <?php echo Forms::ModalLoader('vehicleLoader'); ?>
        </div>
    </div>
</div>
<script type="text/javascript">
    var Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
        Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            Data_Exists = <?php echo json_encode(CommonLanguage::getMessage('Data_Exists')); ?>,
                LLang = <?php echo json_encode(CommonLanguage::getMessage('L')); ?>,
                    Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
                    var IsEditMode = <?php echo $IsEdit ? "true" : "false"; ?>;
                    var UpdateVehicleCurrentFileUploadaddVehicle = "";
    $(function () {
        build_select2($('#driver_emp_id'),'xhr/employeeSearch');

        build_select2($("#Vehicle_Make_Name_ID"), "xhr/CommonSearch?col_id=Vehicle_Type_ID&col_name=Vehicle_Type_Name&table=tbl_vehicle_type");

        build_select2($("#Vehicle_Model_ID"), "xhr/CommonSearch?col_id=Model_ID&col_name=Model_Name&table=tbl_vehicle_model");

        build_select2($("#Vehicle_Color_ID"), "xhr/CommonSearch?col_id=Color_ID&col_name=Color_Name&table=tbl_color");

        build_select2($("#Vehicle_Fuel_Type_ID"), "xhr/CommonSearch?col_id=Fuel_Type_ID&col_name=Fuel_Type_Name&table=tbl_fuel_type");

        build_select2($("#Vehicle_Usage_Type_ID"), "xhr/CommonSearch?col_id=Usage_Type_ID&col_name=Usage_Type_Name&table=tbl_vehicle_usage_type");

        build_select2($("#Vehicle_Zone_ID"), "xhr/CommonSearch?col_id=Zone_ID&col_name=Zone_Name&table=tbl_garage_vehicle_zone");

        build_select2($("#rent_type_id"), "xhr/CommonSearch?col_name=rent_type_name&col_id=rent_type_id&table=tbl_garage_rent_type");
        if(!IsEditMode){
            $("#EditVehicle").on("shown.bs.modal", function () {
                $('#vehicleLoader').removeClass('hidden')
                $.ajax({
                    url: "GarageAjax/getMaxVehicleCode",
                    dataType:"json",
                    complete: function (v1, v2) {
                        if (v2 == 'success') {
                            $('#Vehicle_Code').val(v1.responseJSON)
                        }
                        $('#vehicleLoader').addClass('hidden');
                    }
                });
            });
        }

        $("#insurance_date").datepicker({autoclose: true}); 
        $("#Vehicle_Mecanique").datepicker({autoclose: true, minViewMode: 'months', viewMode: 'months'}); 
        $("#Vehicle_Start_Usage_Date").datepicker({autoclose: true});
        apply_autocomplete($("#insurance_company"), 'xhr/searchInsuranceCompany', function (event, ui, who) {
            who.val(ui.item.text);
        });
        $("#SubmitUpdateVehicle").on("click", function () {
            if (!$("#EditVehiclePostForm").valid())
                return;
            var Data = new FormData(document.getElementById("EditVehiclePostForm"));
            $('#vehicleLoader').removeClass('hidden')
            $.ajax({
                url: "GarageAjax/EditVehicleAjax",
                method: "POST",
                data: Data,
                dataType: 'json',
                processData: false,
                contentType: false,
                complete: function (v1, v2) {
                    $("#EditVehiclePostForm").find("label.error").remove()
                    if (v2 == 'success' && v1.responseJSON == 'OK') {
                        $("#VehicleValidationAlert").addClass("alert-success").empty().html(Submitted_Successfully);
                        setTimeout(function () {
                            if (!IsEditMode) {
                                $("#EditVehiclePostForm").validate().resetForm()
                                RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1, 2, 3], 'GarageAjax/VehicleListRequest')
                            } else {
                                if($('#is_modal_shown').val() != 1)
                                    location.reload(); 
                            }
                            $("#EditVehicle").modal('hide');
                            $("#VehicleValidationAlert").hide();
                        }, 1500);
                    } else {
                        $("#VehicleValidationAlert").addClass("alert-danger").empty().html(v1.responseJSON[0]);
                    }
                    $('#vehicleLoader').addClass('hidden');
                }
            });
        });
        $("#tableWithSearch").on("click", ".copy_row", function () {
            var current_vehicle_id = $(this).attr('data-id');
            CommonViewById('GarageAjax/getVehicleInfo', {current_vehicle_id: current_vehicle_id},
                           '#vehicleLoader', function (response) {
                var vehicle_data = response[0],Keys = Object.keys(response[0])
                $.each(Keys, function (key, value) {
                    $('#'+value).val(vehicle_data[value])
                });
                $("#Vehicle_Name").val('');
                $("#Vehicle_Mecanique").val(vehicle_data.mecanique_month);
                $("#driver_name").val(vehicle_data.driver_name);
                $("#driver_phone").val(vehicle_data.driver_phone);
                $("#note").val(vehicle_data.note);
                $("#insurance_date").val(vehicle_data.insurance_date);
                $("#insurance_company").val(vehicle_data.insurance_company);
                $("#vehicle_year").val(vehicle_data.vehicle_year);

                $("#rent_type_id").val(vehicle_data.rent_type_id).attr('data-select-name',vehicle_data.rent_type_name);
                build_select2($("#rent_type_id"), "xhr/CommonSearch?col_name=rent_type_name&col_id=rent_type_id&table=tbl_garage_rent_type");

                $("#Vehicle_Make_Name_ID").val(vehicle_data.Vehicle_Make_Name_ID).attr('data-select-name',vehicle_data.Vehicle_Type_Name);
                build_select2($("#Vehicle_Make_Name_ID"), "xhr/CommonSearch?col_id=Vehicle_Type_ID&col_name=Vehicle_Type_Name&table=tbl_vehicle_type");

                $("#Vehicle_Model_ID").val(vehicle_data.Vehicle_Model_ID).attr('data-select-name',vehicle_data.Model_Name);
                build_select2($("#Vehicle_Model_ID"), "xhr/CommonSearch?col_id=Model_ID&col_name=Model_Name&table=tbl_vehicle_model");

                $("#Vehicle_Color_ID").val(vehicle_data.Vehicle_Color_ID).attr('data-select-name',vehicle_data.Color_Name);
                build_select2($("#Vehicle_Color_ID"), "xhr/CommonSearch?col_id=Color_ID&col_name=Color_Name&table=tbl_color");

                $("#Vehicle_Fuel_Type_ID").val(vehicle_data.Vehicle_Fuel_Type_ID).attr('data-select-name',vehicle_data.Fuel_Type_Name);
                build_select2($("#Vehicle_Fuel_Type_ID"), "xhr/CommonSearch?col_id=Fuel_Type_ID&col_name=Fuel_Type_Name&table=tbl_fuel_type");

                $("#Vehicle_Usage_Type_ID").val(vehicle_data.Vehicle_Usage_Type_ID).attr('data-select-name',vehicle_data.Usage_Type_Name);
                build_select2($("#Vehicle_Usage_Type_ID"), "xhr/CommonSearch?col_id=Usage_Type_ID&col_name=Usage_Type_Name&table=tbl_vehicle_usage_type");


                $("#Vehicle_Zone_ID").val(vehicle_data.zone_id).attr('data-select-name',vehicle_data.Zone_Name);
                build_select2($("#Vehicle_Zone_ID"), "xhr/CommonSearch?col_id=Zone_ID&col_name=Zone_Name&table=tbl_garage_vehicle_zone");

                $("#driver_emp_id").val(vehicle_data.driver_emp_id).attr('data-select-name',vehicle_data.Employee_Name);
                build_select2($('#driver_emp_id'),'xhr/employeeSearch');

            });
        });
    });
</script>
