Ext.namespace('Shipping');

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

Ext.extend(Shipping.MsOpCtnRepairIndex, Ext.Panel, {

    OprationStatus: null, //仅当弹出界面时使用
    SelectedRecord: null,


    initUIComponents: function () {
        this.formname = 'MsOpCtnRepair';
        //定义数据集
        this.storeList = Ext.create('Ext.data.Store', {
            model: 'OpCtnRepairModel',
            pageSize: 30,
            autoLoad: { start: 0, limit: 30 },
            remoteSort: true,
            proxy: {
                type: 'ajax',
                url: '/MvcShipping/MsOpCtnRepair/GetDataList',
                reader: {
                    id: 'GID',
                    root: 'data',
                    totalProperty: 'totalCount'
                }
            }
        });

        //定义Grid
        this.WMSCB = Ext.create('Ext.selection.CheckboxModel', { checkOnly: true });
        this.columns = [{
            sortable: true,
            dataIndex: 'GID',
            header: 'GID',
            hidden: true,
            width: 120
        }, {
            sortable: true,
            dataIndex: 'IsSendMail',
            header: '已发送邮件',
            width: 80,
            renderer: function (value, cellmeta) {
                if (value == '1' || value == true || value == 'true') {
                    return "是";
                } else {
                    return "否";
                }
            }
        }, {
            sortable: true,
            dataIndex: 'IsLock',
            header: '是否锁定',
            width: 80,
            renderer: function (value, cellmeta) {
                if (value == '1' || value == true || value == 'true') {
                    return "是";
                } else {
                    return "否";
                }
            }
        }, {
            sortable: true,
            dataIndex: 'XiangZhu',
            header: '箱主',
            hidden: true,
            width: 80
        }, {
            sortable: false,
            dataIndex: 'ChangZhan',
            header: '场站',
            width: 80
        }, {
            sortable: true,
            dataIndex: 'ChangZhanSub',
            header: '场站Sub',
            width: 120

        }, {
            sortable: true,
            dataIndex: 'XiuXiangDanHao',
            header: '修箱单号',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'JinChangRiQi',
            header: '进场日期',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'XiuXiangRiQi',
            header: '修箱日期',
            width: 80
        }, {
            sortable: true,
            dataIndex: 'ZaoXiangRiQi',
            header: '造箱日期',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'XiangHao',
            header: '箱号',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'ChuanMing',
            header: '船名',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'MBLNO',
            header: '主提单号',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'XiangXing',
            header: '箱型',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'GongShiFei',
            header: '工时费',
            width: 120
        }, {
            sortable: true,
            dataIndex: 'ShuiLv',
            header: '税率',
            width: 60
        }, {
            sortable: true,
            dataIndex: 'CeLiangDanWei',
            header: '测量单位',
            width: 80
        }
            ];
        this.girdcolums = this.columns;
        this.girdcolums = DsTruck.GetGridPanel(GID, this.formname, this.girdcolums, 0); //使用者id,表名,中间column数组,跳过一开始的几列

        this.gridList = new Ext.grid.GridPanel({
            store: this.storeList,
            enableHdMenu: false,
            region: 'center',
            selModel: this.WMSCB,
            loadMask: { msg: "数据加载中,请稍等..." },
            trackMouseOver: true,
            disableSelection: false,
            features: [{
                ftype: 'summary'//Ext.grid.feature.Summary表格汇总特性
            }],
            columns: this.girdcolums,
            bbar: [{
                xtype: 'pagingtoolbar',
                store: this.storeList,
                displayInfo: true
            }]
        });
        this.gridList.addListener('itemdblclick', function (dataview, record, item, index, e, b) {
            this.SelectedRecord = record;
            this.OprationStatus = 'edit';
            DsOpenEditWin("/MvcShipping/MsOpCtnRepair/Edit", "", "650", "1250");
        }, this);

        //#region formSearch
        Ext.regModel('OutType', {
            fields: [{ name: 'value' }, { name: 'text'}]
        })


        //'场站',
        this.storeYARD = Ext.create('DsExt.ux.RefTableStore', {
            model: 'DsShipping.ux.CustomRefModel',
            proxy: { url: '/CommMng/BasicDataRef/GetCustomRefListYARD' }
        });
        this.storeYARD.load();
        this.comboxYARD = Ext.create('DsExt.ux.RefTableCombox', {
            fieldLabel: "场站",
            store: this.storeYARD,
            forceSelection: true,
            queryMode: 'remote',
            minChars: 0,
            queryParam: 'CODENAME',
            name: 'YARD',
            valueField: 'CustName',
            displayField: 'CodeAndName'
        });

        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',
                    flex: 1,
                    defaults: {
                        anchor: '100%'
                    },
                    items: [{
                        xtype: 'container',
                        layout: 'hbox',
                        defaultType: 'textfield',
                        items: [{
                            fieldLabel: '修箱单号',
                            name: 'XIUXIANGDANHAO'
                        }, this.comboxYARD, {
                            fieldLabel: '修箱日期(从)',
                            name: 'XiuXiangRiQiFrom',
                            format: 'Y-m-d',
                            xtype: 'datefield'
                        }, {
                            fieldLabel: '修箱日期(到)',
                            name: 'XiuXiangRiQiTo',
                            format: 'Y-m-d',
                            xtype: 'datefield'
                        }
                       ]
                    }, { xtype: 'container',
                        layout: 'hbox',
                        defaultType: 'textfield',
                        items: [
                                {
                                    fieldLabel: '船名航次',
                                    name: 'CHUANMINGHANGCI'
                                }, {
                                    fieldLabel: '提单号',
                                    name: 'MBLNO'
                                }, { xtype: 'hidden' },
                           { xtype: 'hidden' }
                           ]
                    }
                    ]//end items(fieldset 1)
                }//end fieldset 1   
            ]//end root items          

        });

        //#endregion formSearch

        //查询工具条
        _this = this;
        this.panelBtn = new Ext.Panel({
            region: "north",
            tbar: [{
                text: '新建',
                iconCls: "btnadd",
                handler: function (menu, event) {
                    _this.OprationStatus = 'add';
                    DsOpenEditWin("/MvcShipping/MsOpCtnRepair/Edit", "", "650", "1250");
                },
                scope: this
            }, '-',
                {
                    text: "删除",
                    iconCls: "btndelete",
                    handler: function (button, event) {
                        this.onDeleteClick(button, event);
                    },
                    scope: this
                },
                '-',
                {
                    text: "执行查询",
                    iconCls: "btnrefresh",
                    handler: function (button, event) {
                        this.onRefreshClick(button, event);
                    },
                    scope: this
                }, '-', {
                    text: "保存列表样式",
                    menu: [{ text: "保存",
                        handler: function (button, event) {
                            _this.girdcolums = DsTruck.SaveGridPanel(GID, _this.formname, _this.gridList.columns, _this.girdcolums, 0, true); //使用者id,表名,中间column数组,跳过一开始的几列

                        }
                    }, { text: "初始化",
                        handler: function (menu, event) {
                            _this.formWMSINBody.reconfigure(_this.storeList, _this.columns);
                            _this.girdcolums = DsTruck.SaveGridPanel(GID, _this.formname, _this.gridList.columns, _this.columns, 0, true); //使用者id,表名,中间column数组,跳过一开始的几列
                        }
                    }],
                    scope: this
                }, '-',
                {
                    text: "锁定",
                    iconCls: 'new_lock',
                    id: 'btnLock',
                    name: 'btnLock',
                    handler: function (button, event) {
                        this.Lock();
                    },
                    scope: this
                }, {
                    text: "解锁",
                    iconCls: 'new_lock',
                    id: 'btnUnLock',
                    name: 'btnUnLock',
                    handler: function (button, event) {
                        this.UnLock();
                    },
                    scope: this
                }, {
                    text: "打印并发送邮件",
                    id: "btnprint",
                    iconCls: 'btnprint',
                    handler: function (button, event) {
                        this.onPrintClick(button, event);
                    },
                    scope: this
                }, '-', {
                    text: '导入Excel',
                    iconCls: "btnexportexcel",
                    tooltip: '导入Excel',
                    handler: function (button, event) {
                        this.onImportCtnClick();
                    },
                    scope: this
                }
            ]
        });

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

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

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

        this.onRefreshClick();

    }, //end initUIComponents

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

    onDeleteClick: function (button, event) {
        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 record = selections[0];
        if (record.data.ISLOCK == "True" || record.data.ISLOCK == "1") {
            Ext.Msg.show({ title: '提示', msg: '有已入账的信息,不允许删除!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
            return;
        }

        var selectedRecords = [];
        selectedRecords = this.gridList.selModel.getSelection();
        var danhaoStr = '';
        for (var i = 0; i < selectedRecords.length; i++) {
            danhaoStr += ("'" + selectedRecords[i].data.XiuXiangDanHao + "',");
        }
        if (danhaoStr.length > 0) {
            danhaoStr = danhaoStr.substring(0, danhaoStr.length - 1);
        }

        Ext.MessageBox.confirm('提示', '确定删除该记录吗?', function (btn) {
            if (btn == 'yes') {
                Ext.Msg.wait('正在删除数据...');
                Ext.Ajax.request({
                    waitMsg: '正在删除数据...',
                    url: '/MvcShipping/MsOpCtnRepair/DeleteData',
                    params: {
                        xiuxiangdanhao: danhaoStr,
                        userid: USERID
                    },
                    callback: function (options, success, response) {
                        if (success) {
                            var jsonresult = Ext.JSON.decode(response.responseText);
                            if (jsonresult.Success) {
                                this.storeList.remove(selections);
                                Ext.Msg.show({ title: '提示', msg: jsonresult.Message, 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);

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

        var sql = '';


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

        var MBLNO = form.findField('MBLNO').getValue();
        sql = sql + getAndConSql(sql, MBLNO, " ( MBLNO like '%" + MBLNO + "%')");
        // var CUSTOMERNAME = form.findField('CUSTOMERNAME').getValue();
        var ChangZhan = this.comboxYARD.getValue();
        if (ChangZhan != '' && ChangZhan != null) {
            sql = sql + getAndConSql(sql, ChangZhan, " (ChangZhan like '%" + ChangZhan + "%' or ChangZhanSub like '%" + ChangZhan + "%')");
        }

        var XiuXiangRiQiFrom = form.findField('XiuXiangRiQiFrom').getRawValue();
        if (XiuXiangRiQiFrom != '' && XiuXiangRiQiFrom != null) {
            sql = sql + getAndConSql(sql, XiuXiangRiQiFrom, " XiuXiangRiQi >= '" + XiuXiangRiQiFrom + "'");
        }

        var XiuXiangRiQiTo = form.findField('XiuXiangRiQiTo').getRawValue();
        if (XiuXiangRiQiTo != '' && XiuXiangRiQiTo != null) {
            sql = sql + getAndConSql(sql, XiuXiangRiQiTo, " XiuXiangRiQi <= '" + XiuXiangRiQiTo + "'");
        }

        return sql;
    },
    OprationSwap: function () {
        var ret = new Array();
        ret[0] = this.OprationStatus;
        ret[1] = this.storeList;
        ret[2] = this.SelectedRecord;

        return ret;
    }, onPrintClick: function (button, event) {
        var selectedRecords = [];
        selectedRecords = this.gridList.selModel.getSelection();
        var danhaoStr = '';
        for (var i = 0; i < selectedRecords.length; i++) {
            if (selectedRecords[i].data.IsLock == '0') {
                Ext.MessageBox.alert('提示', "未锁定无法打印!");
                return;
            }
            danhaoStr += ("'" + selectedRecords[i].data.GID + "',");
        }
        if (danhaoStr.length > 0) {
            danhaoStr = danhaoStr.substring(0, danhaoStr.length - 1);
        }
        var printType = 'MsOpCtnRepairList';
        var uid = USERID;
        var sql1 = "select * from Op_CtnRepair where gid in (" + danhaoStr + ")";
        var wherestr = this.getCondition();
        if (wherestr != '') {
            sql1 += "where 1=1 and " + wherestr;
        }

        PrintComm(printType, sql1);
        //更改邮件发送状态
        var selectedRecords = [];
        selectedRecords = this.gridList.selModel.getSelection();
        var danhaoStr = '';
        for (var i = 0; i < selectedRecords.length; i++) {
            danhaoStr += ("'" + selectedRecords[i].data.GID + "',");
        }
        if (danhaoStr.length > 0) {
            danhaoStr = danhaoStr.substring(0, danhaoStr.length - 1);
        }
        Ext.Ajax.request({
            waitMsg: '正在查询主表数据...',
            url: '/MvcShipping/MsOpCtnRepair/ChangeMailStatus',
            params: {
                RepairIDs: danhaoStr
            },
            callback: function (options, success, response) {
                if (success) {
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.Success == true) {
                        this.onRefreshClick();
                    }
                } else {
                    Ext.MessageBox.alert('请求出现错误,请重试', response.responseText);
                }
            },
            scope: this
        });


    }, UnLock: function () {
        var selectedRecords = [];
        selectedRecords = this.gridList.selModel.getSelection();
        var danhaoStr = '';
        for (var i = 0; i < selectedRecords.length; i++) {
            danhaoStr += ("'" + selectedRecords[i].data.GID + "',");
        }
        if (danhaoStr.length > 0) {
            danhaoStr = danhaoStr.substring(0, danhaoStr.length - 1);
        }
        Ext.Ajax.request({
            waitMsg: '正在查询主表数据...',
            url: '/MvcShipping/MsOpCtnRepair/UnLock',
            params: {
                RepairIDs: danhaoStr
            },
            callback: function (options, success, response) {
                if (success) {
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.Success == true) {
                        Ext.MessageBox.alert('提示', result.Message);
                        this.onRefreshClick();
                    } else {
                        Ext.MessageBox.alert('提示', result.Message);
                        return;
                    }
                } else {
                    Ext.MessageBox.alert('请求出现错误,请重试', response.responseText);
                }
            },
            scope: this
        });
    }, Lock: function () {
        var selectedRecords = [];
        selectedRecords = this.gridList.selModel.getSelection();
        var danhaoStr = '';
        for (var i = 0; i < selectedRecords.length; i++) {
            danhaoStr += ("'" + selectedRecords[i].data.GID + "',");
        }
        if (danhaoStr.length > 0) {
            danhaoStr = danhaoStr.substring(0, danhaoStr.length - 1);
        }
        Ext.Ajax.request({
            waitMsg: '正在查询主表数据...',
            url: '/MvcShipping/MsOpCtnRepair/Lock',
            params: {
                RepairIDs: danhaoStr
            },
            callback: function (options, success, response) {
                if (success) {
                    var result = Ext.JSON.decode(response.responseText);
                    if (result.Success == true) {
                        Ext.MessageBox.alert('提示', result.Message);
                        this.onRefreshClick();
                    } else {
                        Ext.MessageBox.alert('提示', result.Message);
                        return;
                    }
                } else {
                    Ext.MessageBox.alert('请求出现错误,请重试', response.responseText);
                }
            },
            scope: this
        });
    },
    onImportCtnClick: function () {
        me = this;
        var imgform = new Ext.FormPanel({
            region: 'center',
            labelWidth: 20,
            frame: true,
            autoScroll: false,
            border: false,
            fileUpload: true,
            items: [{
                xtype: 'fileuploadfield',
                id: 'LoadExcel',
                name: 'LoadExcel',
                emptyText: '请选择EXCEL文件',
                fieldLabel: 'EXCEL',
                buttonText: '选择文件',
                allowBlank: false,
                width: 200,
                buttonCfg:
                        {
                            iconCls: 'uploaddialog'
                        },
                anchor: '98%'
            }],
            buttons: [{
                text: '上传',
                type: 'submit',
                handler: function () {
                    var UserFilePath = Ext.getCmp('LoadExcel').getValue();
                    if (!CheckFileExt(UserFilePath, /.xls|.xlsx/i)) {
                        Ext.Msg.show({ title: '错误', msg: '请确认你上传的文件为EXCEL文件!', icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                        return;
                    }
                    if (!imgform.form.isValid()) { return; }
                    imgform.form.submit({
                        url: '/MvcShipping/MsOpCtnRepair/ImportExcel',
                        waitMsg: '正在上传',
                        method: 'POST',
                        params: {
                        },
                        success: function (form, action) {
                            me.onRefreshClick();
                            win.close();
                            Ext.Msg.show({ title: '提示', msg: action.result.Message, icon: Ext.Msg.INFO, buttons: Ext.Msg.OK });
                            

                        },
                        failure: function (form, action) {
                            form.reset();
                            if (action.failureType == Ext.form.Action.SERVER_INVALID) {
                                Ext.MessageBox.alert('失败', action.result.Message);
                            } else {
                                Ext.MessageBox.alert('失败', action.result.Message);
                            }
                        }
                    });
                }
            }, {
                text: '关闭',
                type: 'submit',
                handler: function () {
                    win.close(this);
                }
            }]
        });

        var win = new Ext.Window({
            title: "上传EXCEL",
            width: 380,
            height: 120,
            modal: true,
            resizable: false,
            border: false,
            items: imgform

        });
        win.show();
    }


});