var excel;
layui.config({
    base: '/layuiadmin/' //静态资源所在路径
}).extend({
    myexcel: 'layui/lay/modules/excel',
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'excel', 'laydate'], function () {
    var $ = layui.$
        , form = layui.form
        , table = layui.table;

    //- 筛选条件-日期
    var laydate = layui.laydate;
    var layCreateDate = laydate.render({
        elem: '#CreateDate',
        type: 'date',
        range: true,
        trigger: 'click',
        change: function (value, date, endDate) {
            var op = true;
            if (date.year == endDate.year && endDate.month - date.month <= 1) {
                if (endDate.month - date.month == 1 && endDate.date > date.date) {
                    op = false;
                    layCreateDate.hint('日期范围请不要超过1个月');
                    setTimeout(function () {
                        $(".laydate-btns-confirm").addClass("laydate-disabled");
                    }, 1);
                }
            } else {
                op = false;
                layCreateDate.hint('日期范围请不要超过1个月');
                setTimeout(function () {
                    $(".laydate-btns-confirm").addClass("laydate-disabled");
                }, 1);
            }
            if (op) {
                $('#CreateDate').val(value);
            }
        }
    });
    var layTradeDate = laydate.render({
        elem: '#DateStr',
        trigger: 'click',
        type: 'date',
    });


    //excel导入
    excel = layui.excel;
    $('#ExcelFile').change(function (e) {
        var files = e.target.files;
        excel.importExcel(files, {}, function (data) {
            ExcelData = data[0].sheet1;
        });
    });

    //监听单元格编辑
    table.on('edit(LAY-list-manage)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        if (field == "Sort") {
            $.ajax({
                type: "POST",
                url: "/Admin/TradeRecord/Sort?r=" + Math.random(1),
                data: "Id=" + data.Id + "&Sort=" + value,
                dataType: "text",
                success: function (data) {
                }
            });
        }
    });

    //激活预览列表数据
    table.render({
        elem: '#LAY-list-manage'
        , url: '/Admin/TradeRecord/IndexDataSIMDo' //模拟接口
        , cols: [[
            { type: 'checkbox', fixed: 'left' }
            , { field: 'iccId', width: 200, title: '广电卡号', sort: true } //广电卡号
            , { field: 'orderNo', width: 200, title: '订单编码', sort: true } //订单编码
            , { field: 'orderCreateTime', width: 200, title: '订单创建时间', sort: true } //订单创建时间
            , { field: 'phoneNo', width: 200, title: '选择号码', sort: true } //选择号码
            , { field: 'firstCharge', width: 200, title: '是否有首充', sort: true } //是否有首充
            , { field: 'initialChargeAmount', width: 200, title: '首充金额（元）', sort: true } //首充金额（元）
            , { field: 'province', width: 200, title: '号码归属省份', sort: true } //号码归属省份
            , { field: 'city', width: 200, title: '号码归属地市', sort: true } //号码归属地市
            , { field: 'productName', width: 200, title: '商品名称', sort: true } //商品名称
            , { field: 'promotionName', width: 200, title: '促销活动名称', sort: true } //促销活动名称
            , { field: 'productTypeName', width: 200, title: '商品类型名称', sort: true } //商品类型名称
            , { field: 'paidInAmount', width: 200, title: '实付金额', sort: true } //实付金额(元)
            , { field: 'orderStatus', width: 200, title: '订单状态', sort: true } //订单状态
            , { field: 'activationStatusName', width: 200, title: '激活状态名称', sort: true } //激活状态名称
            , { field: 'activationTime', width: 200, title: '激活时间', sort: true } //激活时间
            , { field: 'jobId', width: 200, title: 'BOSS员工工号', sort: true } //BOSS员工工号
        ]]
        , page: true
        , limit: 30
        , height: 'full-' + String($('.layui-card-header').height() + 130)
        , text: '对不起，加载出现异常！'
        , done: function (res, curr, count) {
            $(".layui-none").text("无数据");
        }
    });


    //监听搜索
    form.on('submit(LAY-list-front-search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('LAY-list-manage', {
            where: field,
            page: {
                curr: 1
            }
        });
    });

    form.on('submit(LAY-list-front-GetSimTradePost)', function (data) {
        var field = data.field;
        console.log(field);
        var index = layer.load(1, {
            shade: [0.5, '#000']
        });
        $.ajax({
            type: "POST",
            url: "/Admin/TradeRecord/GetSimTradePost?r=" + Math.random(1),
            data: "DateStr=" + field.DateStr,
            dataType: "text",
            success: function (msg) {
                layer.close(index); //关闭弹层
                if (msg == "success") {
                    layer.msg("提交成功");
                } else {
                    layer.msg(msg);
                }
            }
        });
    });

    form.on('submit(LAY-list-front-searchall)', function (data) {
        table.reload('LAY-list-manage', {
            where: null,
            page: {
                curr: 1
            }
        });
    });

    //事件
    var active = {
        GetActInfo: function () {
            var index = layer.confirm('确定要获取吗？', function (index) {
                //监听提交
                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                    var field = data.field; //获取提交的字段
                    var userdata = "";
                    for (var prop in field) {
                        userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
                    }
                    //提交 Ajax 成功后，静态更新表格中的数据
                    //$.ajax({});

                    $.ajax({
                        type: "POST",
                        url: "/Admin/TradeRecord/GetSimTradePost?r=" + Math.random(1),
                        data: userdata,
                        dataType: "text",
                        success: function (data) {
                            layer.close(index); //关闭弹层
                            if (data == "success") {
                                layer.msg("提交成功");
                            } else {
                                layer.msg(data);
                            }
                        }
                    });
                });
            });
        }
    };

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