﻿/**
* 客訴信箱
* @type  class
* @autor HONG, LaneChang
* @date  2010/07/07
**/
var MailServ = {

    fpnl: undefined,

    win: undefined,

    options: {
        windowWidth: 550,
        windowHeight: 413
    },

    /**
    * 載入
    */
    OnLoad: function () {
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = 'include/ext/resources/images/default/s.gif';

        if (!Ext.isEmpty(MailServ.fpnl, true) || !Ext.isEmpty(MailServ.win, true)) return;

        Ext.onReady(function () {
            Ext.Ajax.request({
                url: "Handler.aspx?fn=IsMember",
                mode: "GET",
                scope: this,
                success: function (response, options) {
                    var o = '';
                    try {
                        o = Ext.util.JSON.decode(response.responseText);
                        if (o.success) 
                        	MailServ.CreateLayout(o.data);
                        else
                        	MailServ.CreateLayout(null);
                    }
                    catch (e) {
                        Util.MsgBox(true, e.message, null);
                    }
                },
                failure: function () {
                    Util.MsgBox(true, "系統忙碌中，<br/>請稍候再試或重試一次。", null);
                }
            });

        });
    },

    /**
    * 建立版面
    * @param {object} mbr -會員資料物件
    */
    CreateLayout: function (mbr) {
        //建立FromPanel
        MailServ.fpnl = new Ext.form.FormPanel({
            id: "MailServ_fpnl",
            autoHeight: true,
            bodyStyle: "background:transparent; padding:5px;",
            border: false,
            labelSeparator: '：',
            labelWidth: 65,
            labelAlign: "right",
            items: [{
                xtype: "textfield",
                id: "cs_name",
                fieldLabel: "姓 名",
                emptyText: "請輸入您的姓名",
                allowBlank: false,
                anchor: "40%",
                listeners: {
                	"render": function() {
                		if (mbr != null && mbr.name != undefined) 
                			this.setValue(mbr.name);
                	}
                }
            }, {
                xtype: "textfield",
                id: "cs_email",
                fieldLabel: "電子郵件",
                emptyText: "請輸入您的電子信箱",
                vtype: "email",
                anchor: "60%",
                listeners: {
                	"render": function() {
                		if (mbr != null && mbr.email != undefined) 
                			this.setValue(mbr.email);
                	}
                }
            }, {
                xtype: "combo",
                width: 120,
                id: "cs_type_show",
                hiddenName: "cs_type",
                fieldLabel: "類 型",
                emptyText: "請選擇類型...",
                triggerAction: "all",
                allowBlank: false,
                mode: "local",
                displayField: "text",
                readOnly: true,
                valueField: "value",
                store: new Ext.data.JsonStore({
                    storeId: "MailServ_store",
                    url: "Handler.aspx?fn=GetQuestionList",
                    totalProperty: "total",
                    root: "data",
                    fields: ["value", "text"],
                    autoLoad: {
                        callback: function () {
                            Ext.ComponentMgr.get("cs_type_show").setValue(1);
                        }
                    }
                })
            }, {
                xtype: "textfield",
                id: "cs_title",
                fieldLabel: "標 題",
                emptyText: "請輸入信件標題",
                allowBlank: false,
                anchor: "95%"
            }, {
                xtype: "textarea",
                id: "cs_content",
                fieldLabel: "內 容",
                emptyText: "請輸入信件內容",
                allowBlank: false,
                anchor: "95%",
                height: 150
            }]
        });


        //建立浮動Window 
        MailServ.win = new Ext.Window({
            id: "MailServ_win",
            layout: "fit",
            width: MailServ.options.windowWidth,
            autoHeight: true,
            bodyStyle: "background:transparent; border:none;",
            border: false,
            constrain: true,
            resizable: false,
            modal: true,
            closeable: true,
            plain: true,
            buttonAlign: "center",
            title: "客 服 信 箱",
            items: [MailServ.fpnl],
            scope: this,
            buttons: [{
                xtype: "button",
                id: "cs_submit",
                name: "cs_submit",
                text: "送 出",
                handler: function () {
                    Ext.MessageBox.show({
                        title: "提示訊息",
                        msg: "您確定要寄出客服郵件？",
                        minWidth: 200,
                        scope: this,
                        buttons: Ext.MessageBox.YESNO,
                        icon: "dialog-QUESTION",
                        fn: function (click) {
                            if (click == "yes") MailServ.Submit();
                        }
                    });
                }
            }],
            listeners: {
                "close": function () {
                    Ext.StoreMgr.get("MailServ_store").removeAll;
                    Ext.ComponentMgr.get("MailServ_win").removeListener("close");
                    if (Ext.ComponentMgr.get("MailServ_win") != undefined) Ext.ComponentMgr.get("MailServ_win").destroy();
                    if (Ext.ComponentMgr.get("MailServ_fpnl") != undefined) Ext.ComponentMgr.get("MailServ_fpnl").destroy();
                    delete MailServ.fpnl;
                    delete MailServ.win;
                    if (Ext.isIE) CollectGarbage();
                }
            }
        }).show();
    },

    /**
    * 送出資料
    */
    Submit: function () {
        if (this.fpnl.getForm().isValid()) {
            this.fpnl.getForm().submit({
                method: "POST",
                url: "Handler.aspx?fn=AddCustomerAction",
                waitMsg: "資料處理中，請稍候...",
                waitTitle: "系統訊息",
                reset: false,
                success: function (form, action) {
                    var o = '';
                    try {
                        o = Ext.util.JSON.decode(action.response.responseText);
                        switch (o.success) {
                            case true:
                                Util.MsgBox(false, o.msg, function () {
                                    Ext.ComponentMgr.get("MailServ_win").close();
                                });
                                break;

                            case false: throw Error(o.msg);
                        }
                    } catch (e) {
                        Util.MsgBox(true, e.Message, null);
                    }
                },
                failure: function (form, action) {
                    Util.MsgBox(true, "系統忙碌中，<br/>請稍候再試或重試一次。", null);
                }
            });
        }
    }
};

