/**
 * 短信配置
 */

layui.use(['form','jquery','layer'],function () {

    var layer = layui.layer,
        $ = layui.jquery,
        form = layui.form

    var msgCofIndex = null;
    var username = $("#username").val();

    var color = '#006565'

    //模板
    var tbody = ' <input msgType="{5}"  type="checkbox"   name="msgBind" value="{0}" title="{1}" {2}>\n' +
                ' <a href="javascript:void(0)" value="{3}" class="lili layui-icon" style="margin-left: -23px;\n' +
                '    margin-top: -3px; position: absolute;{6}" >&#x1007;</a>\n'


    initPage()
    initBaseCof();



    //删除模板
    $('body').on('click','.lili',function () {
       var msgIndex = $(this).attr("value")
        confirmIndentity(username,function () {
            delTimeItem(msgIndex)
        })
    })



    //新增时间范围模板
    $("body").on('click','#addTime',function() {
        confirmIndentity(username,function () {
            var openIndex= layer.open({
                type: 2,
                title: '添加时间范围模板',
                shadeClose: true,
                resize: false,
                shade: 0.3,
                area: ['300px', '330px'],
                content: 'msgtime', //iframe的url
                scrollbar: false,
                end: function () {
                    layer.close();
                }
            });
        })
    })

    //新增类型范围模板
    $("body").on('click','#addType',function() {
        confirmIndentity(username,function () {
            var openIndex2 = layer.open({
                type: 2,
                title: '添加类型模板',
                shadeClose: true,
                resize: false,
                shade: 0.3,
                area: ['300px', '330px'],
                content: 'msgtype', //iframe的url
                scrollbar: false,
                end: function () {
                    layer.close();
                }
            });
        })
    })

    //修改基本配置
    form.on('submit(updateBase)', function(data){
        let arr = []
        var file = data.field

        $("input:checkbox[name='msgBind']:checked").each(function() {
            arr.push($(this).val())
        })

        let map = {
            msgCofIndex:msgCofIndex,
            isSend:file.isSend,
            timeInterval:file.timeInterval,
            offDay:file.offDay,
            list:arr
        }

            $.ajax({
                type: 'POST',
                dataType: "json",
                contentType: 'application/json;charset=UTF-8',
                data:JSON.stringify(map),
                url: '/pqs9900/message/updateMsgCofig',
                success: function (data) {
                    if(data.code == 200){
                        layer.msg(data.message,{icon:1})
                    }else {
                        layer.msg(data.message,{icon:2})
                    }
                    initBaseCof()
                    initPage()
                }
            });


    });


    //初始化页面
    function initPage() {
        $.ajax({
            type: 'GET',
            dataType: "json",
            cache: false,
            beforeSend: function () {
                info = ityzl_SHOW_LOAD_LAYER_PARENT();
            },
            url: '/pqs9900/message/getMsgDicdataList',
            success: function (data) {
                if(data.code == 200){
                    //成功
                    $('#content_time').empty() //清空内容
                    $('#content_type').empty() //清空内容
                    var html = '' //时间
                    var type_html = '' //类型
                    if(data.body!=null && data.body.length!=0){
                        for(var i =0;i<data.body.length;i++){
                            var obj = data.body[i]
                            if(obj.msgType == 1){
                                //时间区域
                                html+=tbody.replace("{0}",obj.msgIndex)
                                    .replace("{1}",obj.value1+':00~'+obj.value2+':00')
                                    .replace("{2}",obj.state == 0 ? '':'checked')
                                    .replace("{3}",obj.msgIndex)
                                    .replace("{5}",obj.msgType)
                                    .replace("{6}",obj.state == 0 ? '':'display:none')
                            }else {
                                //类型区域
                                type_html+=tbody.replace("{0}",obj.msgIndex)
                                    .replace("{1}",obj.msgName)
                                    .replace("{2}",obj.state == 0 ? '':'checked')
                                    .replace("{3}",obj.msgIndex)
                                    .replace("{5}",obj.msgType)
                                    .replace("{6}",obj.state == 0 ? '':'display:none')
                            }
                        }

                    }
                    var but ='<button style="height:30px;line-height:30px;margin-top: 4px" type="button" id="addTime" class="layui-btn layui-btn-primary layui-btn-sm"  lay-submit lay-filter="addType">\n' +
                        '                <i class="layui-icon">&#xe654;</i>\n' +
                        '            </button>'
                    var butType ='<button style="height:30px;line-height:30px; margin-top: 4px" type="button" id="addType" class="layui-btn layui-btn-primary layui-btn-sm"  lay-submit lay-filter="addType">\n' +
                        '                <i class="layui-icon">&#xe654;</i>\n' +
                        '            </button>'
                    html+=but
                    type_html+=butType
                    $('#content_time').append(html)
                    $('#content_type').append(type_html)
                    form.render()

                }
                ityzl_CLOSE_LOAD_LAYER_PARENT(info);
            }
        });
    }



    //初始化基础设置
    function initBaseCof() {
        $.ajax({
            type: 'GET',
            dataType: "json",
            contentType:'application/json;charset=UTF-8',
            url: '/pqs9900/message/getMsgConfig',
            success: function (data) {
                if(data.code == 200){
                    $('#timeInterval').val(data.body.timeInterval)
                    $('#offDay').val(data.body.offDay)
                    $('#isSend').val(data.body.isSend)
                    msgCofIndex = data.body.msgCofIndex
                    form.render(); //刷新select选择框渲染
                }else {
                    layer.msg(data.message,{icon:7})
                    return false
                }
            },
            error:function () {
                layer.msg('未知错误',{icon:2})
                return false
            }
        });
    }


    //删除模板
    function delTimeItem(msgIndex) {
            $.ajax({
                type: 'GET',
                dataType: "json",
                contentType:'application/json;charset=UTF-8',
                url: '/pqs9900/message/delMsgDicBytype?msgIndex='+msgIndex,
                success: function (data) {
                    if(data.code == 200){
                        //成功
                        layer.msg('删除成功!',{icon:1})
                    }else {
                        //失败
                        layer.msg(data.message,{icon:2})
                    }
                    initPage()
                    initBaseCof()
                },
                error:function () {
                    layer.msg('未知错误',{icon:2})
                    return false
                }
            });
    }


    //子页面调用父页面
    window.flushPage = function () {
        initPage()
        initBaseCof()
    }

    window.msgTo = function () {
        layer.msg('添加成功',{icon:1})
    }


})



