<?php
ob_start();
require "_header.php";
require "includeGridFilters.php";
require "data/combo_semester.php";
require "data/combo_branchyear.php";
error_reporting(1);

$student_id = $_REQUEST["ID"];


if (!empty($student_id) && isset($student_id)) {
    $mode = "edit";
    if ($EDIT == "false") {
        redirectToPage();
    }
    $limit = 1;
    $start = 0;
    $condition = " AND student_id = '$student_id' ";
    $order_field = "transport_id";
    $order_type = "DESC";

    $objtransportfacilityMgmt = new transportfacilityMgmt;
    $out = $objtransportfacilityMgmt->getTransportRecord($limit, $start, $condition, $order_field, $order_type);
    if (dbRows($out)) {
        $row = dbArray($out);
        extract($row);
    }
} else {
    header("Location: manage-student.php");
}
?>

<!--<script type="text/javascript" src="js/studentformitems.js"></script>-->
<script language="javascript" type="text/javascript">

    var formMode = '<?php echo $mode; ?>';
    var add = '<?php echo $_REQUEST["add"]; ?>';
    var transport_id = '<?php echo $transport_id; ?>';
    var student_id = '<?php echo $student_id;
    ?>';

    var bid = 0;


    Ext.onReady(function () {

        Ext.QuickTips.init();
        var dt = new Date();
        var transportstore = new Ext.data.ArrayStore({
            fields: ['transport_facility', 'transport_value'],
            data: [['Updown Facility', '0'], ['Hostel Facility', '1']]
        });



        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: 'string'
            }]
        });
        var yearstore = new Ext.data.ArrayStore({
            fields: ['branch_id', 'branchyear_id', 'branchyear_name'],
            data: Ext.year.year
        });
        var batchstore = new Ext.data.ArrayStore({
            fields: ['year_id', 'year_name'],
            data: Ext.semester.yearname
        });

        studentstore.load({
            params: {
                branch_id: '<?= isset($branch_id) ? $branch_id : 0 ?>'
            }
        });


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

            labelWidth: 125,
            url: 'save-hostel-updown.php?ID=' + transport_id,
            method: 'post',
            frame: true,
            bodyStyle: 'padding:5px 5px 0',
            items: [{
                layout: 'column',
                items: [
                    {
                        columnWidth: 1,
                        layout: 'column',
                        items: [{
                            columnWidth: 1,
                            labelWidth: 150,
                            layout: 'form',
                            items: [
                                { height: 10 }, {
                                    xtype: 'combo',
                                    name: 'student_fullname',
                                    id: 'student_fullname',
                                    hiddenName: 'student_id',
                                    fieldLabel: 'Student Name <span class="comp">*</span>',
                                    displayField: 'student_name',
                                    valueField: 'student_id',
                                    width: 200,
                                    triggerAction: 'all',
                                    mode: 'local',
                                    allowBlank: true,
                                    store: studentstore,
                                    disabled: true
                                }, {

                                    xtype: 'combo',
                                    name: 'year_id',
                                    id: 'year_id',
                                    hiddenName: 'year_hidden',
                                    fieldLabel: 'Batch(Year) ',
                                    displayField: 'year_name',
                                    valueField: 'year_id'
                                    , allowBlank: true,
                                    forceSelection: true
                                    , selectOnFocus: true
                                    , triggerAction: 'all'
                                    , mode: 'local'
                                    , store: batchstore
                                    , width: 200,
                                    tabIndex: 2,
                                    lastQuery: '',
                                    disabled: true

                                }, {

                                    type: 'number',
                                    xtype: 'combo',
                                    name: 'branchyear_id',
                                    id: 'branchyear_id',
                                    hiddenName: 'branchyear_hidden',
                                    fieldLabel: 'Course Year ',
                                    forceSelection: true,
                                    displayField: 'branchyear_name',
                                    valueField: 'branchyear_id'
                                    , allowBlank: true
                                    , triggerAction: 'all'
                                    , mode: 'local'
                                    , store: yearstore
                                    , width: 200
                                    , selectOnFocus: true,
                                    tabIndex: 3,
                                    lastQuery: '',
                                    disabled: true
                                }]
                        }]
                    },
                    {
                        columnWidth: 1,
                        labelWidth: 100,
                        layout: 'form',
                        items: [{
                            xtype: 'combo',
                            name: 'transport_facility',
                            id: 'transport_facility',
                            hiddenName: "transport_facility",
                            fieldLabel: 'Transportation Facility ',
                            forceSelection: false,
                            displayField: 'transport_facility',
                            valueField: 'transport_value',
                            width: 150,
                            allowBlank: true,
                            triggerAction: 'all',
                            mode: 'local',
                            store: transportstore,
                            tabIndex: 17
                        }
                        ]
                    },
                    {
                        columnWidth: .5,
                        labelWidth: 80,
                        layout: 'form',
                        items: [
                            {
                                xtype: 'datefield',
                                name: 'updown_strdate',
                                id: 'updown_strdate',
                                width: 150,
                                tabIndex: 19,
                                format: JSDateFormat,
                                fieldLabel: 'Up Down Start Date ',
                                vtype: 'daterange',
                                allowBlank: true
                                //value: new Date(),

                            }, {
                                xtype: 'datefield',
                                name: 'updown_enddate',
                                id: 'updown_enddate',
                                width: 150,
                                format: JSDateFormat,
                                fieldLabel: 'Up Down End Date ',
                                vtype: 'daterange',
                                allowBlank: true,
                                tabIndex: 18,
                                maxValue: new Date()
                                //value: new Date(),

                            }

                        ]
                    },
                    {
                        columnWidth: .5,
                        layout: 'form',
                        labelWidth: 90,
                        items: [
                            {
                                xtype: 'datefield',
                                name: 'hostel_admdate',
                                id: 'hostel_admdate',
                                width: 150,
                                format: JSDateFormat,
                                fieldLabel: 'Hostel Admission Date ',
                                vtype: 'daterange',
                                allowBlank: true,
                                tabIndex: 18,
                                maxValue: new Date()
                                //value: new Date(),
                            },
                            {
                                xtype: 'datefield',
                                name: 'hostel_candate',
                                id: 'hostel_candate',
                                width: 150,
                                format: JSDateFormat,
                                fieldLabel: 'Hostel Admission Cancel Date ',
                                vtype: 'daterange',
                                allowBlank: true,
                                tabIndex: 19,
                                maxValue: new Date()
                                //value: new Date(), 
                            }
                        ]
                    }
                ]
            }
            ]
        });


        var FormWindow = new Ext.Window({
            title: 'Student Hostel/Updown Entry',
            width: 600,
            height: 300,
            modal: true,
            border: false,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            collapsible: true,
            items: studentFacilityEntry,
            buttonAlign: 'center',
            buttons: [{
                text: 'Save',
                id: 'save',
                handler: function () {
                    studentFacilityEntry.getForm().findField('student_fullname').enable();
                    studentFacilityEntry.getForm().findField('branchyear_id').enable();
                    studentFacilityEntry.getForm().findField('year_id').enable();
                    var transportFacility = Ext.getCmp('transport_facility').getValue();
                    var hostelAdmDate = Ext.getCmp('hostel_admdate').getValue();
                    var hostelCanDate = Ext.getCmp('hostel_candate').getValue();
                    var updownStartDate = Ext.getCmp('updown_strdate').getValue();
                    var updownEndDate = Ext.getCmp('updown_enddate').getValue();

                    if (transportFacility === '1') {
                        if (!hostelAdmDate || !hostelCanDate) {
                            Ext.MessageBox.show({
                                title: 'Validation Error',
                                msg: 'Please fill both Hostel Admission Date and Hostel Cancel Date for Hostel Facility.',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR
                            });
                            return false;
                        }
                    } else {
                        if (!updownStartDate || !updownEndDate) {
                            Ext.MessageBox.show({
                                title: 'Validation Error',
                                msg: 'Please fill both Updown Start Date and Updown End Date for Updown Facility.',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR
                            });
                            return false;
                        }
                    }

                    studentFacilityEntry.getForm().submit({
                        waitMsg: 'Saving...',
                        success: function () {
                            Ext.MessageBox.show({
                                title: 'Message',
                                msg: 'Data has been saved..',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.INFO
                            });
                            studentFacilityEntry.getForm().findField('student_fullname').disable();
                            studentFacilityEntry.getForm().findField('branchyear_id').disable();
                            studentFacilityEntry.getForm().findField('year_id').disable();
                            window.location.href = "manage-student.php";
                            //   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: '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-attempt-certificate.php?ID=' + Ext.getCmp('certificate_no').getValue();   
            } else {
                resetform();
                if (formMode == "edit") {
                    location.href = 'view-attempt-certificate.php';
                }
            }
        }*/

        if (formMode == 'edit') {
            studentFacilityEntry.getForm().findField('transport_facility').setValue('<?php print $transport_facility; ?>');
            studentFacilityEntry.getForm().findField('updown_strdate').setValue('<?php print $updown_strdate; ?>');
            studentFacilityEntry.getForm().findField('updown_enddate').setValue('<?php print $updown_enddate; ?>');
            studentFacilityEntry.getForm().findField('hostel_admdate').setValue('<?php print $hostel_admdate; ?>');
            studentFacilityEntry.getForm().findField('hostel_candate').setValue('<?php print $hostel_candate; ?>');
            studentFacilityEntry.getForm().findField('year_id').setValue('<?php print $year_id; ?>');
            studentFacilityEntry.getForm().findField('branchyear_id').setValue('<?php print $cur_branchyearid; ?>');
            studentstore.on({
                'load': {
                    fn: function () {
                        studentFacilityEntry.getForm().findField('student_fullname').setValue('<?php print $student_id; ?>');
                    }
                }
            })


        }
    });
</script>
<div align="center">
    <div id="main-content" style="width:600px" align="left"></div>
</div>
<?php require "_footer.php"; ?>