/**
 * 渠道列表
 */
$(function(){

    AppConfigExtend = {
        Events : {'click .btn': function (e, value, row, index) {
            var event = $(e.target).data('event');
            if(event == 'edit'){
                AppConfigExtend.edit(row['ffrom']);
            } else if(event == 'cfgWxcorp'){
                AppConfigExtend.cfgWxcorp(row['ffrom']);
            } else if (event == 'joinWxcorp'){
                AppConfigExtend.joinWxcorp(row['ffrom']);
            } else if (event == 'unJoinWxcorp'){
                AppConfigExtend.unJoinWxcorp(row['ffrom']);
            } else if (event == 'wxaUrlLink'){
                AppConfigExtend.wxaUrlLink(row['ffrom'], 3);
            }
        }},
        add: function(){
            window.location.href = Config.ROOT + '/marketing/toEditInfo';
        },
        edit: function(ffrom){
            var channelConfigId = $('#search_channelConfigId').val() || '';
            window.location.href = Config.ROOT + '/marketing/toEditInfo?ffrom=' + ffrom + '&channelConfigId=' + channelConfigId;
        },
        cfgWxcorp: function(ffrom){
            var channelConfigId = $('#search_channelConfigId').val() || '';
            window.location.href = Config.ROOT + '/marketing/toEdit?ffrom=' + ffrom + '&channelConfigId=' + channelConfigId;
        },
        joinWxcorp: function(ffrom){
            var channelConfigId = $('#search_channelConfigId').val() || '';
            window.location.href = Config.ROOT + '/marketing/joinWxcorp/golist?ffrom=' + ffrom + '&channelConfigId=' + channelConfigId;
        },
        unJoinWxcorp: function(ffrom){
            var channelConfigId = $('#search_channelConfigId').val() || '';
            window.location.href = Config.ROOT + '/marketing/unJoinWxcorp/golist?ffrom=' + ffrom + '&channelConfigId=' + channelConfigId;
        },
        wxaUrlLink: function(ffrom){
            var inputOptions = {
                '0': '永久有效',
                '3': '有效期三个月',
                '6': '有效期六个月'
            };
            MsgBox.promptEx({
                title: '选择有效期',
                input: 'radio',
                width: 400,
                inputOptions: inputOptions,
                callback: function (val) {
                    App.Ajax.request({
                        url		: Config.ROOT + '/marketing/wxa/generateUrlink?ffrom=' + ffrom + '&at=' + val,
                        headers : {
                            'ffrom' : ffrom
                        },
                        contentType: "application/json",
                        isAutoTip: false,
                        success: function(result){
                            MsgBox.dialog({
                                title: '小程序短链接',
                                html: '<p style="font-size: 18px;margin-top: 10px;">' + result.data['shortUrl'] + '</p><p>' + inputOptions[val] + '</p>'
                            });
                        }
                    });
                }
            });
        },
        search: function () {
            var channelConfigId = $('#search_channelConfigId').val() || '';
            var launchStatus = $('#search_launchStatus').val() || '';
            var postData = {
                'channelConfigId'	: channelConfigId,
                'launchStatus'	: launchStatus
            };
            $table.search(postData);
        }
    };
	
	//初始化表格
    var $table = new App.Table({
        url: Config.ROOT + '/marketing/list',
        search: false,
        // showRefresh: false,
        // showToggle: false,
        // showColumns: false,
        columns	:[
            {field:'channelConfigId', title:'渠道类型', tpl: 'tpl_channelConfigId'},
            {field:'channelSubName', title:'渠道名称'},
            {field:'ffrom', title:'ffrom'},
            {field:'payAmount', title:'付款金额'},
            {field:'link', title:'链接'},
            {field:'wxCorpUseridList', title:'企微成员的userid列表', formatter: function (value, row, index) {
                var html = '';
                if (value) {
                    var vals = value.split(',');
                    for (var i = 0; i < vals.length; i++) {
                        html += '<p>'+vals[i]+'</p>';
                    }
                }
                return html;
            }},
            {field:'qrcodeUrl', title:'企微成员的二维码url列表', formatter: function (value, row, index) {
                var html = '';
                if (value) {
                    var vals = value.split(',');
                    for (var i = 0; i < vals.length; i++) {
                        html += '<p>'+vals[i]+'</p>';
                    }
                }
                return html;
            }},
            {field:'landingConfigExtendId', title:'操作', width: 120, toolbar: 'tableRowToolbar', events: AppConfigExtend.Events}
        ],
        getQueryParam: function () {
            var channelConfigId = $('#search_channelConfigId').val() || '';
            var launchStatus = $('#search_launchStatus').val() || '';
            var postData = {
                'channelConfigId'	: channelConfigId,
                'launchStatus'	: launchStatus
            };
            return postData;
        }
    });

});