// JScript 文件
//商品管理
//商品管理分类


//000000商品管理列表开始
function UpProductPic(msg, picstr) {
    Ext.MessageBox.alert(msg);
    //var temp1 = Ext.get('Adpic');
    //    var temp2 = document.getElementById("Adpic");
    var temp3 = document.getElementsByName("Img_Big");
    if (temp3.length > 1) {

        temp3 = temp3[temp3.length - 1];
        temp3.value = temp3.value + "," + picstr.replace(",", "");
    } else {
        temp3.Img_Big.value =temp3.Img_Big.value+","+ picstr.replace(",", "");

    }

}
var ProductInfoStore;
ProductInfoManage = function(node) {//分页每页显示数量	 
    var pageSize = 12;
    //指定列参数
    //{ "ID", "ProductName", "ClassId", "Price", "State", "Stock", "Brand", "Img_Logo", "Img_Big", "Intro", "Content", "AddDate", "ClickCount", "BuyCount", "UserName", "Point", "IsHtml", "Url" };
    var ProductInfoFields = ["ID", "ProductName", "ClassId", "Price", "State", "Stock", "Brand", "Img_Logo", "Img_Big",  "AddDate", "ClickCount", "BuyCount", "UserName", "Point", "IsHtml", "Url"];
    ProductInfoStore = new Ext.data.GroupingStore({
        proxy: new Ext.data.HttpProxy(
           {
               url: "/MCScriptManageServices/AllClassInfo.aspx",
               method: "POST"
           }),
        reader: new Ext.data.JsonReader(
           {
               fields: ProductInfoFields,
               root: "data",
               id: "ID",
               totalProperty: "totalCount"

           })
        //groupField:'id'
          // sortInfo: { field: 'ID', direction: "DESC" }
    });
    ProductInfoStore.load({ params: { start: 0, limit: pageSize} });




    //--------------------------------------------------列选择模式
    var sm = new Ext.grid.CheckboxSelectionModel({
    //dataIndex:"id"
});
//--------------------------------------------------列头
var cm = new Ext.grid.ColumnModel([
		sm,
//    var ProductInfoFields = ["ID", "ProductName", "ClassId", "Price", "State", "Stock", "Brand", "Img_Logo", "Img_Big", "Intro", "Content", "AddDate", "ClickCount", "BuyCount", "UserName", "Point", "IsHtml", "Url"];

{
    header: "商品名称",
    dataIndex: "ProductName",
    tooltip: "商品名称",
    sortable: true
}, {
    header: "价格",
    tooltip: "价格",
    dataIndex: "Price",
    sortable: true
}]);


    var ProductInfoGrid = new Ext.grid.GridPanel({
        id: "ProductInfoGrid",
        store: ProductInfoStore,
        sm: sm,
        cm: cm,
        loadMask: true,
        stripeRows: true,
        //autoExpandColumn:7,
        //超过长度带自动滚动条
        autoScroll: true,
        border: false,
        view: new Ext.grid.GroupingView({
            //自动填充
            forceFit: true,
            sortAscText: '正序排列',
            sortDescText: '倒序排列',
            columnsText: '列显示/隐藏',
            groupByText: '根据本列分组',
            showGroupsText: '是否采用分组显示',
            groupTextTpl: '{text} (<font color=red><b>{[values.rs.length]}</b></font> {[values.rs.length > 0 ? "条器件信息" : "暂无器件信息"]})'
        }),
        //分页
        bbar: new Ext.PagingToolbar({
            store: ProductInfoStore,
            pageSize: pageSize,
            //显示右下角信息
            displayInfo: true,
            displayMsg: '当前记录 {0} -- {1} 条 共 {2} 条记录',
            emptyMsg: "No results to display",
            prevText: "上一页",
            nextText: "下一页",
            refreshText: "刷新",
            lastText: "最后页",
            firstText: "第一页",
            beforePageText: "当前页",
            afterPageText: "共{0}页"

        }),
        tbar: [
		new Ext.Toolbar.Fill()
		, "", "-", "",
			{
			    text: "刷新信息",
			    //默认样式为按下
			    //pressed:true,
			    tooltip: "刷新当前页面信息",
			    iconCls: "refreshicon",
			    handler: function() {
			        ProductInfoStore.reload();
			    }
			}, "", "-"
		,
		{
		    text: "添加",
		    tooltip: "添加商品信息",
		    iconCls: "addicon",
		    handler: AddProductInfFN
		}, "", "-", "", {
		    text: "编辑",
		    tooltip: "编辑商品信息",
		    iconCls: "editicon",
		    handler: EditProductInfFN
		}, "", "-", "", {
		    text: "删除",
		    tooltip: "删除商品信息",
		    iconCls: "deleteicon",
		    handler: DelProductInfFN

		}, "-"], listeners: {
		    'contextmenu': function(e) {
		        e.stopEvent();
		    },
		    'rowcontextmenu': function(grid, rowIndex, e) {
		        e.stopEvent();
		    }
		}
    });



    GridMain(node, ProductInfoGrid, "usericon");
}

function DelProductInfFN() {
    var row = Ext.getCmp("ProductInfoGrid").getSelectionModel().getSelections();
    if (row.length == 0) {
        Ext.Msg.alert("提示信息", "请您至少选择一个!");
    }
    else {
        Ext.Msg.confirm("提示!", "您确定要删除该商品信息吗?", function(btn) {
            if (btn == "yes") {
                DelProductInfFNn(row);
            }
            else {

            }
        })
    }
}

function EditProductInfFN() {
    var row = Ext.getCmp("ProductInfoGrid").getSelectionModel().getSelections();
    if (row.length == 0) {
        Ext.Msg.alert("提示信息", "您没有选中任何行!");
    }
    else if (row.length > 1) {

        Ext.Msg.alert("提示信息", "对不起只能选择一个!");
    } else if (row.length == 1) {

        EditProductInfos(row[0]); //传行一行记录直接加载
    }
}

//000000商品管理管理结束
//000000商品管理添加开始


AddProductInfFN = function () {
    //------分类类型
    var AddClassRoleFields = Ext.data.Record.create([
        { name: 'ID', mapping: 'ID' }, { name: 'ClassName', mapping: 'ClassName' }
    ]);

    var AddClassStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/MCScriptManageServices/ClassInfoProduct.aspx'
        }),
        reader: new Ext.data.JsonReader({
            root: 'data',
            id: 'ID'
        },
           AddClassRoleFields
            )
    });
    AddClassStore.load();
    //品牌
    var AddBrandRoleFields = Ext.data.Record.create([
        { name: 'ID', mapping: 'ID' }, { name: 'ClassName', mapping: 'ClassName' }
    ]);

    var AddBrandStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/MCScriptManageServices/BrandInfoProduct.aspx'
        }),
        reader: new Ext.data.JsonReader({
            root: 'data',
            id: 'ID'
        },
           AddBrandRoleFields
            )
    });
    AddBrandStore.load();

    var AddProductInfForm = new Ext.form.FormPanel({
        width: 800,
        plain: true,
        layout: "form",
        defaultType: "textfield",
        labelWidth: 60,
        height: 500,
        baseCls: "x-plain",
        defaults: { anchor: "95%", msgTarget: "side" },
        buttonAlign: "center",
        bodyStyle: "padding:0 0 0 0",
        items: [
        //["ID", "ProductName", "ClassId", "Price", "State", "Stock", "Brand", "Img_Logo", "Img_Big", "Intro", "Content", "AddDate", "ClickCount", "BuyCount", "UserName", "Point", "IsHtml", "Url"];

			{
			name: "ProductName",
			fieldLabel: "商品名称",
			allowBlank: false,
			blankText: "商品名称"

}, {

    xtype: "combo",
    tpl: '<tpl for="."><div ext:qtip="{ClassName}" class="x-combo-list-item">{ClassName}</div></tpl>',
    store: AddClassStore,
    typeAhead: true,
    fieldLabel: '分类',
    hiddenName: 'ClassId',
    name: 'ClassId',
    forceSelection: true,
    triggerAction: 'all',
    emptyText: '选择分类',
    selectOnFocus: true,
    width: 130,
    editable: false,
    allowBlank: false,
    blankText: '请选择分类',
    displayField: 'ClassName',
    valueField: 'ID',
    mode: 'remote'
}, {
    name: "Price",
    fieldLabel: "价格",
    allowBlank: false,
    blankText: "价格"
}, {

    name: "State",
    xtype: "combo",
    fieldLabel: "商品状态",
    //传入后台真实值value field /value
    hiddenName: "State",
    editable: false,
    mode: "local",
    displayField: "show",
    valueField: "value",
    triggerAction: "all",
    value: "0",
    store: new Ext.data.SimpleStore({
        fields: ["show", "value"],
        data: [["正常", "0"], ["禁用", "1"]]
    })
}, {

    xtype: "combo",
    tpl: '<tpl for="."><div ext:qtip="{ClassName}" class="x-combo-list-item">{ClassName}</div></tpl>',
    store: AddBrandStore,
    typeAhead: true,
    fieldLabel: '品牌',
    hiddenName: 'Brand',
    name: 'Brand',
    forceSelection: true,
    triggerAction: 'all',
    emptyText: '选择品牌',
    selectOnFocus: true,
    width: 130,
    editable: false,
    allowBlank: false,
    blankText: '请选择品牌',
    displayField: 'ClassName',
    valueField: 'ID',
    mode: 'remote'
}, {//"Img_Logo", "Img_Big", "Intro", "Content", "AddDate", "ClickCount", "BuyCount", "UserName", "Point", "IsHtml", "Url"
    name: "Img_Big",
    fieldLabel: "商品图片",
    allowBlank: false,
    blankText: "商品图片"
}, {
    xtype: 'panel',
    plain: true,
    width: 299,
    height: 76,
    html: "<iframe src='/MCScriptManageServices/upfilespro.aspx'scrolling='no' frameborder=0 width=100% height=100%></iframe>"

}, {
    xtype: 'textarea',
    name: "Intro",
    fieldLabel: "简介",
    allowBlank: false,
    height: 60,
    blankText: "简介"
}, {
    xtype: 'textarea',
    name: "IntroMore",
    fieldLabel: "详细参数",
    allowBlank: false,
    height: 60,
    blankText: "详细参数",
    value: "<li></li>"
}, {
    name: "BuyCount",
    fieldLabel: "购买次数",
    allowBlank: false,
    blankText: "购买次数"
}, {
    name: "Point",
    fieldLabel: "积分",
    allowBlank: false,
    blankText: "积分"
}, {
    xtype: 'htmleditor',
    name: "Content",
    fieldLabel: "详细介绍",
    allowBlank: false,
    height: 200,
    blankText: "详细介绍"
}
			]
    });


    var AddProductInfWin = new Ext.Window({
        title: "添加新商品",
        width: 810,
        plain: true,
        layout: "form",
        iconCls: "addicon",
        //不可以随意改变大小
        resizable: false,
        //是否可以拖动
        //draggable:false,
        defaultType: "textfield",
        labelWidth: 100,
        collapsible: true, //允许缩放条
        closeAction: 'hide',
        closable: true,
        //弹出模态窗体
        modal: 'true',
        buttonAlign: "center",
        bodyStyle: "padding:10px 0 0 15px",
        items: [AddProductInfForm],
        listeners: {
            "show": function () {
                AddProductInfForm.getForm().reset();
            }
        },
        buttons: [{
            text: "保存信息",
            minWidth: 70,
            handler: function () {
                if (AddProductInfForm.getForm().isValid()) {
                    //弹出效果
                    Ext.MessageBox.show
                                (
                                    {
                                        msg: '正在保存，请稍等...',
                                        progressText: 'Saving...',
                                        width: 300,
                                        wait: true,
                                        waitConfig: { interval: 200 },
                                        icon: 'download',
                                        animEl: 'saving'
                                    }
                                );
                    setTimeout(function () { }, 1000);
                    AddProductInfForm.form.submit({
                        url: "/MCScriptManageServices/Addproduct.aspx",
                        method: "POST",
                        success: function (form, action) {
                            //成功后
                           
                            var flag = action.result.success;
                            if (flag == "true") {
                                Ext.MessageBox.alert("恭喜", "添加新商品成功!");
                                ProductInfoStore.reload();
                                AddProductInfWin.hide();
                            } else if (flag == "false") {

                                var reson = action.result.reson;
                                //Ext.MessageBox.alert("提示",""+reson+"!");
                                if (reson != "undefined")
                                    Ext.MessageBox.alert("提示", "系统已存在此商品名!");
                            }
                        },
                        failure: function (form, action) {
                            Ext.MessageBox.alert("提示!", "添加新商品失败!");
                        }
                    });
                }
            }
        }, {
            text: "重置",
            minWidth: 70,
            qtip: "重置数据",
            handler: function () {
                AddProductInfForm.getForm().reset();
            }
        }, {
            text: "取 消",
            minWidth: 70,
            handler: function () {
                AddProductInfWin.hide();
            }
        }]

    });
    AddProductInfWin.show();
}

    //000000商品管理添加结束
    //000000商品管理编辑开始
    EditProductInfos = function(row) {
        var AddClassRoleFields = Ext.data.Record.create([
        { name: 'ID', mapping: 'ID' }, { name: 'ClassName', mapping: 'ClassName' }
    ]);

        var AddClassStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({
                url: '/MCScriptManageServices/ClassInfoProduct.aspx'
            }),
            reader: new Ext.data.JsonReader({
                root: 'data',
                id: 'ID'
            },
           AddClassRoleFields
            )
        });
        AddClassStore.load();
        //品牌
        var AddBrandRoleFields = Ext.data.Record.create([
        { name: 'ID', mapping: 'ID' }, { name: 'ClassName', mapping: 'ClassName' }
    ]);

        var AddBrandStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({
                url: '/MCScriptManageServices/BrandInfoProduct.aspx'
            }),
            reader: new Ext.data.JsonReader({
                root: 'data',
                id: 'ID'
            },
           AddBrandRoleFields
            )
        });
        AddBrandStore.load();

        var EditProductInfoForm = new Ext.form.FormPanel({
            width: 810,
            plain: true,
            layout: "form",
            height:500,
            defaultType: "textfield",
            labelWidth: 60,
            baseCls: "x-plain",
            defaults: { anchor: "95%", msgTarget: "side" },
            buttonAlign: "center",
            bodyStyle: "padding:0 0 0 0",
            items: [
						{
						    xtype: 'hidden',
						    name: "ID",
						    fieldLabel: "登录帐号",
						    allowBlank: false,
						    blankText: "帐号不允许为空",
						    readOnly: true
						}, {
						    name: "ProductName",
						    fieldLabel: "商品名称",
						    allowBlank: false,
						    blankText: "商品名称"
						}, {

						    xtype: "combo",
						    tpl: '<tpl for="."><div ext:qtip="{ClassName}" class="x-combo-list-item">{ClassName}</div></tpl>',
						    store: AddClassStore,
						    typeAhead: true,
						    fieldLabel: '分类',
						    hiddenName: 'ClassId',
						    name: 'ClassId',
						    forceSelection: true,
						    triggerAction: 'all',
						    emptyText: '选择分类',
						    selectOnFocus: true,
						    width: 130,
						    editable: false,
						    allowBlank: false,
						    blankText: '请选择分类',
						    displayField: 'ClassName',
						    valueField: 'ID',
						    mode: 'remote'
						}, {
						    name: "Price",
						    fieldLabel: "价格",
						    allowBlank: false,
						    blankText: "价格"
						}, {

						    name: "State",
						    xtype: "combo",
						    fieldLabel: "商品状态",
						    //传入后台真实值value field /value
						    hiddenName: "State",
						    editable: false,
						    mode: "local",
						    displayField: "show",
						    valueField: "value",
						    triggerAction: "all",
						    value: "0",
						    store: new Ext.data.SimpleStore({
						        fields: ["show", "value"],
						        data: [["正常", "0"], ["禁用", "1"]]
						    })
						}, {

						    xtype: "combo",
						    tpl: '<tpl for="."><div ext:qtip="{ClassName}" class="x-combo-list-item">{ClassName}</div></tpl>',
						    store: AddBrandStore,
						    typeAhead: true,
						    fieldLabel: '品牌',
						    hiddenName: 'Brand',
						    name: 'Brand',
						    forceSelection: true,
						    triggerAction: 'all',
						    emptyText: '选择品牌',
						    selectOnFocus: true,
						    width: 130,
						    editable: false,
						    allowBlank: false,
						    blankText: '请选择品牌',
						    displayField: 'ClassName',
						    valueField: 'ID',
						    mode: 'remote'
						}, {//"Img_Logo", "Img_Big", "Intro", "Content", "AddDate", "ClickCount", "BuyCount", "UserName", "Point", "IsHtml", "Url"
						    name: "Img_Big",
						    fieldLabel: "商品图片",
						    allowBlank: false,
						    blankText: "商品图片"
						}, {
						    xtype: 'panel',
						    plain: true,
						    width: 299,
						    height: 76,
						    html: "<iframe src='/MCScriptManageServices/upfilespro.aspx'scrolling='no' frameborder=0 width=100% height=100%></iframe>"

						}, {
						    xtype: 'textarea',
						    name: "Intro",
						    fieldLabel: "简介",
						    allowBlank: false,
						    height: 60,
						    blankText: "简介"
						}, {
						    xtype: 'textarea',
						    name: "IntroMore",
						    fieldLabel: "详细参数",
						    allowBlank: false,
						    height: 60,
						    blankText: "详细参数",
						    value: "<li></li>"
						}, {
						    name: "BuyCount",
						    fieldLabel: "购买次数",
						    allowBlank: false,
						    blankText: "购买次数"
						}, {
						    name: "Point",
						    fieldLabel: "积分",
						    allowBlank: false,
						    blankText: "积分"
						}, {
						    xtype: 'htmleditor',
						    name: "Content",
						    fieldLabel: "详细介绍",
						    allowBlank: false,
						    height: 200,
						    blankText: "详细介绍"
						}
			]
        });


        var EditProductInfoWin = new Ext.Window({
            title: "编辑分类信息",
            width: 820,
            plain: true,
            //layout:"form",
            iconCls: "addicon",
            //不可以随意改变大小
            resizable: false,
            //是否可以拖动
            //draggable:false,
            defaultType: "textfield",
            labelWidth: 100,
            collapsible: true, //允许缩放条
            closeAction: 'hide',
            closable: true,
            //弹出模态窗体
            modal: 'true',
            buttonAlign: "center",
            bodyStyle: "padding:10px 0 0 15px",
            items: [EditProductInfoForm],
            listeners: {
                "show": function() {

                    EditProductInfoForm.getForm().reset();
                    var classid_input = row.data.ID;
                    EditProductInfoForm.form.load(
                                {
                                    url: "/MCScriptManageServices/GetProduct.aspx?id=" + classid_input,
                                    method: "get",
                                    success: function(form, action) {

                                    },
                                    failure: function(form, action) {
                                        Ext.Msg.alert("提示信息", "对不起加载数据失败!请联系管理员");
                                    }
                                }
                    );
                }
            },
            buttons: [{
                text: "保存信息",
                minWidth: 70,
                handler: function() {
                    if (EditProductInfoForm.getForm().isValid()) {
                        //弹出效果
                        Ext.MessageBox.show
                                (
                                    {
                                        msg: '正在保存，请稍等...',
                                        progressText: 'Saving...',
                                        width: 300,
                                        wait: true,
                                        waitConfig: { interval: 200 },
                                        icon: 'download',
                                        animEl: 'saving'
                                    }
                                );
                        setTimeout(function() { }, 1000);
                        EditProductInfoForm.form.submit({
                            url: "/MCScriptManageServices/UpdateProduct.aspx",
                            method: "POST",
                            success: function(form, action) {
                                //成功后
                                var flag = action.result.success;
                                if (flag == "true") {
                                    Ext.MessageBox.alert("恭喜", "保存商品信息成功!");
                                    ProductInfoStore.reload();
                                    EditProductInfoWin.hide();
                                } else if (flag == "false") {

                                    Ext.MessageBox.alert("提示", "您的商品名可能系统中已经存在，请换一个商品名试下!");
                                    ProductInfoStore.reload();
                                    EditProductInfoWin.hide();
                                }
                            },
                            failure: function(form, action) {
                                Ext.MessageBox.alert("提示!", "保存商品信息失败!");
                            }
                        });
                    }
                }
            }, {
                text: "重置",
                minWidth: 70,
                qtip: "重置数据",
                handler: function() {
                    var classid_input = row.data.ID;
                    EditProductInfoForm.form.load(
                                {
                                    url: "/MCScriptManageServices/GetProduct.aspx?id=" + classid_input,
                                    method: "get",
                                    success: function(form, action) {

                                    },
                                    failure: function(form, action) {
                                        Ext.Msg.alert("提示信息", "对不起加载数据失败!请联系管理员");
                                    }
                                }
                    );
                }
            }, {
                text: "取 消",
                minWidth: 70,
                handler: function() {
                    EditProductInfoWin.hide();
                }
}]

            });
            EditProductInfoWin.show();
        }

        //000000商品管理编辑结束
        //000000商品管理删除开始

        DelProductInfFNn = function(row) {
            var deletesplit = "";
            for (var i = 0; i < row.length; i++) {
                if (row.length == 1) {
                    deletesplit = row[i].data.ID;
                }
                else {

                    if (i < (row.length - 1)) {
                        deletesplit = row[i].data.ID + "," + deletesplit;
                    }
                    if (i == (row.length - 1)) {
                        deletesplit = deletesplit + row[i].data.ID;
                    }
                }
            }
            Ext.Ajax.request({
                url: "/MCScriptManageServices/DelProduct.aspx",
                method: "POST",
                params: {
                    id: deletesplit
                },
                success: function() {
                    ProductInfoStore.reload();
                },
                failure: function() {
                    Ext.Msg.alert("提 示", "删除失败了!");
                }
            });
        }

        //000000商品管理删除结束