layui.use(['layer', 'form', 'table', 'admin', 'ax','upload','laydate','excel'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var excel =layui.excel;

    /**
     * 账号管理管理
     */
    var wxTable = {
     tableId: "wxTable"    //表格id
    };

    //渲染时间选择框
    laydate.render({
        elem: '#beginTime',
        isInitValue: true,
        value: new Date()
    });

    //渲染时间选择框
    laydate.render({
        elem: '#endTime',
        isInitValue: true,
        value: new Date()
    });

    /**
     * 初始化表格的列
     */
    wxTable.initColumn = function () {
        return [[
                {field: 'id', hide: true, sort: true, title: 'id'},
                {title: '注册号码', field: 'phoneNum', visible: true, align: 'center', valign: 'middle'},
                {title: 'wxId', field: 'wxId', visible: true, align: 'center', valign: 'middle'},
                {title: '密码', field: 'password', visible: true, align: 'center', valign: 'middle'},
                {title: '62数据', field: 'sixtwoData', visible: true, align: 'center', valign: 'middle'},
                {title: '创建时间', field: 'updateTime', visible: true, align: 'center', valign: 'middle'},
                {title: '地区', field: 'region', visible: true, align: 'center', valign: 'middle'},
                {title: '操作用户', field: 'QRUSERNAME', visible: true, align: 'center', valign: 'middle'},
                {title: '状态', field: 'tasksatus', visible: true, align: 'center', valign: 'middle'}

        ]];
    };
    /**
     * 点击查询按钮
     */
    wxTable.search = function () {
        var queryData = {};
        queryData['phoneNumber'] = $("#phoneNumber").val();
        queryData['beginTime'] = $("#beginTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['qrUserName'] = $("#qrUserName").val();
        queryData['taskType'] = $("#taskType").val();
        table.reload(wxTable.tableId, {where: queryData});
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + wxTable.tableId,
        url: Feng.ctxPath + '/WxManual/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        limit: 50,
        limits: [50,100,300,500],
        cols: wxTable.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        wxTable.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        wxTable.openAddNotice();
    });

    $('#btnOut').click(function () {
        exportApiDemo("");
    });
    $('#btnDel').click(function () {
        //
    });


    // 工具条点击事件
    table.on('tool(' + wxTable.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            wxTable.onEditWxAccount(data);
        } else if (layEvent === 'delete') {
            wxTable.onDeleteWxAccount(data);
        }
    });

    function exportApiDemo(url) {
        var queryData = {};
        queryData['phoneNumber'] = $("#phoneNumber").val();
        queryData['beginTime'] = $("#beginTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['qrUserName'] = $("#qrUserName").val();
        queryData['taskType'] = $("#taskType").val();

            // 模拟从后端接口读取需要导出的数据
            $.ajax({
                url: Feng.ctxPath + '/WxManual/listExc'
                , dataType: 'json'
                , data:queryData
                , success: function (res) {
                    var data = res.data
                    /*// 重点！！！如果后端给的数据顺序和映射关系不对，请执行梳理函数后导出
                    data = excel.filterExportData(data, {
                        id: 'id'
                        , username: 'username'
                        , experience: 'experience'
                        , sex: 'sex'
                        , score: function (value, line, data) {
                            // 可以直接指定类型为数字
                            return {
                                v: value,
                                t: 'n'
                            }
                        }
                        , city: function (value, line, data) {
                            return {
                                v: value,
                                s: {
                                    font: {sz: 14, bold: true, color: {rgb: "FFFFAA00"}},
                                    fill: {bgColor: {indexed: 64}, fgColor: {rgb: "FFFF00"}}
                                }
                            }
                        }
                        , classify: 'classify'
                        , wealth: 'wealth'
                        , sign: 'sign'
                    })*/
                    // 重点2！！！一般都需要加一个表头，表头的键名顺序需要与最终导出的数据一致
                    data.unshift({
                        phoneNum: "注册号码",
                        wxId: 'wxId',
                        password: '密码',
                        sixtwoData: '62数据',
                        createTime: '创建时间',
                        region: '地区',
                        QRUSERNAME: '操作用户',
                        status:'状态'

                    })
                    excel.exportExcel({
                        sheet1: data
                    }, '数据.xlsx', 'xlsx')
                   /* var timeend = Date.now()

                    var spent = (timeend - timestart) / 1000
                    layer.alert('单纯导出耗时 ' + spent + ' s')*/
                }
                , error: function () {
                    layer.alert('获取数据失败！')
                }
            })

    }


});



