var table = $("#tableList");
var exportParams;
$(document).ready(function() {
	Userarr($("#user"))
	laydate.skin('blue');
	laydate({
		elem: '#starttime',
		event: 'focus',
		istime: true,
		format: 'YYYY-MM-DD hh:mm:ss'
	});
	laydate({
		elem: '#endtime',
		event: 'focus',
		istime: true,
		format: 'YYYY-MM-DD hh:mm:ss'
	});
	initTable();
});

function Userarr(obj) {
	obj.empty();
	obj.append('<option selected="selected" value="">请选择话务员</option>');
	$.ajax({
		type: "get",
		url: huayi.config.callcenter_url + "UserAccount/GetSeatList",
		dataType: "json",
		async: true,
		data: {
			"token": $.cookie("token")
		},
		success: function(data) {
			if(data.state.toLowerCase() == "success") {
				var content = data.data;
				$(content).each(function(i, n) {
					$("<option value='" + n.F_UserCode + "'>" + n.F_UserName + "</option>")
						.appendTo(obj);
				})
			}
		}
	});
}

function initTable(action, isPass) {
	//先销毁表格
	table.bootstrapTable("destroy");
	//初始化表格,动态从服务器加载数据
	table.bootstrapTable({
		method: "get", //使用get请求到服务器获取数据
		url: huayi.config.callcenter_url + "Quality/GetExamineList", //获取数据的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", //表示服务端请求
		//设置为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: $.cookie("token"),
				user: $("#user").val(),
				starttime: $("#starttime").val(),
				endtime: $("#endtime").val(),
				isdc: 0
			};
			exportParams = param
			return param;
		},
		onLoadSuccess: function() {
			//加载成功时执行
			//layer.msg("加载成功");
		},
		onLoadError: function() {
			//加载失败时执行
			//layer.msg("加载数据失败", { time: 1500, icon: 2 });
		},
	});
}
// 导出文件
$(".exportFile").click(function() {
	exportParams.isdc = 1;
	exportFileFun("Quality/GetExamineList", exportParams)
})
// 导出列表
function exportFileFun(api, params) {
    var url = huayi.config.callcenter_url + api;
    if(typeof(params) !== "object") {
        return
    }
    // 分页为空
    params.page = "";
    params.pagesize = "";
    Object.keys(params).forEach(function(key, index) {
        if (index === 0) {
            url += `?${key}=${params[key]}`
        } else {
            url += `&${key}=${params[key]}`
        }
    })
    window.location.href = url;
}
function setCode(val, row, index) {
	return index + 1;
}

function operation(val, row) {
	return(
		'<div class="tool_downs">' +
		'<a href="javascript:;" class="aBtn" authorize="yes" onclick="editBtn(' +
		row.F_ID +
		')" title="编辑">编辑</a>' +
		'<a href="javascript:;" class="aBtn" authorize="yes" onclick="deleteBtn(' +
		row.F_ID +
		')" title="删除">删除</a>' +
		"</div>"
	);
}

$(".searchGo").click(function() {
	initTable();
});

$(".addBtn").click(function() {
	layer.open({
		type: 2,
		content: "./addOrEditScore.html?valueid=" + $("#type").val(), //iframe的url，no代表不显示滚动条
		title: "添加",
		area: ["75%", "60%"], //宽高
	});
});

function editBtn(id) {
	layer.open({
		type: 2,
		content: "./addOrEditScore.html?id=" + id + "&valueid=" + $("#type").val(), //iframe的url，no代表不显示滚动条
		title: "编辑",
		area: ["75%", "60%"], //宽高
	});
}

function deleteBtn(id) {
	layer.confirm('确定删除吗？', {
		btn: ['是', '否'] //按钮
	}, function() {
		$.post(huayi.config.callcenter_url + "Quality/DelExamine", {
			id: id,
			token: $.cookie("token")
		}, function(result) {
			result = JSON.parse(result);
			if(result.state.toLowerCase() == "success") {
				layer.msg("删除成功");
				initTable()
			}
		})
	});
}