+ function toggleClient(input) {
+ let btn = $("#vehicle_client_btn");
+ let client_type = $('#client_type').val();
-
-
-
+ // if (btn.text().trim() === "New Client") {
+ // btn.text("Existing Client");
+ // console.log("✅ Switched to Existing Client");
+ // $('.new_client').show();
+ // $('.old_client').hide();
+ // $(".old_client").find("select, input, textarea").val("").prop("required", false);
+ // $(".new_client").find("select, input, textarea").val("").prop("required", true);
+ // $('#owner').select2();
+ // $('#owner_branch').select2();
+
+ // } else {
+ // btn.text("New Client");
+ // console.log("✅ Switched to New Client");
+ // $('.new_client').hide();
+ // $('.old_client').show();
+ // $(".old_client").find("select, input, textarea").prop("required", true);
+ // $(".new_client").find("select, input, textarea").val("").prop("required", false);
+ // $('#owner').select2();
+ // $('#owner_branch').select2();
+
+ // }
+
+ if($(input).is(":checked")){
+
+ if(client_type == 1){
+ $('.new_client').show();
+ $('.old_client').hide();
+ $(".old_client").find("select, input, textarea").val("").prop("required", false);
+ $(".new_client").find("select, input, textarea").val("").prop("required", true);
+ }else{
+ $('.new_client').show();
+ $('.old_client').hide();
+ $(".old_client").find("select, input, textarea").val("").prop("required", false);
+ $(".new_client").find("select, input, textarea").val("").prop("required", true);
+ $('.v_group_client').hide();
+ $('.v_individual_client').show();
+ $(".v_group_client").find("select, input, textarea").val("").prop("required", true);
+ $(".v_individual_client").find("select, input, textarea").val("").prop("required", true);
+ }
+
+ $('#owner').select2();
+ $('#owner_branch').select2();
+
+ }else {
+
+ if(client_type == 1){
+ $('.new_client').hide();
+ $('.old_client').show();
+ $(".old_client").find("select, input, textarea").prop("required", true);
+ $(".new_client").find("select, input, textarea").val("").prop("required", false);
+ }else{
+ $('.new_client').hide();
+ $('.old_client').show();
+ $(".old_client").find("select, input, textarea").prop("required", true);
+ $(".new_client").find("select, input, textarea").val("").prop("required", false);
+ $('.old_group_client').hide();
+ $(".old_group_client").find("select, input, textarea").val("").prop("required", false);
+ }
+ $('#owner').select2();
+ $('#owner_branch').select2();
+ }
+ }
+
+ function removeVehicleForm(){
+ $('#vehicle_row_div').empty();
+ $('#actcual_clients').show();
+ }
+
+ function removeClientForm(){
+ $('#client_row_div').empty();
+ }
+
+ function getVehicleFormData() {
+
+ console.log("🔹 Collecting Vehicle Form Data...");
+ let vhno = $('#modal_vehicle_no').val();
+ let rc = $('#rc').val();
+ let client_type = $('#client_type').val();
+
+
+ if (vhno === "" && rc === "") {
+ toastr.warning("Please enter any one of these: Vehicle No or Chassis No", "WARNING");
+ return;
+ }
+
+ var isValid = $('#vehicle_form_row_div').parsley().validate();
+
+ if (!isValid) {
+ console.log("❌ Validation failed for Vehicle Form section");
+ return false;
+ }
+
+ let data = {};
+ $("#vehicle_form_row_div").find("input, select, textarea").each(function () {
+ let name = $(this).attr("name");
+ let value = $.trim($(this).val());
+
+ if (name) {
+ data[name] = value;
+ console.log("✅ Added field:", name, "=", value);
+ }
+ });
+ // data["client_type"] = client_type;
+
+ console.log("📌 Final Data Object:", data);
+
+ let url = '= base_url("util/createVehicleWithMinimalDataForBDS"); ?>';
+
+ $('.loader').fadeIn();
+ $('.loader-mask').fadeIn();
+
+ // Send AJAX request
+ sendAjaxRequestForGlobal(url, 'POST', data, function(response) {
+ console.log('Data fetched successfully:', response);
+
+ $('.loader').fadeOut();
+ $('.loader-mask').delay(350).fadeOut('slow');
+
+ if (response.status == true) {
+ toastr.success(response.message, 'SUCCESS');
+ appendClients(response.clients, response.client_id);
+ appendVehicles(response.vehicles, response.vehicle_id);
+ appendBranch(response.branches, response.branch_id);
+ if(response.data.client_type == 1){
+ $('.branchdiv').show();
+ $('#table_tr_34').show();
+ $('#table_tr_37').show();
+ }else{
+ $('.branchdiv').hide();
+ $('#table_tr_34').hide();
+ $('#table_tr_37').hide();
+ }
+ $('#client_type').val(response.data?.client_type ?? "").trigger('change');
+ } else {
+ toastr.warning(response.message, 'WARNING');
+ }
+
+ $('#client_row_div').empty();
+ $('#vehicle_row_div').empty();
+ $('#actcual_clients').show();
+
+ }, function(xhr, status, error) {
+ console.error('Error fetching data:', error);
+ console.error(xhr.responseText);
+ toastr.error('An error occurred while checking the CD amount.', 'ERROR');
+ $('.loader').fadeOut();
+ $('.loader-mask').delay(350).fadeOut('slow');
+ });
+ }
+
+ function getClientFormData() {
+
+ let client_type = $('#client_type').val();
+ console.log("🔹 Collecting Client Form Data...");
+
+ var isValid = $('#client_form_row_div').parsley().validate();
+
+ if (!isValid) {
+ console.log("❌ Validation failed for Client Form section");
+ return false;
+ }
+
+ let data = {};
+ $("#client_form_row_div").find("input, select, textarea").each(function () {
+ let name = $(this).attr("name");
+ let value = $.trim($(this).val());
+
+ if (name) {
+ data[name] = value;
+ console.log("✅ Added field:", name, "=", value);
+ }
+ });
+ data["client_type"] = client_type;
+
+ console.log("📌 Final Data Object:", data);
+
+ let url = '= base_url("util/createClientWithMinimalData"); ?>';
+
+ $('.loader').fadeIn();
+ $('.loader-mask').fadeIn();
+
+ // Send AJAX request
+ sendAjaxRequestForGlobal(url, 'POST', data, function(response) {
+ console.log('Data fetched successfully:', response);
+
+ $('.loader').fadeOut();
+ $('.loader-mask').delay(350).fadeOut('slow');
+
+ if (response.status == true) {
+ toastr.success(response.message, 'SUCCESS');
+ appendClients(response.clients, response.client_id);
+ appendBranch(response.branches, response.branch_id);
+ } else {
+ toastr.warning(response.message, 'WARNING');
+ }
+
+ $('#client_row_div').empty();
+ $('#vehicle_row_div').empty();
+
+ }, function(xhr, status, error) {
+ console.error('Error fetching data:', error);
+ console.error(xhr.responseText);
+ toastr.error('An error occurred while checking the CD amount.', 'ERROR');
+ $('.loader').fadeOut();
+ $('.loader-mask').delay(350).fadeOut('slow');
+
+ });
+
+
+ }
+
+ function appendVehicleForm(){
+ $('#vehicle_row_div').empty();
+
+ let html = `
+
`;
+
+ $('#vehicle_row_div').append(html);
+ appendOwner(client_list);
+ $('#owner').select2();
+ $('#owner_branch').select2();
+
+ // $("#vehicle_form_row_div").on("input change", "input, select, textarea", function () {
+ // if ($("#client_type").val() === "") {
+ // alert("Please select the client type first");
+ // if ($(this).is(":checkbox")) {
+ // // uncheck checkbox
+ // $(this).prop("checked", false);
+ // } else {
+ // // clear text/select/textarea
+ // $(this).val("");
+ // }
+ // $("#client_type").focus(); // focus back to client_type
+ // }
+ // });
+
+ $('#owner').change(function(){
+
+ let owner_id = $(this).val();
+ console.log('owner_id', owner_id);
+ if(branch_list != '') {
+ // console.log(branch_list[client_id]);
+ let data = branch_list[owner_id];
+ appendOwnerBranch(data);
+ }
+
+ })
+
+ $('#owner_client_type').on('change', function() {
+
+ let btn = $("#vehicle_client_btn");
+ let client_type = $(this).val();
+
+ if(client_type == 1){
+
+ if(btn.is(':checked')){
+ $('.v_group_client').show();
+ $('.v_individual_client').hide();
+ $(".v_group_client").find("select, input, textarea").val("").prop("required", true);
+ $(".v_individual_client").find("select, input, textarea").val("").prop("required", false);
+ }else{
+ $('.v_group_client').hide();
+ $('.v_individual_client').hide();
+ $(".v_group_client").find("select, input, textarea").val("").prop("required", false);
+ $(".v_individual_client").find("select, input, textarea").val("").prop("required", false);
+
+ $('.old_group_client').show();
+ $(".old_group_client").find("select, input, textarea").val("").prop("required", true);
+ $(".v_common").find("select, input, textarea").val("").prop("required", false);
+ }
+
+ }else{
+
+ if(btn.is(':checked')){
+ $('.v_group_client').hide();
+ $('.v_individual_client').show();
+ $(".v_group_client").find("select, input, textarea").val("").prop("required", false);
+ $(".v_individual_client").find("select, input, textarea").val("").prop("required", true);
+ }else{
+ $('.v_group_client').hide();
+ $('.v_individual_client').hide();
+ $(".v_group_client").find("select, input, textarea").val("").prop("required", false);
+ $(".v_individual_client").find("select, input, textarea").val("").prop("required", false);
+
+ $('.old_group_client').hide();
+ $(".old_group_client").find("select, input, textarea").val("").prop("required", false);
+ $(".v_common").find("select, input, textarea").val("").prop("required", false);
+
+ }
+ }
+
+ $.each(client_list, function(index, item) {
+ if (item.client_type == client_type) {
+
+ let client_show_name = item.client_name;
+ if (item.client_type == 2) {
+ // client_show_name = item.client_name + ' - ' + (item.dob ?? '') + (item.pan ?? '');
+ client_show_name = item.client_name + (item.dob ? ('- (' + item.dob + ' )' ) : '') + (item.pan ? ('- ' + item.pan ) : '');
+ }
+ var option = $('