<?php
require "_header.php";
require "data/combo_student.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
error_reporting(1);
$certificate_no = $_REQUEST["ID"];
$student_id = Escape($_REQUEST["student_id"]);

if ($certificate_no != "" && isset($certificate_no)) {
    $mode = "edit";
    if ($EDIT == "false") {
        redirectToPage();
    }
    $limit = 1;
    $start = 0;
    $condition = "";
    $order_field = "";
    $order_type = "";

    if ($certificate_no != "") {
        $condition .= " and cm.certificate_no='$certificate_no'";
    }
    if ($student_id != "") {
        $condition .= " and cm.student_id='$student_id'";
    }

    $objcum = new cumrelivingCertificateMgmt();
    $out = $objcum->getAllRecords($limit, $start, $condition, $order_field, $order_type);
    if (dbRows($out)) {
        $row = dbArray($out);
        extract($row);
    }

} else {
    $mode = "add";
    if ($ADD == "false") {
        redirectToPage();
    }
    $certificate_no = 0;
}
?>

<!--<script type="text/javascript" src="js/studentformitems.js"></script>-->
<script language="javascript" type="text/javascript">
    var labelArray;
    labelArray = [
        ['Course <span class="comp">*</span>'],
        ['Batch Year '],
        ['Course Year '],
        ['Semester '],
        ['Student Name '],
        ['Enrollment No']
    ];
    var formMode = '<?php echo $mode; ?>';
    var add = '<?php echo $_REQUEST["add"]; ?>';
    var varcertno = 0;

    var cumrelivingcertificate_id = '<?php echo $cumrelivingcertificate_id; ?>';

    var bid = 0;

    Ext.onReady(function () {

        Ext.QuickTips.init();
        var dt = new Date();
        var varbid = 0;
        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
        });

        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: 'string'
            }]
        });

        var studentcumrelivingentry = new Ext.FormPanel({
            defaults: {
                style: 'padding:0px 0px 12px 8px'
            },

            labelWidth: 125,
            url: 'save-course-completion-certificate.php',
            method: 'post',
            frame: true,
            bodyStyle: 'padding:5px 5px 0',
            items: [{
                layout: 'column',
                items: [{
                    columnWidth: .5,
                    labelWidth: 80,
                    layout: 'form',
                    items: [{
                        xtype: 'combo',
                        name: 'branch_id',
                        id: 'branch_id',
                        hiddenName: 'branch_hidden',
                        fieldLabel: labelArray[0],
                        displayField: 'branch_name',
                        valueField: 'branch_id',
                        width: 150,
                        triggerAction: 'all',
                        mode: 'local',
                        forceSelection: true,
                        selectOnFocus: true,
                        allowBlank: false,
                        store: branchstore,
                        listeners: {
                            'select': function (cmb, rec, idx) {
                                varbid = cmb.getValue('branch_id');
                                studentcumrelivingentry.getForm().findField('student_gturegno').enable();
                                studentcumrelivingentry.getForm().findField('student_gturegno').focus(true, 500);
                                Ext.getCmp('semester_id').reset();
                                Ext.getCmp('year_id').reset();
                                Ext.getCmp('branchyear_id').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);
                                var semesterCombo = Ext.getCmp('semester_id');
                                if (cmb.getValue() == 3 || cmb.getValue() == 5) {
                                    semesterCombo.enable();
                                } else {
                                    semesterCombo.disable();
                                    semesterCombo.reset();
                                }
                                var bid = studentcumrelivingentry.getForm().findField('branch_id').getValue();
                                Ext.Ajax.request({
                                    loadMask: true,
                                    sync: true,
                                    url: 'data/getcertificateno.php',
                                    params: {
                                        branch_id: bid,
                                        action: 'cum'
                                    },
                                    waitMsg: 'Loading',
                                    failure: function () {
                                        Ext.MessageBox.show({
                                            title: 'Error',
                                            msg: 'Invalid GR No.',
                                            buttons: Ext.MessageBox.OK,
                                            icon: Ext.MessageBox.ERROR
                                        });
                                    },
                                    success: function (resp) {
                                        varcertno = Ext.decode(resp.responseText).certno;
                                        studentcumrelivingentry.getForm().findField('certificate_no').setValue(varcertno);
                                    }
                                });
                            }
                        }
                    }, {
                        xtype: 'combo',
                        name: 'branchyear_id',
                        id: 'branchyear_id',
                        hiddenName: 'branchyear_hidden',
                        fieldLabel: labelArray[2],
                        displayField: 'branchyear_name',
                        valueField: 'branchyear_id',
                        lastQuery: '',
                        triggerAction: 'all',
                        mode: 'local',
                        width: 150,
                        allowBlank: true,
                        store: yearstore,
                        listeners: {
                            'select': function (cmb, rec, idx) {
                                if (Ext.getCmp('branch_id').getValue() != "") {
                                    studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_id').getValue(), 'year_id': Ext.getCmp('year_id').getValue(), 'branchyear_id': Ext.getCmp('branchyear_id').getValue() } });
                                } else {
                                    alert('Select Branch First')
                                    this.reset();
                                }
                            }
                        }
                    }, {
                        xtype: 'textfield',
                        name: 'certificate_no',
                        id: 'certificate_no',
                        fieldLabel: 'Certi. No',
                        readOnly: true,
                        width: 100
                    }
                    ]
                },
                {
                    columnWidth: .5,
                    layout: 'form',
                    labelWidth: 90,
                    items: [{
                        xtype: 'combo',
                        name: 'year_id',
                        id: 'year_id',
                        hiddenName: 'year_hidden',
                        fieldLabel: labelArray[1],
                        displayField: 'year_name',
                        valueField: 'year_id',
                        lastQuery: '',
                        triggerAction: 'all',
                        mode: 'local',
                        width: 150,
                        allowBlank: true,
                        store: batchyearstore,
                        listeners: {
                            'select': function (cmb, rec, idx) {
                                if (Ext.getCmp('branch_id').getValue() != "") {
                                    // Ext.getCmp('student_name').reset();
                                    studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_id').getValue(), 'year_id': Ext.getCmp('year_id').getValue() } });
                                } else {
                                    this.reset();
                                }
                            }
                        }
                    }, {
                        xtype: 'combo',
                        name: 'semester_id',
                        id: 'semester_id',
                        hiddenName: 'semester_hidden',
                        fieldLabel: labelArray[3],
                        displayField: 'semester_name',
                        valueField: 'semester_id',
                        lastQuery: '',
                        disabled: true,
                        triggerAction: 'all',
                        mode: 'local',
                        width: 150,
                        allowBlank: true,
                        store: semesterstore,
                        listeners: {
                            'select': function (cmb, rec, idx) {
                                if (Ext.getCmp('branch_id').getValue() != "") {
                                    studentstore.load({ params: { 'branch_id': Ext.getCmp('branch_id').getValue(), 'year_id': Ext.getCmp('year_id').getValue(), 'semester_id': Ext.getCmp('semester_id').getValue() } });
                                } else {
                                    alert('Select Branch First')
                                    this.reset();
                                }
                            }
                        }
                    },
                    ]
                }
                ]
            }, {
                layout: 'column',
                labelWidth: 125,
                items: [{
                    columnWidth: .6,
                    layout: 'form',
                    items: [{
                        xtype: 'textfield',
                        name: 'student_gturegno',
                        id: 'student_gturegno',
                        fieldLabel: labelArray[5],
                        allowBlank: true,
                        width: 150
                    },
                    ]
                }
                ]
            }, {
                layout: 'column',
                labelWidth: 125,
                items: [{
                    columnWidth: .7,
                    layout: 'form',
                    items: [{
                        xtype: 'combo',
                        name: 'student_id',
                        id: 'student_id',
                        hiddenName: 'student_hidden',
                        fieldLabel: labelArray[4],
                        displayField: 'student_name',
                        valueField: 'student_id',
                        width: 200,
                        triggerAction: 'all',
                        mode: 'local',
                        allowBlank: true,
                        store: studentstore,
                        listeners: {
                            'select': function (cmb, rec, idx) {
                                if (Ext.getCmp('branch_id').getValue() != "" && Ext.getCmp('year_id').getValue() != "") {
                                    Ext.getCmp('student_gturegno').setValue(studentstore.getAt(idx).data.student_gturegno);
                                }
                                else if (Ext.getCmp('branch_id').getValue() != "" && Ext.getCmp('branchyear_id').getValue() != "") {
                                    Ext.getCmp('student_gturegno').setValue(studentstore.getAt(idx).data.student_gturegno);
                                }
                                else if (Ext.getCmp('branch_id').getValue() != "" && Ext.getCmp('semester_id').getValue() != "") {
                                    Ext.getCmp('student_gturegno').setValue(studentstore.getAt(idx).data.student_gturegno);
                                }
                                else {
                                    this.reset();
                                }
                            }
                        }
                    }, {
                        xtype: 'textfield',
                        name: 'cumrelivingcertificate_id',
                        id: 'cumrelivingcertificate_id',
                        hidden: true,
                        value: cumrelivingcertificate_id
                    },
                    ]
                }
                ]
            },
            ]
        });

        studentcumrelivingentry.on({
            actioncomplete: function (form, action) {
                if (action.type == 'submit') {
                    if (action.result.success == "false") {
                        if (action.result.msg == "duplicate")
                            Ext.MessageBox.show({
                                title: 'Error',
                                msg: 'Cum Reliving Certificate for this Student already exists',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR
                            });

                    } else if (action.result.success == "true") {
                        Ext.MessageBox.show({
										title: 'Message',
										msg: 'Data has been saved..',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.INFO
									});
                                    studentcumrelivingentry.getForm().reset();
                       // Ext.MessageBox.confirm('Confirmation', ' Do U Want To Print Receipt..?', printrecipt);
                        if (formMode == 'edit') { 
                        } 
                        else {
                            studentcumrelivingentry.getForm().findField('student_id').focus(true, 500);
                        }
                    }
                }
            }
        });

        studentcumrelivingentry.getForm().findField('student_gturegno').on('blur', function () {
            var varstudent_gturegno = studentcumrelivingentry.getForm().findField('student_gturegno').getValue();
            if (varstudent_gturegno != "") {
                studentcumrelivingentry.getForm().load({
                    params: {
                        grno: varstudent_gturegno,
                        branch_id: studentcumrelivingentry.getForm().findField('branch_id').getValue()
                    },
                    url: 'data/get-enrollmentno.php',
                    waitMsg: 'Loading',
                    failure: function () {
                        Ext.MessageBox.show({
                            title: 'Error',
                            msg: 'Invalid GR No.',
                            buttons: Ext.MessageBox.OK,
                            icon: Ext.MessageBox.ERROR
                        });
                        studentcumrelivingentry.getForm().findField('student_gturegno').reset();
                        studentcumrelivingentry.getForm().findField('student_id').reset();
                        studentcumrelivingentry.getForm().findField('student_gturegno').focus(true, 4000)
                    }
                });
            }
        });

        var FormWindow = new Ext.Window({
            title: 'Student Courses Completion Cum Reliving Certificate',
            width: 600,
            height: 250,
            modal: true,
            border: false,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            collapsible: true,
            items: studentcumrelivingentry,
            buttonAlign: 'center',
            buttons: [{
                text: 'Save',
                id: 'save',
                handler: function () {
                    studentcumrelivingentry.getForm().submit({
                        waitMsg: 'Saving...',
                        success: function () {
                            Ext.MessageBox.show({
										title: 'Message',
										msg: 'Data has been saved..',
										buttons: Ext.MessageBox.OK,
										icon: Ext.MessageBox.INFO
									});
                                    studentcumrelivingentry.getForm().reset();
                         //   Ext.MessageBox.confirm('Confirmation',' Do U Want To Print Receipt..?',printrecipt);				  
                        },
                        failure: function () {
                            Ext.MessageBox.show({
                                title: 'Error',
                                msg: 'Please fill complete form',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR
                            });
                        }
                    });
                }
            },
            {
                text: 'Reset',
                name: 'btnReset',
                id: 'btnReset',
                handler: function () {
                    if (formMode != 'edit') {
                        	studentcumrelivingentry.getForm().reset();
                        studentcumrelivingentry.getForm().findField('branch_id').focus(true, 500);
                    }
                }
            },
            {
                text: 'Close',
                name: 'btnClose',
                id: 'btnClose',
                handler: function () {
                    FormWindow.close();
                }
            },
            ]

        });
        var varWidth = (window.innerWidth - FormWindow.width) / 2;
        var varHeight = (window.innerHeight - FormWindow.height) / 4;
        // Set the position of the window
        FormWindow.setPosition(varWidth, varHeight);
        // Show the window
        FormWindow.show();

        /*function printrecipt(btn) {
            if (btn == 'yes') {
                window.location = 'report-course-completion-certificate.php?ID=' + Ext.getCmp('certificate_no').getValue();   
            } else {
                resetform();
                if (formMode == "edit") {
                    location.href = 'view-course-completion-certificate.php';
                }
            }
        }*/
        branchstore.on({
            'load': {
                fn: function (store, options) {
                    if (branchstore.getCount() == 1) {
                        studentcumrelivingentry.getForm().findField('branch_certificate').setValue(branchstore.getAt(0).data.branch_id);
                        Ext.getCmp('student_gturegno').enable();
                        Ext.getCmp('student_gturegno').focus(true, 100);
                        var bid = studentcumrelivingentry.getForm().findField('branch_certificate').getValue();
                        Ext.Ajax.request({
                            loadMask: true,
                            sync: true,
                            url: 'data/getcertificateno.php',
                            params: {
                                branch_id: bid,
                                action: 'cum'
                            },
                            waitMsg: 'Loading',
                            failure: function () {
                                Ext.MessageBox.show({
                                    title: 'Error',
                                    msg: 'Invalid GR No.',
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.ERROR
                                });
                            },
                            success: function (resp) {
                                varcertno = Ext.decode(resp.responseText).certno;
                                studentcumrelivingentry.getForm().findField('certificate_no').setValue(varcertno);
                            }
                        });
                    } else {
                        Ext.getCmp('branch_certificate').focus(true, 500);
                    }
                }
            }
        });
        if (formMode == 'edit') {
                    studentcumrelivingentry.getForm().findField('certificate_no').setValue('<?php print $certificate_no; ?>');
                    studentcumrelivingentry.getForm().findField('branch_id').setValue('<?php print $cur_branchid; ?>');
                    studentcumrelivingentry.getForm().findField('year_id').setValue('<?php print $year_id; ?>');
                    studentcumrelivingentry.getForm().findField('branchyear_id').setValue('<?php print $cur_branchyearid; ?>');
                    studentcumrelivingentry.getForm().findField('semester_id').setValue('<?php print $cur_semesterid; ?>'); 
                    studentcumrelivingentry.getForm().findField('student_gturegno').setValue('<?php print $student_gturegno; ?>');
                    studentcumrelivingentry.getForm().findField('student_id').setValue('<?php print $student_id; ?>');
                    studentcumrelivingentry.getForm().findField('student_id').setRawValue('<?php print $student_name; ?>');
                    Ext.getCmp('btnReset').disable();
                }
    });
</script>
<div align="center">
    <div id="main-content" style="width:600px" align="left"></div>
</div>
<?php require "_footer.php"; ?>