layui.extend({
    common    :   'js/common'
}).use(['jquery','form','common','laydate','layer'],function(){
    var $ = layui.jquery;
    //  页面元素
    var $node = {
        //  取消按钮
        resetBtn        :   $('button[type="reset"]'),
        strategyDate        :   $("input[name='strategyDate']"),
        workingDay        :   $("input[name='workingDay']"),
        laydateTimeItem        :    $(".layui-form .laydate-time-item"),
        strategyDate        :    $("input[name='strategyDate']"),
        timeRangeDiv        :   $("#time-range"),
        datetimeRangeDiv        :    $("#datetime-range-id"),
        weekdayDiv        :    $("#weekday-id"),
        typeRadio        :    $("input[name='type']")
    };
    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(data){
        layui.common.timer($('button[lay-filter="entity_submit_event"]'));
        //类型
        var type=data.field.type;
        if(type==2){
            //日期范围
            var strategyDate=$node.strategyDate.val();
            var strategyDates=strategyDate.split(" - ");
            data.field.beginDate=strategyDates[0];
            data.field.endDate=strategyDates[1];
        }else if(type==1){
            //选中的工作日
            var workingDay="";
            $("input[name='workingDay']:checked").each(function (i) {
                var value=$(this).val();
                if(i==0){
                    workingDay=value;
                }else{
                    workingDay+=","+value;
                }
            });
            data.field.workingDay=workingDay;
        }

        var strategyTimeParams="";
        //获取时间段信息
        $(".layui-form .laydate-time-item").each(function (i) {
            //时间段
            var strategyTime=$(this).find("input[name='strategyTime']").val();
            //声音
            var soundVolume=$(this).find("input[name='soundVolume']").val();
            if(!soundVolume){
                soundVolume="0";
            }
            //屏幕亮度
            var screenLuminance=$(this).find("input[name='screenLuminance']").val();
            if(!screenLuminance){
                screenLuminance="0";
            }
            //补光灯亮度
            var fillLightLuminance=$(this).find("input[name='fillLightLuminance']").val();
            if(!fillLightLuminance){
                fillLightLuminance="0";
            }
            if(i==0){
                strategyTimeParams=strategyTime+";"+soundVolume+";"+screenLuminance+";"+fillLightLuminance;
            }else{
                strategyTimeParams+=","+strategyTime+";"+soundVolume+";"+screenLuminance+";"+fillLightLuminance;
            }
        });
        data.field.strategyTimeParams=strategyTimeParams;
        $.ajax({
            url     :   '/device/time/strategy/addEntity',
            data    :   data.field,
            type    :   'POST',
            success :   function () {
                $node.resetBtn.click();
                parent.location.reload();
            }
        });
    });
    //日期范围选择
    layui.laydate.render({
        elem: 'input[name="strategyDate"]'
        ,type: 'date'
        ,range:true
    });
    //时间范围选择
    layui.laydate.render({
        elem: '#laydate-type-time-head'
        ,type: 'time'
        ,range:true
    });
    //  点击取消事件
    $node.resetBtn.on('click',function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });
    //增加时间段
    $(document).on('click','button[type="add-time-range"]',function () {
        var  item=$node.timeRangeDiv.find(".layui-form-item");
        if(item.length>9){
            layer.msg("最多10条时间段");
            return;
        }
        $node.timeRangeDiv.append($("#time-range-template").html());
        //重新渲染一次时间控件
        $node.timeRangeDiv.find("input[name='strategyTime']").each(function () {
            layui.laydate.render({
                elem:this
                ,type: 'time'
                ,range:true
            });
        });
    });
    //删除时间段
    $(document).on('click','button[type="delete-time-range"]',function (obj) {
        $(this).parents(".layui-form-item").remove();
    });
    layui.form.on('radio(type-filter)', function(data){
        var value=data.value;
        if(value==2){
            $node.datetimeRangeDiv.show();
            $node.weekdayDiv.hide();
            $node.workingDay.removeAttr("lay-verify");
            $node.strategyDate.attr("lay-verify","required");
        }else{
            $node.datetimeRangeDiv.hide();
            $node.weekdayDiv.show();
            $node.workingDay.attr("lay-verify","checkboxMustBe");
            $node.strategyDate.removeAttr("lay-verify");
        }
    });
    //自定义验证规则
    layui.form.verify({
        checkboxMustBe:function(value) {
            if (!$node.workingDay.is(":checked")) {
                return "选择星期必选";
            }
        },
        typeRequired:function(value) {
            if (!$node.typeRadio.is(":checked")) {
                return "策略类型必选";
            }
        }
    });
});