var token = "123";

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

$(function () {
    function inittoken() {
        var t = GetRequest();
        console.log(t.token);
        token = t.token;
        if (t.token == "" || t.token == null) {
            console.log("无token！");
        }

        /* swal({
            html: true,
            title: "<h4>数据更新中，请稍后...</h4>",
            //text: "数据加载中，请稍后...",
            imageUrl: "pagers/img/loading.gif",
            showConfirmButton: false,
         });*/

        $.ajax({
            type: 'get',
            async: false,
            url: 'queryStationList',
            data: {
                'token': token
            },
            dataType: 'json',
            success: function (data) {
                if (data.code == '200') {
                    var dt = data.data;
                    var op = ""
                    for (var a = 0; a < dt.length; a++) {
                        op = op + "<option value='" + dt[a].stationCode + "'>" + dt[a].stationName + "</option>"
                    }
                    $("#fwzsx").append(op);
                    //$('.example-getting-started').refresh();
                   
                }
                 $('.example-getting-started').multipleSelect({
                        selectAllText: "全选",
                        allSelected: "全选",
                        countSelected: '已选择#个',
                        placeholder: "请选择",
                        width: 170,
                        multipleWidth: 55,
                        selectAll: true,
                        onClick: function (obj) {
                            //label,value,checked
                        }
                    });

            } 
        })


        //swal.close()
    }

    inittoken();
})


//------------------------------------下拉框值---------------------------
//$('.example-getting-started').multipleSelect({
//	selectAllText:"全选",
//	allSelected:"全选",
//	countSelected: '已选择#个',
//	placeholder: "请选择",
//    width: 170,
//    multipleWidth: 55,
//    selectAll:true,
//    onClick:function(obj){
//    	//label,value,checked
//    }
//}); 


//-时间--------------------------------------------------------------------------------------------
var beginTimeStore = '';
var endTimeStore = '';
var format = "YYYY-MM-DD";
$('#sjdid').daterangepicker({
    showDropdowns: true,
    autoUpdateInput: false,
//    timePicker12Hour: false,
//    timePicker: true,
//    timePickerIncrement: 1,
    format: format,
    locale: {
        applyLabel: "应用",
        cancelLabel: "取消",
        fromLabel: '起始时间',
        toLabel: '结束时间',
        customRangeLabel: '自定义',
        daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
        monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
        firstDay: 1
    },
}, function (start, end) {
    //$('#sjdid').val(start+ " —— " +end);
    /*beginTimeStore = start;
    endTimeStore = end;
    if (!this.startDate) {
        this.element.val('');
    } else {
        this.element.val(this.startDate.format(format) + " —— " + this.endDate.format(format));
    } */
});

function clearData(obj) {
    $(obj).prevAll().val('');
}


$("#gxsj").click(function () {//更新数据**************************
    swal({
        html: true,
        title: "<h4>数据更新中，请稍后...</h4>",
        //text: "数据加载中，请稍后...",
        imageUrl: "pagers/img/loading.gif",
        showConfirmButton: false,
    });
    window.setTimeout(function () {
        $.ajax({
            type: 'get',
            async: false,
            url: 'getHeMadata',
            data: {
                'token': token
            },
            dataType: 'json',
            success: function (data) {
                console.log(data);
                if (data.code != 200) {
                    swal({
                        html: true,
                        title: "<h4>提示.</h4>",
                        text: "数据更新失败",
                        showConfirmButton: true,
                    });
                } else {
                    swal.close();
                }

            }
        })

    }, 1000 * 1);
})


//---------------------------筛选数据------------------------------------------------
function initData() {
//		alert($('#gdlx').val());
    /*swal({
        html: true,
        title: "<h4>数据加载中，请稍后...</h4>",
        //text: "数据加载中，请稍后...",
        imageUrl: "pagers/img/loading.gif",
        showConfirmButton: false,
    });*/
    //swal.close()
    if (!(/^1(3|4|5|7|8)\d{9}$/.test($('#phoneida').val())) && $('#phoneida').val() != '') {
        $('#phoneida').focus();
        alert("请输入正确的手机号！");
        return "";
    }
    if (!(/^1(3|4|5|7|8)\d{9}$/.test($('#phoneidb').val())) && $('#phoneidb').val() != '') {
        $('#phoneidb').focus();
        alert("请输入正确的手机号！");
        return "";
    }

    if ($('#sja').val() >= $('#sjb').val()) {
        $('#sjb').focus();
        alert("时间段（时）结束时间应大于开始时间");
        return "";
    }

    var fucsa = $('#fucsa').val() * 1;
    var fucsb = $('#fucsb').val() * 1;
    if (fucsa != 0) {
        $('#fucsa').val(fucsa);
    }
    if (fucsb != 0) {
        $('#fucsb').val(fucsb);
    }
    if (fucsa > fucsb) {
        $('#fwcsb').focus();
        alert("服务次数结束值应大于开始值");
        return "";
    }

    $('#form').ajaxSubmit({
        success: function (data) {
            console.log(data);
            jjiexihtml(data, data.pages, data.pageNo, data.total, data.pageSize, data.startPage, data.endPage, data.pageNo, data.dataList)
        }
    });

}


//***************分页查询***************
function jjiexihtml(data, _pages, _pageNo, _total, pageSize, startPage, endPage, currentPage, pagedata) {
    if (_total > 0) {
        $('#pagetextid').html("共 " + _pages + " 页 " + _total + "条记录，当前为第" + _pageNo + " 页 ");
        $('#countid').html(data.countsize);
    }
    $('#pageTotal').val(_pages)
    pageinit(_pages, _pageNo, _total, pageSize, startPage, endPage, currentPage);


    var tr = "";
    for (var i = 0; i < pagedata.length; i++) {
        var gdlbtxt = "";
        var zttxt = "";
        if (pagedata[i].serviceType == '2') {
            gdlbtxt = "服务工单";
        }
        if (pagedata[i].serviceType == '9') {
            gdlbtxt = "派遣工单";
        }

        if (pagedata[i].status == '0') {
            zttxt = "待接单";
        }
        if (pagedata[i].status == '1') {
            zttxt = "待履约";
        }
        if (pagedata[i].status == '2') {
            zttxt = "已完成";
        }
        if (pagedata[i].status == '3') {
            zttxt = "已过期";
        }
        if (pagedata[i].status == '4') {
            zttxt = "已取消";
        }
        if (pagedata[i].status == '5') {
            zttxt = "已拒绝";
        }

        var line = "<td>" + (i + 1) + "</td>" +
            "<td>" + pagedata[i].bookTradeNo + "</td>" +
            "<td>" + pagedata[i].stationName + "</td>" +
            "<td>" + gdlbtxt + "</td>" +
            "<td>" + pagedata[i].servantName + "</td>" +
            "<td>" + pagedata[i].servantPhone + "</td>" +
            "<td>" + pagedata[i].itemName + "</td>" +
            "<td>" + pagedata[i].beginTime + "</td>" +
            "<td>" + pagedata[i].userName + "</td>" +
            "<td>" + pagedata[i].userPhone + "</td>" +
            "<td>" + pagedata[i].addrName + pagedata[i].addrDetail + "</td>" +
            "<td>" + pagedata[i].remark + "</td>" +
            "<td>" + zttxt + "</td>";
        tr = tr + "<tr>" + line + "</tr>";

    }
    $('#tabbodyid').html(tr)

}

function exportExcel() {
    var form = $("form");
    form.attr("action", "exportExcel");
    form.submit();
}
