// 默认初始化日期一个月
var tdaytime = moment();
var start_time = "";
var end_time = "";
if (tdaytime.get('D') >= 26) {
    start_time = tdaytime.set('date', 26).format("YYYY-MM-DD");
    tdaytime = moment();
    end_time = tdaytime.set('date', 25).add(1, 'M').format("YYYY-MM-DD");
} else {
    start_time = tdaytime.set('date', 26).subtract(1, 'M').format("YYYY-MM-DD");
    tdaytime = moment();
    end_time = tdaytime.set('date', 25).format("YYYY-MM-DD");

}
$(".div-start input").val(start_time);
$(".div-end input").val(end_time);
var start_date = dateChange(new Date($(".div-start input").val() + " 00:00:00"), 60 * 60 * 1000);
var end_date = $(".div-end input").val() + " 23:00:00";
var user_id = $("#select-personal").val();
var cur_status;
var hide = false;

/**
 *
 * @param {*} datet
 * @param {*} changVal
 */
function dateChange(datet, changVal) {
    var tempDate = new Date(datet.valueOf() - changVal);
    return tempDate.Format("yyyy-MM-dd HH:mm:ss");
}

var datatable = null;
var pageindex = 1;

$(function() {



    //*************************************数据表格******************************************
    function initAppealPage(start_date, end_date, user_id) {

        initQueryParams();
        queryAppeal(start_date, end_date, user_id);
    }

    function initQueryParams() {
        // 仅选择日期
        $(".div-start input,.div-end input").datetimepicker({
            language: "zh-CN",
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            format: "yyyy-mm-dd"
        });
        //获取页面参数
        var page_params = $.zui.store.get("page_params");
        if (page_params && page_params.status) {
            cur_status = page_params.status;
        }

        var pdata = $.zui.store.get('store_param_' + current_url);
        if (pdata != null) {
            $.zui.store.set('store_param_' + current_url, null);
            $(".div-start input").val(pdata.startdate)
            $(".div-end input").val(pdata.enddate)
            $("#select-personal").val(pdata.user_id)
            $(".score-select").val(pdata.score_falge)
            pageindex = pdata.currentpage;
        }



    }

    function queryAppeal(start_date, end_date, user_id) {

        var data = {
            "start_date": start_date,
            "end_date": end_date,
            "user_id": user_id,
            "queryType": 1,
            "check_status": cur_status,
            "score_flag": $(".score-select").val()
        }
        if (hasRole(ROLE_STATION_ADMIN) || hasRole(ROLE_STATION) || hasRole(ROLE_JICHA) || roleContains('STATION')) {
            hide = true;
        }

        var cols = [
            { width: 40, height: 60, text: '序号', flex: true, colClass: 'text-center', field: 'num' },
            { width: 40, height: 60, text: '稽查人员', flex: true, colClass: 'text-center', field: 'checkman_name' },
            { width: 50, height: 60, text: '任务名称', flex: true, type: 'href_link', colClass: 'text-center', field: '<a href=\"javascript:void(0);\" onclick=\"jumptodetail(\'$field=task_id$\')\">$field=check_task_name$</a>' },
            { width: 60, height: 60, text: '时间段', flex: true, colClass: 'text-center', field: 'check_task_time_name' },
            { width: 40, height: 60, text: '考核人员', flex: true, colClass: 'text-center', field: 'checked_name' },
            { width: 60, height: 60, text: '考核部门', flex: true, colClass: 'text-center', field: 'feeStationName' },
            { width: 40, height: 60, text: '扣分数', flex: true, colClass: 'text-center', field: 'score' },
            { width: 60, height: 60, text: '考核类型', flex: true, colClass: 'text-center', field: 'parent_name' },
            { width: 60, height: 60, text: '考核提交时间', flex: true, colClass: 'text-center', field: 'update_time' },
            { width: 100, height: 60, text: '考核项', flex: true, colClass: 'text-center', field: 'name' },
            { width: 200, height: 60, text: '扣分凭证', type: 'imagedd', flex: true, field: 'pics' },
        ];
        if (cur_status == 12 || cur_status == 13) {
            data.check_status_arr = [12, 13];
        }

        datatable = $('#check_workload_table').mytable({
            'cols': cols,
            'url': "/statistics/one/checkedItem/detail/info",
            'param': data,
            'pager': {
                'page_size': 10,
                'p_cur': pageindex
            }
        });
    }


    // 导出excel
    $("#export-check-info").click(function() {
        var param = '';
        var start_date = dateChange(new Date($(".div-start input").val() + " 00:00:00"), 60 * 60 * 1000);
        var end_date = $(".div-end input").val() + " 23:00:00";
        if ($("#select-personal").val() != '') {
            param += "&user_id=" + $("#select-personal").val();
        }

        window.open(getserveraddr() + "/file/check/info?queryType=1&start_date=" + start_date + '&end_date=' + end_date + param);
    })


    $(".query button").click(function() {
        pageindex = 1;
        var start_date = dateChange(new Date($(".div-start input").val() + " 00:00:00"), 60 * 60 * 1000);
        var end_date = $(".div-end input").val() + " 23:00:00";
        var startStr = start_date.replace(/-/g, '/');
        var endStr = end_date.replace(/-/g, '/');
        var start = new Date(startStr);
        var end = new Date(endStr);
        if (start > end) {
            tip("开始时间不能比结束时间大");
            return;
        }
        var user_id = $("#select-personal").val();
        // 表格
        checkman_info(start_date, end_date, user_id);
        initAppealPage(start_date, end_date, user_id);

    });

    /**
     * 稽查人员下拉框
     */
    function select_personal() {
        var sec = document.getElementById('select-personal');
        post_common_service("user/getAllCheckman", {}, function(data) {
            if (data.length != 0) {
                var tempStr = '<option value="">全部</option>';
                for (var i = 0; i < data.length; i++) {
                    tempStr += "<option value=" + data[i].id + ">" + data[i].truename + "</option>";
                }
                sec.innerHTML = tempStr;
            } else {
                sec.innerHTML = '';
            }
            var user_id = $("#select-personal").val();
            checkman_info(start_date, end_date, user_id);
            // 初始化表格
            initAppealPage(start_date, end_date, user_id);

        }, function(error) {
            return "";
        });
    }
    select_personal();




    /**
     * 稽查人员信息
     * @param {*} start_date
     * @param {*} end_date
     * @param {*} user_id
     */
    function checkman_info(start_date, end_date, user_id) {
        var param = {
            "start_date": start_date,
            "end_date": end_date,
            "checkman_id": user_id
        }


        post_common_service("statistics/checkman/info", param, function(data) {
            // console.log(data)
            if (data != null) {
                WH(".all_check_task", GDV(data.all_task_num, 0));
                WH(".all_check_minute", convertT(GDV(data.all_check_minute, 0)));
                WH(".all_checkman", GDV(data.all_checkman, 0));
                WH(".all_check_score", GDV(data.all_checked_score, 0));
                WH(".checked_num", GDV(data.checked_num, 0));
                WH(".checked_task_num", GDV(data.checked_task_num, 0));
                WH(".unable_task_num", GDV(data.unable_task_num, 0));
                WH(".checked_people_num", GDV(data.checked_people_num, 0));
            } else {
                WH(".all_check_task", 0);
                WH(".all_check_minute", 0);
                WH(".all_checkman", 0);
                WH(".all_check_score", 0);
                WH(".checked_num", 0);
                WH(".checked_task_num", 0);
                WH(".unable_task_num", 0);
                WH(".checked_people_num", 0);
            }
        }, function(error) {
            return "";
        });
    }

    function filter(num, num1, num2) {
        if (num1 == 0) {
            return num2;
        } else {
            return (num / num1).toFixed(2);
        }
    }
});

function jumptodetail(id) {

    post_common_service("task/getById", { "id": id }, function(data) {

        $.checkTask = data;
        if (typeof($.checkTask) != "undefined" && typeof($.checkTask.appeal) != "undefined") {
            $.checkTask.appeal = null;
        }

        var start_date = $(".div-start input").val()
        var end_date = $(".div-end input").val()
        var user_id = $("#select-personal").val()
        var score_falge = $(".score-select").val()
        var pdata = {
            "currentpage": datatable.options.pager.p_cur,
            "startdate": start_date,
            "enddate": end_date,
            "userid": user_id,
            "scorefalge": score_falge
        }

        $.zui.store.set('store_param_' + current_url, pdata);

        url = "/view/mytask/taskDetail.html"

        post_common_service("/checkAppeal/getByTaskId", { "task_id": id }, function(data) {
            $.checkTask.appeal = data;
            changePage(url);
        });
    }, function(error) {

    });



}