layui.define(['form', 'laydate', 'table', 'festivalProductApi' ], function(exports) {
	var form = layui.form;
	var laydate = layui.laydate;
	var table = layui.table;
	var channelUrlApi=layui.channelUrlApi;
	var festivalTable = null;
	var fescod = null;
	var fescod = null;
	var view = {
		init: function(fescod){
			this.initTable(fescod);
			this.initSearchForm();
			this.initToolBar(fescod);

			window.dataReload = function(){
				Lib.doSearchForm($("#searchForm"), festivalTable)
			}
		},
		initTable:function(fescod) {
			festivalTable = table.render({
				elem : '#festivalTable',
				height : Lib.getTableHeight(2),
				method : 'post',
				url : Common.ctxPath + '/directed/festivalProduct/list.json?fescod=' + fescod //数据接口
				,page : true //开启分页
				,limit : 10,
				cols : [[ //表头
                    {
                        type : 'checkbox'
                    }, {
                        field : 'id',
                        title : 'ID',
                        width : 80,
                    }, {
                        field : 'fescod',
                        title : '活动编码',
                        width : 80,
                    }, {
                        field : 'cpid',
                        title : 'cpid',
                        width : 140,
                    }, {
                        field : 'productid',
                        title : '产品ID',
                        width : 120,
                    }, {
                        field : 'productname',
                        title : '产品名称',
                        width : 200,
                    }, {
                        field : 'icon',
                        title : '验证码（订购,退订,预约订购,激活,领取)',
                        width : 250,
                    }, {
                        field : 'smsord',
                        title : '订购成功短信',
                        width : 120,
                    }, {
                        field : 'smscan',
                        title : '退订成功短信',
                        width : 120,
                    }, {
                        field : 'stime',
                        title : '开始时间(含当天)',
                        width : 140,
                        sort : true,
                    }, {
                        field : 'etime',
                        title : '结束时间(含当天)',
                        width : 140,
                        sort : true,
                    }
				]],done: function (res, curr, count) {
	               	 layer.closeAll('loading');
	            }
			});
		},

		initSearchForm:function(){
			Lib.initSearchForm( $("#searchForm"), festivalTable, form);
		},
		initToolBar : function(fescod) {
			toolbar = {
                add : function() { //获取选中数据
                    var url = "/directed/festivalProduct/add.do?fescod=" + fescod;
                    Common.openDlg(url,"活动管理 > 添加产品");
                },
                edit : function() { //获取选中数目
                    var data = Common.getOneFromTable(table, "festivalTable");
                    if (data == null) {
                        return ;
                    }

                    var url = "/directed/festivalProduct/edit.do?id=" + data.id;
                    Common.openDlg(url,"活动管理 > 编辑");
                },
                del : function() {
                    var data = Common.getOneFromTable(table, "festivalTable");
                    if (data == null) {
                        return ;
                    }

                    layer.confirm('确认删除选择的产品吗?', function(index) {
                        $.post({
                            url: Common.ctxPath + '/directed/festivalProduct/del.json?id=' + data.id,
                            dataType: 'JSON',
                            success: function (data) {
                                window.dataReload();
                                if (data.code == "0")  {
                                    Common.info("删除成功");
                                } else {
                                    Common.info(data.msg);
                                }
                            },
                            error: function (data) {
                                console.log(data)
                            }
                        });

                        layer.close(index);
                    });


                }
            };
			$('.ext-toolbar').on('click', function() {
				var type = $(this).data('type');
				toolbar[type] ? toolbar[type].call(this) : '';
			});
		}
	};

	 exports('index',view);

});
