layui.use([ 'laypage', 'layer', 'table',  'element','form'], function(){

    var laypage = layui.laypage //分页
        ,layer = layui.layer //弹层
        ,table = layui.table //表格
        ,element = layui.element //元素操作
        ,form = layui.form;
    //第一个实例
    table.render({
        elem: '#emailList'
        ,url: '/othermail/collecthomeform' //数据接口
        ,method:'post'
        ,page: true //开启分页
        ,text:'数据加载中'
        ,limit:20
        ,height : 600
        ,id:'actionemail'
        ,cols: [[ //表头
            {type:'numbers'}
            ,{field: 'email', title: '邮箱', align:'center'}
            ,{field: 'city_name', title: '区域', align:'center'}
            ,{field: 'name', title: '姓名', align:'center'}
            ,{fixed: 'right',title:'操作', width: 200, align:'center', toolbar: '#operate'}
        ]]
    });


    //列表操作
    table.on('tool(emailList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){
            layer.open({
                type: 2,
                title: '修改邮箱',
                area: ['600px', '500px'],
                content:'/othermail/collectedit?id='+data.id, //iframe的url,no代表不显示滚动条
                end:function () {
                    table.reload('actionemail');
                }
            });
        } else if(layEvent === 'del') {

            layer.confirm('您确定要删除吗?',{   btn: ['确定', '取消']},function () {
                $.post('/othermail/collectdel',{'id':data.id},function (data) {
                    layer.msg(data.msg);
                    table.reload('actionemail');
                })
            })
        }

    });


    var $ = layui.$, active = {
        addData: function(){ //获取选中数据
            layer.open({
                type: 2,
                title: '添加邮箱',
                area: ['600px', '500px'],
                content: '/othermail/collectadd', //iframe的url,no代表不显示滚动条
                end:function () {
                    table.reload('actionemail');
                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

$('.imputSelect').find('input').removeAttr("readonly");
    //发件人设置
    form.on('submit(tijiao)', function(data){
        $.post('/othermail/homemailform',{'data':data.field},function (data) {
                    layer.msg(data.msg);
        })
        return false;
    });

    //接受邮件测试
    form.on('submit(add_send_email)', function(data){
        $.post('/othermail/testemail',{'data':data.field},function (data) {
            layer.msg(data.msg);
        })
        return false;
    });


    //收件人添加
    form.on('submit(email_tijiao)', function(data){
        $.post('/othermail/collectaddform',{'data':data.field},function (data) {
            if(data.data != null){
                layer.msg(ReturnInfo(data.data),{icon:5});
                return ;
            }
                layer.msg(data.msg);
        })
                return false;
    });



    //收件人修改
    form.on('submit(email_edit)', function(data){
        $.post('/othermail/collecteditform',{'data':data.field},function (data) {
            if(data.data != null){
                layer.msg(ReturnInfo(data.data),{icon:5});
                return ;
            }
            layer.msg(data.msg);
        })
        return false;
    });

});