layui.use(['table', 'jquery'], function () {
    var table = layui.table;
    var form = layui.form,
      layer = parent.layer === undefined ? layui.layer : parent.layer,
      $ = layui.jquery;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        console.log(obj)
    });


    //监听工具条
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'view') {
            top.document.getElementById("mainframe").src = '/Customer/CusOrderList/?callNumber=' + data.F_Telephone;
        }
        if (obj.event === 'edit') {
            helper.cookies.set('F_CustomerId', data.F_CustomerId);
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/Customer/Add',
                area: ['600px', '600px'],
                id: 'NoticeEdit',
                title: '新增客户'
                , end: function () {

                    refushTreeAndData();
                }
            });
        }
       
    });

    var $ = layui.$, active = {
        //查询按钮
        
        reload: function () {
            
            loadData();
        }
        //新增
        , rowAdd: function () {
            debugger
            helper.cookies.set('F_CustomerId', "");
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/Customer/Add',
                area: ['600px', '600px'],
                id: 'NoticeEdit',
                title: '新增客户'
                , end: function () {
                    refushTreeAndData();
                    
                }
            });
            
        }
        //标记特别关注
        , startuser: function () {
            
            changeDataStatus(2);
        }
        //加入黑名单
        , blackuser: function () {
            
            changeDataStatus(3);
        }
        //加入白名单
        , whiteuser: function () {
            
            changeDataStatus(4);
        }
        //取消特别关注
        , restartuser: function () {
           
            changeDataStatus(1);
        }
        //取消黑名单
        , reblackuser: function () {
            
            changeDataStatus(1);
        }
    };


    //回车执行
    document.onkeydown = function (event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) {
            loadData();
        }
    };


    function changeDataStatus(thisStatus) {
        var checkStatus = table.checkStatus('recordtable')
                 , data = checkStatus.data;


        if (data.length == 0) {
            layui.layer.alert("请先选中要操作的数据行！", {
                icon: 3
                ,offset: '40%'
            });
        }
        else {
            var strId = "";
            for (var i = 0; i < data.length; i++) {

                if (i == 0) {
                    strId += data[i].F_CustomerId;
                }
                else {
                    strId += "," + data[i].F_CustomerId;
                }

            }
            layer.confirm('确认要操作选中行的数据？', function (index) {
                $.ajax({
                    url: "/Customer/SaveReceivedData",
                    type: "get",
                    async: false,//同步请求
                    contentType: "application/json",
                    dataType: "text",
                    cache: false,
                    data: "keyList=" + strId + '&status=' + thisStatus,
                    success: function (thisResult, result) {

                        if (thisResult == "True") {
                            layer.open({
                                content: '操作成功'
                                 , icon: 1
                                 ,offset: '40%'
                                 , btn: ['确定']
                                 , yes: function (index, layero) {
                                     loadData();
                                     layer.close(index);
                                 }
                            });
                        } else {
                            layui.layer.alert("操作失败!", {
                                icon: 2
                                ,offset: '40%'
                            });

                        }
                    },
                    error: function (error) {
                        return false;
                    }
                });
                layer.close(index);
            });
        }
    }


    function loadData() {
        var cusName = $('#cusName');
        var cusNum = $('#cusNum');
        //执行重载
        table.reload('recordtable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
          , where: {
              NowDateTime: new Date(),
              cusType:$("#cusType").val(),
              cusName: cusName.val(),
              cusNum: cusNum.val()

          }
        });
    }
    $('.searchfilter .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    formatcalldir = function (d) {
        var rescontent = "";
        if (d == 1) {
            rescontent = "<i class=\"iconfont icon-huchu\"></i>";
        }
        else {
            rescontent = "<i class=\"iconfont icon-huru\"></i>";
        }
        return rescontent;
    }

    playMp3 = function (d) {
        //  return  " <audio controls=\"controls\"><source src=\"/content/test.mp3\" type=\"audio/mpeg\"> </audio>";
        return " <audio src=\"/content/test.mp3\" controls></audio>";

    }



    function refushTreeAndData() {
        //执行重载
        table.reload('recordtable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                NowDateTime: new Date(),
                parentId: $("#selectTypeId").val()

            }
        }); 
    }


});