layui.define(['form', 'laydate', 'table', 'channelUrlApi' ], function(exports) {
	var form = layui.form;
	var laydate = layui.laydate;
	var table = layui.table;
	var channelUrlApi=layui.channelUrlApi;
	var channelUrlTable = null;
	var view = {
		init: function(){
			this.initTable();
			this.initSearchForm();
			this.initToolBar();
			window.dataReload = function(){
				Lib.doSearchForm($("#searchForm"), channelUrlTable)
			}
		},
		initTable:function(){
			channelUrlTable = table.render({
				elem : '#channelUrlTable',
				height : Lib.getTableHeight(2),
				method : 'post',
				url : Common.ctxPath + '/directed/channelUrl/list.json' //数据接口
				,page : true //开启分页
				,limit : 10,
				cols : [ [ //表头
                    {
                        type : 'checkbox'
                    },{
                        field : 'fescod',
                        title : '活动编码',
                        width : 80,
                    }, {
                        field : 'fesname',
                        title : '活动名称',
                        width : 260,
                    }, {
                        field : 'orderchannel',
                        title : '渠道编码',
                        width : 80,
                    },  {
                        field : 'channelname',
                        title : '渠道名称',
                        width : 200,
                    }, {
                        field : 'url',
                        title : '活动链接',
                        width : 500,
                    }, {
                        field : 'shorturl',
                        title : '短网址',
                        width : 200,
                    /*}, {
                        field : 'starttime',
                        title : '开始时间(含当天)',
                        width : 150,
                        sort : true,
                    }, {
                        field : 'endedtime',
                        title : '结束时间(含当天)',
                        width : 150,
                        sort : true,*/
                    }, {
                        field : 'inserttime',
                        title : '插入时间',
                        width : 150,
                        sort : true,
                    }, {
                        field : 'updatetime',
                        title : '更新时间',
                        width : 150,
                        sort : true,
                    }

				]],done: function (res, curr, count) {
	               	 layer.closeAll('loading');
	            }

			});
		},
		
		initSearchForm:function(){
			Lib.initSearchForm( $("#searchForm"), channelUrlTable, form);
		},
		initToolBar:function(){
			toolbar = {
                add : function() { //获取选中数据
                    var url = "/directed/channelUrl/add.do";
                    Common.openDlg(url,"渠道链接管理 > 新增活动");
                },
                edit : function() { //获取选中数目
                    var data = Common.getOneFromTable(table,"channelUrlTable");
                    if(data==null){
                        return ;
                    }

                    var url = "/directed/channelUrl/edit.do?id="+data.id;
                    Common.openDlg(url,"渠道链接管理 > 编辑");
                },
                del : function() {
                    var data = Common.getOneFromTable(table,"channelUrlTable");
                    if (data == null) {
                        return ;
                    }

                    layer.confirm('确认删除选择的记录吗?', function(index) {
                        $.post({
                            url: Common.ctxPath + '/directed/channelUrl/del.json?fescod=' + data.fescod +
                                    '&id=' + data.id + '&channelid=' + data.channelid + '&orderchannel=' + data.orderchannel,
                            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);
                    });
                },
                shortUrl : function() { //获取选中数目
                    var data = Common.getOneFromTable(table,"channelUrlTable");
                    if (data == null) {
                        return ;
                    }

                    $.post({
                        url: Common.ctxPath + '/directed/channelUrl/shortUrl.json?id=' + data.id + '&fescod=' + data.fescod +
                                '&channelid=' + data.channelid + '&orderchannel=' + data.orderchannel,
                        dataType: 'JSON',
                        success: function (data) {
                            window.dataReload();
                            if (data.code == "0")  {
                                Common.info("短链生成成功");
                            } else {
                                Common.info(data.msg);
                            }
                        },
                        error: function (data) {
                            console.log(data)
                        }
                    });
                },
                qrcode : function() { //获取选中数目
                    var data = Common.getOneFromTable(table,"channelUrlTable");
                    if(data == null){
                        return ;
                    }

                    var url = "/directed/channelUrl/myqrcode.do?id=" + data.id;
                    Common.openDlg(url,"渠道链接管理 > 短链接");
                },
            };
			$('.ext-toolbar').on('click', function() {
				var type = $(this).data('type');
				toolbar[type] ? toolbar[type].call(this) : '';
			});
		}
	};

	 exports('index',view);
	
});
