Ext.namespace('Shipping');

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

Ext.extend(Shipping.ChPayapplicationAudit, Ext.Panel, {
    PageSize: 15,
    OprationStatus: null, //仅当弹出界面时使用
    SelectedRecord: null,
    isAudit: '',


    initUIComponents: function () {
        this.BSNO = "";

        //定义数据集
        this.storeList = Ext.create('Ext.data.Store', {
            pageSize: this.PageSize,
            model: 'MsChPayapplication',
            remoteSort: true,
            proxy: {
                type: 'ajax',
                url: '/Account/Chfee_ForeignPay/GetAuditDataList',
                reader: {
                    id: 'BILLNO',
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        });

        //定义Grid
        this.gridList = new Ext.grid.GridPanel({
            store: this.storeList,
            enableHdMenu: false,
            height: 180,
            region: 'north',
            split: true,
            loadMask: { msg: "数据加载中,请稍等..." },
            trackMouseOver: true,
            disableSelection: false,
            columns: [{
                sortable: true,
                dataIndex: 'BILLNO',
                header: '申请单号',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'BILLSTATUSREF',
                header: '状态',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'CUSTOMERNAME',
                header: '结算单位',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'AMOUNTRMB',
                header: 'RMB',
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) { try { var lsValue = usMoney(value, 2, '', false); if (lsValue != "NaN") { value = lsValue; if (parseFloat(lsValue) < 0) { return '<SPAN style="COLOR: red">' + lsValue + '</SPAN>'; } } else { return value; } } catch (e) { return value; } return value; },
                align: 'right',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'AMOUNTUSD',
                header: 'USD',
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) { try { var lsValue = usMoney(value, 2, '', false); if (lsValue != "NaN") { value = lsValue; if (parseFloat(lsValue) < 0) { return '<SPAN style="COLOR: red">' + lsValue + '</SPAN>'; } } else { return value; } } catch (e) { return value; } return value; },
                align: 'right',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'APPLYTIME',
                header: '申请日期',
                width: 100
            }, {
                sortable: true,
                dataIndex: 'APPLICANTNAME',
                header: '申请人',
                width: 100
            }, {
                sortable: true,
                dataIndex: 'PAYABLETIME',
                header: '最晚支付日期',//申请支付日期
                width: 100
            }, {
                sortable: true,
                dataIndex: 'PAYCOMPANY',
                header: '付费公司',
                width: 160
            }, {
                sortable: true,
                dataIndex: 'PAYBANK',
                header: '付费银行',
                width: 160
            }, {
                sortable: true,
                dataIndex: 'ISINVOICE',
                header: '已开发票',
                width: 160
            }, {
                sortable: true,
                dataIndex: 'INVNO',
                header: '发票号',
                width: 160
            }, {
                sortable: true,
                dataIndex: 'BS_CUSTOMERNAME',
                header: '委托单位',
                width: 160
            }, {
                sortable: true,
                dataIndex: 'REMARK',
                header: '备注',
                width: 160
            }


            ],
            // paging bar on the bottom
            bbar: Ext.create('Ext.PagingToolbar', {
                store: this.storeList,
                displayInfo: true,
                displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
                emptyMsg: "没有数据"
            })
        });


        //#region formSearch

        //#region formSearch枚举参照相关     

        //#endregion


        this.storeAddDCType = Ext.create('Ext.data.Store', {
            fields: ['DC', 'NAME']
        });
        this.storeAddDCType.add({ "DC": "", "NAME": "全部" });
        this.storeAddDCType.add({ "DC": "1", "NAME": "未审核" });
        this.storeAddDCType.add({ "DC": "2", "NAME": "已审核" });

        this.comboxaddDCType = Ext.create('DsExt.ux.RefTableCombox', {
            fieldLabel: '审核状态',
            store: this.storeAddDCType,
            valueField: 'DC',
            displayField: 'NAME',
            forceSelection: true,
            name: 'DC',
            value: '1'
        });


        //人员信息加载
        this.storeOpCode = Ext.create('DsExt.ux.RefTableStore', {
            model: 'DsShipping.ux.UserRefModel',
            proxy: { url: '/CommMng/BasicDataRef/GetUserRefList' }
        });
        this.storeOpCode.load();
        //
        this.comboxAPPLICANT = Ext.create('DsExt.ux.RefTableCombox', {
            fieldLabel: '申请人',
            store: this.storeOpCode,
            forceSelection: true,
            name: 'APPLICANT',
            valueField: 'GID',
            displayField: 'CodeAndName'
        });

        var _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: 'BsNo',
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, {
                            fieldLabel: '付费客户',
                            name: 'CustName',
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, {
                            fieldLabel: '从付费申请日期',
                            format: 'Y-m-d',
                            xtype: 'datefield',
                            name: 'ExpDateBgn',
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, {
                            fieldLabel: '到付费申请日期',
                            format: 'Y-m-d',
                            xtype: 'datefield',
                            name: 'ExpDateEnd',
                            listeners: {
                                specialkey: function (field, e) {
                                    if (e.getKey() == e.ENTER) {
                                        _this.onRefreshClick();
                                    }
                                }
                            }
                        }, this.comboxAPPLICANT, this.comboxaddDCType
                       ]
                    }
                    ]//end items(fieldset 1)
                }//end fieldset 1   
            ]//end root items          

        });



        this.storeBodyListdetail = Ext.create('Ext.data.Store', {
            model: 'Chfee_do_detail',
            remoteSort: true,
            proxy: {
                type: 'ajax',
                url: '/Account/Chfee_payapplication/GetBillAuditList',
                reader: {
                    id: 'BSNO,FEEDOID',
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        });


        this.gridListdetail = new Ext.grid.GridPanel({
            store: this.storeBodyListdetail,
            enableHdMenu: false,
            region: 'center',
            loadMask: { msg: "数据加载中,请稍等..." },
            trackMouseOver: true,
            disableSelection: false,

            columns: [{
                sortable: true,
                dataIndex: 'BILLNO',
                header: 'BILL',
                hidden: true,
                width: 100
            }, {
                sortable: true,
                dataIndex: 'BSNO',
                header: 'BSNO',
                hidden: true,
                width: 100
            }, {
                sortable: true,
                dataIndex: 'FEEDOID',
                header: 'FEEDOID',
                hidden: true,
                width: 100
            }, {
                sortable: true,
                dataIndex: 'CUSTNO',
                header: '委托编号',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'MBLNO',
                header: '主提单号',
                width: 120
            }, {
                sortable: true,
                dataIndex: 'CUSTOMERNAME',
                header: '委托单位',
                width: 100
            }, {
                sortable: true,
                dataIndex: 'ETD',
                header: '开船日期',
                renderer: Ext.util.Format.dateRenderer('Y-m-d'),
                width: 80
            }, {
                sortable: true,
                dataIndex: 'BSSOURCE',
                header: '业务来源',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'FEENAME',
                header: '费用名称',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'FEETYPEREF',
                header: '收付',
                width: 40
            }, {
                sortable: true,
                dataIndex: 'AMOUNT',
                header: '核销金额',
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {
                    try {
                        var lsValue = usMoney(value, 2, '', false);
                        if (lsValue != "NaN") {
                            value = lsValue; if (parseFloat(lsValue) < 0) {
                                return '<SPAN style="COLOR: red">' + lsValue + '</SPAN>';
                            }
                        }
                        else {
                            return value;
                        }
                    }
                    catch (e) {
                        return value;
                    }
                    return value;
                },
                width: 80
            }, {
                sortable: true,
                dataIndex: 'ORIGCURRENCY',
                header: '原始币别',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'ORIGAMOUNT',
                header: '原始金额',
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {
                    try {
                        var lsValue = usMoney(value, 2, '', false);
                        if (lsValue != "NaN") {
                            value = lsValue; if (parseFloat(lsValue) < 0) {
                                return '<SPAN style="COLOR: red">' + lsValue + '</SPAN>';
                            }
                        }
                        else {
                            return value;
                        }
                    }
                    catch (e) {
                        return value;
                    }
                    return value;
                },
                width: 80
            }, {
                sortable: true,
                dataIndex: 'INVOICENO',
                header: '发票号',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'SALE',
                header: '揽货人',
                width: 80
            }
            ]
        });


        this.storeBodySum = Ext.create('Ext.data.Store', {
            model: 'Chfee_do_sum',
            remoteSort: true,
            proxy: {
                type: 'ajax',
                url: '/Account/Chfee_payapplication/GetBillSum',
                reader: {
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        });


        this.gridSum = new Ext.grid.GridPanel({
            store: this.storeBodySum,
            enableHdMenu: false,
            region: 'center',
            loadMask: { msg: "数据加载中,请稍等..." },
            trackMouseOver: true,
            disableSelection: false,
            tbar: [{
                xtype: 'label',
                width: 120,
                height: 22,
                text: '申请金额合计'
            }],
            columns: [{
                sortable: true,
                dataIndex: 'FEETYPEREF',
                header: '收付',
                width: 40
            }, {
                sortable: true,
                dataIndex: 'CURRENCY',
                header: '币 别',
                width: 80
            }, {
                sortable: true,
                dataIndex: 'AMOUNT',
                header: '金额',
                renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {
                    try {
                        var lsValue = usMoney(value, 2, '', false);
                        if (lsValue != "NaN") {
                            value = lsValue; if (parseFloat(lsValue) < 0) {
                                return '<SPAN style="COLOR: red">' + lsValue + '</SPAN>';
                            }
                        }
                        else {
                            return value;
                        }
                    }
                    catch (e) {
                        return value;
                    }
                    return value;
                },
                width: 80
            }
            ]
        });

        //#endregion formSearch

        //查询工具条
        this.panelBtn = new Ext.Panel({
            region: "north",
            tbar: [

                {
                    text: "执行查询",
                    iconCls: "btnrefresh",
                    handler: function (button, event) {
                        this.onRefreshClick(button, event);
                    },
                    scope: this
                },
                {
                    text: "重置条件",
                    iconCls: "btnreset",
                    handler: function (button, event) {
                        this.onClearSql(button, event);
                    },
                    scope: this
                }, '-', {
                    text: "审核通过",
                    iconCls: "btnadd",
                    handler: function (button, event) {
                        this.onAuditPassClick();
                    },
                    scope: this

                },
                '-',
                {
                    text: "驳回提交",
                    iconCls: "btndelete",
                    handler: function (button, event) {
                        this.onAuditBackClick();
                    },
                    scope: this
                }
            ]
        });

        this.panelTop = new Ext.Panel({
            layout: "border",
            region: "north",
            height: 80,
            items: [this.formSearch, this.panelBtn]
        });

        this.panelSum = new Ext.Panel({
            layout: "border",
            region: 'east',
            width: 260,
            split: true,
            margin: '2 2',
            items: [this.gridSum]
        });

        this.paneldetail = new Ext.Panel({
            title: '付费申请明细',
            layout: "border",
            region: 'center',

            items: [
                this.gridListdetail, this.panelSum
            ]
        });

        Ext.apply(this, {
            items: [this.panelTop, this.gridList, this.paneldetail]
        });

        this.storeList.on('beforeload', function (store) {
            var sql = this.getCondition();
            Ext.apply(store.proxy.extraParams, { condition: sql, isaudit: this.isAudit });
        }, this);

        this.onRefreshClick();



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

            }
        }, this);



        _this = this;
        this.gridList.getSelectionModel().on('select', function (model, record, index) {

            var billno = record.data.BILLNO;
            var sql = "";
            sql = " BILLNO='" + billno + "'";
            _this.storeBodyListdetail.load({ params: { condition: sql} });
            _this.storeBodySum.load({ params: { condition: sql} });
        }, this);


        this.gridListdetail.addListener('itemdblclick', function (dataview, record, item, index, e, b) {
            this.SelectedRecord = record;
            this.BSNO = record.data.BSNO;
            this.OprationStatus = 'view';
            DsOpenEditWin('/Import/PortOperate/Edit', this.BSNO, "750", "1000");
        }, this);

    }, //end initUIComponents
    OprationSwap: function () {
        var ret = new Array();
        ret[0] = 'view';
        ret[1] = this.BSNO;
        //ret[2] = this.SelectedRecord;

        return ret;
    },

    onAuditPassClick: function () {

        var selections = this.gridList.getSelectionModel().getSelection();
        if (selections.length == 0) {
            Ext.Msg.show({ title: '提示', msg: '请先选择需审核的单据!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
            return;
        }

        var bodyAddDatas = [];
        for (var i = 0; i < selections.length; i++) {
            var rec = selections[i];
            var BILLSTATUS = rec.data.BILLSTATUS;
            if (BILLSTATUS == '6' || BILLSTATUS == '2') {
                bodyAddDatas.push(rec);
            } else {
                Ext.Msg.show({ title: '提示', msg: '当前状态不允许审核!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                return;
            };
        }

        var jsonbodyAddDatas = ConvertRecordsToJsonAll(bodyAddDatas);

        _this = this;

        Ext.MessageBox.confirm('提示', '确定审核通过选中的业务吗?', function (btn) {
            if (btn == 'yes') {
                Ext.Msg.wait('正在审核数据...');
                Ext.Ajax.request({
                    waitMsg: '正在审核数据...',
                    url: '/Account/Chfee_payapplication/AuditList',
                    params: {
                        data: jsonbodyAddDatas
                    },
                    callback: function (options, success, response) {
                        if (success) {
                            var jsonresult = Ext.JSON.decode(response.responseText);
                            if (jsonresult.Success) {
                                _this.onRefreshClick();
                                Ext.Msg.show({ title: '提示', msg: '审核成功!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                            }
                            else {
                                Ext.Msg.show({ title: '错误', msg: jsonresult.Message, icon: Ext.Msg.ERROR, buttons: Ext.Msg.OK });
                            }
                        }
                    },
                    failure: function (response, options) {
                        Ext.Msg.show({ title: '警告', msg: '服务器响应出错,请重试', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                    },
                    success: function (response, options) {
                    },
                    scope: this
                });   //end Ext.Ajax.request
            }
        }, this);

    },

    onAuditBackClick: function () {

        var selections = this.gridList.getSelectionModel().getSelection();
        if (selections.length == 0) {
            Ext.Msg.show({ title: '提示', msg: '请先选择需驳回的单据!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
            return;
        }

        var bodyAddDatas = [];
        for (var i = 0; i < selections.length; i++) {
            var rec = selections[i];
            var BILLSTATUS = rec.data.BILLSTATUS;
            if (BILLSTATUS == '0' || BILLSTATUS == '2') {
                bodyAddDatas.push(rec);
            } else {
                Ext.Msg.show({ title: '提示', msg: '当前状态不允许驳回!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                return;
            };

        }

        var jsonbodyAddDatas = ConvertRecordsToJsonAll(bodyAddDatas);

        _this = this;

        Ext.MessageBox.confirm('提示', '确定驳回选中的业务吗?', function (btn) {
            if (btn == 'yes') {
                Ext.Msg.wait('正在驳回数据...');
                Ext.Ajax.request({
                    waitMsg: '正在驳回数据...',
                    url: '/Account/Chfee_payapplication/AuditBackList',
                    params: {
                        data: jsonbodyAddDatas
                    },
                    callback: function (options, success, response) {
                        if (success) {
                            var jsonresult = Ext.JSON.decode(response.responseText);
                            if (jsonresult.Success) {
                                _this.onRefreshClick();
                                Ext.Msg.show({ title: '提示', msg: '驳回成功!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                            }
                            else {
                                Ext.Msg.show({ title: '错误', msg: jsonresult.Message, icon: Ext.Msg.ERROR, buttons: Ext.Msg.OK });
                            }
                        }
                    },
                    failure: function (response, options) {
                        Ext.Msg.show({ title: '警告', msg: '服务器响应出错,请重试', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                    },
                    success: function (response, options) {
                    },
                    scope: this
                });   //end Ext.Ajax.request
            }
        }, this);

    },

    onClearSql: function () {
        var form = this.formSearch.getForm();
        form.reset();
    }, //onDeleteClick

    onRefreshClick: function (button, event) {
        var sql = this.getCondition();
        this.storeList.load({
            params: { start: 0, limit: this.PageSize, sort: '', condition: sql, isaudit: this.isAudit },
            waitMsg: "正在查询数据...",
            callback: function (r, options, success) {
                if (success) {
                    if (r.length == 0) {

                        //var sql = this.getCondition;
                        this.storeBodyListdetail.load({ params: { condition: sql} });
                        this.storeBodySum.load({ params: { condition: sql} });

                    }

                }
            },
            scope: this
        });
    },

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

        var sql = '';

        var customNo = form.findField('BsNo').getValue();
        sql = sql + getAndConSql(sql, customNo, "i.BILLNO like '%" + customNo + "%'");

        var custName = form.findField('CustName').getValue();
        sql = sql + getAndConSql(sql, custName, "i.CUSTOMERNAME like '%" + custName + "%'");

        var expDateBgn = form.findField('ExpDateBgn').getRawValue();
        sql = sql + getAndConSql(sql, expDateBgn, "i.APPLYTIME >='" + expDateBgn + "'");

        var expDateEnd = form.findField('ExpDateEnd').getRawValue();
        sql = sql + getAndConSql(sql, expDateEnd, "i.APPLYTIME <='" + expDateEnd + "'");

        var APPLICANT = form.findField('APPLICANT').getValue();
        sql = sql + getAndConSql(sql, APPLICANT, "i.APPLICANT like '%" + APPLICANT + "%'");

        this.isAudit = form.findField('DC').getValue();


        return sql;
    }

});