var table = $('#table1');
var token = $.cookie("token");
laydate.skin('molv');
laydate({
    elem: '#startTime',
    event: 'focus',
    format: 'YYYY-MM-DD', // 分隔符可以任意定义，该例子表示只显示年月
});

//客户名称
getAccount()
function getAccount() {
    $.ajax({
        type: "get",
        url: huayi.config.callcenter_url + "Customer/BindLDdep",
        dataType: 'json',
        async: true,
        data: {
            token: $.cookie("token")
        },
        success: function (data) {
            var Count = data.data.dep;
             $("<option value=''>请选择客户名称</option>").appendTo($("#customer_name"));
            $(Count).each(function (i, n) {
                $("<option value='" + n + "'>" + n + "</option>").appendTo($("#customer_name"));
            })
//          $('#customer_name').selectpicker('refresh');
			$('#customer_name').comboSelect();
        }
    });
}
//省份
getProvince()
function getProvince() {
    var Count = geo.provinces;
    var arr=[]
    $(Count).each(function (i, n) {
        $("<option value='" + n.provinceName + "'>" + n.provinceName + "</option>").appendTo($("#province"));
        arr.push(n.provinceName);
    })
    $('#province').selectpicker('refresh');
}


$('.search').click(function () {
    initTable(token);
})
//添加
$('.add').click(function () {
    layer.open({
        type: 2,
        title: '添加客户信息',
        maxmin: true, //开启最大化最小化按钮
        area: ['50%', '50%'],
        content: 'addCustomerFile.html'
        //              content: 'addZhiShiKuManger.html?addId=' + id + '&addPid='+ pid +'&addName='+ name +''
    });
})

//修改
var did;
$('.change').click(function () {
    var ids = $.map(table.bootstrapTable('getSelections'), function (row) {
        return row.F_CustomerId;
    });
    did = ids.toString();
    if (ids.length != 1) {
        layer.confirm('请选择一行进行修改！', {
            btn: ['确定']
        });
        return;
    } else {
        layer.open({
            type: 2,
            title: '修改客户信息',
            maxmin: true, //开启最大化最小化按钮
            area: ['50%', '50%'],
            //              content: 'addCustomerFile.html'
            content: 'addCustomerFile.html?ids=' + did
        });


    }
})

//删除
var delid;
$('.del').click(function () {
    var ids = $.map(table.bootstrapTable('getSelections'), function (row) {
        return row.F_CustomerId;
    });
    delid = ids.toString();
    if (ids.length <= 0) {
        layer.confirm('请选择要删除的行！', {
            btn: ['确定']
        });
        return;
    } else {
        var laye = layer.confirm('您确定要删除吗？', {
            btn: ['确定', '取消'] //可以无限个按钮
        }, function () {
            //按钮【按钮一】的回调
            $.ajax({
                type: "post",
                url: huayi.config.callcenter_url + "Customer/DelCustomer",
                async: true,
                dataType: "json",
                data: {
                    token: token,
                    ids: delid
                },
                success: function (data) {
                    if (data.state.toLowerCase() == 'success') {
                        layer.msg("删除成功！");
                        table.bootstrapTable('refresh');
                    }
                }
            });
        }, function (index) {
            //按钮【按钮二】的回调
            layer.close(laye)
        });

    }

})
//列表
initTable(token);
function initTable(token) {
    //先销毁表格
    table.bootstrapTable('destroy');
    //初始化表格,动态从服务器加载数据
    table.bootstrapTable({
        method: "get", //使用get请求到服务器获取数据
        url: huayi.config.callcenter_url + "Customer/GetList", //获取数据的Servlet地址
        contentType: 'application/x-www-form-urlencoded',
        //			url: "kehu.json", //获取数据的Servlet地址
        striped: true, //表格显示条纹
        pagination: true, //启动分页
        pageSize: 10, //每页显示的记录数
        pageNumber: 1, //当前第几页
        pageList: [10, 15, 20, 50, 100], //记录数可选列表
        search: false, //是否启用查询
        showColumns: false, //显示下拉框勾选要显示的列
        showRefresh: false, //显示刷新按钮
        sidePagination: "server", //表示服务端请求
        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
        //设置为limit可以获取limit, offset, search, sort, order
        queryParamsType: "undefined",
        queryParams: function queryParams(params) { //设置查询参数
            var param = {
                page: params.pageNumber,
                pagesize: params.pageSize,
                token: token,
                Cusname: $('#customer_name').val(), //客户名称
                name: $('.contact_name').val(), //联系人
                province: $('#province').val(), //省份
                tel: $('.phone').val(), //电话
                //					logindate: $('.loginTime').val() //登录时间
            };
            return param;
        },
        onLoadSuccess: function () { //加载成功时执行
            //layer.msg("加载成功");
        },
        onLoadError: function () { //加载失败时执行
            //layer.msg("加载数据失败", { time: 1500, icon: 2 });
        }
    });
}

function setCode(val, row, index) {
    //		return index + 1;
    var page = table.bootstrapTable("getPageCode");
    return page.pageSize * (page.pageNumber - 1) + index + 1;
}
var regBox = {
    //regEmail: /^([A-Za-z0-9_\.-]+)@([\dA-Za-z_\.-]+)\.([A-Za-z_\.-]{2,6})$/,  //邮箱
    //regEmail: /@/,    //邮箱
    //regName: /^[a-z0-9_-]{3,16}$/,    //用户名
    //regMobile: /^1(3|4|5|7|8)\d{9}$/    //手机
    //regTel: /^0[\d]{2,3}-[\d]{7,8}$/,
    //regData: /^[0-9]*$/ //数字
    regMobOrTel: /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
}
//判断
function Isnullorempty(selstr) {
    if (selstr == null || selstr == "" || selstr == undefined || selstr == "null" || selstr == "undefined" || parseFloat(selstr) <= 0) {
        return true;
    } else {
        return false;
    }
}

//上传文件

$("#btndr").click(function () {
    $("#upFile").trigger("click");
})
$("#upFile").change(function () {
    upload();
})

function ExcelMod() {
    window.location.href = huayi.config.callcenter_url + "ExcelMod/CustomerList_Mod.xls";
}


//上传文件
function upload() {
    if (document.getElementById("upFile").files.length > 0) {
        var formData = new FormData();
        formData.append("upFile", document.getElementById("upFile").files[0]);
        formData.append("token", $.cookie("token"));
        $.ajax({
            url: huayi.config.callcenter_url + "Customer/ExportExcel",
            type: "POST",
            data: formData,
            /**
             *必须false才会自动加上正确的Content-Type
             */
            contentType: false,
            /**
             * 必须false才会避开jQuery对 formdata 的默认处理
             * XMLHttpRequest会对 formdata 进行正确的处理
             */
            processData: false,
            success: function (result) {
                document.getElementById("upFile").outerHTML = document.getElementById("upFile").outerHTML;
                $("#upFile").change(function () {
                    upload();
                });
                var r = $.parseJSON(result);
                if (r.state.toLowerCase() == "success") {
                    //                          $(".yhtx").attr("src", huayi.config.callcenter_url.substr(0, huayi.config.callcenter_url.length - 1) + r.data);
                    //                          var tel = $("#tels").val();
                    //                          if (tel) {
                    //                              if (tel.substr(huayi.config.callcenter_url.length - 1, 1) == ',') {
                    //                                  $("#tels").val(tel + r.data);
                    //                              }
                    //                              else {
                    //                                  $("#tels").val(tel + ',' + r.data);
                    //                              }
                    //                          }
                    //                          else {
                    //                              $("#tels").val(r.data);
                    //                          }
                    layer.msg("导入成功");
                }
            }
        });
    } else {
        layer.confirm('请上传文件！', {
            btn: ['确定']
        });
    }
}

