layui.extend({
    common: 'js/common',
    region: 'layui/lay/mymodules/region',
    offlinePushType: 'layui/lay/mymodules/offline-push-type'
}).use(['jquery', 'layer', 'form', 'common', 'laydate', 'element', 'table', 'region', 'offlinePushType'], function () {
    var $ = layui.jquery;
    //  引入表单插件
    var form = layui.form;
    var common = layui.common;
    //  弹窗插件
    var layer = layui.layer;
    //  日期选择插件
    var laydate = layui.laydate;
    //Tab的切换功能，切换事件监听等，需要依赖element模块
    var element = layui.element;
    var table = layui.table;

    //  初始化发布类型
    layui.offlinePushType.init({height: 'full-235'});

    //日期时间范围
    laydate.render({
        elem: '#date_range'
        , type: 'datetime'
        , range: true
        // , format: 'yyyy-MM-dd HH:mm:ss'
        // , min: 0 //最小日期为当天

    });
    //监听提交
    form.on('submit(addConfirm)', function (data) {
        common.timer($('button[lay-filter="addConfirm"]'));
        var params = {};

        // if (table_ad_checkbox_id_list.length == 0) {
        if (table_ad_checkbox_map.size == 0) {
            layer.msg("请先关联广告");
            return;
        } else {
            var adIds = new Array();
            table_ad_checkbox_map.forEach(function (item, key) {
                adIds.push(key);
            });
            //关联的广告总数
            params['contentCount'] = table_ad_checkbox_map.size;
            params['adId'] = adIds.toString();
            /*params['contentCount'] = table_ad_checkbox_id_list.length;
            params['adIds'] = table_ad_checkbox_id_list;*/
        }
        // var isHasImg = $('#search_value').attr('isHasImg');
        // var isImgPlayTime = $('#isImgPlayTime').val();
        // if (isImgPlayTime == "1") {
            //广告图片播放时间
        //     var imgPlayTime = $('#imgPlayTime').val();
        //     if (!imgPlayTime) {
        //         layer.msg("时长只能填写数字");
        //         return;
        //     }
        //     params['imgPlayTime'] = imgPlayTime;
        // }

        var date_range = $('#date_range').val();
        if (!date_range) {
            layer.msg("请选择时间范围");
            return;
        }
        var arrayDate = date_range.split(" - ");
        params['beginDate'] = arrayDate[0];
        params['endDate'] = arrayDate[1];

        //选中的数据
        var obj = layui.offlinePushType.getSelectedData();
        //1-按小区  2-按广告位 3-按区域
        var pushType = obj.type;
        params['pushType'] = pushType;
        var pushLocationId = obj.list;
        if (pushLocationId.length <= 0) {
            var targetName = "小区";
            if (pushType == 1) {
                targetName = "小区";
            } else if (pushType == 2) {
                targetName = "广告位";
            } else if (pushType == 3) {
                targetName = "区域";
            }
            layer.msg("请选择要发布的" + targetName);
            return;
        }

        params['pushLocationId'] = pushLocationId.toString();
        common.sendAsyncRequest("/ad/offline/push/add", 'POST', params, callbackFn);

        //  $.ajax({
        //      url     :   '/ad/push/task/add',
        //      data    :  params,
        //      type    :   'POST',
        //      success :   () => {
        //          callbackFn();
        //      }
        //  });
    });

    //回调函数
    function callbackFn(result) {
        if (result.code == '200') {
            layer.msg("添加成功", {time: 1000, shade: [0.1, '#000', true]}, function () {
                // 父页面刷新
                parent.location.reload();
                //获取当前弹窗出层的名字
                var mylay = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(mylay);
            });
        } else {//添加失败
            layer.msg("添加失败");
        }
    }

    //取消按钮
    $('button[type="reset"]').on('click', function () {
        if (parent) {
            //  关闭当前页面弹窗
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        } else {
            //  关闭页面
            window.close();
        }
    });

    //关联广告按钮
    $('#relevance_btn').on('click', function () {
        layer.open({
            //  0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
            type: 2,
            //  宽高
            area: ['100%', '100%'],
            //  禁用最大最小化
            maxmin: false,
            //  禁用拖拽
            move: false,
            //  标题
            title: '关联广告',
            //  打开内容
            content: '/ad/push/task/relevance/view?onlineOrOffline=0'
        });
    });

});


