layui.extend({
    common  : 'js/common',
    pushType: 'layui/lay/mymodules/push-type',
}).use(['jquery','pushType','common','table','form'],function(){
    var $ = layui.jquery;
    //选中的发布策略集合
    var pbMap = new Map();
    //  初始化发布类型
    layui.pushType.init({ height  :   'full-235'});

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

    //  提交表单
    layui.form.on('submit(entity_submit_event)',function(data){
        layui.common.timer($('button[lay-filter="entity_submit_event"]'));
        var selectedData = layui.pushType.getSelectedData();
        if (selectedData.list.length === 0){
            var targetName="小区";
            if(selectedData.type==1){
                targetName="小区";
            }else if(selectedData.type==2){
                targetName="单元";
            }else if(selectedData.type==3){
                targetName="设备";
            }else if(selectedData.type==4){
                targetName="区域";
            }
            layer.alert("请选择发布的"+targetName);
            return;
        }
        //  表单数据
        var body = data.field;
        if (pbMap.size === 0){
            layer.alert("请先关联策略");
            return;
        }
        if (pbMap.size > 1){
            layer.alert("请关联一条策略");
            return;
        }
        body.pushType = selectedData.type;
        body.fkIdList = selectedData.list;

        pbMap.forEach(function (data) {
            body.timeStrategyId=data.id;
        });

        $.ajax({
            url     :   '/device/time/strategy/push/task/add',
            data    :   JSON.stringify(body),
            type    :   'POST',
            contentType     :   'application/json;charset=utf-8',
            success :   function(){
                $('button[type="reset"]').click();
                parent.location.reload();
            }
        });
    });

    var layuiTable;
    var relation_ts_window;
    $('#relation_ts_btn').on('click',function () {
        relation_ts_window=layer.open({
            title   :   '关联策略',
            type    :   1,
            maxmin  :   false,
            area    :   ['1000px', '500px'],
            content :   $('#ts_content_table').html()
        });
        layuiTable = layui.common.layuiTable.render(layui.table,{
            elem: '#ts_entity_table'
            ,url: '/device/time/strategy/pagingQuery'
            ,toolbar: '#table-head-id'
            , cols: [[
                { type:'radio' }
                , { field: 'name', title: '策略名称',align:'center',width:200 }
                , {title: '使用时间',align:'center',width:300,templet:function(d){
                        var type=d.type;
                        if(type==2){
                            //获取日期范围
                            var beginDate=layui.util.toDateString(d.beginDate, 'yyyy-MM-dd');
                            var endDate=layui.util.toDateString(d.endDate, 'yyyy-MM-dd');
                            return beginDate+" ~ "+endDate;
                        }else{
                            //获取星期
                            var weekDays="";
                            var workingDay=d.workingDay;
                            if(workingDay){
                                var dayArray=workingDay.split(",");
                                for( var i in dayArray){
                                    var week=dayArray[i];
                                    if(i==0){
                                        weekDays+=getWeekDay(week);
                                    }else{
                                        weekDays+=","+getWeekDay(week);
                                    }
                                }
                            }
                            return weekDays;
                        }
                    }}
                , { field: 'remark',title: '备注',align:'center'}
            ]]
            ,done : function(result){
                var list = result.data;
                for (var i = 0; i < list.length; i++){
                    var entity = list[i];
                    var data = pbMap.get(entity.id);
                    if (data){
                        $('div[lay-id="ts_entity_table"] .layui-table-box table tr[data-index="' + i + '"]').find('td:first .layui-form-radio').click();
                    }
                }
            }
            , height: '350'
        });

    });

    //  查询策略
    $(document).on('click','#select_btn',function () {
        layuiTable.reload({
            //  设定异步数据接口的额外参数
            where: {
                name    :   $('#ts_name').val()
            }
            //  重新从第1页开始
            ,page: {
                curr: 1
            }
        })
    });

    //单选框事件
    layui.table.on('radio(ts_entity_event)',function (obj) {
        //清空
        pbMap.clear();
        var id = obj.data.id;
        pbMap.set(id,obj.data);
        $('.select_count').html(pbMap.size);
    });

    //头工具栏事件 lay-filter="ts_entity_event"
    layui.table.on('toolbar(ts_entity_event)', function(obj){
        var data = obj.data;
        //确认选择
        if(obj.event === 'confirm'){
            //获取当前弹窗出层的名字
            //关闭弹窗层
            layer.close(relation_ts_window);
        } else if(obj.event === 'clear'){ //清空选择
            $('#ts_entity_table').next().find('input:radio[lay-type=layTableRadio]').prop('checked', false);
            layui.form.render('radio');
            //清空
            pbMap.clear();
            $('.select_count').html(pbMap.size);
        }
    });

});
/**
 * 获取星期几
 * @param weekDay
 * @returns {string}
 */
var getWeekDay=function (weekDay) {
    switch (weekDay) {
        case "1":
            return "星期一"
            break;
        case "2":
            return "星期二"
            break;
        case "3":
            return "星期三"
            break;
        case "4":
            return "星期四"
            break;
        case "5":
            return "星期五"
            break;
        case "6":
            return "星期六"
            break;
        case "7":
            return "星期七"
            break;
        default:
            return "";
            break;
    }
}