<?php
require "_header.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
?>
<div id="main-content">
    <script type="text/javascript">
        Ext.onReady(function () {
            Ext.QuickTips.init();
            var labelArray;
            labelArray = [
                ['Course '],
                ['Batch Year '],
                ['Course Year '],
                ['Semester '],
                ['Student Name '],
                ['Scholarship Name '],
                ['Formfill Date '],
                ['Scholarship Approve Date '],
                ['Scholarship Recive Date '],
                ['Scholarship Recive Price '],
                ['College Recive Date '],
                ['College Recive Price '],
            ];
            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.student.semester
            });

            var yearstore = new Ext.data.ArrayStore({
                fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
                data: Ext.year.year
            });
            var scholarshipstore = new Ext.data.ArrayStore({
                fields: ['scholarship_id', 'scholarship_name'],
                data: Ext.student.scholarship
            });

            var studentstore = new Ext.data.JsonStore({
                url: 'data/getstudent-feesrecive.php',
                root: 'data',
                totalProperty: 'total',
                fields: [
                    { name: 'student_id', type: 'int' },
                    { name: 'student_name', type: 'string' },
                    { name: 'student_gturegno', type: 'int' }
                ]
            });


            var branch_name = new Ext.form.ComboBox({
                name: 'branch_name',
                id: 'branch_name',
                hiddenName: 'branch_hidden',
                fieldLabel: labelArray[0],
                displayField: 'branch_name',
                valueField: 'branch_id',
                width: 163,
                tabIndex: 1,
                triggerAction: 'all',
                mode: 'local',
                allowBlank: true,
                store: branchstore,
                listeners: {
                    'select': function (cmb, rec, idx) {
                        Ext.getCmp('semester_name').reset();
                        Ext.getCmp('branchyear_name').reset();
                        //Ext.getCmp('scholarship_name').reset();
                        Ext.getCmp('student_id').reset();
                        studentstore.removeAll();
                        semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
                        yearstore.filter('branch_id', cmb.getValue(), false, true, true);
                        // scholarshipstore.filter('branch_id', cmb.getValue(), false, true, true);
                        var semesterCombo = Ext.getCmp('semester_name');
                        if (cmb.getValue() == 3 || cmb.getValue() == 5) {
                            semesterCombo.enable();
                        } else {
                            semesterCombo.disable();
                            semesterCombo.reset();
                        }
                    }
                }
            });
            var year_name = new Ext.form.ComboBox({
                name: 'year_name',
                id: 'year_name',
                hiddenName: 'year_hidden',
                fieldLabel: labelArray[1],
                displayField: 'year_name',
                valueField: 'year_id',
                lastQuery: '',
                triggerAction: 'all',
                mode: 'local',
                width: 163,
                tabIndex: 2,
                allowBlank: true,
                store: batchyearstore
            });
            var branchyear_name = new Ext.form.ComboBox({
                name: 'branchyear_name',
                id: 'branchyear_name',
                hiddenName: 'branchyear_hidden',
                fieldLabel: labelArray[2],
                displayField: 'branchyear_name',
                valueField: 'branchyear_id',
                lastQuery: '',
                triggerAction: 'all',
                mode: 'local',
                width: 163,
                tabIndex: 3,
                allowBlank: true,
                store: yearstore,
                listeners: {
                    'select': function (cmb, rec, idx) {
                        if (Ext.getCmp('branch_name').getValue() != "") {

                            Ext.getCmp('semester_name').reset();
                            Ext.getCmp('student_id').reset();
                            studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'branchyear_id': Ext.getCmp('branchyear_name').getValue() } });

                        } else {
                            alert('Select Branch First')
                            this.reset();
                        }

                    }
                }
            });
            var semester_name = new Ext.form.ComboBox({


                name: 'semester_name',
                id: 'semester_name',
                hiddenName: 'semester_hidden',
                fieldLabel: labelArray[3],
                displayField: 'semester_name',
                valueField: 'semester_id',
                disabled: true,
                lastQuery: '',
                triggerAction: 'all',
                mode: 'local',
                width: 163,
                tabIndex: 4,
                allowBlank: true,
                store: semesterstore,
                listeners: {
                    'select': function (cmb, rec, idx) {

                        if (Ext.getCmp('branch_name').getValue() != "") {
                            Ext.getCmp('branchyear_name').reset();
                            Ext.getCmp('student_id').reset();
                            studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'semester_id': Ext.getCmp('semester_name').getValue() } });

                        } else {
                            alert('Select Branch First')
                            this.reset();
                        }

                    }
                }
            });
            var student_id = new Ext.form.ComboBox({
                name: 'student_id',
                id: 'student_id',
                hiddenName: 'student_hidden',
                labelSeparator: '',
                fieldLabel: labelArray[4],
                displayField: 'student_name',
                valueField: 'student_id',
                width: 163,
                tabIndex: 5,
                triggerAction: 'all',
                mode: 'local',
                allowBlank: true,
                store: studentstore,
                listeners: {
                    'select': function (cmb, rec, idx) {
                        //scholarshipstore.load({ params: { 'branch_id': Ext.getCmp('branch_name').getValue(), 'semester_id': Ext.getCmp('semester_name').getValue(), 'branchyear_id': Ext.getCmp('branchyear_name').getValue(), 'student_id': Ext.getCmp('student_id').getValue() } });
                        if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('semester_name').getValue() != "") {
                        }
                        else if (Ext.getCmp('branch_name').getValue() != "" && Ext.getCmp('branchyear_name').getValue() != "") {
                        } else {
                            alert('Select Branch')
                            this.reset();
                        }
                    }
                }
            });
            var scholarship_name = new Ext.form.ComboBox({
                name: 'scholarship_name',
                id: 'scholarship_name',
                hiddenName: 'scholarship_hidden',
                fieldLabel: labelArray[5],
                displayField: 'scholarship_name',
                valueField: 'scholarship_id',
                lastQuery: '',
                triggerAction: 'all',
                mode: 'local',
                width: 163,
                tabIndex: 6,
                allowBlank: true,
                store: scholarshipstore,
                listeners: {
                    select: function (combo, record, index) {
                        var formfillDate = Ext.getCmp('formfill_date');
                        var scholarshipAproveDate = Ext.getCmp('scholarshipaprove_date');
                        var scholarshipReciveDate = Ext.getCmp('scholarshiprecive_date');
                        var scholarshipRecivePrice = Ext.getCmp('scholarshiprecive_price');
                        var collegeRevicedate = Ext.getCmp('collegerecive_date');
                        var collegeRevicePrice = Ext.getCmp('collegerecive_price');

                        formfillDate.setDisabled(false);
                        scholarshipAproveDate.setDisabled(false);
                        scholarshipReciveDate.setDisabled(false);
                        scholarshipRecivePrice.setDisabled(false);
                        collegeRevicedate.setDisabled(false);
                        collegeRevicePrice.setDisabled(false);

                    },
                    change: function (combo, newValue, oldValue) {
                        var formfillDate = Ext.getCmp('formfill_date');
                        var scholarshipAproveDate = Ext.getCmp('scholarshipaprove_date');
                        var scholarshipReciveDate = Ext.getCmp('scholarshiprecive_date');
                        var scholarshipRecivePrice = Ext.getCmp('scholarshiprecive_price');
                        var collegeRevicedate = Ext.getCmp('collegerecive_date');
                        var collegeRevicePrice = Ext.getCmp('collegerecive_price');
                        if (!newValue) {
                            formfillDate.setDisabled(true);
                            scholarshipAproveDate.setDisabled(true);
                            scholarshipReciveDate.setDisabled(true);
                            scholarshipRecivePrice.setDisabled(true);
                            collegeRevicedate.setDisabled(true);
                            collegeRevicePrice.setDisabled(true);
                        }
                    }
                }
            });
            var formfill_date = new Ext.form.DateField({
                name: 'formfill_date',
                id: 'formfill_date',
                fieldLabel: labelArray[6],
                width: 163,
                format: JSDateFormat,
                vtype: 'daterange',
                allowBlank: true,
                tabIndex: 7,
                maxValue: new Date(),
                disabled: true
            });
            var scholarshipaprove_date = new Ext.form.DateField({
                name: 'scholarshipaprove_date',
                id: 'scholarshipaprove_date',
                fieldLabel: labelArray[7],
                width: 163,
                format: JSDateFormat,
                vtype: 'daterange',
                allowBlank: true,
                tabIndex: 8,
                maxValue: new Date(),
                disabled: true
            });
            var scholarshiprecive_date = new Ext.form.DateField({
                name: 'scholarshiprecive_date',
                id: 'scholarshiprecive_date',
                fieldLabel: labelArray[8],
                width: 163,
                format: JSDateFormat,
                vtype: 'daterange',
                allowBlank: true,
                tabIndex: 9,
                maxValue: new Date(),
                disabled: true
            });
            var scholarshiprecive_price = new Ext.form.NumberField({
                name: 'scholarshiprecive_price',
                fieldLabel: labelArray[9],
                id: 'scholarshiprecive_price',
                width: 163,
                tabIndex: 10,
                allowBlank: true,
                disabled: true
            });
            var collegerecive_date = new Ext.form.DateField({

                name: 'collegerecive_date',
                id: 'collegerecive_date',
                fieldLabel: labelArray[10],
                width: 163,
                format: JSDateFormat,
                vtype: 'daterange',
                allowBlank: true,
                tabIndex: 11,
                maxValue: new Date(),
                disabled: true
            });
            var collegerecive_price = new Ext.form.NumberField({
                name: 'collegerecive_price',
                fieldLabel: labelArray[11],
                id: 'collegerecive_price',
                width: 163,
                tabIndex: 12,
                allowBlank: true,
                disabled: true
            });

            var studentscholarship_id = new Ext.form.TextField({
                name: 'studentscholarship_id',
                id: 'studentscholarship_id',
                value: '0',
                hidden: true
            });





            var summary = new Ext.grid.GroupSummary();
            var Scholarship_Fields = [
                { name: 'studentscholarship_id', type: 'int' },
                { name: 'branch_name' },
                { name: 'year_name' },
                { name: 'branchyear_name' },
                { name: 'semester_name' },
                { name: 'scholarship_name' },
                { name: 'student_name' },
                { name: 'financial_year', type: 'string' },
                { name: 'educational_year', type: 'string' },
                { name: 'formfill_date', type: 'date' },
                { name: 'scholarshipaprove_date', type: 'date' },
                { name: 'scholarshiprecive_date', type: 'date' },
                { name: 'scholarshiprecive_price', type: 'float' },
                { name: 'collegerecive_date', type: 'date' },
                { name: 'collegerecive_price', type: 'float' }
            ]


            var reader = new Ext.data.JsonReader({
                idProperty: 'studentscholarship_id',
                root: 'data',
                fields: Scholarship_Fields
            });
            var scholarship_grid_store = new Ext.data.GroupingStore({
                url: 'data/get-student-scholarship.php',
                reader: reader,
                successProperty: 'success',
                sortInfo: {
                    field: 'head_id',
                    autoLoad: true,
                    direction: 'ASC'
                },
                groupField: 'branch_name'
            });
            var colModel = new Ext.grid.ColumnModel([
                new Ext.grid.RowNumberer, {

                    dataIndex: 'studentscholarship_id',
                    width: 50,
                    sortable: true,
                    hidden: true
                }, {

                    dataIndex: 'branch_id',
                    sortable: true,
                    hidden: true
                }, {

                    dataIndex: 'semester_id',
                    sortable: true,
                    hidden: true

                }, {

                    dataIndex: 'branchyear_id',
                    sortable: true,
                    hidden: true

                }, {
                    dataIndex: 'scholarship_name',
                    header: 'Scholarship Name',
                    sortable: true,
                    editor: new Ext.form.TextField(),
                    width: 100
                },
                {
                    dataIndex: 'formfill_date',
                    header: 'Formfill Date',
                    sortable: true,
                    renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
                    editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
                    width: 100
                },
                {
                    dataIndex: 'scholarshipaprove_date',
                    header: 'Scholarship Approve Date',
                    sortable: true,
                    renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
                    editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
                    width: 100
                },
                {
                    dataIndex: 'scholarshiprecive_date',
                    header: 'Scholarship Recive Date',
                    sortable: true,
                    renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
                    editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
                    width: 100
                },
                {
                    dataIndex: 'scholarshiprecive_price',
                    header: 'Scholarship Recive Price',
                    sortable: true,
                    editor: new Ext.form.NumberField({ allowDecimals: true, decimalPrecision: 2 }),
                    width: 120
                },
                {
                    dataIndex: 'collegerecive_date',
                    header: 'College Recive Date',
                    sortable: true,
                    renderer: Ext.util.Format.dateRenderer('Y-m-d'),// Format the date as 'YYYY-MM-DD'
                    editor: new Ext.form.DateField({ format: 'Y-m-d' }),// Use a date field editor with the same format
                    width: 100
                },
                {
                    dataIndex: 'collegerecive_price',
                    header: 'College Recive Price',
                    sortable: true,
                    editor: new Ext.form.NumberField({ allowDecimals: true, decimalPrecision: 2 }),
                    width: 120
                }
            ]);
            var grid = new Ext.grid.EditorGridPanel({
	ds: scholarship_grid_store,
	cm: scholarship_colModel,
	height: 280,
	title:'',
	border: true,
	loadMask:true,
	columnLines:true,
	stripeRows:true,
	clicksToEdit:1,
	enableHdMenu: false,
	tbar: [{text: 'Scholarship'}],
	view: new Ext.grid.GroupingView({
		forceFit:true,
		showGroupName: false,
		enableNoGroups:false,
		hideGroupedColumn: true
	}),
	plugins:summary
});

            var grid = new Ext.grid.GridPanel({
                store: ScholarshipStore,
                cm: colModel,
                columnLines: true,
                loadMask: true,
                stripeRows: true,
                sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function (sm, row, rec) {
                            Ext.getCmp("ScholarshipForm").getForm().loadRecord(rec);
                            if (flgEdit == "true") {
                                Ext.getCmp('btnSave').enable();
                            }
                            else {
                                Ext.getCmp('btnSave').disable();
                                alert('You do not have access to edit the record.');
                            }
                            if (flgDelete == "true") {
                                Ext.getCmp('btnDelete').enable();
                            }
                        }
                    }
                }),
                height: 285,
                border: true
            });

            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 = "Please click here to edit record.";
                        }
                    }
                });
            });

            var ScholarshipForm = new Ext.FormPanel({
                id: 'ScholarshipForm',
                frame: true,
                url: 'save-studentscholarship.php',
                labelAlign: 'left',
                autoHeight: true,
                bodyStyle: 'padding:5px',
                layout: 'column',
                items: [{
                    xtype: 'container',
                    autoEl: {},
                    layout: 'form',
                    columnWidth: .32,
                    labelWidth: 90,
                    items: [

                    ]
                }, {
                    xtype: 'container',
                    autoEl: {},
                    layout: 'form',
                    columnWidth: .68,
                    items: [grid]
                }]
            });

            var ScholarshipWindow = new Ext.Window({
                title: 'Scholarship Master',
                width: 1010,
                autoHeight: true,
                frame: true,
                border: false,
                modal: true,
                layout: 'fit',
                closeAction: 'close',
                collapsible: true,
                items: ScholarshipForm,
                buttons: [
                    {
                        text: 'Save', iconCls: 'icon-save', name: 'btnSave', id: 'btnSave',
                        handler: function () {
                            var branch_name = ScholarshipForm.getForm().findField('branch_name').getValue();
                            var semester_name = ScholarshipForm.getForm().findField('semester_name').getValue();
                            var branchyear_name = ScholarshipForm.getForm().findField('branchyear_name').getValue();
                            var year_name = ScholarshipForm.getForm().findField('year_name').getValue();
                            var student_id = ScholarshipForm.getForm().findField('student_id').getValue();
                            var scholarship_name = ScholarshipForm.getForm().findField('scholarship_name').getValue();
                            var studentscholarship_id = ScholarshipForm.getForm().findField('studentscholarship_id').getValue();
                            var formfill_date = ScholarshipForm.getForm().findField('formfill_date').getValue();
                            var scholarshipaprove_date = ScholarshipForm.getForm().findField('scholarshipaprove_date').getValue();
                            var scholarshiprecive_date = ScholarshipForm.getForm().findField('scholarshiprecive_date').getValue();
                            var scholarshiprecive_price = ScholarshipForm.getForm().findField('scholarshiprecive_price').getValue();
                            var collegerecive_date = ScholarshipForm.getForm().findField('collegerecive_date').getValue();
                            var collegerecive_price = ScholarshipForm.getForm().findField('collegerecive_price').getValue();
                            ScholarshipForm.getForm().submit({
                                waitMsg: 'Saving...',
                                success: function () {
                                    Ext.MessageBox.show({
                                        title: 'Message',
                                        msg: 'Data has been saved.',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.INFO
                                    });
                                    ScholarshipStore.load();
                                    ScholarshipForm.getForm().reset();
                                    grid.getSelectionModel().clearSelections();
                                },
                                failure: function () {
                                    Ext.MessageBox.show({
                                        title: 'Error',
                                        msg: 'Please fill up complete form.',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.ERROR
                                    });
                                }
                            });
                        }
                    },
                    {
                        text: 'Delete',
                        iconCls: 'remove',
                        disabled: true,
                        id: 'btnDelete',
                        name: 'btnDelete',
                        handler: function () {
                            Ext.MessageBox.confirm('Confirmation', ' Are you sure want to delete This Record.?', deleterecord);
                        }
                    },
                    {
                        text: 'Reset',
                        name: 'btnreset',
                        iconCls: 'icon-reset',
                        id: 'btnreset',
                        handler: function () {
                            ScholarshipForm.getForm().reset();
                            ScholarshipStore.load();
                            Ext.getCmp('btnDelete').disable();
                            Ext.getCmp('branch_id').focus(true, 500);
                            if (flgAdd == "true") {
                                Ext.getCmp('btnSave').enable();
                            }
                        }
                    },
                    {
                        text: 'Close',
                        name: 'btnclose',
                        iconCls: 'icon-close',
                        id: 'btnclose',
                        handler: function () {
                            ScholarshipWindow.close();
                        }
                    }
                ]
            });
            function deleterecord(btn) {
                if (btn == 'yes') {
                    ScholarshipForm.getForm().submit({
                        waitMsg: 'Saving...',
                        params: { act: 'del' },
                        success: function () {
                            Ext.getCmp('scholarship_id').reset();
                            Ext.getCmp('branch_id').reset();
                            Ext.getCmp('semester_id').reset();
                            Ext.getCmp('year_id').reset();
                            Ext.getCmp('branchyear_id').reset();
                            Ext.getCmp('semester_id').reset();
                            Ext.getCmp('year_id').reset();
                            Ext.getCmp('btnDelete').disable();
                            Ext.getCmp('studentscholarship_id').setValue('0');
                            LoadData();
                            Ext.getCmp('studentscholarship_id').focus(true, 500);
                        }
                    });
                }
            }
            var varWidth = (screen.width - ScholarshipWindow.width) / 2;
            ScholarshipWindow.setPosition(varWidth, ((screen.height / 4) - 85));
            ScholarshipWindow.show();
            if (flgAdd == "true") {
                Ext.getCmp('btnSave').enable();
            }
            else {
                Ext.getCmp('btnSave').disable();
            }
        });
    </script>
    <div id="abcd"></div>
</div>
<?php require "_footer.php"; ?>