<?php
require "_header.php";
require "includeMultiSelect.php";
require "data/combo_semester.php";
require "data/combo_student.php";
require "data/combo_branchyear.php"; ?>

<div id="main-content">
    <style>
        label.myBold {
            font-weight: bold;
            color: #006;
            font-size: 14px;
        }
    </style>

    <div id="abcd">
        <script language="javascript" type="text/javascript">
            Ext.onReady(function () {
                Ext.QuickTips.init();
                var labelArray;
                labelArray = [
                    ['Course:<span class="comp">*</span>'],
                    ['Year:<span class="comp">*</span>'],
                    ['Course Year:'],
                    ['Semester:']
                ];

                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 branch_left = new Ext.form.ComboBox({
                    name: 'branch_left',
                    id: 'branch_left',
                    hiddenName: 'branch_hidden_left',
                    labelSeparator: '',
                    fieldLabel: labelArray[0],
                    displayField: 'branch_name',
                    valueField: 'branch_id',
                    editable: true,
                    triggerAction: 'all',
                    mode: 'local',
                    allowBlank: false,
                    store: branchstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {
                            semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
                            yearstore.filter('branch_id', cmb.getValue(), false, true, true);
                            semesterstore.filter('branch_id', cmb.getValue(), false, true, true);

                            var semesterCombo = Ext.getCmp('semester_left');
                            if (cmb.getValue() == 3 || cmb.getValue() == 5) {
                                semesterCombo.enable();
                            } else {
                                semesterCombo.disable();
                                semesterCombo.reset();
                            }
                        }
                    }
                });

                var branch_right = new Ext.form.ComboBox({
                    name: 'branch_right',
                    id: 'branch_right',
                    hiddenName: 'branch_hidden_right',
                    labelSeparator: '',
                    fieldLabel: labelArray[0],
                    displayField: 'branch_name',
                    valueField: 'branch_id',
                    editable: true,
                    triggerAction: 'all',
                    mode: 'local',
                    allowBlank: false,
                    store: branchstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {
                            semesterstore.filter('branch_id', cmb.getValue(), false, true, true);
                            yearstore.filter('branch_id', cmb.getValue(), false, true, true);
                            semesterstore.filter('branch_id', cmb.getValue(), false, true, true);

                            var semesterCombo = Ext.getCmp('semester_right');
                            if (cmb.getValue() == 3 || cmb.getValue() == 5) {
                                semesterCombo.enable();
                            } else {
                                semesterCombo.disable();
                                semesterCombo.reset();
                            }
                        }
                    }
                });

                var year_left = new Ext.form.ComboBox({
                    name: 'year_left',
                    id: 'year_left',
                    hiddenName: 'year_hidden_left',
                    labelSeparator: '',
                    fieldLabel: labelArray[1],
                    displayField: 'year_name',
                    valueField: 'year_id',
                    editable: true,
                    triggerAction: 'all',
                    mode: 'local',
                    lastQuery: '',
                    allowBlank: false,
                    store: batchyearstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {

                        }
                    }

                });

                var year_right = new Ext.form.ComboBox({
                    name: 'year_right',
                    id: 'year_right',
                    hiddenName: 'year_hidden_right',
                    labelSeparator: '',
                    fieldLabel: labelArray[1],
                    displayField: 'year_name',
                    valueField: 'year_id',
                    editable: true,
                    lastQuery: '',
                    triggerAction: 'all',
                    mode: 'local',
                    allowBlank: false,
                    store: batchyearstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {

                        }
                    }
                });
                var branchyear_left = new Ext.form.ComboBox({
                    name: 'branchyear_left',
                    id: 'branchyear_left',
                    hiddenName: 'branchyear_hidden_left',
                    labelSeparator: '',
                    fieldLabel: labelArray[2],
                    displayField: 'branchyear_name',
                    valueField: 'branchyear_id',
                    editable: true,
                    triggerAction: 'all',
                    mode: 'local',
                    lastQuery: '',
                    allowBlank: true,
                    store: yearstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {

                        }
                    }

                });

                var branchyear_right = new Ext.form.ComboBox({
                    name: 'branchyear_right',
                    id: 'branchyear_right',
                    hiddenName: 'branchyear_hidden_right',
                    labelSeparator: '',
                    fieldLabel: labelArray[2],
                    displayField: 'branchyear_name',
                    valueField: 'branchyear_id',
                    editable: true,
                    lastQuery: '',
                    triggerAction: 'all',
                    mode: 'local',
                    allowBlank: true,
                    store: yearstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {

                        }
                    }
                });


                var semester_left = new Ext.form.ComboBox({
                    name: 'semester_left',
                    id: 'semester_left',
                    hiddenName: 'semester_hidden_left',
                    labelSeparator: '',
                    fieldLabel: labelArray[3],
                    displayField: 'semester_name',
                    valueField: 'semester_id',
                    editable: true,
                    triggerAction: 'all',
                    mode: 'local',
                    lastQuery: '',
                    allowBlank: true,
                    disabled: true,
                    store: semesterstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {

                        }
                    }

                });

                var semester_right = new Ext.form.ComboBox({
                    name: 'semester_right',
                    id: 'semester_right',
                    hiddenName: 'semester_hidden_right',
                    labelSeparator: '',
                    fieldLabel: labelArray[3],
                    displayField: 'semester_name',
                    valueField: 'semester_id',
                    editable: true,
                    lastQuery: '',
                    triggerAction: 'all',
                    mode: 'local',
                    allowBlank: true,
                    disabled: true,
                    store: semesterstore,
                    listeners: {
                        'select': function (cmb, rec, idx) {

                        }
                    }
                });

                var studentstore_left = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({ url: 'data/getstudentdata.php' }),
                    reader: new Ext.data.JsonReader({ id: 'student_id', root: 'data' },
                        [{ name: 'value', mapping: 'student_id' }, { name: 'text', mapping: 'student_name' }])
                });


                studentstore_right = new Ext.data.JsonStore({
                    url: 'data/getstudentdata1.php',
                    root: 'data',
                    totalProperty: 'total',
                    fields: [{ name: 'value', mapping: 'student_id' }, { name: 'text', mapping: 'student_name' }]
                });

                var YearTransferForm = new Ext.FormPanel({
                    id: 'YearTransferForm',
                    frame: true,
                    labelAlign: 'left',
                    bodyStyle: 'padding:5px',
                    url: 'save-yeartransfer.php',
                    method: 'post',
                    layout: 'column',
                    items: [{
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [{
                            xtype: 'label',
                            text: 'From Year',
                            style: 'padding:0px 0px',
                            cls: 'x-form-item myBold'
                        }]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [{
                            xtype: 'label',
                            text: 'To Year',
                            style: 'padding:0px 0px',
                            cls: 'x-form-item myBold'
                        }]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [branch_left]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [branch_right]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [year_left]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [year_right]
                    },
                    {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [branchyear_left]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [branchyear_right]
                    },
                    {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [semester_left]
                    }, {
                        columnWidth: .5,
                        labelWidth: 75,
                        layout: 'form',
                        items: [semester_right]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                            xtype: 'button',
                            text: 'Show',
                            style: 'padding:0px 0px 10px 235px',

                            handler: function () {
                                var branch_left = Ext.getCmp('branch_left').getValue();
                                var branch_right = Ext.getCmp('branch_right').getValue();
                                var year_left = Ext.getCmp('year_left').getValue();
                                var year_right = Ext.getCmp('year_right').getValue();

                                var branchyear_left = Ext.getCmp('branchyear_left').getValue();
                                var branchyear_right = Ext.getCmp('branchyear_right').getValue();
                                var semester_left = Ext.getCmp('semester_left').getValue();
                                var semester_right = Ext.getCmp('semester_right').getValue();
                                if (branch_left != "" && year_left != "" && branchyear_left != "") {

                                    Ext.getCmp('branch_left').disable();
                                    Ext.getCmp('branch_right').disable();
                                    Ext.getCmp('year_left').disable();
                                    Ext.getCmp('year_right').disable();
                                    Ext.getCmp('semester_left').disable();
                                    Ext.getCmp('semester_right').disable();
                                    Ext.getCmp('branchyear_left').disable();
                                    Ext.getCmp('branchyear_right').disable();

                                    studentstore_left.load({
                                        params: {
                                            branch_id: Ext.getCmp('branch_left').getValue(),
                                            year_id: Ext.getCmp('year_left').getValue(),
                                            branchyear_id: Ext.getCmp('branchyear_left').getValue(),
                                           semester_id: Ext.getCmp('semester_left').getValue()
                                        }
                                    });
                                    studentstore_right.load({
                                        params: {
                                            branch_id: Ext.getCmp('branch_right').getValue(),
                                            year_id: Ext.getCmp('year_right').getValue(),
                                            branchyear_id: Ext.getCmp('branchyear_right').getValue(),
                                            semester_id: Ext.getCmp('semester_right').getValue()
                                        }
                                    });

                                }
                                else if (branch_left != "" && year_left != "" && branchyear_left != "" && semester_left != "") {

                                    Ext.getCmp('branch_left').disable();
                                    Ext.getCmp('branch_right').disable();
                                    Ext.getCmp('year_left').disable();
                                    Ext.getCmp('year_right').disable();
                                    Ext.getCmp('semester_left').disable();
                                    Ext.getCmp('semester_right').disable();
                                    Ext.getCmp('branchyear_left').disable();
                                    Ext.getCmp('branchyear_right').disable();

                                    studentstore_left.load({
                                        params: {
                                            branch_id: Ext.getCmp('branch_left').getValue(),
                                            year_id: Ext.getCmp('year_left').getValue(),
                                            semester_id: Ext.getCmp('semester_left').getValue(),
                                            semester_id: Ext.getCmp('branchyear_left').getValue()
                                           
                                        }
                                    });
                                    studentstore_right.load({
                                        params: {
                                            branch_id: Ext.getCmp('branch_right').getValue(),
                                            year_id: Ext.getCmp('year_right').getValue(),
                                            semester_id: Ext.getCmp('semester_right').getValue(),
                                            semester_id: Ext.getCmp('branchyear_right').getValue()
                                        }
                                    });

                                }
                                else {
                                    Ext.MessageBox.show({
                                        title: 'Error',
                                        msg: 'Fill up all of the above Fields',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.ERROR
                                    });
                                }
                            }
                        }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                            xtype: 'button',
                            text: 'Reset',
                            style: 'padding:0px 0px 10px 0px',
                            handler: function () {
                                Ext.getCmp('branch_left').reset();
                                Ext.getCmp('branch_right').reset();
                                Ext.getCmp('year_left').reset();
                                Ext.getCmp('year_right').reset();

                                Ext.getCmp('branchyear_left').reset();
                                Ext.getCmp('branchyear_right').reset();
                                Ext.getCmp('semester_left').reset();
                                Ext.getCmp('semester_right').reset();
                                studentstore_left.removeAll();
                                studentstore_right.removeAll();
                                Ext.getCmp('branch_left').enable();
                                Ext.getCmp('branch_right').enable();
                                Ext.getCmp('year_left').enable();
                                Ext.getCmp('year_right').enable();

                                Ext.getCmp('branchyear_left').enable();
                                Ext.getCmp('branchyear_right').enable();
                                Ext.getCmp('semester_left').enable();
                                Ext.getCmp('semester_right').enable();
                                Ext.getCmp('branch_left').focus(true, 500);
                            }
                        }]
                    }, {
                        columnWidth: 1,
                        layout: 'form',
                        labelWidth: 10,
                        items: [{
                            xtype: 'itemselector',
                            name: 'studentlist_left',
                            id: 'studentlist_left',
                            fieldLabel: '',
                            multiselects: [{
                                name: 'first',
                                id: 'first',
                                width: 370,
                                height: 200,
                                store: studentstore_left,
                                displayField: 'text',
                                valueField: 'value',
                                legend: ''
                            }, {
                                width: 370,
                                height: 200,
                                store: studentstore_right,
                                displayField: 'text',
                                valueField: 'value',
                                legend: ''
                            }]
                        }]
                    }, {
                        columnWidth: 1,
                        layout: 'form',
                        labelWidth: 10,
                        items: [{
                            xtype: 'itemselector',
                            name: 'studentlist_right',
                            id: 'studentlist_right',
                            fieldLabel: '',
                            hidden: true,
                            multiselects: [{
                                width: 225,
                                height: 200,
                                store: studentstore_right,
                                displayField: 'text',
                                valueField: 'value',
                                legend: ''
                            }, {
                                width: 225,
                                height: 200,
                                store: studentstore_left,
                                displayField: 'text',
                                valueField: 'value',
                                legend: ''
                            }]
                        }]
                    }]
                });

                studentstore_right.on({
                    'afterload': {
                        fn: function (store, options) {
                            if (studentstore_right.getCount() <= 0 && studentstore_left.getCount() <= 0) {
                                Ext.MessageBox.show({
                                    title: 'Error',
                                    msg: 'There is no Student Found in this Selection',
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.ERROR
                                });
                            }
                        }
                    }
                });

                var YearTransferWindow = new Ext.Window({
                    title: 'Year Transfer',
                    width: 820,
                    height: 460,
                    modal: true,
                    border: false,
                    layout: 'fit',
                    closeAction: 'close',
                    collapsible: true,
                    items: YearTransferForm,
                    buttonAlign: 'center',
                    buttons: [{
                        text: 'Save',
                        handler: function () {
                            //alert("in save click");  
                            var year_left = Ext.getCmp('year_left').getValue();
                            var year_right = Ext.getCmp('year_right').getValue();


                            var yearLeftStart = parseInt(year_left.split('-')[0], 10);
                            var yearRightStart = parseInt(year_right.split('-')[0], 10);

                            if (yearLeftStart > yearRightStart) {
                                
                                Ext.MessageBox.show({
                                    title: 'Error',
                                    msg: 'The "From Year" cannot be greater than the "To Year". Please select valid years.',
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.ERROR
                                });
                                return; 
                            }
                            Ext.getCmp('branch_left').enable();
                            Ext.getCmp('branch_right').enable();
                            Ext.getCmp('year_left').enable();
                            Ext.getCmp('year_right').enable();


                            Ext.getCmp('branchyear_left').enable();
                            Ext.getCmp('branchyear_right').enable();
                            Ext.getCmp('semester_left').enable();
                            Ext.getCmp('semester_right').enable();
                            //alert(Ext.getCmp('year_right').getValue());
                            //alert(Ext.getCmp('branch_right').getValue());

                            YearTransferForm.getForm().submit({
                                waitMsg: 'Saving...',
                                success: function (resp) {
                                    Ext.MessageBox.show({
                                        title: 'Message',
                                        msg: 'Data has been saved..',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.INFO
                                    });
                                    YearTransferForm.getForm().reset();
                                    studentstore_left.removeAll();
                                    studentstore_right.removeAll();
                                },
                                failure: function (resp) {
                                    Ext.MessageBox.show({
                                        title: 'Error',
                                        msg: 'Saving data failed!',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.ERROR
                                    });
                                    Ext.getCmp('branch_left').disable();
                                    Ext.getCmp('branch_right').disable();
                                    Ext.getCmp('year_left').disable();
                                    Ext.getCmp('year_right').disable();
                                    Ext.getCmp('branchyear_left').disable();
                                    Ext.getCmp('branchyear_right').disable();
                                    Ext.getCmp('semester_left').disable();
                                    Ext.getCmp('semester_right').disable();
                                }
                            });
                        }
                    }, {
                        text: 'Close',
                        handler: function () {
                            YearTransferWindow.close();
                        }
                    }]
                });


  
                var varWidth = (screen.width - YearTransferWindow.width) / 2;
                YearTransferWindow.setPosition(varWidth, ((screen.height / 4) - 85));
                YearTransferWindow.show();
                Ext.getCmp('branch_left').focus(true, 1000);
            });
        </script>
    </div>
</div>
<?php require "_footer.php"; ?>