$(document).ready(function(){
    initTable()
    laydate.render({
        elem: '#greetingTimes',
        range: '~',
        theme: '#1ab394',
    });
    laydate.render({
        elem: '#duplicateTime',
        range: '~',
        theme: '#1ab394',
    });
    $(".search").click(function() {
        initTable()
    })
    // getProvinceCounty($("#searchProvince"), 0, "请选择省")
    getFounder($("#searchFounder"))
});


function initTable() {
    //先销毁表格
    $('#registerList').bootstrapTable('destroy');
    //初始化表格,动态从服务器加载数据
    $("#registerList").bootstrapTable({
        method: "post", //使用get请求到服务器获取数据
        url: huayi.config.callcenter_url + "RegRecords/GetList", //获取数据的Servlet地址
        contentType: 'application/x-www-form-urlencoded',
        striped: true, //表格显示条纹
        pagination: true, //启动分页
        pageSize: 10, //每页显示的记录数
        pageNumber: 1, //当前第几页
        pageList: [10, 20, 50, 100], //记录数可选列表
        search: false, //是否启用查询
        showColumns: false, //显示下拉框勾选要显示的列
        showRefresh: false, //显示刷新按钮
        sidePagination: "server", //表示服务端请求
        fixedColumns: true,
        fixedNumber: 14,
        //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
        //设置为limit可以获取limit, offset, search, sort, order
        queryParamsType: "undefined",
        queryParams: function queryParams(params) { //设置查询参数
            var param = {
                token: $.cookie("token"),
                Zsz: $.cookie("seatGroupName"), //坐席组名称
                key: $("#searchKeyword").val().replace(/\s+/g, ""),//工单编号
                direction: $("#searchDirection").val(),//方向
                type: $("#searchRegistrationType").val(),//登记类型
                usercode: $("#searchFounder").val(),//创建人
                pageindex: params.pageNumber, //页码
                pagesize: params.pageSize, //条数
            };
            return param;
        },
        onLoadSuccess: function() { //加载成功时执行
            //layer.msg("加载成功");
        },
        onLoadError: function() { //加载失败时执行
            //layer.msg("加载数据失败", { time: 1500, icon: 2 });
        }
    });
}

// 工单编号
function View(val, row) {
    return '<div class="imgs" ><a class="view" onclick= btn_register_details("' + row.F_ID + '") >' + val + '</a></div>';
}

//登记类型
function formatterRegisterType(val, row) {
    var formatterRegisterTypeArr = ["", "咨询", "投诉", "求助", "其他"]
    return formatterRegisterTypeArr[val]
}

//呼叫方向
function formatterRegisterDirection(val, row) {
    var formatterRegisterDirectionArr = ["呼入", "呼出"]
    return formatterRegisterDirectionArr[val]
}

//登记信息
function formatterOperating(val, row) {
    return '<ul class="tool_downs">' +
		'<li><a href="javascript:;" class="aBtn" authorize="yes" id="HY_edit_' + row.F_ID + '" onclick="btn_edit(\'' + row.F_ID + '\')" title="编辑">编辑</a></li>' +
		'<li><a href="javascript:;" class="aBtn" authorize="yes" id="HY_delete_' + row.F_ID + '" onclick="btn_delete(\'' + row.F_ID + '\')" title="删除">删除</a></li>' +
		'</ul>';
}

//转移登记
function formatterOperatingTransfer(val, row) {
    return '<ul class="tool_downs">' +
		'<li><a href="javascript:;" class="aBtn" authorize="yes" id="HY_transfer_' + row.F_ID + '" onclick="btn_transfer(\'' + row.F_ID + '\')" title="转移">转移</a></li>' +
		'</ul>';
}

//接收登记
function formatterOperatingReceive(val, row) {
    return '<ul class="tool_downs">' +
		'<li><a href="javascript:;" class="aBtn" authorize="yes" id="HY_receive_' + row.F_ID + '" onclick="btn_receive(\'' + row.F_ID + '\')" title="接收">接收</a></li>' +
		'</ul>';
}

function btn_edit(id) {
    layer.open({
        type: 2,
        title: '编辑登记信息',
        maxmin: true, //开启最大化最小化按钮
        area: ['80%', '80%'],
        content: 'edit.html?ids=' + id
    });
}

function btn_register_details(id) {
    layer.open({
        type: 2,
        title: '登记详情',
        maxmin: true, //开启最大化最小化按钮
        area: ['80%', '80%'],
        content: 'detail.html?ids=' + id
    });
}

function btn_delete (id) {
    var laye = layer.confirm('您确定要删除吗？', {
        btn: ['确定', '取消'] //可以无限个按钮
    }, function () {
        //按钮【按钮一】的回调
        $.ajax({
            type: "post",
            url: huayi.config.callcenter_url + "RegRecords/Del",
            async: true,
            dataType: "json",
            data: {
                token: $.cookie("token"),
                ids: id
            },
            success: function (data) {
                if (data.state.toLowerCase() == 'success') {
                    layer.msg("删除成功！");
                    $("#registerList").bootstrapTable('refresh');
                }
            }
        });
    }, function (index) {
        //按钮【按钮二】的回调
        layer.close(laye)
    });
}

/* 
    获取 省 市 县/区
    parameter:
        pid     int
*/
function getProvinceCounty(element, pid, text) {
    element.empty();
    element.append('<option value="">'+ text +'</option>');
	$.getJSON(huayi.config.callcenter_url + "RegionCategory/GetCountyList", {
		"token": $.cookie("token"),
		pid: pid,
	}, function (data) {
		if (data.state.toLowerCase() == "success") {
			var content = data.data;
			$(content).each(function (i, n) {
            	element.append("<option value='" + n.F_RegionId + "'>" + n.F_RegionName + "</option>");
			})
		}
	})
}

$("#searchProvince").on('change',function(){
	getProvinceCounty($("#searchCity"), $("#searchProvince").val(), "请选择市");
});
$("#searchCity").on('change',function(){
	getProvinceCounty($("#searchCounty"), $("#searchCity").val(), "请选择县/区");
});

//创建人
function getFounder(element) {
    element.empty();
    element.append('<option value="">请选择创建人</option>');
	$.getJSON(huayi.config.callcenter_url + "UserAccount/GetSeatList", {
        "token": $.cookie("token"),
	}, function (data) {
        var content = data.data;
        $(content).each(function (i, n) {
            element.append("<option value='" + n.F_UserCode + "'>" + n.F_UserName + "</option>");
        })
	})
}

// 按回车搜索
document.onkeydown = function (e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13) {
        initTable();
    }
}
