Ext.namespace('Shipping');

Shipping.SeaeHistryFen = function (config) {
    Ext.applyIf(this, config);
    this.initUIComponents();
    window.Shipping.SeaeHistryFen.superclass.constructor.call(this);
};

Ext.extend(Shipping.SeaeHistryFen, Ext.Panel, {
    PageSize: 30,
    ParentWin: null,
    feeType: 0,
    OprationStatus: null, //仅当弹出界面时使用
    SelectedRecord: null,
    storeBodyList: null,
    StoreFeeUnit: null,
    StoreFeeCust: null,
    EditRecord: null,

    initUIComponents: function () {
        //定义数据集
        this.storeList = Ext.create('Ext.data.Store', {
            pageSize: this.PageSize,
            model: 'MsOpSeaeModel',
            remoteSort: true,
            proxy: {
                type: 'ajax',
                url: '/MvcShipping/MsOpSeae/GetFenList',
                reader: {
                    id: 'GId',
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        });

        //#region 主grid
        this.gridListCheckBoxModel = Ext.create('Ext.selection.CheckboxModel');

        this.gridList = new Ext.grid.GridPanel({
            store: this.storeList,
            enableHdMenu: false,
            region: 'center',
            loadMask: { msg: "数据加载中,请稍等..." },
            trackMouseOver: true,
            disableSelection: false,
            selModel: this.gridListCheckBoxModel,
            tbar: [{
                text: '确定引入',
                tooltip: '确定引入',
                iconCls: "btnadddetail",
                id: "btnadddetail",
                handler: function (button, event) {
                    this.onAddClick(button, event);
                },
                scope: this
            }],

            viewConfig: {
                autoFill: true,
                enableTextSelection: true
            },
            columns: [{
                sortable: true,
                dataIndex: 'MBLNO',
                header: '主提单号',
                width: 130
            }, {
                sortable: true,
                dataIndex: 'HBLNO',
                header: '分提单号',
                width: 130
            }, {
                sortable: true,
                dataIndex: 'VESSEL',
                header: '船名',
                width: 130
            }, {
                sortable: true,
                dataIndex: 'VOYNO',
                header: '航次',
                width: 50
            }, {
                sortable: true,
                dataIndex: 'ETD',
                header: '开船日期',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                width: 86
            }, {
                sortable: true,
                dataIndex: 'PORTLOAD',
                header: '装货港',
                width: 130
            }, {
                sortable: true,
                dataIndex: 'PORTDISCHARGE',
                header: '卸货港',
                width: 130
            }, {
                dataIndex: 'CUSTOMERNAME',
                header: '委托单位',
                width: 120
            }, {
                dataIndex: 'SALE',
                header: '销售',
                width: 70
            }, {
                sortable: true,
                dataIndex: 'PKGS',
                header: '件数',
                width: 80
            }, {
                dataIndex: 'KINDPKGS',
                header: '包装',
                width: 100
            }, {
                dataIndex: 'KGS',
                header: '重量',
                width: 80
            }, {
                dataIndex: 'CBM',
                header: '尺码',
                width: 80
            }, {
                dataIndex: 'BLFRT',
                header: '付费方式',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'INPUTBY',
                header: '录入人',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'CREATETIME',
                header: '录入日期',
                width: 86
            }, {
                sortable: true,
                dataIndex: 'OP',
                header: '操作',
                width: 80
            }
                        ],
            // paging bar on the bottom
            bbar: Ext.create('Ext.PagingToolbar', {
                store: this.storeList,
                displayInfo: true,
                displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
                emptyMsg: "没有数据"
            })
        });




        //#endregion

        //#region 固定费用明细表



        //#region formSearch
        //#region formSearch枚举参照相关     


        //#endregion

        _this = this;
        this.formSearch = Ext.widget('form', {
            frame: true,
            region: 'center',
            bodyPadding: 5,
            fieldDefaults: {
                margins: '2 2 2 2',
                labelAlign: 'right',
                flex: 1,
                labelWidth: 90,
                msgTarget: 'qtip'
            },

            items: [
                {//fieldset 1
                    xtype: 'container',
                    defaultType: 'textfield',
                    layout: 'anchor',
                    defaults: {
                        anchor: '100%'
                    },
                    items: [{
                        xtype: 'container',
                        layout: 'hbox',
                        defaultType: 'textfield',
                        items: [{
                            fieldLabel: '编号',
                            name: 'MblNo',
                            enableKeyEvents: true,
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, {
                            fieldLabel: '从开船日期',
                            format: 'Y-m-d',
                            xtype: 'datefield',
                            name: 'CustDate_Min',
                            enableKeyEvents: true,
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, {
                            fieldLabel: '至开船日期',
                            format: 'Y-m-d',
                            xtype: 'datefield',
                            name: 'CustDate_Max',
                            enableKeyEvents: true,
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, {
                            xtype: 'button',
                            width: 90,
                            text: "执行查询",
                            iconCls: "btnrefresh",
                            handler: function (button, event) {
                                this.onRefreshClick(button, event);
                            },
                            scope: this
                        }
                       ]

                    }]

                }]

        });

        //#endregion formSearch

        //#region 按钮ToolBar

        //#endregion

        //#region 布局
        this.panelTop = new Ext.Panel({
            layout: "border",
            region: "north",
            height: 55,
            items: [this.formSearch]
        });




        Ext.apply(this, {
            items: [this.panelTop, this.gridList]
        });
        //#endregion
        parentWin = window.parent.opener._this;
        if (parentWin) {
            this.storeBodyList = parentWin.storeBodyList;
            this.EditRecord = parentWin.EditRecord;
        }
        //#region 事件绑定
        this.storeList.on('beforeload', function (store) {
            var sql = this.getCondition();
            Ext.apply(store.proxy.extraParams, { condition: sql });
        }, this);

        this.storeList.on('load', function (store, records) {
            if (store.getCount() > 0) {
                this.gridList.getSelectionModel().select(0);
                //this.gridListCheckBoxModel.selectAll();   
            }
        }, this);


        //#endregion

        //#region 自动刷新(已经提交但是尚未审核的业务,哪怕一票货中有一笔费用没审核也显示)
        this.onAutoRefresh();
        //#endregion

        //       this.SetBtnRight();

    }, //end initUIComponents

    onAutoRefresh: function () {
        var sql = '';
        this.storeList.load({
            params: { start: 0, limit: this.PageSize, sort: '', condition: sql },
            waitMsg: "正在查询数据...",
            scope: this
        });
    },

    onRefreshClick: function (button, event) {
        var sql = this.getCondition();
        this.storeList.load({
            params: { start: 0, limit: this.PageSize, sort: '', condition: sql },
            waitMsg: "正在查询数据...",
            scope: this
        });
    },

    onAddClick: function (button, event) {

        var myDate = new Date();

        selectedRecords = this.gridListCheckBoxModel.selected.items;

        for (i = 0; i < selectedRecords.length; i += 1) {

            var memberyf = selectedRecords[i];
            var record = null;

            var myDate = new Date();
            var mydatestr = Ext.util.Format.date(myDate, 'Y-m-d');
            record = Ext.create('MsOpSeaeModel', {
                BSNO: 'topseae' + NewGuid(),
                ORDERTYPE: '操作录入',
                MASTERNO: '*',
                CUSTNO: '',
                MBLNO: this.EditRecord.get('MBLNO'),
                HBLNO: '',
                BSSTATUS: false,
                FEESTATUS: false,
                BSSTATUSREF: '未锁定',
                FEESTATUSREF: '未锁定',
                ACCDATE: this.EditRecord.get('ACCDATE'),
                BSDATE: mydatestr,
                INPUTBY: SHOWNAME,
                OP: SHOWNAME,
                CUSTSERVICE: SHOWNAME,
                CUSTOMERNAME: memberyf.data.CUSTOMERNAME,
                BSSOURCE: memberyf.data.BSSOURCE,
                BSSOURCEDETAIL: memberyf.data.BSSOURCEDETAIL,
                SALE: memberyf.data.SALE,
                SHIPPER: memberyf.data.SHIPPER,
                CONSIGNEEID: memberyf.data.CONSIGNEEID,
                CONSIGNEE: memberyf.data.CONSIGNEE,
                NOTIFYPARTYID: memberyf.data.NOTIFYPARTYID,
                NOTIFYPARTY: memberyf.data.NOTIFYPARTY,
                GOODSNAME: memberyf.data.GOODSNAME,
                NOBILL: memberyf.data.NOBILL,
                ISSUETYPE: memberyf.data.ISSUETYPE,
                MARKS: memberyf.data.MARKS,
                DESCRIPTION: memberyf.data.DESCRIPTION,
                GROSSWEIGHT: memberyf.data.GROSSWEIGHT,
                MEASUREMENT: memberyf.data.MEASUREMENT,
                TOTALNO: memberyf.data.TOTALNO,
                LANE: memberyf.data.LANE,
                HSCODE: memberyf.data.HSCODE,
                NOPKGS: memberyf.data.NOPKGS,
                VESSEL: this.EditRecord.get('VESSEL'),
                VESSELID: this.EditRecord.get('VESSELID'),
                VOYNO: this.EditRecord.get('VOYNO'),
                ETD: this.EditRecord.get('ETD'),
                PORTLOADID: this.EditRecord.get('PORTLOADID'),
                PORTLOAD: this.EditRecord.get('PORTLOAD'),
                PORTDISCHARGEID: this.EditRecord.get('PORTDISCHARGEID'),
                PORTDISCHARGE: this.EditRecord.get('PORTDISCHARGE'),
                BLTYPE: '拼箱分票',
                AGENTID: this.EditRecord.get('AGENTID'),
                AGENT: this.EditRecord.get('AGENT'),
                YARD: this.EditRecord.get('YARD'),
                ETA: this.EditRecord.get('ETA'),
                CLOSINGDATE: this.EditRecord.get('CLOSINGDATE'),
                PLACEDELIVERYID: memberyf.data.PLACEDELIVERYID,
                PLACEDELIVERY: memberyf.data.PLACEDELIVERY,
                DESTINATIONID: memberyf.data.DESTINATIONID,
                DESTINATION: memberyf.data.DESTINATION,
                BLFRT: memberyf.data.BLFRT,
                SERVICE: 'FCL-FCL',
                LANE: this.EditRecord.get('LANE'),
                FORWARDER: this.EditRecord.get('FORWARDER'),
                CARRIER: this.EditRecord.get('CARRIER'),
                KINDPKGS: memberyf.data.KINDPKGS,
                CARGOID: memberyf.data.CARGOID,
                PKGS: memberyf.data.PKGS,
                KGS: memberyf.data.KGS,
                CBM: memberyf.data.CBM,
                CNTR1: 0,
                CNTR2: 0,
                CNTR3: 0,
                CNTR4: 0,
                CNTR5: 0,
                CNTR6: 0,
                CNTR7: 0,
                CNTR8: 0,
                CNTR9: 0,
                CNTR10: 0,
                OTCNTR: 0,
                TEU: 0,
                CUSTOMSNUM: 0,
                INAMOUT: 0,
                ISVOU: false,
                ISCONTAINERSOC: false
            });


            record.id = NewGuid();
            record.internalId = NewGuid();
            this.storeBodyList.add(record);

        };
        window.closeAction = 'destroy';
        window.close();
    },

    getCondition: function () {
        var form = this.formSearch.getForm();
        if (!form.isValid()) {
            Ext.Msg.alert('提示', '查询条件赋值错误,请检查。');
            return '';
        }

        var sql = '';



        var mblNo = form.findField('MblNo').getValue();
        sql = sql + getAndConSql(sql, mblNo, " (MBLNO like '%" + mblNo + "%' or HBLNO like '%" + mblNo + "%') ");

        var custDate_Min = form.findField('CustDate_Min').getRawValue();
        sql = sql + getAndConSql(sql, custDate_Min, "ETD >= '" + custDate_Min + "'");

        var custDate_Max = form.findField('CustDate_Max').getRawValue();
        sql = sql + getAndConSql(sql, custDate_Max, "ETD<= '" + custDate_Max + " 23:59:59'");


        return sql;
    }

});