$(document).ready(function() {
	getDepts();
	initTable(null);
})

$("#query").click(function () {
	queryOverRun();
});

function getDepts(){
	$.ajax({
		type:"POST",
		dataType:"json",
		url:"/pqs9300/technical/getDepts",
		success:function(data){
            if(data.code==200){
            	var option;
            	var depts = data.body;
                for(var i=0;i<depts.length;i++){
                	if(i==0){
                        option = option+"<option selected value='"
                            + depts[i].deptsIndex + "'>"
                            + depts[i].deptsName + "</option>";
                    }else{
                        option = option+"<option value='"
                            + depts[i].deptsIndex + "'>"
                            + depts[i].deptsName + "</option>";
                    }
                }

				$("#depts").append(option);
            }else{
                layer.msg('获取部门信息失败！',{icon:2,time:1000});
            }
		}
	})
}

function queryOverRun(){
	var startTime = $("#startTime").val();
	var depts = $("#depts").val();
	var type = $("#type").val();

	$.ajax({
		type:"POST",
		dataType:"json",
		data : {
			startTime:startTime,
			deptIndex:depts,
			type:type
		},
		url:"/pqs9300/technical/getTechnicalDetail",
		success:function(data){
			var resultData;
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000})
                resultData = null;
            } else if(data.body.length == 0){
                resultData = null;
            }else{
                resultData = data.body;
            }
			initTable(resultData);
		}
	})
}

function initTable(data) {
    var height = $(window).height() - 140;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    
    var dataTable = null;
    dataTable = $("#vdlist").DataTable();
    dataTable.clear().draw();
	dataTable = $("#vdlist").DataTable({
	    order: [],
	    columnDefs: [
	        {
	            orderable: false,
	            targets: [0]
	        }
	    ],
	    columns: [
	        {	
	        	width: "2%", data: "lineIndex",
                render: function (data, type, full, meta) {
                        return "<input type ='checkbox' name='test' class='icheckbox_minimal' value='" + data + "'>";
                }
	        },
	        {width: "15%", data: "gdName"},
	        {width: "15%", data: "subName"},
	        {width: "15%", data: "lineName"},
	        {width: "8%", data: "overRunTimes"},
	        {
	            width: "35%", data: "overRunDescribe", render: function (data, type, full, meta) {
	                var value = data;
	                if (data.length > 50) {
	                    value = data.substring(0, 49) + "...";
	                    value = "<span title=" + data + ">" + value + "</span>";
	                }
	                return value;
	            }
	        }
	    ],
	    destroy: true,
	    data: data,
	    lengthChange: false,
	    searching: true,
	    dom: 'rtip',
	    scrollY: height,
	    scrollX: false,
	    lengthMenu: [length],
	    language: {
	        url: '/pqs9300/json/chine.json'
	    },
	    fnDrawCallback: function (oTable) {
	        $("#vdlist_info").addClass("fl");
	        $("#vdlist_paginate").addClass("fr");
	        $("#vdlist_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
	        var table = $("#vdlist").dataTable();
	        $('#dataTable-btn').click(function (e) {
	            if ($("#changePage").val() && $("#changePage").val() > 0) {
	                var redirectpage = $("#changePage").val() - 1;
	            } else {
	                var redirectpage = 0;
	            }
	            table.fnPageChange(redirectpage);
	        });
	 		$.fn.dataTable.tables( {visible: true, api: true} ).columns.adjust();
	    }
	});
}


//实现全选功能
$("#all_checked").click(function () {
    $('[name=test]:checkbox').prop('checked', this.checked);//checked为true时为默认显示的状态
});

//实现反选功能
$("#checkrev").click(function () {
    $('[name=test]:checkbox').each(function () {
        this.checked = !this.checked;
    });
});

//点击生成
$("body").on("click", "#btn_deal", function () {
    var indexs = [];
    var $checkboxs = $("[name=test]:checked");
    var length = $checkboxs.length;
    if (length === 0) {
        layer.msg('请至少选择一条记录', {icon: 2, time: 2000});
    } else {
        for (var i = 0; i < length; i++) {
            indexs.push($checkboxs.eq(i).val())
        }
        saveOverRunlog(indexs);
    }
})

function saveOverRunlog(indexs){
	var startTime = $("#startTime").val();
	var depts = $("#depts").val();
	var type = $("#type").val();

	$.ajax({
		type:"POST",
		dataType:"json",
		data : {
			startTime:startTime,
			deptIndex:depts,
			type:type,
			indexs:indexs
		},
		url:"/pqs9300/technical/saveTechnicalDetail",
		success:function(data){
			var resultData;
            if (data.code === 500) {
                layer.msg("生成失败！", {icon: 2, time: 2000})
            }else{
                layer.msg("生成成功！", {icon: 1, time: 2000})
            }
		}
	})
}