$(function () {
	"use strict";
	$(document).ready(function () {
		$('#basic-datatables').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
		});
		$('#basic-datatable01').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
		});
		$('#basic-datatables1').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
		});
		$('#basic-datatables2').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
		});
		$('#basic-datatabless2').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [0, 'desc']
		});
		$('#basic-datatables3').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [4, 'desc']
		});
		$('#basic-datatables4').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [0, 'desc']
		});
		$('#basic-datatables5').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [4, 'desc']
		});
		$('#basic-datatables6').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [0, 'desc']
		});
		$('#basic-datatables7').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [4, 'desc']
		});
		$('#basic-datatabless').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
		});
		$('#basic-datatablesss').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
		});
		$('#basic-datatableses').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 3,
					type: 'date' // Set the column type to 'date'
				}
			],
			order: [[3, 'desc']]
		});
		$('#basic-datatables11').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [0, 'desc']
		});
		$('#basic-datatabless11').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [0, 'desc']
		});
		$('#basic-datatables11a').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 0,
					type: 'date'
				}
			],
			order: [0, 'desc']
		});
		$('#basic-datatables11b').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 0,
					type: 'date'
				}
			],
			order: [[0, 'desc']]
		});
		$('#basic-datatabless12').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[0, 'desc']],
		});
		$('#basic-datatables13').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 5,
					type: 'date' // Set the column type to 'date'
				}
			],
			order: [[5, 'desc']] //for making desc with date
		});
		$('#basic-datatables13a').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 4,
					type: 'date'
				}
			],
			order: [[4, 'desc']]
		});
		$('#basic-datatables13b').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 4,
					type: 'date'
				}
			],
			order: [[4, 'desc']]
		});
		$('#basic-datatables13c').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 4,
					type: 'date'
				}
			],
			order: [[4, 'desc']]
		});
		$('#basic-datatables13d').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 4,
					type: 'date'
				}
			],
			order: [[4, 'desc']]
		});
		$('#basic-datatables13e').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 4,
					type: 'date'
				}
			],
			order: [[4, 'desc']]
		});
		$('#basic-datatables14').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 4,
					type: 'date' // Set the column type to 'date'
				}
			],
			order: [[4, 'desc']] //for making desc with date
		});
		$('#basic-datatables14a').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[13, 'asc']]
		});
		setupColumnSorting('#basic-datatables14a', 'Registered On', 13, 'desc');
		// $('#basic-datatables14a thead th:contains("Registered On")').click();

		$('#basic-datatables14aa').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[9, 'asc']]
		});
		setupColumnSorting('#basic-datatables14aa', 'Registered On', 9, 'desc');
		// $('#basic-datatables14aa thead th:contains("Registered On")').click();

		$('#basic-datatables14b').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[12, 'asc']]
		});
		setupColumnSorting('#basic-datatables14b', 'Registered On', 12, 'desc');
		// $('#basic-datatables14b thead th:contains("Registered On")').click(); 

		$('#basic-datatables15').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 2,
					type: 'date' // Set the column type to 'date'
				}
			],
			order: [[2, 'desc']] //for making desc with date
		});
		$('#basic-datatables16').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 2,
					type: 'date' // Set the column type to 'date'
				}
			],
			order: [[2, 'desc']] //for making desc with date
		})
		$('#basic-datatables01').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[10, 'asc']]
		});
		setupColumnSorting('#basic-datatables01', 'Registered On', 10, 'desc');
		// $('#basic-datatables01 thead th:contains("Registered On")').click();

		$('#basic-datatables02').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[11, 'asc']]
		});
		setupColumnSorting('#basic-datatables02', 'Registered On', 11, 'desc');
		// $('#basic-datatables02 thead th:contains("Registered On")').click();

		$('#basic-datatables03').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[11, 'asc']]
		});
		setupColumnSorting('#basic-datatables03', 'Registered On', 11, 'desc');
		// $('#basic-datatables03 thead th:contains("Registered On")').click();

		$('#basic-datatables04').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ orderData: [0], targets: 6 }
			],
			order: [[6, 'desc']]
		});

		$('#basic-datatablesCNPCust').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 6 }
			],
			order: [[6, 'desc']]
		});

		$('#basic-datatablesallcnp').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});
		$('#basic-datatablesallcnp1').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});

		$('#basic-datatablesallcnp2').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});

		$('#basic-datatablesallcnp3').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});
		$('#basic-datatablesallcnp4').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});
		$('#basic-datatablesallcnp5').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});
		$('#basic-datatablesallcnp6').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ type: 'date', targets: 7 }
			],
			order: [[7, 'desc']]
		});

		$('#basic-datatables05').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ orderData: [0], targets: 5 }
			],
			order: [[0, 'desc']]
		});
		$('#basic-datatabless06').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{ orderData: [0], targets: 3 }
			],
			order: [[0, 'desc']]
		});
		$('#basic-datatablesRs').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			columnDefs: [
				{
					targets: 3,
					type: 'date'
				}
			],
			order: [[3, 'desc']] // Sort by the fourth column (date column) in descending order
		});

		// this below table ids currntly not used
		$('#basic-datatabless10').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [1, 'asc'],
		});
		$('#basic-datatablesuser').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			"columnDefs": [
				{
					"orderable": false, "targets": [7]
				},
			],
		});

		$('#basic-datatabless13').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [1, 'desc'],
		});

		$('#basic-datatablessSus').DataTable({
			retrieve: true,
			paging: true,
			stateSave: false
		});
	});
	$('#basic-datatables07').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{
				targets: 0,
				type: 'date' // Set the column type to 'date'
			}
		],
		order: [[0, 'desc']]
	});
	$('#basic-datatablessP').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		// columnDefs: [
		// 	{ orderData: [0], targets: 6 }
		// ],
		order: [[6, 'asc']]
	});
	setupColumnSorting('#basic-datatablessP', 'Registered On', 6, 'desc');

	$('#basic-datatablesRej').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ orderData: [0], targets: 5 }
		],
		order: [[0, 'desc']]
	});
	$('#basic-datatablesVer').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ orderData: [0], targets: 5 }
		],
		order: [[0, 'desc']]
	});
	$('#basic-datatablesFed').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ orderData: [0], targets: 5 }
		],
		order: [[0, 'desc']]
	});
	$('#basic-datatablesFA').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ orderData: [0], targets: 5 }
		],
		order: [[0, 'desc']]
	});
	$('#basic-datatablesFR').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ orderData: [0], targets: 6 }
		],
		order: [[0, 'desc']]
	});
	$('#basic-datatablesSetting').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{
				targets: 4,
				type: 'date' // Set the column type to 'date'
			}
		],
		order: [[4, 'desc']] //for making desc with date
	});
	$('#basic-datatablessPen').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		order: [[10, 'asc']]
	});
	setupColumnSorting('#basic-datatablessPen', 'Registered On', 10, 'desc');
	// $('#basic-datatablessPen thead th:contains("Registered On")').click();

	$('#basic-datatablesRed').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		order: [[13, 'asc']]
	});
	setupColumnSorting('#basic-datatablesRed', 'Registered On', 13, 'desc');
	// $('#basic-datatablesRed thead th:contains("Registered On")').click();

	$('#basic-datatablesAC').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		order: [[10, 'asc']]
	});
	setupColumnSorting('#basic-datatablesAC', 'Registered On', 10, 'desc');
	// $('#basic-datatablesAC thead th:contains("Registered On")').click();

	$('#basic-datatablessCus').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		order: [[10, 'asc']]
	});
	setupColumnSorting('#basic-datatablessCus', 'Registered On', 10, 'desc');
	// $('#basic-datatablessCus thead th:contains("Registered On")').click();

	$('#basic-datatablesRed2').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ orderData: [0], targets: 9 }
		],
		order: [[10, 'desc']]
	});
	$('#basic-datatablescampaign').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		order: [[11, 'asc']]
	});
	setupColumnSorting('#basic-datatablescampaign', 'Registered On', 11, 'desc');
	// $('#basic-datatablescampaign thead th:contains("Registered On")').click();

	$('#basic-datatablesesG').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{
				targets: 6,
				orderData: [9],
				type: 'date'
			}
		],
		order: [9, 'desc']
	});

	$('#basic-datatablesMess').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{
				targets: 0,
				type: 'date'
			}
		],
		order: [0, 'desc']
	});
	$('#basic-datatablesSum').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{
				targets: 1,
				type: 'date'
			}
		],
		order: [1, 'desc']
	});
	$('#basic-datatableredtab').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{
				targets: 9,
				type: 'date' // Set the column type to 'date'
			}
		],
		order: [[9, 'desc']]
	});


	$('#basic-datatablessgroup').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ type: 'date', targets: [4, 5] }
		],
		order: [[4, 'desc']]
	});


	$('#basic-datatablesMess1').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
			{ type: 'date', targets: [5] }
		],
		order: [[5, 'desc']]
	});

	$.fn.dataTable.ext.type.order['date-mmddyyyy-pre'] = function (date) {
		const parts = date.split('/');
		return new Date(parts[2], parts[0] - 1, parts[1]).getTime();
	};

	$('#basic-datatablesdelete').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		columnDefs: [
        { type: 'date', targets: [0] }, // hidden datetime column
        { targets: [0], visible: false }, // hide it
        { targets: 7, orderData: [0] } // "Removed Date" column uses hidden column for sorting
    ],
    order: [[0, 'desc']] // initial sort by hidden column
	});



	$('#basic-datatablesUS').DataTable({
		retrieve: true,
		paging: true,
		stateSave: false,
		order: [[10, 'asc']]
	});
	setupColumnSorting('#basic-datatablesUS', 'Signup Date', 10, 'desc');

	/**
	 * Function to set up dynamic column sorting
	 * @param {string} tableSelector 
	 * @param {string} headerName 
	 * @param {number} targetColumnIndex
	 * @param {string} initialDirection 
	 */
	function setupColumnSorting(tableSelector, headerName, targetColumnIndex, initialDirection) {
		const table = $(tableSelector).DataTable({
			retrieve: true,
			paging: true,
			stateSave: false,
			order: [[targetColumnIndex, initialDirection]],
		});

		let sortDirection = initialDirection;
		$(`${tableSelector} thead`).on('click', 'th', function () {
			const headerText = $(this).text().trim();
			if (headerText === headerName) {
				table.order([targetColumnIndex, sortDirection]).draw();
				$(`${tableSelector} thead th`).removeClass('sorting_asc sorting_desc');
				const sortingClass = sortDirection === 'asc' ? 'sorting_asc' : 'sorting_desc';
				$(this).addClass(sortingClass);
				sortDirection = sortDirection === 'asc' ? 'desc' : 'asc';
			}
		});
	}
});

