<?php
require "_header.php";
require "includeGridFilters.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
$username = $_SESSION["VedantnrUserName"];
?>
<div id="main-content">
	<script>
		var sec;
		var store = new Ext.data.JsonStore({
			url: 'data/view-conduct-certificate-data.php',
			root: 'certificates',
			idProperty: 'conductcertificate_id',
			storeId: 'myStore',
			totalProperty: 'count',
			fields: [{
				name: 'conductcertificate_id',
				type: 'int'
			}, {
				name: 'college_id',
				type: 'int'
			},{
				name: 'student_name'
			},{
				name: 'branch_id',
				type: 'int'
			},{
				name: 'year_id',
				type: 'int'
			},{
				name: 'branchyear_id',
				type: 'int'
			},{
				name: 'semester_id',
				type: 'int'
			},{
				name: 'student_id',
				type: 'int'
			},{
				name: 'student_gturegno',
				type: 'string'
			},{
				name: 'certificate_no',
				type: 'int'
			},{
				name: 'student_name'
			}],
			remoteSort: true
		});

		var branchstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'branch_name'],
				data: Ext.semester.branch
			});
			var batchyearstore = new Ext.data.ArrayStore({
				fields: ['year_id', 'year_name'],
				data: Ext.semester.yearname
			});
			var semesterstore = new Ext.data.ArrayStore({
			fields: ['branch_id', 'semester_id', 'semester_name'],
			data: Ext.semester.semester
			});
			var yearstore = new Ext.data.ArrayStore({
				fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
				data: Ext.year.year
			});

			var BranchCombo = {
				xtype: 'combo',
				store: branchstore,
				id: 'branch_id',
				displayField: 'branch_name',
				valueField: 'branch_id',
				editable: false,
				mode: 'local',
				forceSelection: true,
				triggerAction: 'all',
				//emptyText: 'Select a Branch...',
				selectOnFocus: true,
				width: 220,
				listeners: {
					'select': function (cmb, rec, idx) {
						sec = this.getValue(),
						
						
						store.load({
							params: {
								'branch_id': this.getValue(),
								start: 0, limit: 10
							}
						});
					}

				}
			}
		Ext.onReady(function () {
			//start delete code
			var xg = Ext.grid;
			var sm2 = new xg.CheckboxSelectionModel({
				listeners: {
					selectionchange: function (sm) {
						if (sm.getCount() && flgDelete == 'true') {
							grid.removeButton.enable();
						} else {
							grid.removeButton.disable();
						}
					}
				}
			});
			//over code delete

			var encode = false;
			var local = false;
			//delete code start
			function confirmDelete() {
				if (grid.selModel.getCount() == 1) {
					Ext.MessageBox.confirm('Confirmation', 'You are about to delete a  Certificate. Continue?', deleteconduct);
				} else if (grid.selModel.getCount() > 1) {
					Ext.MessageBox.confirm('Confirmation', 'Delete these  Certificate(s)?', deleteconduct);
				}
			}
			function deleteconduct(btn) {
				if (btn == 'yes') {
					var selections = grid.selModel.getSelections();
					var prez = [];
					for (i = 0; i < grid.selModel.getCount(); i++) {
						prez.push(selections[i].json.conductcertificate_id);
					}
					var encoded_array = Ext.encode(prez);
					Ext.Ajax.request({
						waitMsg: 'Please Wait',
						url: 'conduct-grid-delete.php',
						params: {
							task: "deleteconduct",
							ids: encoded_array
						},
						success: function (response) {
							var result = eval(response.responseText);
							switch (result) {
								case 1:
									store.reload();
									break;
								default:
									Ext.MessageBox.alert('Warning', 'Could not delete the entire selection.');
									break;
							}
						},
						failure: function (response) {
							var result = response.responseText;
							Ext.MessageBox.alert('error', 'could not connect to the database. retry later');
						}
					});
				}
			}

			//over code
			var filters = new Ext.ux.grid.GridFilters({

				filters: [{
					type: 'numeric',
					dataIndex: 'certificate_no',
					phpMode: true
				},
				{
					type: 'numeric',
					dataIndex: 'student_gturegno',
					phpMode: true
				}, {
					type: 'string',
					dataIndex: 'student_name',
					phpMode: true
				}/*,  {
			type: 'date',
			dataIndex: 'student_birthdate',
			phpMode: true
		}*/  ]

			});

			Ext.ux.grid.PagingRowNumberer = Ext.extend(Ext.grid.RowNumberer, {
				renderer: function (v, p, record, rowIndex, colIndex, store) {
					if (this.rowspan) {
						p.cellAttr = 'rowspan="' + this.rowspan + '"';
					}
					var so = store.lastOptions;
					var sop = so ? so.params : null;
					return ((sop && sop.start) ? sop.start : 0) + rowIndex + 1;
				}
			});
			var grid = new Ext.grid.GridPanel({
				plugins: [filters],
				store: store,
				tbar: [{
					iconCls: 'icon-add',
					text: 'Add New ',
					handler: function () {
						location.href = "add-conduct-certificate.php";
					}
				}, ' | ', {
					text: 'Delete',
					handler: confirmDelete,
					iconCls: 'remove',
					ref: '../removeButton',
					disabled: true
				}, ' | ',
				{ xtype: 'tbtext', text: 'Course:' }, ' ', BranchCombo, { width: 3 },
			],
				columns: [sm2,
					new Ext.ux.grid.PagingRowNumberer(),
					{
						header: "Print", width: 43, renderer: function (value, metaData, record, rowIndex, colIndex, store) {
							return '<a href="javascript:void(0);" onclick="Ext.Msg.confirm(\'Confirmation\', \'Do you want to open the report for this certificate with header?\', function(btn) { if (btn == \'yes\') { window.open(\'report-conduct-certificate.php?ID=\' + ' + record.get('certificate_no') + ' + \'&student_id=\' + ' + record.get('student_id') + ' + \'&withHeader=true\', \'_blank\'); } else { window.open(\'report-conduct-certificate.php?ID=\' + ' + record.get('certificate_no') + ' + \'&student_id=\' + ' + record.get('student_id') + ' + \'&withHeader=false\', \'_blank\'); } });">Print</a>';
						}
					}, 
					
					{
						header: "Edit", width: 40, renderer: function (value, metaData, record, rowIndex, colIndex, store) {
							return '<a href="add-conduct-certificate.php?ID=' +  record.get('student_id') + '">Edit</a>';
						}
					},
					{ header: "Certi. No", width: 75, dataIndex: 'certificate_no', align: 'center', sortable: true },
					{ header: "Enrollment No", width: 100, dataIndex: 'student_gturegno', align: 'center', sortable: true },
					{ header: "Student Name", width: 200, dataIndex: 'student_name', align: 'left', sortable: true },
					],

				width: 532,
				height: 380,
				loadMask: true,
				frame: true,
				sm: sm2,

				bbar: new Ext.PagingToolbar({
					pageSize: 10,
					store: store,
					displayInfo: true,
					displayMsg: 'Displaying data {0} - {1} of {2}',
					emptyMsg: "No Data found",
					plugins: [filters]
				})
			});
			grid.getBottomToolbar().add([
				'->',
				{
					text: 'Clear Filter Data',
					handler: function () {
						grid.filters.clearFilters();
					}
				}
			]);
			grid.on('render', function () {
				grid.tip = new Ext.ToolTip({
					view: grid.getView(),
					target: grid.getView().mainBody,
					delegate: '.x-grid3-row',
					trackMouse: true,
					renderTo: document.body,
					listeners: {
						beforeshow: function updateTipBody(tip) {
							tip.body.dom.innerHTML = "Click on Edit Link To Edit Record.";
						}
					}
				});
			});
			store.on({
				'beforeload': {
					fn: function (store, options) {
						options.params || (options.params = {});
						Ext.apply(options.params, {
							pageNo: this.pageNumber,
							section: sec
						});
					},
					scope: this
				}
			});

			var FormWindow = new Ext.Window({
				title: 'View Course Completion / Conduct Certificate',
				height: 380,
				width: 570,
				layout: 'fit',
				modal: true,
				border: false,
				closeAction: 'close',
				collapsible: 'true',
				items: grid
			}).show();
			var loadflg = 0;

			var varWidth = (window.innerWidth - FormWindow.width) / 2;
			var varHeight = (window.innerHeight - FormWindow.height) / 4;

			FormWindow.setPosition(varWidth, varHeight);

			FormWindow.show();

			Ext.EventManager.onWindowResize(function () {
				var varWidth = (window.innerWidth - FormWindow.width) / 2;
				var varHeight = (window.innerHeight - FormWindow.height) / 4;
				FormWindow.setPosition(varWidth, varHeight);
			});
			SetSectionValue('add', branchstore, 'branch_id', 'branch_id');
		});
	</script>

	<div id="abcd" align="center"></div>
</div>
<?php require "_footer.php"; ?>