var jQuery_1_8_2 = jQuery_1_8_2 || $.noConflict();
(function ($, undefined) {
	$(function () {
		"use strict";
		var $frmCreateClient = $("#frmCreateClient"),
			$frmUpdateClient = $("#frmUpdateClient"),
			select2 = ($.fn.select2 !== undefined),
			datagrid = ($.fn.datagrid !== undefined);
			datetimeOptions = null;
		
		if (select2 && $(".select-item").length) {
            $(".select-item").select2({
            	placeholder: "-- " + myLabel.choose + " --",
                allowClear: true
            });
        };
		if($frmCreateClient.length > 0)
		{
			$frmCreateClient.validate({
				rules: {
					"email": {
						required: true,
						email: true,
						remote: "index.php?controller=pjAdminDrivers&action=pjActionCheckEmail"
					}
				},
				messages: {
					"email": {
						remote: myLabel.email_exists
					}
				}
			});
		}
		if ($frmUpdateClient.length > 0) {
			$frmUpdateClient.validate({
				rules: {
					"email": {
						required: true,
						email: true,
						remote: "index.php?controller=pjAdminDrivers&action=pjActionCheckEmail&id=" + $frmUpdateClient.find("input[name='id']").val()
					}
				},
				messages: {
					"email": {
						remote: myLabel.email_exists
					}
				}
			});
		}
		function formatOrders(str, obj) 
		{
			if(parseInt(str, 10) > 0)
			{
				return '<a href="index.php?controller=pjAdminBookings&action=pjActionIndex&client_id='+obj.id+'">'+str+'</a>';
			}else{
				return str;
			}
		}
		if ($("#grid").length > 0 && datagrid) 
		{
			var buttonOpts = [];
			var actionOpts = [];
			if(pjGrid.hasDeleteMulti)
			{
				actionOpts.push({text: myLabel.delete_selected, url: "index.php?controller=pjAdminDrivers&action=pjActionDeleteDriverBulk", render: true, confirmation: myLabel.delete_confirmation});
			}
			if(pjGrid.hasUpdate)
			{
				buttonOpts.push({type: "edit", url: "index.php?controller=pjAdminDrivers&action=pjActionUpdate&id={:id}"});
			}
			if(pjGrid.hasDeleteSingle)
			{
				buttonOpts.push({type: "delete", url: "index.php?controller=pjAdminDrivers&action=pjActionDeleteDriver&id={:id}"});
			}
			if(pjGrid.hasRevertStatus)
			{
				actionOpts.push({text: myLabel.revert_status, url: "index.php?controller=pjAdminDrivers&action=pjActionStatusDriver", render: true});
			}
			if(pjGrid.hasExport)
			{
				actionOpts.push({text: myLabel.exported, url: "index.php?controller=pjAdminDrivers&action=pjActionExportDriver", ajax: false});
			}

			function formatImage(val, obj) {
				console.log(val); 
			var src = val != null ? val : 'app/web/img/backend/no-image.png';
			return ['<a href="#', obj.id ,'"><img src="', src, '" style="width: 100px" /></a>'].join("");
		}

			var $grid = $("#grid").datagrid({
				buttons: buttonOpts,
				columns: [
						{text: myLabel.thumb_path, type: "text", sortable: false, editable: false, renderer: formatImage},
						{text: myLabel.name, type: "text", sortable: true, editable: false},
						{text: myLabel.email, type: "text", sortable: true, editable: pjGrid.hasUpdate},
						{text: myLabel.phone, type: "text", sortable: true, editable: pjGrid.hasUpdate},
						{text: myLabel.license_number, type: "text", sortable: true, editable: pjGrid.hasUpdate},
						{text: myLabel.license_expiry, type: "text", sortable: true, editable: pjGrid.hasUpdate},
						{text: myLabel.vehicle_id, type: "text", sortable: true, editable: pjGrid.hasUpdate},
						{text: myLabel.status, type: "toggle", sortable: true, editable: pjGrid.hasUpdate, positiveLabel: myLabel.active, positiveValue: "T", negativeLabel: myLabel.inactive, negativeValue: "F"},
				          ],
				dataUrl: "index.php?controller=pjAdminDrivers&action=pjActionGetDriver",
				dataType: "json",
				fields: ['thumb_path', 'name', 'email', 'phone', 'license_number', 'license_expiry', 'vehicle_id', 'status'],
				paginator: {
					actions: actionOpts,
					gotoPage: true,
					paginate: true,
					total: true,
					rowCount: true
				},
				saveUrl: "index.php?controller=pjAdminDrivers&action=pjActionSaveClient&id={:id}",
				select: {
					field: "id",
					name: "record[]",
					cellClass: 'cell-width-2'
				}
			});
		}
			
		if ($('#dateTimePickerOptionss').length)
		{
			var currentDate = new Date(),
				$optionsEle = $('#dateTimePickerOptionss');
			moment.updateLocale('en', {
				week: { dow: parseInt($optionsEle.data('wstart'), 10) },
				months: $optionsEle.data('months').split("_"),
				weekdaysMin: $optionsEle.data('days').split("_")
			});
			var datetimeOptions = {
				format: $optionsEle.data('format'),
				locale: moment.locale('en'),
				allowInputToggle: true,
				ignoreReadonly: true,
				useCurrent: false
			};

			var dateOnlyOptions = {
				format: 'YYYY-MM-DD',
				locale: moment.locale('en'),
				allowInputToggle: true,
				ignoreReadonly: true,
				useCurrent: false
			};
			$('.datetimepick').datetimepicker(dateOnlyOptions);
			$('.datetimepick_dob').datetimepicker(dateOnlyOptions);
		}

		$(document).on("click", ".btn-all", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			$(this).addClass("btn-primary active").removeClass("btn-default")
				.siblings(".btn").removeClass("btn-primary active").addClass("btn-default");
			var content = $grid.datagrid("option", "content"),
				cache = $grid.datagrid("option", "cache");
			$.extend(cache, {
				status: "",
				q: ""
			});
			$grid.datagrid("option", "cache", cache);
			$grid.datagrid("load", "index.php?controller=pjAdminDrivers&action=pjActionGetDriver", "name", "ASC", content.page, content.rowCount);
			
		}).on("click", ".image_btn", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			var fileId = $(this).val(); // get value attribute
    		 // Call your AJAX delete function here
			$.ajax({
				url: "index.php?controller=pjAdminDrivers&action=pjActionDeleteDriverFiles&id=" + fileId,
				type: 'POST',
				data: { id: fileId },
				success: function(response) {
					console.log('Deleted:', response);
					// Optionally remove file from UI:
					// $(this).closest('.file-wrapper').remove();
				},
				error: function(xhr) {
					console.error('Delete failed:', xhr);
				}
			});
			
		}).on("click", ".btn-filter", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			var $this = $(this),
				content = $grid.datagrid("option", "content"),
				cache = $grid.datagrid("option", "cache"),
				obj = {};
			$this.addClass("btn-primary active").removeClass("btn-default")
				.siblings(".btn").removeClass("btn-primary active").addClass("btn-default");
			obj.status = "";
			obj[$this.data("column")] = $this.data("value");
			$.extend(cache, obj);
			$grid.datagrid("option", "cache", cache);
			$grid.datagrid("load", "index.php?controller=pjAdminDrivers&action=pjActionGetDriver", "name", "ASC", content.page, content.rowCount);
			
		}).on("submit", ".frm-filter", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			var $this = $(this),
				content = $grid.datagrid("option", "content"),
				cache = $grid.datagrid("option", "cache");
			$.extend(cache, {
				q: $this.find("input[name='q']").val()
			});
			$grid.datagrid("option", "cache", cache);
			$grid.datagrid("load", "index.php?controller=pjAdminDrivers&action=pjActionGetDriver", "name", "ASC", content.page, content.rowCount);
			return false;
		});
	});
})(jQuery_1_8_2);